@calcifer-design/tokens 0.2.0 → 0.4.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.
@@ -0,0 +1,4 @@
1
+ /** Every `var(--name…)` read in `css` whose name is not in `defined`, deduplicated and sorted. */
2
+ export declare function findUnknownCustomProperties(css: string, defined: ReadonlySet<string>): string[];
3
+ /** Every `--name:` declaration in `css` (a generated sheet or a stylesheet). */
4
+ export declare function customPropertyNamesOf(css: string): Set<string>;
@@ -1,3 +1,16 @@
1
- import type { Tokens } from './tokens.js';
1
+ import type { Theme, TokenValues } from './theme.js';
2
+ import type { SharedTokens, ThemeTokens, Tokens } from './tokens.js';
2
3
  export declare function toKebab(name: string): string;
4
+ /** The mode-independent declarations, one per line, without `color-scheme`. */
5
+ export declare function sharedLinesOf(shared: TokenValues<SharedTokens> | SharedTokens): string[];
6
+ /** One mode's declarations, one per line, without `color-scheme`. */
7
+ export declare function modeLinesOf(mode: TokenValues<ThemeTokens> | ThemeTokens): string[];
8
+ /** The declarations that stop motion under `prefers-reduced-motion`: zero durations, critically damped springs. */
9
+ export declare function reducedMotionLinesOf(shared: TokenValues<SharedTokens> | SharedTokens): string[];
3
10
  export declare function tokensToCss(tokens: Tokens): string;
11
+ /**
12
+ * The complete stylesheet for one brand, scoped to its class: the whole shared set and the light
13
+ * mode on the scope, the dark mode under the host's `data-theme`, the OS preference and a pin on the
14
+ * scope element, and reduced motion. Custom tokens ride in the blocks of their mode.
15
+ */
16
+ export declare function themeToCss(theme: Theme): string;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,10 @@
1
1
  export { breakpoint, themes, shared, textContrastPairs, uiContrastPairs, tokens } from './tokens.js';
2
- export type { BreakpointName, ColorName, ThemeName, Tokens } from './tokens.js';
3
- export { tokensToCss, toKebab } from './generate.js';
2
+ export type { BreakpointName, ColorName, ElevationName, FontName, FontWeightName, RadiusName, ShadowName, SharedTokens, SizeName, TextSizeName, ThemeName, ThemeTokens, Tokens, } from './tokens.js';
3
+ export { themeToCss, tokensToCss, toKebab } from './generate.js';
4
4
  export { contrastRatio, relativeLuminance } from './contrast.js';
5
5
  export { findWidthQueryViolations } from './breakpoint-scan.js';
