-
Notifications
You must be signed in to change notification settings - Fork 921
/
_utility-builder.scss
387 lines (297 loc) · 10.3 KB
/
_utility-builder.scss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/* stylelint-disable max-nesting-depth */
/*
----------------------------------------
@render-pseudoclass
----------------------------------------
Build a pseucoclass utiliy from values
calculated in the @render-utilities-in
loop
----------------------------------------
*/
@mixin render-pseudoclass($utility, $pseudoclass, $selector, $property, $value, $mq) {
$important: if(
$utilities-use-important,
' !important',
null
);
$this-mq: null;
@if $mq {
$this-mq: '#{$mq}\\:';
}
.#{$this-mq}#{$pseudoclass}\:#{ns('utility')}#{$selector}:#{$pseudoclass} {
@each $this-property in $property {
#{$this-property}: unquote('#{$value}#{$important}');
}
}
}
// utility-feature? utility-property
@mixin add-utility-declaration($declaration, $utility-type, $important) {
@each $ext-prop, $ext-value in map-get($declaration, $utility-type) {
#{strunquote($ext-prop)}: unquote('#{strunquote($ext-value)}#{$important}');
}
}
/*
----------------------------------------
@render-media-queries
----------------------------------------
Build @media media queries from values
calculated in the @render-utilities-in
loop
----------------------------------------
*/
@mixin render-media-queries($utility, $selector, $property, $value, $val-props) {
$important: if(
$utilities-use-important,
' !important',
null
);
$our-breakpoints: map-deep-get($system-properties, breakpoints, standard);
$mq: null;
$value-is-map: if(
type-of($val-props) == 'map',
true,
false
);
@each $media-key, $media-value in $our-breakpoints {
$mq: unquote($media-key);
$media-value-px: rem-to-px($media-value);
@if map-get($theme-utility-breakpoints, $media-key) {
@include at-media($media-key) {
.#{$mq}\:#{ns('utility')}#{$selector} {
@if $value-is-map and map-has-key($val-props, extend) {
@include add-utility-declaration($val-props, extend, $important);
}
@each $this-property in $property {
#{$this-property}: unquote('#{$value}#{$important}');
}
@if map-has-key($utility, extend) {
@include add-utility-declaration($utility, extend, $important);
}
@if map-deep-get($utility, settings, hover) {
@include render-pseudoclass($utility, hover, $selector, $property, $value, $mq);
}
@if map-deep-get($utility, settings, visited) {
@include render-pseudoclass($utility, visited, $selector, $property, $value, $mq);
}
}
}
}
}
}
/*
----------------------------------------
@render-utility
----------------------------------------
Build a utility from values calculated
in the @render-utilities-in loop
----------------------------------------
TODO: Determine the proper use of
unquote() in the following. Changed to
account for a 'interpolation near
operators will be simplified in a
future version of Sass' warning.
----------------------------------------
*/
@mixin render-utility($utility, $selector, $property, $value, $val-props) {
$important: if(
$utilities-use-important,
' !important',
null
);
$mq: null;
$value-is-map: if(
type-of($val-props) == 'map',
true,
false
);
.#{ns('utility')}#{$selector} {
@if $value-is-map and map-has-key($val-props, extend) {
@include add-utility-declaration($val-props, extend, $important);
}
@if $value-is-map and map-has-key($val-props, extends) {
@extend %#{map-get($val-props, extends)};
}
@each $this-property in $property {
#{$this-property}: unquote('#{$value}#{$important}');
}
@if map-has-key($utility, extend) {
@include add-utility-declaration($utility, extend, $important);
}
}
// Add the pseudoclass variants, if applicable
@if map-deep-get($utility, settings, hover) {
@include render-pseudoclass($utility, hover, $selector, $property, $value, $mq);
}
@if map-deep-get($utility, settings, active) {
@include render-pseudoclass($utility, active, $selector, $property, $value, $mq);
}
@if map-deep-get($utility, settings, visited) {
@include render-pseudoclass($utility, visited, $selector, $property, $value, $mq);
}
@if map-deep-get($utility, settings, focus) {
@include render-pseudoclass($utility, focus, $selector, $property, $value, $mq);
}
// And add the responsive prefixes, if applicable
@if map-deep-get($utility, settings, responsive) {
@include render-media-queries($utility, $selector, $property, $value, $val-props);
}
}
/*
----------------------------------------
@render-utilities-in
----------------------------------------
The master loop that sets the building
blocks of utilities from the values
in individual rule settings and loops
through all possible variants
----------------------------------------
*/
@mixin render-utilities-in($utilities) {
// loop through the $utilities
@each $utility-name, $utility in $utilities {
// Only do this if the the utility is meant to output
@if map-deep-get($utility, settings, output) or
$output-all-utilities {
// set intital variants
// $property-default is a single value for all these utilities
$base-props: null;
$modifier: null;
$selector: null;
$property-default: map-get($utility, property);
$property: null;
$value: null;
$our-modifiers: ();
$b: null;
$v: null;
$mv: null;
$val-props: ();
$no-value: false;
$mq: null;
$b: map-get($utility, base);
// Each utility rule takes a value, so let's start here
// and begin building.
// -------- For each value in utility.values ----------
@each $val-key, $val-value in map-get($utility, values) {
// If $val-value == null, or if $val-value is a map and
// the content key or the dependency key has a null value
// set $val-value to `false`...
@if type-of($val-value) == 'map' {
@if not map-get($val-value, content) {
$val-value: false;
}
@else if map-has-key($val-value, dependency)
and not map-get($val-value, dependency) {
$val-value: false;
}
}
// ...so we can skip building this rule altogether.
// So, if $val-value is _not_ false...
@if $val-value {
// Set the value of our rule.
// If its a map, use val-value.content.
$val-slug: if(
type-of($val-value) == 'map',
map-get($val-value, 'slug'),
$val-key
);
$value: if(
type-of($val-value) == 'map',
map-get($val-value, 'content'),
$val-value
);
@if $val-slug == ''
or smart-quote($val-slug) == 'noValue' {
$no-value: true;
}
// Add any appended values...
@if map-get($utility, valueAppend) {
$value: $value + map-get($utility, valueAppend);
}
// ...or prepended values.
@if map-get($utility, valuePrepend) {
$value: map-get($utility, valuePrepend) + $value;
}
// Then unquote the entire value string.
$value: strunquote($value);
// And we'll set the $v as $val-slug for use in
// constructing the selector (.$b-$m-$v).
$v: $val-slug;
// -------- Start of Modifiers ----------
// Now we'll check for modifiers and loop through them
// to get the props we need to build our rule.
// Modifiers are held in a MAP,
// where each individual modifer has the keypair
// [slug]:[value]
// So, check for modifiers.
@if map-get($utility, modifiers) != null {
// If there are modifiers, capture them as $our-modifiers.
$our-modifiers: map-get($utility, modifiers);
}
@else {
// If there aren't, build a dummy so we can keep
// all our build in the same loop.
$our-modifiers: (
'slug': null,
);
}
// OK! C'mon, let's loop!
// https://www.youtube.com/watch?v=X9i2i07wPUw
// -------- For each modifier in $our-modifiers ----------
@each $mod-key, $mod-val in $our-modifiers {
$property: if(
$mod-val == null or $mod-val == '',
$property-default,
multi-cat($property-default, $mod-val)
);
// Now we go through to set the $selector.
// If mod-props.slug is noModifier...
@if $mod-key == ''
or $mod-key == slug
or smart-quote($mod-key) == 'noModifier' {
// First, we can test to see if the base $b is null
@if $b == null {
// If it _is_ null, the rule's selector is $v.
$selector: $v;
// if the value is noValue ('')
}
@else if $no-value {
// selector is the base only
$selector: $b;
}
@else {
// otherwise, selctor is joined with a hyphen.
$selector: $b + '-' + $v;
// Nice! We just took care of the non-modifier cases!
}
}
// If there _is_ a modifier...
@else {
$mv: if(
$no-value,
$mod-key,
$mod-key + '-' + $v
);
// Once we have $mv, test for $b
// and build the selector as before.
$selector: if(
$b == null,
$mv,
$b + '-' + $mv
);
}
// finished setting modifier vars
// Hey. Did we just finish $selector?
// And do we also have $property and $value?
// We do?!?!?! We do!
// FINALLY, 'BUILD THE RULE, MAX!'
// https://www.youtube.com/watch?v=R3Igz5SfBCE
@include render-utility($utility, $selector, $property, $value, $val-value);
} // end the modifier loop
} // end the null value conditional
} // end the value loop
} // end the output conditional
} // end the utility loop
// (ノ◕ヮ◕)ノ*:・゚✧
}
// Helper to generate an @font-face declaration
/* stylelint-enable */