@ramstack/sass-utility-generator 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,6 +16,20 @@ $grid-breakpoints: (
16
16
 
17
17
  $custom-media-queries: () !default;
18
18
 
19
+ // Returns the value if it is a map
20
+ // An empty configuration `()` is a list in Sass, so it is accepted as an empty map
21
+ //
22
+ // @value The configuration value
23
+ // @name The variable name for the error message
24
+ // @returns The value
25
+ @function -assert-map($value, $name) {
26
+ @if meta.type-of($value) != "map" and list.length($value) != 0 {
27
+ @error "#{$name} must be a map.";
28
+ }
29
+
30
+ @return $value;
31
+ }
32
+
19
33
  @function -create-media-queries-map() {
20
34
  $map: (
21
35
  light: "(prefers-color-scheme: light)",
@@ -25,33 +39,26 @@ $custom-media-queries: () !default;
25
39
  contrast: "(prefers-contrast: more)",
26
40
  reduce: "(prefers-reduced-motion: reduce)",
27
41
  motion: "(prefers-reduced-motion: no-preference)",
28
- print: "print"
42
+ print: "print",
43
+ // Filled from $grid-breakpoints below; listed here to reserve the name
44
+ responsive: ()
29
45
  );
30
46
 
31
- @if meta.type-of($custom-media-queries) != "map" and list.length($custom-media-queries) != 0 {
32
- @error "$custom-media-queries must be a map.";
33
- }
34
-
35
- @each $key, $value in $custom-media-queries {
36
- @if map.has-key($map, $key) or $key == responsive {
47
+ @each $key, $value in -assert-map($custom-media-queries, "$custom-media-queries") {
48
+ @if map.has-key($map, $key) {
37
49
  @error "Custom media variant `#{$key}` conflicts with a built-in variant.";
38
50
  }
39
51
 
40
52
  @if meta.type-of($value) != "string" {
41
53
  @error "Custom media query `#{$key}` must be a string.";
42
54
  }
43
-
44
- $map: map.set($map, $key, $value);
45
55
  }
46
56
 
57
+ $map: map.merge($map, $custom-media-queries);
47
58
  $responsive: ();
48
59
 
49
- @if meta.type-of($grid-breakpoints) != "map" and list.length($grid-breakpoints) != 0 {
50
- @error "$grid-breakpoints must be a map.";
51
- }
52
-
53
- @each $key, $value in $grid-breakpoints {
54
- @if map.has-key($map, $key) or $key == responsive {
60
+ @each $key, $value in -assert-map($grid-breakpoints, "$grid-breakpoints") {
61
+ @if map.has-key($map, $key) {
55
62
  @error "Breakpoint `#{$key}` conflicts with a media variant.";
56
63
  }
57
64
 
@@ -67,7 +74,7 @@ $custom-media-queries: () !default;
67
74
  @return map.set($map, responsive, $responsive);
68
75
  }
69
76
 
70
- $-media-map: -create-media-queries-map() !default;
77
+ $-media-map: -create-media-queries-map();
71
78
 
72
79
  // Returns whether the target consists of exactly one class selector
73
80
  //
@@ -76,12 +83,8 @@ $-media-map: -create-media-queries-map() !default;
76
83
  // For example, `.button` is a simple-class target,
77
84
  // while `.button.active` and `.button:hover` are not
78
85
  @function -is-simple-class($selector) {
79
- @if meta.type-of($selector) != "string" {
80
- @return false;
81
- }
82
-
83
- $simple-selectors: selector.simple-selectors($selector);
84
- @return list.length($simple-selectors) == 1 and string.index($selector, ".") == 1;
86
+ // A variant target without pseudo-classes is a single class
87
+ @return -is-variant-target($selector) and list.length(selector.simple-selectors($selector)) == 1;
85
88
  }
86
89
 
87
90
  // Returns whether the target can receive a variant prefix
@@ -114,12 +117,39 @@ $-media-map: -create-media-queries-map() !default;
114
117
  @return true;
115
118
  }
116
119
 
120
+ // Fails unless the target of each selector can receive a variant
121
+ //
122
+ // @selectors The list of selectors to check
123
+ // @simple-class Whether each target must be a single class without pseudo-classes
124
+ // @returns The selectors
125
+ @function -assert-targets($selectors, $simple-class: false) {
126
+ @each $selector in $selectors {
127
+ // Sass stores each complex selector as a list; its last item is the target
128
+ $target: list.nth($selector, -1);
129
+ $valid: false;
130
+
131
+ @if $simple-class {
132
+ $valid: -is-simple-class($target);
133
+ }
134
+ @else {
135
+ $valid: -is-variant-target($target);
136
+ }
137
+
138
+ @if not $valid {
139
+ @error "Expected the final selector segment to be a single class, got `#{$target}`.";
140
+ }
141
+ }
142
+
143
+ @return $selectors;
144
+ }
145
+
117
146
  // Appends an option suffix to the target class of each selector
118
147
  // Only the final compound selector is modified,
119
148
  // so `.parent .w` becomes `.parent .w-100`
120
149
  //
121
150
  // @selectors The list of selectors to modify
122
151
  // @option The option name to append to the selector as a suffix
152
+ // @validate Whether to check that each target is a single class
123
153
  // @returns The list of modified selectors
124
154
  //
125
155
  // @example
@@ -127,20 +157,19 @@ $-media-map: -create-media-queries-map() !default;
127
157
  // $selector: -append-option(".parent .w", "100"); --> ".parent .w-100"
128
158
  // $selector: -append-option(".w", ""); --> ".w"
129
159
  // $selector: -append-option(".w", null); --> ".w"
130
- @function -append-option($selectors, $option) {
131
- $result: ();
132
-
160
+ @function -append-option($selectors, $option, $validate: true) {
133
161
  @if $option == null or $option == "" {
134
162
  @return $selectors;
135
163
  }
136
164
 
137
- @each $selector in $selectors {
138
- $target: list.nth($selector, -1);
165
+ @if $validate {
166
+ $selectors: -assert-targets($selectors, $simple-class: true);
167
+ }
139
168
 
140
- @if not -is-simple-class($target) {
141
- @error "Expected the final selector segment to be a single class, got `#{$target}`.";
142
- }
169
+ $result: ();
143
170
 
171
+ @each $selector in $selectors {
172
+ $target: list.nth($selector, -1);
144
173
  $selector: list.set-nth($selector, -1, "#{$target}-#{$option}");
145
174
  $result: list.append($result, $selector, comma);
146
175
  }
@@ -148,40 +177,66 @@ $-media-map: -create-media-queries-map() !default;
148
177
  @return $result;
149
178
  }
150
179
 
151
- @function -append-variant($selectors, $variant, $is-media-query: false) {
180
+ // Prefixes the target class of each selector with a variant name
181
+ // Media variants use this directly; state and group variants build on it
182
+ //
183
+ // @selectors The list of selectors to modify
184
+ // @variant The variant name to use as a prefix
185
+ // @validate Whether to check that each target can receive a variant
186
+ // @returns The list of modified selectors
187
+ //
188
+ // @example
189
+ // $selector: -prefix-targets(".class", "md"); --> ".md\:class"
190
+ // $selector: -prefix-targets(".parent .child", "md"); --> ".parent .md\:child"
191
+ @function -prefix-targets($selectors, $variant, $validate: true) {
192
+ @if $validate {
193
+ $selectors: -assert-targets($selectors);
194
+ }
195
+
152
196
  $result: ();
153
197
 
154
198
  @each $selector in $selectors {
155
- // Sass stores each complex selector as a list; its last item is the target
156
199
  $target: list.nth($selector, -1);
157
200
 
158
- @if not -is-variant-target($target) {
159
- @error "Expected the final selector segment to be a single class, got `#{$target}`.";
160
- }
161
-
162
201
  // Prefix the target class with the escaped variant separator
163
- // .class -> .focus\:class
164
- $target: string.insert($target, "#{$variant}\\:", 2);
202
+ $selector: list.set-nth($selector, -1, string.insert($target, "#{$variant}\\:", 2));
203
+ $result: list.append($result, $selector, comma);
204
+ }
205
+
206
+ @return $result;
207
+ }
208
+
209
+ // Applies a state or group variant to the target class of each selector
210
+ // The caller checks the targets with -assert-targets
211
+ //
212
+ // @selectors The list of selectors to modify
213
+ // @variant The state or group variant name
214
+ // @returns The list of modified selectors
215
+ //
216
+ // @example
217
+ // $selector: -append-variant(".class", "focus"); --> ".focus\:class:focus"
218
+ // $selector: -append-variant(".class", "group-hover"); --> ".group:hover .group-hover\:class"
219
+ @function -append-variant($selectors, $variant) {
220
+ $result: ();
221
+
222
+ @each $selector in -prefix-targets($selectors, $variant, $validate: false) {
223
+ $target: list.nth($selector, -1);
165
224
 
166
225
  @if string.index($variant, "group-") == 1 {
167
- // Replace the target with the group state
168
- // .cls -> .group:hover
169
- // .parent .child -> .parent .group:hover
226
+ // Replace the prefixed target with the group state
227
+ // .group-hover\:cls -> .group:hover
228
+ // .parent .group-hover\:child -> .parent .group:hover
170
229
  $selector: list.set-nth($selector, -1, ".group:#{string.slice($variant, 7)}");
171
230
 
172
231
  // Append the prefixed target as a descendant of the group
173
- // .group:hover -> .group-hover\:cls
232
+ // .group:hover -> .group:hover .group-hover\:cls
174
233
  // .parent .group:hover -> .parent .group:hover .group-hover\:child
175
234
  $selector: list.append($selector, $target);
176
235
  }
177
236
  @else {
178
- @if not $is-media-query {
179
- // State variants add a pseudo-class; media variants only add a prefix
180
- // .focus\:class:focus
181
- $target: $target + ":" + $variant;
182
- }
183
-
184
- $selector: list.set-nth($selector, -1, $target);
237
+ // State variants add a pseudo-class
238
+ // .focus\:class -> .focus\:class:focus
239
+ $selector: list.set-nth($selector, -1, $target + ":" + $variant);
185
240
  }
186
241
 
187
242
  $result: list.append($result, $selector, comma);
@@ -190,7 +245,28 @@ $-media-map: -create-media-queries-map() !default;
190
245
  @return $result;
191
246
  }
192
247
 
193
- @mixin -options($options, $variants...) {
248
+ // Returns the selectors for an option
249
+ // Inside a selector, the option name is appended to the target class,
250
+ // at the root it is the complete class name
251
+ //
252
+ // @parent The enclosing selector, or null at the root
253
+ // @option The option name
254
+ // @validate Whether to check that the enclosing selector can receive a suffix
255
+ // @returns The list of option selectors
256
+ @function -option-selectors($parent, $option, $validate: true) {
257
+ @if $parent {
258
+ @return -append-option($parent, $option, $validate);
259
+ }
260
+
261
+ @if $option == null or $option == "" {
262
+ @error "Root options must use non-empty class names.";
263
+ }
264
+
265
+ @return selector.parse(".#{$option}");
266
+ }
267
+
268
+ @mixin options($options, $variants...) {
269
+ $states: ();
194
270
  $media-queries: ();
195
271
  $map: ();
196
272
 
@@ -203,54 +279,79 @@ $-media-map: -create-media-queries-map() !default;
203
279
  @else if $media {
204
280
  $media-queries: map.merge($media-queries, $media);
205
281
  }
282
+ @else {
283
+ $states: list.append($states, $variant);
284
+ }
206
285
  }
207
286
 
287
+ // Print is emitted separately, after the other media queries
288
+ $print: map.has-key($media-queries, print);
289
+ $media-queries: map.remove($media-queries, print);
290
+
291
+ // Each selector is checked once, where it is first used. Option and variant
292
+ // names come from the user, so generated selectors need checking too,
293
+ // but every state variant of an option starts from the same option selectors,
294
+ // and every media query prefixes the same selectors
295
+ $check-parent: true;
296
+
208
297
  @each $k, $v in $options {
209
- $root: -append-option(&, $k);
210
- $list: ($root);
298
+ $root: -option-selectors(&, $k, $check-parent);
211
299
 
212
- @each $variant in $variants {
213
- @if not map.has-key($-media-map, $variant) {
214
- $s: -append-variant($root, $variant);
215
- $list: list.join($list, $s);
216
- }
300
+ // Every option name is appended to the same enclosing selector,
301
+ // so it is checked with the first name that adds a suffix
302
+ @if $k != null and $k != "" {
303
+ $check-parent: false;
304
+ }
305
+
306
+ @if list.length($states) > 0 {
307
+ $root: -assert-targets($root);
308
+ }
309
+
310
+ $list: $root;
311
+
312
+ @each $state in $states {
313
+ $list: list.join($list, -append-variant($root, $state));
217
314
  }
218
315
 
219
316
  @at-root #{$list} {
220
317
  @content($k, $v);
221
318
  }
222
319
 
223
- $map: map.set($map, $k, $list);
320
+ // Keep the value with the selectors: options given as a list of pairs
321
+ // cannot be looked up by key. Each map.set copies the map,
322
+ // so skip it when there are no media queries to read it
323
+ @if list.length($media-queries) > 0 {
324
+ $map: map.set($map, $k, (selectors: $list, value: $v));
325
+ }
224
326
  }
225
327
 
226
- @if list.length($media-queries) {
227
- @each $media-key, $media-query in $media-queries {
228
- @if $media-key != "print" {
229
- @media #{$media-query} {
230
- @each $k, $selectors in $map {
231
- $s: -append-variant($selectors, $media-key, true);
232
- @at-root #{$s} {
233
- $v: null;
234
-
235
- @if meta.type-of($options) == map {
236
- $v: map.get($options, $k);
237
- }
238
-
239
- @content($k, $v);
240
- }
241
- }
328
+ // Only the first media query checks the selectors
329
+ $validate: true;
330
+
331
+ @each $media-key, $media-query in $media-queries {
332
+ @media #{$media-query} {
333
+ @each $k, $rule in $map {
334
+ $s: -prefix-targets(map.get($rule, selectors), $media-key, $validate);
335
+ @at-root #{$s} {
336
+ @content($k, map.get($rule, value));
242
337
  }
243
338
  }
244
339
  }
340
+
341
+ $validate: false;
245
342
  }
246
343
 
247
344
  // Print starts from the base selector because interactive states
248
345
  // do not apply to printed output
249
- @if map.has-key($media-queries, "print") {
346
+ @if $print {
347
+ // The first pass checked the enclosing selector for every option name,
348
+ // and the option selectors too if a state variant or a media query used them
349
+ $validate: $validate and list.length($states) == 0;
350
+
250
351
  @media print {
251
352
  @each $k, $v in $options {
252
- $s: -append-option(&, $k);
253
- $s: -append-variant($s, "print", true);
353
+ $s: -option-selectors(&, $k, $validate: false);
354
+ $s: -prefix-targets($s, "print", $validate);
254
355
 
255
356
  @at-root #{$s} {
256
357
  @content($k, $v);
@@ -268,75 +369,11 @@ $-media-map: -create-media-queries-map() !default;
268
369
  @error "variants() must be included inside a selector.";
269
370
  }
270
371
 
271
- $media-queries: ();
272
-
273
- // The current selector is always included as the base variant
274
- $selectors: (&);
275
-
276
- @each $variant in $variants {
277
- $media: map.get($-media-map, $variant);
278
-
279
- @if not $media {
280
- // Add a state or group variant alongside the base selector
281
- $s: -append-variant(&, $variant);
282
- $selectors: list.join($selectors, $s);
283
- }
284
- @else if meta.type-of($media) == "string" {
285
- $media-queries: map.set($media-queries, $variant, $media);
286
- }
287
- @else {
288
- $media-queries: map.merge($media-queries, $media);
289
- }
290
- }
291
-
292
- @at-root #{$selectors} {
372
+ // A null option leaves the selector unchanged,
373
+ // so a single null option emits the variants of the selector itself
374
+ @include options((null: null), $variants...) using ($k, $v) {
293
375
  @content;
294
376
  }
295
-
296
- @if list.length($media-queries) {
297
- @each $key, $value in $media-queries {
298
- @if $key != "print" {
299
- @media #{$value} {
300
- $s: -append-variant($selectors, $key, /* $is-media-query: */ true);
301
- @at-root #{$s} {
302
- @content;
303
- }
304
- }
305
- }
306
- }
307
- }
308
-
309
- // Print starts from the base selector because interactive states
310
- // do not apply to printed output
311
- @if map.has-key($media-queries, "print") {
312
- @media print {
313
- $s: -append-variant(&, "print", true);
314
- @at-root #{$s} {
315
- @content;
316
- }
317
- }
318
- }
319
- }
320
-
321
- @mixin options($options, $variants...) {
322
- @if & {
323
- @include -options($options, $variants...) using ($k, $v) {
324
- @content($k, $v);
325
- }
326
- }
327
- @else {
328
- @each $key, $value in $options {
329
- @if $key == null or $key == "" {
330
- @error "Root options must use non-empty class names.";
331
- }
332
-
333
- .#{$key} {
334
- @include variants($variants...) {
335
- @content($key, $value);
336
- }
337
- }
338
- }
339
- }
340
377
  }
341
378
 
342
379
  @mixin responsive() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ramstack/sass-utility-generator",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "A Sass utility class generator with support for options and variants.",
5
5
  "type": "module",
6
6
  "author": "rameel <rameel-b@hotmail.com>",