@ramstack/sass-utility-generator 1.0.0

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.
package/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright 2022 Rameel (https://github.com/rameel)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,503 @@
1
+ # Sass Utility Generator
2
+
3
+ A small Sass module that turns lists and maps into utility classes.
4
+
5
+ You choose the class names, values, and CSS rules. The generator adds variants
6
+ for states like hover, group states, media queries, and responsive breakpoints.
7
+
8
+ ## Requirements
9
+
10
+ This package requires [Dart Sass](https://sass-lang.com/dart-sass/) 1.71.0 or later.
11
+ You can use either `sass` or `sass-embedded`.
12
+
13
+ ## Installation
14
+
15
+ Install the package along with Dart Sass:
16
+
17
+ ```sh
18
+ npm install -D @ramstack/sass-utility-generator sass
19
+ ```
20
+
21
+ In Vite and other tools with Sass package support, import it by name:
22
+
23
+ ```scss
24
+ @use "@ramstack/sass-utility-generator" as utilities;
25
+ ```
26
+
27
+ For the Dart Sass CLI, add `pkg:` to the import and enable the
28
+ [Node.js package importer](https://sass-lang.com/documentation/cli/dart-sass/#pkg-importer-node):
29
+
30
+ ```scss
31
+ @use "pkg:@ramstack/sass-utility-generator" as utilities;
32
+ ```
33
+
34
+ ```sh
35
+ npx sass --pkg-importer=node src/styles.scss dist/styles.css
36
+ ```
37
+
38
+ With the JavaScript API, use the same `pkg:` import and add the importer:
39
+
40
+ ```js
41
+ import * as sass from "sass";
42
+
43
+ const result = sass.compile("src/styles.scss", {
44
+ importers: [new sass.NodePackageImporter()],
45
+ });
46
+ ```
47
+
48
+ If your tool doesn't support package imports, add the package directory to
49
+ the Sass load path and use the file name:
50
+
51
+ ```sh
52
+ npx sass --load-path=node_modules/@ramstack/sass-utility-generator src/styles.scss dist/styles.css
53
+ ```
54
+
55
+ ```scss
56
+ @use "utility-generator" as utilities;
57
+ ```
58
+
59
+ For manual installation, copy `_utility-generator.scss` next to your stylesheet
60
+ and use `@use "./utility-generator" as utilities;`.
61
+
62
+ The examples below use the first import form.
63
+
64
+ ## Quick start
65
+
66
+ ```scss
67
+ @use "@ramstack/sass-utility-generator" as utilities;
68
+
69
+ .opacity {
70
+ @include utilities.options((50: 0.5, 100: 1), hover) using ($name, $value) {
71
+ opacity: $value;
72
+ }
73
+ }
74
+ ```
75
+
76
+ Generated CSS:
77
+
78
+ ```css
79
+ .opacity-50,
80
+ .hover\:opacity-50:hover {
81
+ opacity: 0.5;
82
+ }
83
+
84
+ .opacity-100,
85
+ .hover\:opacity-100:hover {
86
+ opacity: 1;
87
+ }
88
+ ```
89
+
90
+ Use the classes in your HTML:
91
+
92
+ ```html
93
+ <button class="opacity-50 hover:opacity-100">...</button>
94
+ ```
95
+
96
+ ## Samples
97
+
98
+ Each sample is a complete stylesheet you can compile on its own:
99
+
100
+ 1. [Basic utility](samples/01-basic-utility.scss)
101
+ 2. [List options](samples/02-list-options.scss)
102
+ 3. [Map options](samples/03-map-options.scss)
103
+ 4. [State variants](samples/04-state-variants.scss)
104
+ 5. [Group variant](samples/05-group-variant.scss)
105
+ 6. [Responsive utilities](samples/06-responsive.scss)
106
+ 7. [Custom breakpoints](samples/07-custom-breakpoints.scss)
107
+ 8. [Custom media variants](samples/08-custom-media.scss)
108
+ 9. [Combining variants](samples/09-combining-variants.scss)
109
+
110
+ To build all samples, run these commands from the repository root.
111
+ The CSS goes in `samples/css/`:
112
+
113
+ ```sh
114
+ npm install
115
+ npm run build:samples
116
+ ```
117
+
118
+ ## How it works
119
+
120
+ Options add a suffix like `-block`. Variants add a prefix like `hover:`
121
+ and a condition for when the styles apply:
122
+
123
+ | Input | Generated selector or rule |
124
+ |---------------------------------------|---------------------------------------|
125
+ | `.display` with option `block` | `.display-block` |
126
+ | `.display-block` with variant `hover` | `.hover\:display-block:hover` |
127
+ | `.display-block` with breakpoint `md` | `@media (...) { .md\:display-block }` |
128
+
129
+ The base class, such as `.display-block`, is always included alongside its variants.
130
+
131
+ ## Variants
132
+
133
+ Use `variants($variants...)` to add states such as `hover` and `focus-visible`:
134
+
135
+ ```scss
136
+ @use "@ramstack/sass-utility-generator" as utilities;
137
+
138
+ .text-red {
139
+ @include utilities.variants(hover, focus-visible) {
140
+ color: red;
141
+ }
142
+ }
143
+ ```
144
+
145
+ Generated CSS:
146
+
147
+ ```css
148
+ .text-red,
149
+ .hover\:text-red:hover,
150
+ .focus-visible\:text-red:focus-visible {
151
+ color: red;
152
+ }
153
+ ```
154
+
155
+ Names such as `hover` and `focus-visible` become CSS pseudo-classes.
156
+ The generator doesn't check these names, so it accepts new and vendor-specific
157
+ pseudo-classes, but won't catch typos.
158
+
159
+ ### Group variants
160
+
161
+ Add `group-` before a state to react to that state on a `.group` ancestor.
162
+ For example, `group-hover` applies when that ancestor is hovered:
163
+
164
+ ```scss
165
+ .bg-gray {
166
+ @include utilities.variants(group-hover) {
167
+ background-color: #555;
168
+ }
169
+ }
170
+ ```
171
+
172
+ ```css
173
+ .bg-gray,
174
+ .group:hover .group-hover\:bg-gray {
175
+ background-color: #555;
176
+ }
177
+ ```
178
+
179
+ Put `group` on a containing element and `group-hover:bg-gray` on the element
180
+ you want to style:
181
+
182
+ ```html
183
+ <div class="group">
184
+ <span class="group-hover:bg-gray">...</span>
185
+ </div>
186
+ ```
187
+
188
+ ### Built-in media variants
189
+
190
+ | Variant | Media query |
191
+ |--------------|-------------------------------------------|
192
+ | `light` | `(prefers-color-scheme: light)` |
193
+ | `dark` | `(prefers-color-scheme: dark)` |
194
+ | `pointer` | `(pointer: fine)` |
195
+ | `touch` | `(pointer: coarse)` |
196
+ | `contrast` | `(prefers-contrast: more)` |
197
+ | `reduce` | `(prefers-reduced-motion: reduce)` |
198
+ | `motion` | `(prefers-reduced-motion: no-preference)` |
199
+ | `print` | `print` |
200
+ | `responsive` | All configured non-zero breakpoints |
201
+
202
+ `pointer` and `touch` check the primary pointing device, even when several
203
+ pointing devices are available.
204
+
205
+ Each media variant gets its own `@media` rule:
206
+
207
+ ```scss
208
+ .text-red {
209
+ @include utilities.variants(dark, print) {
210
+ color: red;
211
+ }
212
+ }
213
+ ```
214
+
215
+ ```css
216
+ .text-red {
217
+ color: red;
218
+ }
219
+
220
+ @media (prefers-color-scheme: dark) {
221
+ .dark\:text-red {
222
+ color: red;
223
+ }
224
+ }
225
+
226
+ @media print {
227
+ .print\:text-red {
228
+ color: red;
229
+ }
230
+ }
231
+ ```
232
+
233
+ `print` uses the base selector and does not combine with pseudo-class variants
234
+ passed in the same call.
235
+
236
+ ## Responsive variants
237
+
238
+ Use `responsive` to generate variants for your breakpoints:
239
+
240
+ ```scss
241
+ .display-block {
242
+ @include utilities.variants(responsive) {
243
+ display: block;
244
+ }
245
+ }
246
+ ```
247
+
248
+ The default breakpoints are:
249
+
250
+ ```scss
251
+ (
252
+ xs: 0,
253
+ sm: 576px,
254
+ md: 768px,
255
+ lg: 992px,
256
+ xl: 1200px,
257
+ xxl: 1400px
258
+ )
259
+ ```
260
+
261
+ The base `.display-block` works at all screen sizes. With these breakpoints,
262
+ `responsive` adds `.sm\:display-block` through `.xxl\:display-block` inside
263
+ `min-width` media queries. The `xs: 0` entry uses the base class, so there's
264
+ no separate `xs:` class.
265
+
266
+ To use your own breakpoints, set `$grid-breakpoints` in `@use`:
267
+
268
+ ```scss
269
+ @use "@ramstack/sass-utility-generator" as utilities with (
270
+ $grid-breakpoints: (
271
+ mobile: 0,
272
+ tablet: 48rem,
273
+ desktop: 75rem
274
+ )
275
+ );
276
+ ```
277
+
278
+ Use lengths such as `576px` or `48rem`, or `0` for the base size.
279
+ Negative values aren't allowed. Choose names that aren't already used by
280
+ media variants or `responsive`.
281
+
282
+ ## Custom media variants
283
+
284
+ Define your own media variants with `$custom-media-queries`:
285
+
286
+ ```scss
287
+ @use "@ramstack/sass-utility-generator" as utilities with (
288
+ $custom-media-queries: (
289
+ landscape: "(orientation: landscape)",
290
+ retina: "(min-resolution: 2dppx)"
291
+ )
292
+ );
293
+
294
+ .visible {
295
+ @include utilities.variants(landscape, retina) {
296
+ visibility: visible;
297
+ }
298
+ }
299
+ ```
300
+
301
+ Write media queries as strings. Names already used by built-in variants or
302
+ `responsive` are reserved.
303
+
304
+ ## Options
305
+
306
+ Use `options($options, $variants...)` to reuse CSS rules across a list or map of values:
307
+
308
+ ### Sass lists
309
+
310
+ With a simple list, each item becomes both the class suffix and the CSS value:
311
+
312
+ ```scss
313
+ .display {
314
+ @include utilities.options(block inline none, responsive) using ($value...) {
315
+ display: $value;
316
+ }
317
+ }
318
+ ```
319
+
320
+ This generates `.display-block`, `.display-inline`, and `.display-none`, plus
321
+ their responsive variants.
322
+
323
+ The `$value...` parameter collects the two arguments passed by `options()`:
324
+ the list item and `null`. Sass leaves `null` out of the CSS, so `display`
325
+ receives just the item.
326
+
327
+ For values with multiple parts, such as `1px solid red`, use a map.
328
+
329
+ ### Sass maps
330
+
331
+ Use a map to give each value its own class suffix:
332
+
333
+ ```scss
334
+ .text {
335
+ $colors: (
336
+ muted: #666,
337
+ danger: #c00
338
+ );
339
+
340
+ @include utilities.options($colors, hover) using ($name, $value) {
341
+ color: $value;
342
+ }
343
+ }
344
+ ```
345
+
346
+ This generates `.text-muted`, `.text-danger`, and their `hover:` variants.
347
+
348
+ For a default value, use `null` or `""` as the key to keep the original class name:
349
+
350
+ ```scss
351
+ .rounded {
352
+ @include utilities.options((null: 0.25rem, pill: 9999px)) using ($name, $value) {
353
+ border-radius: $value;
354
+ }
355
+ }
356
+ ```
357
+
358
+ The result contains `.rounded` and `.rounded-pill`.
359
+
360
+ ### Options with pseudo-classes
361
+
362
+ To make an option apply only on hover or focus, nest those states inside `options()`:
363
+
364
+ ```scss
365
+ .link {
366
+ @include utilities.options((
367
+ primary: #2563eb,
368
+ danger: #dc2626
369
+ )) using ($name, $color) {
370
+ &:hover,
371
+ &:focus-visible {
372
+ color: $color;
373
+ }
374
+ }
375
+ }
376
+ ```
377
+
378
+ This creates selectors such as `.link-primary:hover` and `.link-danger:focus-visible`.
379
+ The colors apply only in these states; no base color or `hover:` class is generated.
380
+ Use `options(..., hover)` when you also want base classes and `hover:` variants.
381
+
382
+ Keep `:hover` and similar states inside the mixin block. Calling `options()`
383
+ inside `.link:hover` isn't supported.
384
+
385
+ ### Root options
386
+
387
+ Outside a selector, map keys become complete class names:
388
+
389
+ ```scss
390
+ @include utilities.options((visible: visible, invisible: hidden)) using ($name, $value) {
391
+ visibility: $value;
392
+ }
393
+ ```
394
+
395
+ This generates `.visible` and `.invisible`. Keys can't be `null` or `""` here
396
+ because there's no existing class name to use.
397
+
398
+ ## Combining variants
399
+
400
+ Pass state and media variants together to combine them (except for `print`):
401
+
402
+ ```scss
403
+ .text-red {
404
+ @include utilities.variants(hover, dark) {
405
+ color: red;
406
+ }
407
+ }
408
+ ```
409
+
410
+ This creates `.text-red` and its `hover:` variant, then adds `dark:` versions
411
+ of both inside the media query.
412
+
413
+ Each media variant in a call gets a separate media rule. To require both
414
+ conditions at once, nest mixins:
415
+
416
+ ```scss
417
+ .text-red {
418
+ @include utilities.responsive() {
419
+ @include utilities.colorschemes() {
420
+ color: red;
421
+ }
422
+ }
423
+ }
424
+ ```
425
+
426
+ Alongside the responsive and color-scheme variants, this generates combinations such as:
427
+
428
+ ```css
429
+ @media (min-width: 576px) and (prefers-color-scheme: dark) {
430
+ .dark\:sm\:text-red {
431
+ color: red;
432
+ }
433
+ }
434
+ ```
435
+
436
+ The inner mixin's prefix comes first in the class name. Swap the nesting
437
+ to get `.sm\:dark\:text-red` instead.
438
+
439
+ ## Shorthands
440
+
441
+ | Mixin | Equivalent call |
442
+ |------------------|-------------------------|
443
+ | `responsive()` | `variants(responsive)` |
444
+ | `light()` | `variants(light)` |
445
+ | `dark()` | `variants(dark)` |
446
+ | `colorschemes()` | `variants(light, dark)` |
447
+ | `print()` | `variants(print)` |
448
+
449
+ ```scss
450
+ .text-red {
451
+ @include utilities.dark() {
452
+ color: red;
453
+ }
454
+ }
455
+ ```
456
+
457
+ ## Selector rules and limitations
458
+
459
+ - Call `variants()` inside a class selector.
460
+ - Descendant selectors and selector lists work too, as long as each target
461
+ ends in a single class: `.card .title, .dialog .title`.
462
+ - For pseudo-elements such as `::before`, nest them inside the mixin block:
463
+
464
+ ```scss
465
+ .icon {
466
+ @include utilities.variants(hover) {
467
+ &::before {
468
+ color: red;
469
+ }
470
+ }
471
+ }
472
+ ```
473
+
474
+ - Targets such as `.button.active` aren't supported: the generator can't tell
475
+ which class to modify.
476
+ - Option keys, variant names, and breakpoint names become part of CSS class names.
477
+ Use valid class-name fragments; the generator doesn't escape spaces, slashes,
478
+ or punctuation.
479
+
480
+ ## API reference
481
+
482
+ | API | Purpose |
483
+ |-----------------------------------|----------------------------------------------|
484
+ | `$grid-breakpoints` | Configures responsive min-width variants |
485
+ | `$custom-media-queries` | Adds named media variants |
486
+ | `variants($variants...)` | Adds pseudo-class, group, and media variants |
487
+ | `options($options, $variants...)` | Generates classes from a list or map |
488
+ | `responsive()` | Adds all responsive variants |
489
+ | `light()` / `dark()` | Adds one color-scheme variant |
490
+ | `colorschemes()` | Adds both color-scheme variants |
491
+ | `print()` | Adds the print variant |
492
+
493
+ ## Development
494
+
495
+ Install dependencies, compile each sample to `samples/css/`, and run the test suite:
496
+
497
+ ```sh
498
+ npm install
499
+ npm run build:samples
500
+ npm test
501
+ ```
502
+
503
+ The project is licensed under the [MIT License](LICENSE).
@@ -0,0 +1,370 @@
1
+ @use "sass:list";
2
+ @use "sass:map";
3
+ @use "sass:math";
4
+ @use "sass:meta";
5
+ @use "sass:selector";
6
+ @use "sass:string";
7
+
8
+ $grid-breakpoints: (
9
+ xs: 0,
10
+ sm: 576px,
11
+ md: 768px,
12
+ lg: 992px,
13
+ xl: 1200px,
14
+ xxl: 1400px
15
+ ) !default;
16
+
17
+ $custom-media-queries: () !default;
18
+
19
+ @function -create-media-queries-map() {
20
+ $map: (
21
+ light: "(prefers-color-scheme: light)",
22
+ dark: "(prefers-color-scheme: dark)",
23
+ pointer: "(pointer: fine)",
24
+ touch: "(pointer: coarse)",
25
+ contrast: "(prefers-contrast: more)",
26
+ reduce: "(prefers-reduced-motion: reduce)",
27
+ motion: "(prefers-reduced-motion: no-preference)",
28
+ print: "print"
29
+ );
30
+
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 {
37
+ @error "Custom media variant `#{$key}` conflicts with a built-in variant.";
38
+ }
39
+
40
+ @if meta.type-of($value) != "string" {
41
+ @error "Custom media query `#{$key}` must be a string.";
42
+ }
43
+
44
+ $map: map.set($map, $key, $value);
45
+ }
46
+
47
+ $responsive: ();
48
+
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 {
55
+ @error "Breakpoint `#{$key}` conflicts with a media variant.";
56
+ }
57
+
58
+ @if meta.type-of($value) != "number" or $value < 0 or ($value != 0 and math.is-unitless($value)) {
59
+ @error "Breakpoint `#{$key}` must be zero or a non-negative length.";
60
+ }
61
+
62
+ @if $value != 0 {
63
+ $responsive: map.set($responsive, $key, "(min-width: #{$value})");
64
+ }
65
+ }
66
+
67
+ @return map.set($map, responsive, $responsive);
68
+ }
69
+
70
+ $-media-map: -create-media-queries-map() !default;
71
+
72
+ // Returns whether the target consists of exactly one class selector
73
+ //
74
+ // A simple-class target has no type selectors, additional classes, IDs,
75
+ // attributes, pseudo-classes, or pseudo-elements
76
+ // For example, `.button` is a simple-class target,
77
+ // while `.button.active` and `.button:hover` are not
78
+ @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;
85
+ }
86
+
87
+ // Returns whether the target can receive a variant prefix
88
+ //
89
+ // A variant target starts with one class selector and may be followed by
90
+ // pseudo-classes, but not by a type selector, another class, an ID,
91
+ // an attribute, or a pseudo-element. Pseudo-classes are allowed so media variants
92
+ // can process selectors generated by state variants, such as `.hover\:button:hover`
93
+ @function -is-variant-target($selector) {
94
+ @if meta.type-of($selector) != "string" {
95
+ @return false;
96
+ }
97
+
98
+ $simple-selectors: selector.simple-selectors($selector);
99
+
100
+ @if string.index(list.nth($simple-selectors, 1), ".") != 1 {
101
+ @return false;
102
+ }
103
+
104
+ @if list.length($simple-selectors) > 1 {
105
+ @for $i from 2 through list.length($simple-selectors) {
106
+ $part: list.nth($simple-selectors, $i);
107
+
108
+ @if string.index($part, ":") != 1 or string.index($part, "::") == 1 {
109
+ @return false;
110
+ }
111
+ }
112
+ }
113
+
114
+ @return true;
115
+ }
116
+
117
+ // Appends an option suffix to the target class of each selector
118
+ // Only the final compound selector is modified,
119
+ // so `.parent .w` becomes `.parent .w-100`
120
+ //
121
+ // @selectors The list of selectors to modify
122
+ // @option The option name to append to the selector as a suffix
123
+ // @returns The list of modified selectors
124
+ //
125
+ // @example
126
+ // $selector: -append-option(".w", "100"); --> ".w-100"
127
+ // $selector: -append-option(".parent .w", "100"); --> ".parent .w-100"
128
+ // $selector: -append-option(".w", ""); --> ".w"
129
+ // $selector: -append-option(".w", null); --> ".w"
130
+ @function -append-option($selectors, $option) {
131
+ $result: ();
132
+
133
+ @if $option == null or $option == "" {
134
+ @return $selectors;
135
+ }
136
+
137
+ @each $selector in $selectors {
138
+ $target: list.nth($selector, -1);
139
+
140
+ @if not -is-simple-class($target) {
141
+ @error "Expected the final selector segment to be a single class, got `#{$target}`.";
142
+ }
143
+
144
+ $selector: list.set-nth($selector, -1, "#{$target}-#{$option}");
145
+ $result: list.append($result, $selector, comma);
146
+ }
147
+
148
+ @return $result;
149
+ }
150
+
151
+ @function -append-variant($selectors, $variant, $is-media-query: false) {
152
+ $result: ();
153
+
154
+ @each $selector in $selectors {
155
+ // Sass stores each complex selector as a list; its last item is the target
156
+ $target: list.nth($selector, -1);
157
+
158
+ @if not -is-variant-target($target) {
159
+ @error "Expected the final selector segment to be a single class, got `#{$target}`.";
160
+ }
161
+
162
+ // Prefix the target class with the escaped variant separator
163
+ // .class -> .focus\:class
164
+ $target: string.insert($target, "#{$variant}\\:", 2);
165
+
166
+ @if string.index($variant, "group-") == 1 {
167
+ // Replace the target with the group state
168
+ // .cls -> .group:hover
169
+ // .parent .child -> .parent .group:hover
170
+ $selector: list.set-nth($selector, -1, ".group:#{string.slice($variant, 7)}");
171
+
172
+ // Append the prefixed target as a descendant of the group
173
+ // .group:hover -> .group-hover\:cls
174
+ // .parent .group:hover -> .parent .group:hover .group-hover\:child
175
+ $selector: list.append($selector, $target);
176
+ }
177
+ @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);
185
+ }
186
+
187
+ $result: list.append($result, $selector, comma);
188
+ }
189
+
190
+ @return $result;
191
+ }
192
+
193
+ @mixin -options($options, $variants...) {
194
+ $media-queries: ();
195
+ $map: ();
196
+
197
+ @each $variant in $variants {
198
+ $media: map.get($-media-map, $variant);
199
+
200
+ @if meta.type-of($media) == "string" {
201
+ $media-queries: map.set($media-queries, $variant, $media);
202
+ }
203
+ @else if $media {
204
+ $media-queries: map.merge($media-queries, $media);
205
+ }
206
+ }
207
+
208
+ @each $k, $v in $options {
209
+ $root: -append-option(&, $k);
210
+ $list: ($root);
211
+
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
+ }
217
+ }
218
+
219
+ @at-root #{$list} {
220
+ @content($k, $v);
221
+ }
222
+
223
+ $map: map.set($map, $k, $list);
224
+ }
225
+
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
+ }
242
+ }
243
+ }
244
+ }
245
+ }
246
+
247
+ // Print starts from the base selector because interactive states
248
+ // do not apply to printed output
249
+ @if map.has-key($media-queries, "print") {
250
+ @media print {
251
+ @each $k, $v in $options {
252
+ $s: -append-option(&, $k);
253
+ $s: -append-variant($s, "print", true);
254
+
255
+ @at-root #{$s} {
256
+ @content($k, $v);
257
+ }
258
+ }
259
+ }
260
+ }
261
+ }
262
+
263
+ // Emits the base selector and the requested state, group, and media variants
264
+ //
265
+ // @variants The variant names to generate
266
+ @mixin variants($variants...) {
267
+ @if not & {
268
+ @error "variants() must be included inside a selector.";
269
+ }
270
+
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} {
293
+ @content;
294
+ }
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
+ }
341
+
342
+ @mixin responsive() {
343
+ @include variants(responsive) {
344
+ @content;
345
+ }
346
+ }
347
+
348
+ @mixin light() {
349
+ @include variants(light) {
350
+ @content;
351
+ }
352
+ }
353
+
354
+ @mixin dark() {
355
+ @include variants(dark) {
356
+ @content;
357
+ }
358
+ }
359
+
360
+ @mixin colorschemes() {
361
+ @include variants(light, dark) {
362
+ @content;
363
+ }
364
+ }
365
+
366
+ @mixin print() {
367
+ @include variants(print) {
368
+ @content;
369
+ }
370
+ }
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@ramstack/sass-utility-generator",
3
+ "version": "1.0.0",
4
+ "description": "A Sass utility class generator with support for options and variants.",
5
+ "type": "module",
6
+ "author": "rameel <rameel-b@hotmail.com>",
7
+ "license": "MIT",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/rameel/ramstack.sass-utility-generator.git"
11
+ },
12
+ "homepage": "https://github.com/rameel/ramstack.sass-utility-generator#readme",
13
+ "bugs": "https://github.com/rameel/ramstack.sass-utility-generator/issues",
14
+ "keywords": [
15
+ "sass",
16
+ "scss",
17
+ "utilities",
18
+ "css",
19
+ "generator",
20
+ "responsive",
21
+ "variants"
22
+ ],
23
+ "files": [
24
+ "_utility-generator.scss"
25
+ ],
26
+ "sass": "./_utility-generator.scss",
27
+ "exports": {
28
+ ".": {
29
+ "sass": "./_utility-generator.scss"
30
+ }
31
+ },
32
+ "scripts": {
33
+ "clean:samples": "node -e \"require('node:fs').rmSync('samples/css', { recursive: true, force: true })\"",
34
+ "build:samples": "npm run clean:samples && sass --no-source-map samples:samples/css",
35
+ "test": "node --test"
36
+ },
37
+ "peerDependencies": {
38
+ "sass": ">=1.71.0",
39
+ "sass-embedded": ">=1.71.0"
40
+ },
41
+ "peerDependenciesMeta": {
42
+ "sass": {
43
+ "optional": true
44
+ },
45
+ "sass-embedded": {
46
+ "optional": true
47
+ }
48
+ },
49
+ "devDependencies": {
50
+ "sass": "^1.108.2"
51
+ }
52
+ }