6
+ export { customPropertyNamesOf, findUnknownCustomProperties } from './custom-property-scan.js';
7
+ export { defineTheme, themeBrand } from './theme.js';
8
+ export type { ContrastPair, CustomNameOf, CustomTokens, NoExtraKeys, Theme, ThemeDefinition, TokenOverrides, TokenValues, } from './theme.js';
9
+ export { checkThemeContrast } from './theme-contrast.js';
10
+ export type { ContrastViolation, ExtraContrastPairs } from './theme-contrast.js';
package/dist/index.js CHANGED
@@ -24,6 +24,7 @@ const lightColor = {
24
24
  accentWash: 'rgba(59, 111, 224, 0.035)',
25
25
  border: 'rgba(28, 25, 23, 0.08)',
26
26
  borderStrong: 'rgba(28, 25, 23, 0.18)',
27
+ borderControl: '#857F79',
27
28
  borderAccent: 'rgba(59, 111, 224, 0.28)',
28
29
  cardBorder: 'transparent',
29
30
  scrim: 'rgba(28, 25, 23, 0.45)',
@@ -37,7 +38,39 @@ const lightColor = {
37
38
  chartPhase1: '#C9D6F5',
38
39
  chartPhase2: '#3B6FE0',
39
40
  chartPhase3: '#8FAAEB',
40
- chartPhase4: '#1F8A5B'
41
+ chartPhase4: '#1F8A5B',
42
+ selected: '#3B6FE0',
43
+ selectedText: '#FFFFFF',
44
+ selectedSoft: 'rgba(59, 111, 224, 0.14)',
45
+ selectedSoftText: '#1C1917',
46
+ inverse: '#1C1917',
47
+ inverseText: '#FFFFFF',
48
+ inverseSoft: 'rgba(255, 255, 255, 0.22)',
49
+ inverseSoftText: '#FFFFFF',
50
+ navText: '#57534E',
51
+ navSelected: 'transparent',
52
+ navSelectedText: '#1C1917',
53
+ tabBar: '#FFFFFF',
54
+ tabBarText: '#57534E',
55
+ tabBarActive: '#3B6FE0',
56
+ tabBarActiveText: '#FFFFFF',
57
+ sheetHandle: 'rgba(28, 25, 23, 0.18)',
58
+ segmentedTrack: '#F4F2EE',
59
+ segmentedBorder: 'rgba(28, 25, 23, 0.18)',
60
+ scrimSheet: 'rgba(28, 25, 23, 0.45)',
61
+ avatarRing: '#FFFFFF',
62
+ avatarFill1: '#ECE9E3',
63
+ avatarFill2: '#ECE9E3',
64
+ avatarFill3: '#ECE9E3',
65
+ avatarFill4: '#ECE9E3',
66
+ avatarFill5: '#ECE9E3',
67
+ avatarFill6: '#ECE9E3',
68
+ avatarInk1: '#57534E',
69
+ avatarInk2: '#57534E',
70
+ avatarInk3: '#57534E',
71
+ avatarInk4: '#57534E',
72
+ avatarInk5: '#57534E',
73
+ avatarInk6: '#57534E'
41
74
  };
42
75
  const darkColor = {
43
76
  surface1: '#0B0D12',
@@ -58,6 +91,7 @@ const darkColor = {
58
91
  accentWash: 'rgba(127, 164, 245, 0.05)',
59
92
  border: 'rgba(255, 255, 255, 0.08)',
60
93
  borderStrong: 'rgba(255, 255, 255, 0.18)',
94
+ borderControl: '#8A909C',
61
95
  borderAccent: 'rgba(127, 164, 245, 0.32)',
62
96
  cardBorder: 'rgba(255, 255, 255, 0.08)',
63
97
  scrim: 'rgba(3, 4, 6, 0.6)',
@@ -71,36 +105,114 @@ const darkColor = {
71
105
  chartPhase1: '#2B3652',
72
106
  chartPhase2: '#7FA4F5',
73
107
  chartPhase3: '#4E6BB0',
74
- chartPhase4: '#4FC38A'
108
+ chartPhase4: '#4FC38A',
109
+ selected: '#7FA4F5',
110
+ selectedText: '#0B0D12',
111
+ selectedSoft: 'rgba(127, 164, 245, 0.18)',
112
+ selectedSoftText: '#EDEEF2',
113
+ inverse: '#EDEEF2',
114
+ inverseText: '#0B0D12',
115
+ inverseSoft: 'rgba(11, 13, 18, 0.22)',
116
+ inverseSoftText: '#0B0D12',
117
+ navText: '#A8ADB8',
118
+ navSelected: 'transparent',
119
+ navSelectedText: '#EDEEF2',
120
+ tabBar: '#111420',
121
+ tabBarText: '#A8ADB8',
122
+ tabBarActive: '#7FA4F5',
123
+ tabBarActiveText: '#0B0D12',
124
+ sheetHandle: 'rgba(255, 255, 255, 0.18)',
125
+ segmentedTrack: '#171B28',
126
+ segmentedBorder: 'rgba(255, 255, 255, 0.18)',
127
+ scrimSheet: 'rgba(3, 4, 6, 0.6)',
128
+ avatarRing: '#111420',
129
+ avatarFill1: '#1F2434',
130
+ avatarFill2: '#1F2434',
131
+ avatarFill3: '#1F2434',
132
+ avatarFill4: '#1F2434',
133
+ avatarFill5: '#1F2434',
134
+ avatarFill6: '#1F2434',
135
+ avatarInk1: '#A8ADB8',
136
+ avatarInk2: '#A8ADB8',
137
+ avatarInk3: '#A8ADB8',
138
+ avatarInk4: '#A8ADB8',
139
+ avatarInk5: '#A8ADB8',
140
+ avatarInk6: '#A8ADB8'
141
+ };
142
+ const lightElevation = {
143
+ 1: '0 2px 8px rgba(28, 25, 23, 0.05), 0 8px 24px rgba(28, 25, 23, 0.05)',
144
+ 2: '0 6px 16px rgba(28, 25, 23, 0.07), 0 20px 48px rgba(28, 25, 23, 0.08)',
145
+ 3: '0 16px 40px rgba(28, 25, 23, 0.10), 0 2px 6px rgba(28, 25, 23, 0.06)'
146
+ };
147
+ const darkElevation = {
148
+ 1: 'inset 0 1px 0 rgba(255, 255, 255, 0.06)',
149
+ 2: 'inset 0 1px 0 rgba(255, 255, 255, 0.06), 0 0 0 1px rgba(255, 255, 255, 0.18)',
150
+ 3: 'inset 0 1px 0 rgba(255, 255, 255, 0.06), 0 16px 40px rgba(0, 0, 0, 0.5)'
151
+ };
152
+ const lightShadow = {
153
+ soft: lightElevation[1],
154
+ chip: lightElevation[1],
155
+ raised: lightElevation[2],
156
+ dialog: lightElevation[3],
157
+ drawer: lightElevation[3],
158
+ sheet: lightElevation[3],
159
+ tabBar: lightElevation[2],
160
+ action: 'none',
161
+ actionInverse: 'none',
162
+ segmented: 'none'
163
+ };
164
+ const darkShadow = {
165
+ soft: darkElevation[1],
166
+ chip: darkElevation[1],
167
+ raised: darkElevation[2],
168
+ dialog: darkElevation[3],
169
+ drawer: darkElevation[3],
170
+ sheet: darkElevation[3],
171
+ tabBar: darkElevation[2],
172
+ action: 'none',
173
+ actionInverse: 'none',
174
+ segmented: 'none'
75
175
  };
76
176
  const themes = {
77
177
  light: {
78
178
  color: lightColor,
79
- elevation: {
80
- 1: '0 2px 8px rgba(28, 25, 23, 0.05), 0 8px 24px rgba(28, 25, 23, 0.05)',
81
- 2: '0 6px 16px rgba(28, 25, 23, 0.07), 0 20px 48px rgba(28, 25, 23, 0.08)',
82
- 3: '0 16px 40px rgba(28, 25, 23, 0.10), 0 2px 6px rgba(28, 25, 23, 0.06)'
83
- },
179
+ elevation: lightElevation,
180
+ shadow: lightShadow,
84
181
  material: {
85
182
  glassBg: 'rgba(251, 250, 248, 0.78)'
86
183
  }
87
184
  },
88
185
  dark: {
89
186
  color: darkColor,
90
- elevation: {
91
- 1: 'inset 0 1px 0 rgba(255, 255, 255, 0.06)',
92
- 2: 'inset 0 1px 0 rgba(255, 255, 255, 0.06), 0 0 0 1px rgba(255, 255, 255, 0.18)',
93
- 3: 'inset 0 1px 0 rgba(255, 255, 255, 0.06), 0 16px 40px rgba(0, 0, 0, 0.5)'
94
- },
187
+ elevation: darkElevation,
188
+ shadow: darkShadow,
95
189
  material: {
96
190
  glassBg: 'rgba(11, 13, 18, 0.78)'
97
191
  }
98
192
  }
99
193
  };
100
- const shared = {
194
+ const sansStack = '"Geist Variable", "Geist", -apple-system, "SF Pro Display", "Segoe UI", Helvetica, Arial, sans-serif';
195
+ const tokens_shared = {
101
196
  font: {
102
- sans: '"Geist Variable", "Geist", -apple-system, "SF Pro Display", "Segoe UI", Helvetica, Arial, sans-serif',
103
- mono: '"Geist Mono Variable", "Geist Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace'
197
+ sans: sansStack,
198
+ mono: '"Geist Mono Variable", "Geist Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace',
199
+ display: sansStack,
200
+ body: sansStack
201
+ },
202
+ fontWeight: {
203
+ body: '400',
204
+ label: '500',
205
+ button: '500',
206
+ heading: '600',
207
+ nav: '400',
208
+ navSelected: '500',
209
+ display: '600'
210
+ },
211
+ fontStretch: {
212
+ display: 'normal'
213
+ },
214
+ textTransform: {
215
+ display: 'none'
104
216
  },
105
217
  text: {
106
218
  '2xs': '0.6875rem',
@@ -149,7 +261,13 @@ const shared = {
149
261
  md: '0.625rem',
150
262
  lg: '0.75rem',
151
263
  xl: '1rem',
152
- full: '999px'
264
+ full: '999px',
265
+ control: '0.625rem',
266
+ option: '0.5rem',
267
+ panel: '0.625rem',
268
+ popup: '0.75rem',
269
+ card: '1rem',
270
+ dialog: '1rem'
153
271
  },
154
272
  borderWidth: {
155
273
  emphasis: '2px',
@@ -176,7 +294,11 @@ const shared = {
176
294
  header: '4rem',
177
295
  headerWide: '4.5rem',
178
296
  pageMax: '75rem',
179
- pageMaxWide: 'min(112.5rem, 90vw)'
297
+ pageMaxWide: 'min(112.5rem, 90vw)',
298
+ drawer: '28rem',
299
+ control: '44px',
300
+ controlSm: '2.5rem',
301
+ close: '2.25rem'
180
302
  },
181
303
  measure: '65ch',
182
304
  focusRingWidth: '2px',
@@ -258,6 +380,42 @@ const textContrastPairs = [
258
380
  [
259
381
  'textInverse',
260
382
  'surfaceInverse'
383
+ ],
384
+ [
385
+ 'selectedText',
386
+ 'selected'
387
+ ],
388
+ [
389
+ 'inverseText',
390
+ 'inverse'
391
+ ],
392
+ [
393
+ 'tabBarActiveText',
394
+ 'tabBarActive'
395
+ ],
396
+ [
397
+ 'avatarInk1',
398
+ 'avatarFill1'
399
+ ],
400
+ [
401
+ 'avatarInk2',
402
+ 'avatarFill2'
403
+ ],
404
+ [
405
+ 'avatarInk3',
406
+ 'avatarFill3'
407
+ ],
408
+ [
409
+ 'avatarInk4',
410
+ 'avatarFill4'
411
+ ],
412
+ [
413
+ 'avatarInk5',
414
+ 'avatarFill5'
415
+ ],
416
+ [
417
+ 'avatarInk6',
418
+ 'avatarFill6'
261
419
  ]
262
420
  ];
263
421
  const uiContrastPairs = [
@@ -348,13 +506,101 @@ const uiContrastPairs = [
348
506
  [
349
507
  'neutral',
350
508
  'surface2'
509
+ ],
510
+ [
511
+ 'selected',
512
+ 'surface1'
513
+ ],
514
+ [
515
+ 'selected',
516
+ 'surfaceRaised'
517
+ ],
518
+ [
519
+ 'tabBarText',
520
+ 'tabBar'
521
+ ],
522
+ [
523
+ 'borderControl',
524
+ 'surface1'
525
+ ],
526
+ [
527
+ 'borderControl',
528
+ 'surface2'
529
+ ],
530
+ [
531
+ 'borderControl',
532
+ 'surfaceRaised'
533
+ ],
534
+ [
535
+ 'borderControl',
536
+ 'surfaceInput'
351
537
  ]
352
538
  ];
353
539
  const tokens_tokens = {
354
540
  breakpoint: breakpoint,
355
541
  themes: themes,
356
- shared: shared
542
+ shared: tokens_shared
357
543
  };
544
+ const themeBrand = Symbol('calcifer-design-theme');
545
+ const classNamePattern = /^[a-z][a-z0-9-]*$/;
546
+ const prefixPattern = /^[a-z][a-z0-9]*$/;
547
+ const libraryFamilyRoots = new Set([
548
+ ...Object.keys(tokens_tokens.shared),
549
+ ...Object.keys(tokens_tokens.themes.light)
550
+ ].map((key)=>/^[a-z]+/.exec(key)?.[0] ?? key));
551
+ function isPlainTree(value) {
552
+ return 'object' == typeof value && null !== value && !Array.isArray(value);
553
+ }
554
+ function mergeTree(base, overrides) {
555
+ const overrideTree = isPlainTree(overrides) ? overrides : {};
556
+ const merged = {};
557
+ for (const [key, baseValue] of Object.entries(base)){
558
+ const override = Object.hasOwn(overrideTree, key) ? overrideTree[key] : void 0;
559
+ if (isPlainTree(baseValue)) merged[key] = mergeTree(baseValue, override);
560
+ else merged[key] = void 0 === override ? baseValue : override;
561
+ }
562
+ return merged;
563
+ }
564
+ function matchesPattern(value, pattern) {
565
+ return 'string' == typeof value && pattern.test(value);
566
+ }
567
+ function deepFreeze(value) {
568
+ if ('object' == typeof value && null !== value) {
569
+ for (const member of Object.values(value))deepFreeze(member);
570
+ Object.freeze(value);
571
+ }
572
+ return value;
573
+ }
574
+ function defineTheme(definition) {
575
+ const { className, custom, contrastExemptions } = definition;
576
+ if (!matchesPattern(className, classNamePattern)) throw new Error(`defineTheme: className "${className}" must match ${classNamePattern}`);
577
+ if (void 0 !== custom && !matchesPattern(custom.prefix, prefixPattern)) throw new Error(`defineTheme: custom.prefix "${custom.prefix}" must match ${prefixPattern}`);
578
+ if (void 0 !== custom && libraryFamilyRoots.has(custom.prefix)) throw new Error(`defineTheme: custom.prefix "${custom.prefix}" would shadow the library's --${custom.prefix}-* tokens`);
579
+ const theme = {
580
+ className,
581
+ shared: mergeTree(tokens_tokens.shared, definition.shared),
582
+ light: mergeTree(tokens_tokens.themes.light, definition.light),
583
+ dark: mergeTree(tokens_tokens.themes.dark, definition.dark),
584
+ custom: {
585
+ prefix: void 0 === custom ? null : custom.prefix,
586
+ shared: {
587
+ ...custom?.shared
588
+ },
589
+ light: {
590
+ ...custom?.light
591
+ },
592
+ dark: {
593
+ ...custom?.dark
594
+ }
595
+ },
596
+ contrastExemptions: (contrastExemptions ?? []).map((pair)=>[
597
+ pair[0],
598
+ pair[1]
599
+ ]),
600
+ [themeBrand]: true
601
+ };
602
+ return deepFreeze(theme);
603
+ }
358
604
  function toKebab(name) {
359
605
  return name.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/(\d)([A-Z])/g, '$1-$2').replace(/([a-zA-Z]{2,})(\d)/g, '$1-$2').toLowerCase();
360
606
  }
@@ -369,37 +615,55 @@ function block(selector, lines, indent = '') {
369
615
  const body = lines.map((line)=>`${indent}${line}`).join('\n');
370
616
  return `${indent}${selector} {\n${body}\n${indent}}`;
371
617
  }
618
+ function sharedLinesOf(shared) {
619
+ const lines = [];
620
+ flatten('--font', shared.font, lines);
621
+ flatten('--font-weight', shared.fontWeight, lines);
622
+ flatten('--font-stretch', shared.fontStretch, lines);
623
+ flatten('--text-transform', shared.textTransform, lines);
624
+ flatten('--text', shared.text, lines);
625
+ flatten('--leading', shared.leading, lines);
626
+ flatten('--tracking', shared.tracking, lines);
627
+ flatten('--space', shared.space, lines);
628
+ flatten('--radius', shared.radius, lines);
629
+ flatten('--border-width', shared.borderWidth, lines);
630
+ flatten('--motion', shared.motion, lines);
631
+ flatten('--size', shared.size, lines);
632
+ flatten('--material', shared.material, lines);
633
+ lines.push(` --measure: ${shared.measure};`);
634
+ lines.push(` --focus-ring-width: ${shared.focusRingWidth};`);
635
+ lines.push(` --focus-ring-offset: ${shared.focusRingOffset};`);
636
+ return lines;
637
+ }
638
+ function modeLinesOf(mode) {
639
+ const lines = [];
640
+ flatten('--color', mode.color, lines);
641
+ flatten('--elevation', mode.elevation, lines);
642
+ flatten('--shadow', mode.shadow, lines);
643
+ flatten('--material', mode.material, lines);
644
+ return lines;
645
+ }
646
+ function reducedMotionLinesOf(shared) {
647
+ return [
648
+ ...Object.keys(shared.motion.duration).map((step)=>` --motion-duration-${step}: 0ms;`),
649
+ ' --motion-spring-default-damping: 1;',
650
+ ' --motion-spring-momentum-damping: 1;'
651
+ ];
652
+ }
653
+ function customLinesOf(prefix, values) {
654
+ const lines = [];
655
+ if (null !== prefix) flatten(`--${prefix}`, values, lines);
656
+ return lines;
657
+ }
372
658
  function tokensToCss(tokens) {
373
659
  const sharedLines = [
374
- ' color-scheme: light dark;'
660
+ ' color-scheme: light dark;',
661
+ ...sharedLinesOf(tokens.shared)
375
662
  ];
376
- flatten('--font', tokens.shared.font, sharedLines);
377
- flatten('--text', tokens.shared.text, sharedLines);
378
- flatten('--leading', tokens.shared.leading, sharedLines);
379
- flatten('--tracking', tokens.shared.tracking, sharedLines);
380
- flatten('--space', tokens.shared.space, sharedLines);
381
- flatten('--radius', tokens.shared.radius, sharedLines);
382
- flatten('--border-width', tokens.shared.borderWidth, sharedLines);
383
- flatten('--motion', tokens.shared.motion, sharedLines);
384
- flatten('--size', tokens.shared.size, sharedLines);
385
- flatten('--material', tokens.shared.material, sharedLines);
386
- sharedLines.push(` --measure: ${tokens.shared.measure};`);
387
- sharedLines.push(` --focus-ring-width: ${tokens.shared.focusRingWidth};`);
388
- sharedLines.push(` --focus-ring-offset: ${tokens.shared.focusRingOffset};`);
389
- const lightLines = [];
390
- flatten('--color', tokens.themes.light.color, lightLines);
391
- flatten('--elevation', tokens.themes.light.elevation, lightLines);
392
- flatten('--material', tokens.themes.light.material, lightLines);
663
+ const lightLines = modeLinesOf(tokens.themes.light);
393
664
  const darkLines = [
394
- ' color-scheme: dark;'
395
- ];
396
- flatten('--color', tokens.themes.dark.color, darkLines);
397
- flatten('--elevation', tokens.themes.dark.elevation, darkLines);
398
- flatten('--material', tokens.themes.dark.material, darkLines);
399
- const reducedMotionLines = [
400
- ...Object.keys(tokens.shared.motion.duration).map((step)=>` --motion-duration-${step}: 0ms;`),
401
- ' --motion-spring-default-damping: 1;',
402
- ' --motion-spring-momentum-damping: 1;'
665
+ ' color-scheme: dark;',
666
+ ...modeLinesOf(tokens.themes.dark)
403
667
  ];
404
668
  return [
405
669
  '/* Generated from packages/tokens/src/tokens.ts. Do not edit. */',
@@ -412,10 +676,42 @@ function tokensToCss(tokens) {
412
676
  ' color-scheme: light;'
413
677
  ]),
414
678
  `@media (prefers-color-scheme: dark) {\n${block(':root:not([data-theme="light"])', darkLines, ' ')}\n}`,
415
- `@media (prefers-reduced-motion: reduce) {\n${block(':root', reducedMotionLines, ' ')}\n}`,
679
+ `@media (prefers-reduced-motion: reduce) {\n${block(':root', reducedMotionLinesOf(tokens.shared), ' ')}\n}`,
416
680
  ''
417
681
  ].join('\n\n');
418
682
  }
683
+ function themeToCss(theme) {
684
+ if (true !== theme[themeBrand]) throw new Error('themeToCss: not a theme made by defineTheme');
685
+ const scope = `.${theme.className}`;
686
+ const { prefix } = theme.custom;
687
+ const lightModeLines = modeLinesOf(theme.light);
688
+ const customLightLines = customLinesOf(prefix, theme.custom.light);
689
+ const darkLines = [
690
+ ' color-scheme: dark;',
691
+ ...modeLinesOf(theme.dark),
692
+ ...customLinesOf(prefix, theme.custom.dark)
693
+ ];
694
+ const sections = [
695
+ `/* Generated by @calcifer-design/tokens themeToCss for "${scope}". Do not edit. */`,
696
+ block(scope, [
697
+ ' color-scheme: light;',
698
+ ...sharedLinesOf(theme.shared),
699
+ ...lightModeLines,
700
+ ...customLinesOf(prefix, theme.custom.shared),
701
+ ...customLightLines
702
+ ]),
703
+ block(`:root[data-theme="dark"] ${scope}`, darkLines),
704
+ `@media (prefers-color-scheme: dark) {\n${block(`:root:not([data-theme="light"]) ${scope}`, darkLines, ' ')}\n}`,
705
+ block(`:root ${scope}[data-theme="light"]`, [
706
+ ' color-scheme: light;',
707
+ ...lightModeLines,
708
+ ...customLightLines
709
+ ]),
710
+ block(`:root ${scope}[data-theme="dark"]`, darkLines),
711
+ `@media (prefers-reduced-motion: reduce) {\n${block(scope, reducedMotionLinesOf(theme.shared), ' ')}\n}`
712
+ ];
713
+ return `${sections.join('\n\n')}\n`;
714
+ }
419
715
  function channelToLinear(channel) {
420
716
  const scaled = channel / 255;
421
717
  return scaled <= 0.04045 ? scaled / 12.92 : ((scaled + 0.055) / 1.055) ** 2.4;
@@ -454,4 +750,98 @@ function findWidthQueryViolations(css, allowedWidths) {
454
750
  }
455
751
  return violations;
456
752
  }
457
- export { breakpoint, contrastRatio, findWidthQueryViolations, relativeLuminance, shared, textContrastPairs, themes, toKebab, tokensToCss, tokens_tokens as tokens, uiContrastPairs };
753
+ const readPattern = /var\(\s*(--[A-Za-z0-9_-]+)/g;
754
+ const declarationPattern = /(?:^|[\s;{])(--[A-Za-z0-9_-]+)\s*:/g;
755
+ function findUnknownCustomProperties(css, defined) {
756
+ const unknownNames = new Set();
757
+ for (const match of css.matchAll(readPattern)){
758
+ const name = match[1] ?? '';
759
+ if (!defined.has(name)) unknownNames.add(name);
760
+ }
761
+ return [
762
+ ...unknownNames
763
+ ].sort();
764
+ }
765
+ function customPropertyNamesOf(css) {
766
+ const names = new Set();
767
+ for (const match of css.matchAll(declarationPattern))names.add(match[1] ?? '');
768
+ return names;
769
+ }
770
+ const hexPattern = /^#[0-9A-Fa-f]{6}$/;
771
+ const modes = [
772
+ 'light',
773
+ 'dark'
774
+ ];
775
+ function isHex(value) {
776
+ return 'string' == typeof value && hexPattern.test(value);
777
+ }
778
+ function pairKey([foreground, background]) {
779
+ return `${foreground}/${background}`;
780
+ }
781
+ function checkThemeContrast(theme, extraPairs = {}) {
782
+ if (true !== theme[themeBrand]) throw new Error('checkThemeContrast: not a theme made by defineTheme');
783
+ const lists = [
784
+ {
785
+ pairs: textContrastPairs,
786
+ required: 4.5
787
+ },
788
+ {
789
+ pairs: extraPairs.text ?? [],
790
+ required: 4.5
791
+ },
792
+ {
793
+ pairs: uiContrastPairs,
794
+ required: 3
795
+ },
796
+ {
797
+ pairs: extraPairs.ui ?? [],
798
+ required: 3
799
+ }
800
+ ];
801
+ const exemptKeys = new Set(theme.contrastExemptions.map(pairKey));
802
+ const listedKeys = new Set();
803
+ const violations = [];
804
+ for (const { pairs, required } of lists)for (const pair of pairs){
805
+ const key = pairKey(pair);
806
+ listedKeys.add(key);
807
+ if (exemptKeys.has(key)) continue;
808
+ const [foreground, background] = pair;
809
+ for (const mode of modes){
810
+ const foregroundColor = theme[mode].color[foreground];
811
+ const backgroundColor = theme[mode].color[background];
812
+ if (!isHex(foregroundColor) || !isHex(backgroundColor)) {
813
+ violations.push({
814
+ mode,
815
+ foreground,
816
+ background,
817
+ kind: 'not-hex',
818
+ ratio: null,
819
+ required
820
+ });
821
+ continue;
822
+ }
823
+ const ratio = contrastRatio(foregroundColor, backgroundColor);
824
+ if (ratio < required) violations.push({
825
+ mode,
826
+ foreground,
827
+ background,
828
+ kind: 'ratio',
829
+ ratio,
830
+ required
831
+ });
832
+ }
833
+ }
834
+ for (const exemption of theme.contrastExemptions)if (!listedKeys.has(pairKey(exemption))) {
835
+ const [foreground, background] = exemption;
836
+ violations.push({
837
+ mode: 'light',
838
+ foreground,
839
+ background,
840
+ kind: 'unknown-exemption',
841
+ ratio: null,
842
+ required: null
843
+ });
844
+ }
845
+ return violations;
846
+ }
847
+ export { breakpoint, checkThemeContrast, contrastRatio, customPropertyNamesOf, defineTheme, findUnknownCustomProperties, findWidthQueryViolations, relativeLuminance, textContrastPairs, themeBrand, themeToCss, themes, toKebab, tokensToCss, tokens_shared as shared, tokens_tokens as tokens, uiContrastPairs };
@@ -0,0 +1,26 @@
1
+ import type { ContrastPair, Theme } from './theme.js';
2
+ import type { ColorName, ThemeName } from './tokens.js';
3
+ export interface ContrastViolation {
4
+ readonly mode: ThemeName;
5
+ readonly foreground: ColorName;
6
+ readonly background: ColorName;
7
+ /** `ratio` fell short; a colour is not an opaque six-digit hex; an exemption names no listed pair. */
8
+ readonly kind: 'ratio' | 'not-hex' | 'unknown-exemption';
9
+ /** `null` for `not-hex` and `unknown-exemption`. */
10
+ readonly ratio: number | null;
11
+ /** The list's floor (4.5 text, 3 ui); `null` for `unknown-exemption`. */
12
+ readonly required: 3 | 4.5 | null;
13
+ }
14
+ export interface ExtraContrastPairs {
15
+ readonly text?: ReadonlyArray<ContrastPair>;
16
+ readonly ui?: ReadonlyArray<ContrastPair>;
17
+ }
18
+ /**
19
+ * Holds `theme` to the library's contrast pairs plus `extraPairs` (pairs only this theme's values
20
+ * make opaque), in both modes. Pairs in `theme.contrastExemptions` are skipped in both modes; an
21
+ * exemption naming no listed pair is reported, so it cannot outlive its pair.
22
+ *
23
+ * Order (L1-6): text pairs, `extraPairs.text`, ui pairs, `extraPairs.ui`; within a list each pair
24
+ * `light` then `dark`; `unknown-exemption` entries last, one per exemption.
25
+ */
26
+ export declare function checkThemeContrast(theme: Theme, extraPairs?: ExtraContrastPairs): ContrastViolation[];