@seyuna/postcss 1.0.0-canary.14 → 1.0.0-canary.15

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.
Files changed (66) hide show
  1. package/.github/workflows/release.yml +1 -1
  2. package/CHANGELOG.md +7 -0
  3. package/README.md +179 -0
  4. package/dist/at-rules/color-scheme.d.ts +4 -0
  5. package/dist/at-rules/color-scheme.js +43 -0
  6. package/dist/at-rules/color.d.ts +3 -2
  7. package/dist/at-rules/color.js +10 -21
  8. package/dist/at-rules/conditional.d.ts +6 -0
  9. package/dist/at-rules/conditional.js +29 -0
  10. package/dist/at-rules/container.d.ts +2 -1
  11. package/dist/at-rules/container.js +12 -8
  12. package/dist/at-rules/custom-media.d.ts +15 -0
  13. package/dist/at-rules/custom-media.js +40 -0
  14. package/dist/at-rules/index.d.ts +3 -2
  15. package/dist/at-rules/index.js +22 -22
  16. package/dist/at-rules/mixin.d.ts +10 -0
  17. package/dist/at-rules/mixin.js +37 -0
  18. package/dist/config.d.ts +20 -0
  19. package/dist/config.js +47 -0
  20. package/dist/errors.d.ts +7 -0
  21. package/dist/errors.js +14 -0
  22. package/dist/functions/color.d.ts +7 -2
  23. package/dist/functions/color.js +103 -27
  24. package/dist/functions/index.d.ts +2 -1
  25. package/dist/functions/index.js +10 -12
  26. package/dist/functions/theme.d.ts +6 -0
  27. package/dist/functions/theme.js +17 -0
  28. package/dist/helpers.d.ts +3 -2
  29. package/dist/helpers.js +13 -28
  30. package/dist/index.d.ts +2 -2
  31. package/dist/index.js +6 -5
  32. package/dist/parser.d.ts +4 -0
  33. package/dist/parser.js +59 -0
  34. package/dist/plugin.d.ts +2 -4
  35. package/dist/plugin.js +17 -22
  36. package/dist/types.d.ts +1 -19
  37. package/dist/types.js +1 -2
  38. package/package.json +12 -5
  39. package/src/at-rules/color-scheme.ts +52 -0
  40. package/src/at-rules/color.ts +11 -15
  41. package/src/at-rules/conditional.ts +34 -0
  42. package/src/at-rules/container.ts +13 -3
  43. package/src/at-rules/custom-media.ts +50 -0
  44. package/src/at-rules/index.ts +13 -6
  45. package/src/at-rules/mixin.ts +46 -0
  46. package/src/config.ts +74 -0
  47. package/src/errors.ts +23 -0
  48. package/src/functions/color.ts +120 -26
  49. package/src/functions/index.ts +9 -4
  50. package/src/functions/theme.ts +20 -0
  51. package/src/helpers.ts +23 -22
  52. package/src/index.ts +3 -1
  53. package/src/parser.ts +80 -0
  54. package/src/plugin.ts +13 -21
  55. package/src/types.ts +1 -19
  56. package/tests/plugin.test.ts +251 -0
  57. package/tsconfig.json +2 -2
  58. package/dist/at-rules/dark.d.ts +0 -23
  59. package/dist/at-rules/dark.js +0 -65
  60. package/dist/at-rules/light.d.ts +0 -23
  61. package/dist/at-rules/light.js +0 -65
  62. package/dist/functions/spacing.d.ts +0 -1
  63. package/dist/functions/spacing.js +0 -6
  64. package/src/at-rules/dark.ts +0 -69
  65. package/src/at-rules/light.ts +0 -69
  66. package/src/functions/spacing.ts +0 -3
package/src/plugin.ts CHANGED
@@ -1,34 +1,26 @@
1
1
  import { PluginCreator } from "postcss";
2
2
  import { functions } from "./functions";
3
3
  import { atRuleHandlers } from "./at-rules";
4
+ import { loadConfig, PluginOptions as ConfigOptions, PluginContext } from "./config";
5
+ import { processFunctions, FunctionMap } from "./parser";
4
6
 
5
- export interface PluginOptions {
6
- functions?: Record<string, (...args: string[]) => string>;
7
- }
8
-
9
- export const dynamicFunctionsPlugin: PluginCreator<PluginOptions> = (
7
+ export const dynamicFunctionsPlugin: PluginCreator<ConfigOptions> = (
10
8
  opts = {}
11
9
  ) => {
12
- const fnMap = { ...functions, ...opts.functions };
10
+ const { config, options } = loadConfig(opts);
11
+ const fnMap: FunctionMap = { ...functions, ...opts.functions };
12
+ const context: PluginContext = {
13
+ config,
14
+ options,
15
+ functions: fnMap,
16
+ mixins: {},
17
+ };
13
18
 
14
19
  return {
15
20
  postcssPlugin: "postcss-dynamic-functions",
16
21
 
17
22
  Declaration(decl) {
18
- let value = decl.value;
19
-
20
- for (const fnName in fnMap) {
21
- const regex = new RegExp(`${fnName}\\(([^)]*)\\)`, "g");
22
-
23
- value = value.replace(regex, (_match, argsStr) => {
24
- const args = argsStr
25
- .split(",")
26
- .map((a: string) => a.trim().replace(/^['"]|['"]$/g, ""));
27
- return fnMap[fnName](...args);
28
- });
29
- }
30
-
31
- decl.value = value;
23
+ decl.value = processFunctions(decl.value, fnMap, decl, context);
32
24
  },
33
25
 
34
26
  // Override AtRule handler to ensure ordered execution
@@ -36,7 +28,7 @@ export const dynamicFunctionsPlugin: PluginCreator<PluginOptions> = (
36
28
  // Iterate over handlers in order (array) instead of object spread
37
29
  for (const { name, handler } of atRuleHandlers) {
38
30
  if (atRule.name === name) {
39
- handler(atRule);
31
+ handler(atRule, context);
40
32
  }
41
33
  }
42
34
  },
package/src/types.ts CHANGED
@@ -1,19 +1 @@
1
- export interface SeyunaConfig {
2
- ui: {
3
- theme: {
4
- hues: Record<string, number>;
5
- light: {
6
- colors: Record<string, Color>;
7
- };
8
- dark: {
9
- colors: Record<string, Color>;
10
- };
11
- };
12
- };
13
- }
14
-
15
- type Color = {
16
- lightness: number;
17
- chroma: number;
18
- hue: number;
19
- };
1
+ export type { Config as SeyunaConfig, Color } from "@seyuna/cli/types";
@@ -0,0 +1,251 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import postcss from 'postcss';
3
+ import plugin from '../src/index';
4
+
5
+ const mockConfig = {
6
+ ui: {
7
+ theme: {
8
+ hues: {
9
+ primary: "200",
10
+ secondary: "100"
11
+ },
12
+ light: {
13
+ lightness: 0.66,
14
+ colors: {
15
+ surface: { lightness: 1, chroma: 0, hue: 0 }
16
+ }
17
+ },
18
+ dark: {
19
+ lightness: 0.66,
20
+ colors: {
21
+ surface: { lightness: 0, chroma: 0, hue: 0 }
22
+ }
23
+ },
24
+ breakpoints: {
25
+ tablet: "48rem"
26
+ }
27
+ }
28
+ }
29
+ };
30
+
31
+ async function run(input: string, opts = {}) {
32
+ return postcss([plugin(opts)]).process(input, { from: undefined });
33
+ }
34
+
35
+ describe('Seyuna PostCSS Plugin', () => {
36
+ it('processes sc() function', async () => {
37
+ const input = '.test { color: sc(primary); }';
38
+ const output = '.test { color: oklch(var(--lightness) var(--chroma) var(--primary-hue) / 1)';
39
+ const result = await run(input);
40
+ expect(result.css).toContain(output);
41
+ });
42
+
43
+ it('processes sc() with alpha', async () => {
44
+ const input = '.test { color: sc(primary, 0.5); }';
45
+ const output = '.test { color: oklch(var(--lightness) var(--chroma) var(--primary-hue) / 0.5)';
46
+ const result = await run(input);
47
+ expect(result.css).toContain(output);
48
+ });
49
+
50
+ it('processes alpha() function', async () => {
51
+ const input = '.test { color: alpha(oklch(0.5 0.1 200 / 1), 0.5); }';
52
+ const output = 'color: oklch(0.5 0.1 200 / 0.5)';
53
+ const result = await run(input);
54
+ expect(result.css).toContain(output);
55
+ });
56
+
57
+ it('processes lighten() function', async () => {
58
+ const input = '.test { color: lighten(oklch(0.5 0.1 200 / 1), 0.1); }';
59
+ const output = 'color: oklch(0.6 0.1 200 / 1)';
60
+ const result = await run(input);
61
+ expect(result.css).toContain(output);
62
+ });
63
+
64
+ it('processes lighten() with CSS variables', async () => {
65
+ const input = '.test { color: lighten(oklch(var(--l) 0.1 200 / 1), 0.1); }';
66
+ const output = 'color: oklch(calc(var(--l) + 0.1) 0.1 200 / 1)';
67
+ const result = await run(input);
68
+ expect(result.css).toContain(output);
69
+ });
70
+
71
+ it('processes theme() function', async () => {
72
+ // We pass the config via opts for testing
73
+ const input = '.test { border-radius: theme(ui.theme.breakpoints.tablet); }';
74
+ const result = await run(input, { config: mockConfig });
75
+ expect(result.css).toContain('.test { border-radius: 48rem');
76
+ });
77
+
78
+ it('processes @xs container at-rule', async () => {
79
+ const input = '@xs { .box { color: red; } }';
80
+ const result = await run(input);
81
+ expect(result.css).toContain('@container (min-width: 20rem)');
82
+ expect(result.css).toContain('.box { color: red');
83
+ });
84
+
85
+ it('processes @light at-rule', async () => {
86
+ const input = '@light { .test { color: black; } }';
87
+ const result = await run(input);
88
+ expect(result.css).toContain('@media (prefers-color-scheme: light)');
89
+ expect(result.css).toContain('[data-mode="system"] &');
90
+ expect(result.css).toContain('[data-mode="light"] &');
91
+ });
92
+
93
+ it('processes mixins using @define-mixin and @apply', async () => {
94
+ const input = `
95
+ @define-mixin btn {
96
+ padding: 1rem;
97
+ border-radius: 4px;
98
+ }
99
+ .primary-btn {
100
+ @apply btn;
101
+ color: white;
102
+ }
103
+ `;
104
+ const result = await run(input);
105
+ expect(result.css).toContain('.primary-btn {');
106
+ expect(result.css).toContain('padding: 1rem');
107
+ expect(result.css).toContain('border-radius: 4px');
108
+ expect(result.css).toContain('color: white');
109
+ expect(result.css).not.toContain('@define-mixin');
110
+ });
111
+
112
+ it('processes @if directive with literal', async () => {
113
+ const input = `
114
+ .test {
115
+ @if (true) {
116
+ color: red;
117
+ }
118
+ @if (false) {
119
+ color: blue;
120
+ }
121
+ }
122
+ `;
123
+ const result = await run(input);
124
+ expect(result.css).toContain('color: red');
125
+ expect(result.css).not.toContain('color: blue');
126
+ });
127
+
128
+ it('processes @if directive with theme()', async () => {
129
+ const input = `
130
+ .glass {
131
+ @if (theme(ui.features.glass)) {
132
+ backdrop-filter: blur(10px);
133
+ }
134
+ }
135
+ `;
136
+ const config = {
137
+ ui: {
138
+ features: {
139
+ glass: "true"
140
+ }
141
+ }
142
+ };
143
+ const result = await run(input, { config });
144
+ expect(result.css).toContain('backdrop-filter: blur(10px)');
145
+ });
146
+
147
+ it('resolves custom media tokens', async () => {
148
+ const input = `
149
+ @media (--tablet) {
150
+ .sidebar { display: block; }
151
+ }
152
+ `;
153
+ const config = {
154
+ media: {
155
+ tablet: "(min-width: 768px)"
156
+ }
157
+ };
158
+ const result = await run(input, { config });
159
+ expect(result.css).toContain('@media (min-width: 768px)');
160
+ });
161
+
162
+ it('handles @custom-media directive', async () => {
163
+ const input = `
164
+ @custom-media --mobile (max-width: 480px);
165
+ @media (--mobile) {
166
+ .full-width { width: 100%; }
167
+ }
168
+ `;
169
+ const result = await run(input);
170
+ expect(result.css).toContain('@media (max-width: 480px)');
171
+ });
172
+
173
+ it('processes alpha() with color name', async () => {
174
+ const input = '.test { color: alpha(primary, 0.5); }';
175
+ const output = 'color: oklch(var(--primary-lightness) var(--primary-chroma) var(--primary-hue) / 0.5)';
176
+ const result = await run(input);
177
+ expect(result.css).toContain(output);
178
+ });
179
+
180
+ it('processes lighten() with color name', async () => {
181
+ const input = '.test { color: lighten(primary, 0.1); }';
182
+ const output = 'color: oklch(calc(var(--primary-lightness) + 0.1) var(--primary-chroma) var(--primary-hue) / 1)';
183
+ const result = await run(input);
184
+ expect(result.css).toContain(output);
185
+ });
186
+
187
+ it('processes darken() with color name', async () => {
188
+ const input = '.test { color: darken(primary, 0.1); }';
189
+ const output = 'color: oklch(calc(var(--primary-lightness) - 0.1) var(--primary-chroma) var(--primary-hue) / 1)';
190
+ const result = await run(input);
191
+ expect(result.css).toContain(output);
192
+ });
193
+
194
+ it('processes contrast() with color name', async () => {
195
+ const input = '.test { color: contrast(surface); }';
196
+ const output = 'color: oklch(calc((var(--surface-lightness) - 0.6) * -1000) 0 0)';
197
+ const result = await run(input, { config: mockConfig });
198
+ expect(result.css).toContain(output);
199
+ });
200
+
201
+ it('processes contrast() with standard color', async () => {
202
+ const input = '.test { color: contrast(primary); }';
203
+ const output = 'color: oklch(calc((var(--lightness) - 0.6) * -1000) 0 0)';
204
+ const result = await run(input, { config: mockConfig });
205
+ expect(result.css).toContain(output);
206
+ });
207
+
208
+ it('processes alpha() with standard color (sc)', async () => {
209
+ const input = '.test { color: alpha(primary, 0.5); }';
210
+ // 'primary' is in hues in mockConfig, so it should use var(--lightness) and var(--chroma)
211
+ const output = 'color: oklch(var(--lightness) var(--chroma) var(--primary-hue) / 0.5)';
212
+ const result = await run(input, { config: mockConfig });
213
+ expect(result.css).toContain(output);
214
+ });
215
+
216
+ it('processes alpha() with fixed color (fc)', async () => {
217
+ const input = '.test { color: alpha(surface, 0.5); }';
218
+ // 'surface' is in light.colors/dark.colors in mockConfig, so it should use its own lightness/chroma
219
+ const output = 'color: oklch(var(--surface-lightness) var(--surface-chroma) var(--surface-hue) / 0.5)';
220
+ const result = await run(input, { config: mockConfig });
221
+ expect(result.css).toContain(output);
222
+ });
223
+
224
+ it('processes nested functions: contrast(fc(white))', async () => {
225
+ const input = '.test { color: contrast(fc(white)); }';
226
+ // fc(white) -> oklch(var(--white-lightness) ...)
227
+ // contrast extracts lightness -> var(--white-lightness)
228
+ const output = 'color: oklch(calc((var(--white-lightness) - 0.6) * -1000) 0 0)';
229
+ const result = await run(input, { config: mockConfig });
230
+ expect(result.css).toContain(output);
231
+ });
232
+
233
+ it('processes nested functions: alpha(sc(primary), 0.5)', async () => {
234
+ const input = '.test { color: alpha(sc(primary), 0.5); }';
235
+ // sc(primary) -> oklch(var(--lightness) var(--chroma) var(--primary-hue) / 1)
236
+ // alpha(..., 0.5) extracts components and reapplies alpha
237
+ const output = 'color: oklch(var(--lightness) var(--chroma) var(--primary-hue) / 0.5)';
238
+ const result = await run(input, { config: mockConfig });
239
+ expect(result.css).toContain(output);
240
+ });
241
+
242
+ it('processes multiple nested functions: contrast(alpha(fc(surface), 1))', async () => {
243
+ const input = '.test { color: contrast(alpha(fc(surface), 1)); }';
244
+ // inner-most: fc(surface) -> oklch(var(--surface-lightness) ...)
245
+ // next: alpha(..., 1) -> oklch(var(--surface-lightness) ...)
246
+ // outer: contrast extracts lightness -> var(--surface-lightness)
247
+ const output = 'color: oklch(calc((var(--surface-lightness) - 0.6) * -1000) 0 0)';
248
+ const result = await run(input, { config: mockConfig });
249
+ expect(result.css).toContain(output);
250
+ });
251
+ });
package/tsconfig.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  "target": "ES2020",
4
- "module": "CommonJS",
4
+ "module": "ES2020",
5
5
  "lib": ["ES2020"],
6
6
  "declaration": true,
7
7
  "outDir": "dist",
8
8
  "strict": true,
9
- "moduleResolution": "node",
9
+ "moduleResolution": "bundler",
10
10
  "esModuleInterop": true,
11
11
  "skipLibCheck": true
12
12
  },
@@ -1,23 +0,0 @@
1
- import { AtRule } from "postcss";
2
- /**
3
- * Custom PostCSS plugin handler for `@dark` at-rules.
4
- *
5
- * Transforms:
6
- *
7
- * @dark {
8
- * color: white;
9
- * }
10
- *
11
- * Into:
12
- *
13
- * @media (prefers-color-scheme: dark) {
14
- * [data-mode="system"] & {
15
- * color: white;
16
- * }
17
- * }
18
- *
19
- * [data-mode="dark"] & {
20
- * color: white;
21
- * }
22
- */
23
- export default function dark(atRule: AtRule): void;
@@ -1,65 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = dark;
4
- const postcss_1 = require("postcss");
5
- /**
6
- * Custom PostCSS plugin handler for `@dark` at-rules.
7
- *
8
- * Transforms:
9
- *
10
- * @dark {
11
- * color: white;
12
- * }
13
- *
14
- * Into:
15
- *
16
- * @media (prefers-color-scheme: dark) {
17
- * [data-mode="system"] & {
18
- * color: white;
19
- * }
20
- * }
21
- *
22
- * [data-mode="dark"] & {
23
- * color: white;
24
- * }
25
- */
26
- function dark(atRule) {
27
- const clonedNodes = [];
28
- // Clone all child nodes inside the @dark block
29
- // (so we can reuse them in both generated rules).
30
- atRule.each((node) => {
31
- clonedNodes.push(node.clone());
32
- });
33
- /**
34
- * Rule 1: [data-mode="dark"] & { ... }
35
- *
36
- * This applies the styles when the user explicitly sets dark mode.
37
- */
38
- const darkRule = new postcss_1.Rule({
39
- selector: `[data-mode="dark"] &`,
40
- });
41
- clonedNodes.forEach((node) => darkRule.append(node.clone()));
42
- /**
43
- * Rule 2: @media (prefers-color-scheme: dark) { [data-mode="system"] & { ... } }
44
- *
45
- * This applies the styles only when:
46
- * - The user’s OS prefers dark mode
47
- * - AND the app is in "system" mode (i.e. follow system preference)
48
- */
49
- const mediaAtRule = new postcss_1.AtRule({
50
- name: "media",
51
- params: "(prefers-color-scheme: dark)",
52
- });
53
- // Wrap cloned rules under `[data-mode="system"]`
54
- const systemRule = new postcss_1.Rule({
55
- selector: `[data-mode="system"] &`,
56
- });
57
- clonedNodes.forEach((node) => systemRule.append(node.clone()));
58
- // Nest `[data-mode="system"]` inside the @media block
59
- mediaAtRule.append(systemRule);
60
- /**
61
- * Replace the original @dark rule in the CSS tree
62
- * with our two new generated rules.
63
- */
64
- atRule.replaceWith(mediaAtRule, darkRule);
65
- }
@@ -1,23 +0,0 @@
1
- import { AtRule } from "postcss";
2
- /**
3
- * Custom PostCSS plugin handler for `@light` at-rules.
4
- *
5
- * Transforms:
6
- *
7
- * @light {
8
- * color: white;
9
- * }
10
- *
11
- * Into:
12
- *
13
- * @media (prefers-color-scheme: light) {
14
- * [data-mode="system"] & {
15
- * color: white;
16
- * }
17
- * }
18
- *
19
- * [data-mode="light"] & {
20
- * color: white;
21
- * }
22
- */
23
- export default function light(atRule: AtRule): void;
@@ -1,65 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = light;
4
- const postcss_1 = require("postcss");
5
- /**
6
- * Custom PostCSS plugin handler for `@light` at-rules.
7
- *
8
- * Transforms:
9
- *
10
- * @light {
11
- * color: white;
12
- * }
13
- *
14
- * Into:
15
- *
16
- * @media (prefers-color-scheme: light) {
17
- * [data-mode="system"] & {
18
- * color: white;
19
- * }
20
- * }
21
- *
22
- * [data-mode="light"] & {
23
- * color: white;
24
- * }
25
- */
26
- function light(atRule) {
27
- const clonedNodes = [];
28
- // Clone all child nodes inside the @light block
29
- // (so we can reuse them in both generated rules).
30
- atRule.each((node) => {
31
- clonedNodes.push(node.clone());
32
- });
33
- /**
34
- * Rule 1: [data-mode="light"] & { ... }
35
- *
36
- * This applies the styles when the user explicitly sets light mode.
37
- */
38
- const lightRule = new postcss_1.Rule({
39
- selector: `[data-mode="light"] &`,
40
- });
41
- clonedNodes.forEach((node) => lightRule.append(node.clone()));
42
- /**
43
- * Rule 2: @media (prefers-color-scheme: light) { [data-mode="system"] & { ... } }
44
- *
45
- * This applies the styles only when:
46
- * - The user’s OS prefers light mode
47
- * - AND the app is in "system" mode (i.e. follow system preference)
48
- */
49
- const mediaAtRule = new postcss_1.AtRule({
50
- name: "media",
51
- params: "(prefers-color-scheme: light)",
52
- });
53
- // Wrap cloned rules under `[data-mode="system"]`
54
- const systemRule = new postcss_1.Rule({
55
- selector: `[data-mode="system"] &`,
56
- });
57
- clonedNodes.forEach((node) => systemRule.append(node.clone()));
58
- // Nest `[data-mode="system"]` inside the @media block
59
- mediaAtRule.append(systemRule);
60
- /**
61
- * Replace the original @light rule in the CSS tree
62
- * with our two new generated rules.
63
- */
64
- atRule.replaceWith(mediaAtRule, lightRule);
65
- }
@@ -1 +0,0 @@
1
- export default function spacing(multiplier: string): string;
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = spacing;
4
- function spacing(multiplier) {
5
- return `${parseFloat(multiplier) * 1}rem`;
6
- }
@@ -1,69 +0,0 @@
1
- import { Rule, AtRule, ChildNode } from "postcss";
2
-
3
- /**
4
- * Custom PostCSS plugin handler for `@dark` at-rules.
5
- *
6
- * Transforms:
7
- *
8
- * @dark {
9
- * color: white;
10
- * }
11
- *
12
- * Into:
13
- *
14
- * @media (prefers-color-scheme: dark) {
15
- * [data-mode="system"] & {
16
- * color: white;
17
- * }
18
- * }
19
- *
20
- * [data-mode="dark"] & {
21
- * color: white;
22
- * }
23
- */
24
- export default function dark(atRule: AtRule) {
25
- const clonedNodes: ChildNode[] = [];
26
-
27
- // Clone all child nodes inside the @dark block
28
- // (so we can reuse them in both generated rules).
29
- atRule.each((node: ChildNode) => {
30
- clonedNodes.push(node.clone());
31
- });
32
-
33
- /**
34
- * Rule 1: [data-mode="dark"] & { ... }
35
- *
36
- * This applies the styles when the user explicitly sets dark mode.
37
- */
38
- const darkRule = new Rule({
39
- selector: `[data-mode="dark"] &`,
40
- });
41
- clonedNodes.forEach((node) => darkRule.append(node.clone()));
42
-
43
- /**
44
- * Rule 2: @media (prefers-color-scheme: dark) { [data-mode="system"] & { ... } }
45
- *
46
- * This applies the styles only when:
47
- * - The user’s OS prefers dark mode
48
- * - AND the app is in "system" mode (i.e. follow system preference)
49
- */
50
- const mediaAtRule = new AtRule({
51
- name: "media",
52
- params: "(prefers-color-scheme: dark)",
53
- });
54
-
55
- // Wrap cloned rules under `[data-mode="system"]`
56
- const systemRule = new Rule({
57
- selector: `[data-mode="system"] &`,
58
- });
59
- clonedNodes.forEach((node) => systemRule.append(node.clone()));
60
-
61
- // Nest `[data-mode="system"]` inside the @media block
62
- mediaAtRule.append(systemRule);
63
-
64
- /**
65
- * Replace the original @dark rule in the CSS tree
66
- * with our two new generated rules.
67
- */
68
- atRule.replaceWith(mediaAtRule, darkRule);
69
- }
@@ -1,69 +0,0 @@
1
- import { Rule, AtRule, ChildNode } from "postcss";
2
-
3
- /**
4
- * Custom PostCSS plugin handler for `@light` at-rules.
5
- *
6
- * Transforms:
7
- *
8
- * @light {
9
- * color: white;
10
- * }
11
- *
12
- * Into:
13
- *
14
- * @media (prefers-color-scheme: light) {
15
- * [data-mode="system"] & {
16
- * color: white;
17
- * }
18
- * }
19
- *
20
- * [data-mode="light"] & {
21
- * color: white;
22
- * }
23
- */
24
- export default function light(atRule: AtRule) {
25
- const clonedNodes: ChildNode[] = [];
26
-
27
- // Clone all child nodes inside the @light block
28
- // (so we can reuse them in both generated rules).
29
- atRule.each((node: ChildNode) => {
30
- clonedNodes.push(node.clone());
31
- });
32
-
33
- /**
34
- * Rule 1: [data-mode="light"] & { ... }
35
- *
36
- * This applies the styles when the user explicitly sets light mode.
37
- */
38
- const lightRule = new Rule({
39
- selector: `[data-mode="light"] &`,
40
- });
41
- clonedNodes.forEach((node) => lightRule.append(node.clone()));
42
-
43
- /**
44
- * Rule 2: @media (prefers-color-scheme: light) { [data-mode="system"] & { ... } }
45
- *
46
- * This applies the styles only when:
47
- * - The user’s OS prefers light mode
48
- * - AND the app is in "system" mode (i.e. follow system preference)
49
- */
50
- const mediaAtRule = new AtRule({
51
- name: "media",
52
- params: "(prefers-color-scheme: light)",
53
- });
54
-
55
- // Wrap cloned rules under `[data-mode="system"]`
56
- const systemRule = new Rule({
57
- selector: `[data-mode="system"] &`,
58
- });
59
- clonedNodes.forEach((node) => systemRule.append(node.clone()));
60
-
61
- // Nest `[data-mode="system"]` inside the @media block
62
- mediaAtRule.append(systemRule);
63
-
64
- /**
65
- * Replace the original @light rule in the CSS tree
66
- * with our two new generated rules.
67
- */
68
- atRule.replaceWith(mediaAtRule, lightRule);
69
- }
@@ -1,3 +0,0 @@
1
- export default function spacing(multiplier: string) {
2
- return `${parseFloat(multiplier) * 1}rem`;
3
- }