@calcifer-design/tokens 0.1.1 → 0.3.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/dist/custom-property-scan.d.ts +4 -0
- package/dist/generate.d.ts +14 -1
- package/dist/index.d.ts +7 -2
- package/dist/index.js +421 -47
- package/dist/theme-contrast.d.ts +26 -0
- package/dist/theme.d.ts +61 -0
- package/dist/tokens.css +150 -0
- package/dist/tokens.d.ts +301 -0
- package/package.json +2 -2
|
@@ -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>;
|
package/dist/generate.d.ts
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
-
import type {
|
|
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
|
@@ -26,6 +26,7 @@ const lightColor = {
|
|
|
26
26
|
borderStrong: 'rgba(28, 25, 23, 0.18)',
|
|
27
27
|
borderAccent: 'rgba(59, 111, 224, 0.28)',
|
|
28
28
|
cardBorder: 'transparent',
|
|
29
|
+
scrim: 'rgba(28, 25, 23, 0.45)',
|
|
29
30
|
focus: '#3B6FE0',
|
|
30
31
|
success: '#1F8A5B',
|
|
31
32
|
warning: '#B7791F',
|
|
@@ -36,7 +37,39 @@ const lightColor = {
|
|
|
36
37
|
chartPhase1: '#C9D6F5',
|
|
37
38
|
chartPhase2: '#3B6FE0',
|
|
38
39
|
chartPhase3: '#8FAAEB',
|
|
39
|
-
chartPhase4: '#1F8A5B'
|
|
40
|
+
chartPhase4: '#1F8A5B',
|
|
41
|
+
selected: '#3B6FE0',
|
|
42
|
+
selectedText: '#FFFFFF',
|
|
43
|
+
selectedSoft: 'rgba(59, 111, 224, 0.14)',
|
|
44
|
+
selectedSoftText: '#1C1917',
|
|
45
|
+
inverse: '#1C1917',
|
|
46
|
+
inverseText: '#FFFFFF',
|
|
47
|
+
inverseSoft: 'rgba(255, 255, 255, 0.22)',
|
|
48
|
+
inverseSoftText: '#FFFFFF',
|
|
49
|
+
navText: '#57534E',
|
|
50
|
+
navSelected: 'transparent',
|
|
51
|
+
navSelectedText: '#1C1917',
|
|
52
|
+
tabBar: '#FFFFFF',
|
|
53
|
+
tabBarText: '#57534E',
|
|
54
|
+
tabBarActive: '#3B6FE0',
|
|
55
|
+
tabBarActiveText: '#FFFFFF',
|
|
56
|
+
sheetHandle: 'rgba(28, 25, 23, 0.18)',
|
|
57
|
+
segmentedTrack: '#F4F2EE',
|
|
58
|
+
segmentedBorder: 'rgba(28, 25, 23, 0.18)',
|
|
59
|
+
scrimSheet: 'rgba(28, 25, 23, 0.45)',
|
|
60
|
+
avatarRing: '#FFFFFF',
|
|
61
|
+
avatarFill1: '#ECE9E3',
|
|
62
|
+
avatarFill2: '#ECE9E3',
|
|
63
|
+
avatarFill3: '#ECE9E3',
|
|
64
|
+
avatarFill4: '#ECE9E3',
|
|
65
|
+
avatarFill5: '#ECE9E3',
|
|
66
|
+
avatarFill6: '#ECE9E3',
|
|
67
|
+
avatarInk1: '#57534E',
|
|
68
|
+
avatarInk2: '#57534E',
|
|
69
|
+
avatarInk3: '#57534E',
|
|
70
|
+
avatarInk4: '#57534E',
|
|
71
|
+
avatarInk5: '#57534E',
|
|
72
|
+
avatarInk6: '#57534E'
|
|
40
73
|
};
|
|
41
74
|
const darkColor = {
|
|
42
75
|
surface1: '#0B0D12',
|
|
@@ -59,6 +92,7 @@ const darkColor = {
|
|
|
59
92
|
borderStrong: 'rgba(255, 255, 255, 0.18)',
|
|
60
93
|
borderAccent: 'rgba(127, 164, 245, 0.32)',
|
|
61
94
|
cardBorder: 'rgba(255, 255, 255, 0.08)',
|
|
95
|
+
scrim: 'rgba(3, 4, 6, 0.6)',
|
|
62
96
|
focus: '#7FA4F5',
|
|
63
97
|
success: '#4FC38A',
|
|
64
98
|
warning: '#E0B04A',
|
|
@@ -69,36 +103,114 @@ const darkColor = {
|
|
|
69
103
|
chartPhase1: '#2B3652',
|
|
70
104
|
chartPhase2: '#7FA4F5',
|
|
71
105
|
chartPhase3: '#4E6BB0',
|
|
72
|
-
chartPhase4: '#4FC38A'
|
|
106
|
+
chartPhase4: '#4FC38A',
|
|
107
|
+
selected: '#7FA4F5',
|
|
108
|
+
selectedText: '#0B0D12',
|
|
109
|
+
selectedSoft: 'rgba(127, 164, 245, 0.18)',
|
|
110
|
+
selectedSoftText: '#EDEEF2',
|
|
111
|
+
inverse: '#EDEEF2',
|
|
112
|
+
inverseText: '#0B0D12',
|
|
113
|
+
inverseSoft: 'rgba(11, 13, 18, 0.22)',
|
|
114
|
+
inverseSoftText: '#0B0D12',
|
|
115
|
+
navText: '#A8ADB8',
|
|
116
|
+
navSelected: 'transparent',
|
|
117
|
+
navSelectedText: '#EDEEF2',
|
|
118
|
+
tabBar: '#111420',
|
|
119
|
+
tabBarText: '#A8ADB8',
|
|
120
|
+
tabBarActive: '#7FA4F5',
|
|
121
|
+
tabBarActiveText: '#0B0D12',
|
|
122
|
+
sheetHandle: 'rgba(255, 255, 255, 0.18)',
|
|
123
|
+
segmentedTrack: '#171B28',
|
|
124
|
+
segmentedBorder: 'rgba(255, 255, 255, 0.18)',
|
|
125
|
+
scrimSheet: 'rgba(3, 4, 6, 0.6)',
|
|
126
|
+
avatarRing: '#111420',
|
|
127
|
+
avatarFill1: '#1F2434',
|
|
128
|
+
avatarFill2: '#1F2434',
|
|
129
|
+
avatarFill3: '#1F2434',
|
|
130
|
+
avatarFill4: '#1F2434',
|
|
131
|
+
avatarFill5: '#1F2434',
|
|
132
|
+
avatarFill6: '#1F2434',
|
|
133
|
+
avatarInk1: '#A8ADB8',
|
|
134
|
+
avatarInk2: '#A8ADB8',
|
|
135
|
+
avatarInk3: '#A8ADB8',
|
|
136
|
+
avatarInk4: '#A8ADB8',
|
|
137
|
+
avatarInk5: '#A8ADB8',
|
|
138
|
+
avatarInk6: '#A8ADB8'
|
|
139
|
+
};
|
|
140
|
+
const lightElevation = {
|
|
141
|
+
1: '0 2px 8px rgba(28, 25, 23, 0.05), 0 8px 24px rgba(28, 25, 23, 0.05)',
|
|
142
|
+
2: '0 6px 16px rgba(28, 25, 23, 0.07), 0 20px 48px rgba(28, 25, 23, 0.08)',
|
|
143
|
+
3: '0 16px 40px rgba(28, 25, 23, 0.10), 0 2px 6px rgba(28, 25, 23, 0.06)'
|
|
144
|
+
};
|
|
145
|
+
const darkElevation = {
|
|
146
|
+
1: 'inset 0 1px 0 rgba(255, 255, 255, 0.06)',
|
|
147
|
+
2: 'inset 0 1px 0 rgba(255, 255, 255, 0.06), 0 0 0 1px rgba(255, 255, 255, 0.18)',
|
|
148
|
+
3: 'inset 0 1px 0 rgba(255, 255, 255, 0.06), 0 16px 40px rgba(0, 0, 0, 0.5)'
|
|
149
|
+
};
|
|
150
|
+
const lightShadow = {
|
|
151
|
+
soft: lightElevation[1],
|
|
152
|
+
chip: lightElevation[1],
|
|
153
|
+
raised: lightElevation[2],
|
|
154
|
+
dialog: lightElevation[3],
|
|
155
|
+
drawer: lightElevation[3],
|
|
156
|
+
sheet: lightElevation[3],
|
|
157
|
+
tabBar: lightElevation[2],
|
|
158
|
+
action: 'none',
|
|
159
|
+
actionInverse: 'none',
|
|
160
|
+
segmented: 'none'
|
|
161
|
+
};
|
|
162
|
+
const darkShadow = {
|
|
163
|
+
soft: darkElevation[1],
|
|
164
|
+
chip: darkElevation[1],
|
|
165
|
+
raised: darkElevation[2],
|
|
166
|
+
dialog: darkElevation[3],
|
|
167
|
+
drawer: darkElevation[3],
|
|
168
|
+
sheet: darkElevation[3],
|
|
169
|
+
tabBar: darkElevation[2],
|
|
170
|
+
action: 'none',
|
|
171
|
+
actionInverse: 'none',
|
|
172
|
+
segmented: 'none'
|
|
73
173
|
};
|
|
74
174
|
const themes = {
|
|
75
175
|
light: {
|
|
76
176
|
color: lightColor,
|
|
77
|
-
elevation:
|
|
78
|
-
|
|
79
|
-
2: '0 6px 16px rgba(28, 25, 23, 0.07), 0 20px 48px rgba(28, 25, 23, 0.08)',
|
|
80
|
-
3: '0 16px 40px rgba(28, 25, 23, 0.10), 0 2px 6px rgba(28, 25, 23, 0.06)'
|
|
81
|
-
},
|
|
177
|
+
elevation: lightElevation,
|
|
178
|
+
shadow: lightShadow,
|
|
82
179
|
material: {
|
|
83
180
|
glassBg: 'rgba(251, 250, 248, 0.78)'
|
|
84
181
|
}
|
|
85
182
|
},
|
|
86
183
|
dark: {
|
|
87
184
|
color: darkColor,
|
|
88
|
-
elevation:
|
|
89
|
-
|
|
90
|
-
2: 'inset 0 1px 0 rgba(255, 255, 255, 0.06), 0 0 0 1px rgba(255, 255, 255, 0.18)',
|
|
91
|
-
3: 'inset 0 1px 0 rgba(255, 255, 255, 0.06), 0 16px 40px rgba(0, 0, 0, 0.5)'
|
|
92
|
-
},
|
|
185
|
+
elevation: darkElevation,
|
|
186
|
+
shadow: darkShadow,
|
|
93
187
|
material: {
|
|
94
188
|
glassBg: 'rgba(11, 13, 18, 0.78)'
|
|
95
189
|
}
|
|
96
190
|
}
|
|
97
191
|
};
|
|
98
|
-
const
|
|
192
|
+
const sansStack = '"Geist Variable", "Geist", -apple-system, "SF Pro Display", "Segoe UI", Helvetica, Arial, sans-serif';
|
|
193
|
+
const tokens_shared = {
|
|
99
194
|
font: {
|
|
100
|
-
sans:
|
|
101
|
-
mono: '"Geist Mono Variable", "Geist Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace'
|
|
195
|
+
sans: sansStack,
|
|
196
|
+
mono: '"Geist Mono Variable", "Geist Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace',
|
|
197
|
+
display: sansStack,
|
|
198
|
+
body: sansStack
|
|
199
|
+
},
|
|
200
|
+
fontWeight: {
|
|
201
|
+
body: '400',
|
|
202
|
+
label: '500',
|
|
203
|
+
button: '500',
|
|
204
|
+
heading: '600',
|
|
205
|
+
nav: '400',
|
|
206
|
+
navSelected: '500',
|
|
207
|
+
display: '600'
|
|
208
|
+
},
|
|
209
|
+
fontStretch: {
|
|
210
|
+
display: 'normal'
|
|
211
|
+
},
|
|
212
|
+
textTransform: {
|
|
213
|
+
display: 'none'
|
|
102
214
|
},
|
|
103
215
|
text: {
|
|
104
216
|
'2xs': '0.6875rem',
|
|
@@ -147,7 +259,13 @@ const shared = {
|
|
|
147
259
|
md: '0.625rem',
|
|
148
260
|
lg: '0.75rem',
|
|
149
261
|
xl: '1rem',
|
|
150
|
-
full: '999px'
|
|
262
|
+
full: '999px',
|
|
263
|
+
control: '0.625rem',
|
|
264
|
+
option: '0.5rem',
|
|
265
|
+
panel: '0.625rem',
|
|
266
|
+
popup: '0.75rem',
|
|
267
|
+
card: '1rem',
|
|
268
|
+
dialog: '1rem'
|
|
151
269
|
},
|
|
152
270
|
borderWidth: {
|
|
153
271
|
emphasis: '2px',
|
|
@@ -174,7 +292,11 @@ const shared = {
|
|
|
174
292
|
header: '4rem',
|
|
175
293
|
headerWide: '4.5rem',
|
|
176
294
|
pageMax: '75rem',
|
|
177
|
-
pageMaxWide: 'min(112.5rem, 90vw)'
|
|
295
|
+
pageMaxWide: 'min(112.5rem, 90vw)',
|
|
296
|
+
drawer: '28rem',
|
|
297
|
+
control: '44px',
|
|
298
|
+
controlSm: '2.5rem',
|
|
299
|
+
close: '2.25rem'
|
|
178
300
|
},
|
|
179
301
|
measure: '65ch',
|
|
180
302
|
focusRingWidth: '2px',
|
|
@@ -256,6 +378,42 @@ const textContrastPairs = [
|
|
|
256
378
|
[
|
|
257
379
|
'textInverse',
|
|
258
380
|
'surfaceInverse'
|
|
381
|
+
],
|
|
382
|
+
[
|
|
383
|
+
'selectedText',
|
|
384
|
+
'selected'
|
|
385
|
+
],
|
|
386
|
+
[
|
|
387
|
+
'inverseText',
|
|
388
|
+
'inverse'
|
|
389
|
+
],
|
|
390
|
+
[
|
|
391
|
+
'tabBarActiveText',
|
|
392
|
+
'tabBarActive'
|
|
393
|
+
],
|
|
394
|
+
[
|
|
395
|
+
'avatarInk1',
|
|
396
|
+
'avatarFill1'
|
|
397
|
+
],
|
|
398
|
+
[
|
|
399
|
+
'avatarInk2',
|
|
400
|
+
'avatarFill2'
|
|
401
|
+
],
|
|
402
|
+
[
|
|
403
|
+
'avatarInk3',
|
|
404
|
+
'avatarFill3'
|
|
405
|
+
],
|
|
406
|
+
[
|
|
407
|
+
'avatarInk4',
|
|
408
|
+
'avatarFill4'
|
|
409
|
+
],
|
|
410
|
+
[
|
|
411
|
+
'avatarInk5',
|
|
412
|
+
'avatarFill5'
|
|
413
|
+
],
|
|
414
|
+
[
|
|
415
|
+
'avatarInk6',
|
|
416
|
+
'avatarFill6'
|
|
259
417
|
]
|
|
260
418
|
];
|
|
261
419
|
const uiContrastPairs = [
|
|
@@ -346,13 +504,85 @@ const uiContrastPairs = [
|
|
|
346
504
|
[
|
|
347
505
|
'neutral',
|
|
348
506
|
'surface2'
|
|
507
|
+
],
|
|
508
|
+
[
|
|
509
|
+
'selected',
|
|
510
|
+
'surface1'
|
|
511
|
+
],
|
|
512
|
+
[
|
|
513
|
+
'selected',
|
|
514
|
+
'surfaceRaised'
|
|
515
|
+
],
|
|
516
|
+
[
|
|
517
|
+
'tabBarText',
|
|
518
|
+
'tabBar'
|
|
349
519
|
]
|
|
350
520
|
];
|
|
351
521
|
const tokens_tokens = {
|
|
352
522
|
breakpoint: breakpoint,
|
|
353
523
|
themes: themes,
|
|
354
|
-
shared:
|
|
524
|
+
shared: tokens_shared
|
|
355
525
|
};
|
|
526
|
+
const themeBrand = Symbol('calcifer-design-theme');
|
|
527
|
+
const classNamePattern = /^[a-z][a-z0-9-]*$/;
|
|
528
|
+
const prefixPattern = /^[a-z][a-z0-9]*$/;
|
|
529
|
+
const libraryFamilyRoots = new Set([
|
|
530
|
+
...Object.keys(tokens_tokens.shared),
|
|
531
|
+
...Object.keys(tokens_tokens.themes.light)
|
|
532
|
+
].map((key)=>/^[a-z]+/.exec(key)?.[0] ?? key));
|
|
533
|
+
function isPlainTree(value) {
|
|
534
|
+
return 'object' == typeof value && null !== value && !Array.isArray(value);
|
|
535
|
+
}
|
|
536
|
+
function mergeTree(base, overrides) {
|
|
537
|
+
const overrideTree = isPlainTree(overrides) ? overrides : {};
|
|
538
|
+
const merged = {};
|
|
539
|
+
for (const [key, baseValue] of Object.entries(base)){
|
|
540
|
+
const override = Object.hasOwn(overrideTree, key) ? overrideTree[key] : void 0;
|
|
541
|
+
if (isPlainTree(baseValue)) merged[key] = mergeTree(baseValue, override);
|
|
542
|
+
else merged[key] = void 0 === override ? baseValue : override;
|
|
543
|
+
}
|
|
544
|
+
return merged;
|
|
545
|
+
}
|
|
546
|
+
function matchesPattern(value, pattern) {
|
|
547
|
+
return 'string' == typeof value && pattern.test(value);
|
|
548
|
+
}
|
|
549
|
+
function deepFreeze(value) {
|
|
550
|
+
if ('object' == typeof value && null !== value) {
|
|
551
|
+
for (const member of Object.values(value))deepFreeze(member);
|
|
552
|
+
Object.freeze(value);
|
|
553
|
+
}
|
|
554
|
+
return value;
|
|
555
|
+
}
|
|
556
|
+
function defineTheme(definition) {
|
|
557
|
+
const { className, custom, contrastExemptions } = definition;
|
|
558
|
+
if (!matchesPattern(className, classNamePattern)) throw new Error(`defineTheme: className "${className}" must match ${classNamePattern}`);
|
|
559
|
+
if (void 0 !== custom && !matchesPattern(custom.prefix, prefixPattern)) throw new Error(`defineTheme: custom.prefix "${custom.prefix}" must match ${prefixPattern}`);
|
|
560
|
+
if (void 0 !== custom && libraryFamilyRoots.has(custom.prefix)) throw new Error(`defineTheme: custom.prefix "${custom.prefix}" would shadow the library's --${custom.prefix}-* tokens`);
|
|
561
|
+
const theme = {
|
|
562
|
+
className,
|
|
563
|
+
shared: mergeTree(tokens_tokens.shared, definition.shared),
|
|
564
|
+
light: mergeTree(tokens_tokens.themes.light, definition.light),
|
|
565
|
+
dark: mergeTree(tokens_tokens.themes.dark, definition.dark),
|
|
566
|
+
custom: {
|
|
567
|
+
prefix: void 0 === custom ? null : custom.prefix,
|
|
568
|
+
shared: {
|
|
569
|
+
...custom?.shared
|
|
570
|
+
},
|
|
571
|
+
light: {
|
|
572
|
+
...custom?.light
|
|
573
|
+
},
|
|
574
|
+
dark: {
|
|
575
|
+
...custom?.dark
|
|
576
|
+
}
|
|
577
|
+
},
|
|
578
|
+
contrastExemptions: (contrastExemptions ?? []).map((pair)=>[
|
|
579
|
+
pair[0],
|
|
580
|
+
pair[1]
|
|
581
|
+
]),
|
|
582
|
+
[themeBrand]: true
|
|
583
|
+
};
|
|
584
|
+
return deepFreeze(theme);
|
|
585
|
+
}
|
|
356
586
|
function toKebab(name) {
|
|
357
587
|
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();
|
|
358
588
|
}
|
|
@@ -367,37 +597,55 @@ function block(selector, lines, indent = '') {
|
|
|
367
597
|
const body = lines.map((line)=>`${indent}${line}`).join('\n');
|
|
368
598
|
return `${indent}${selector} {\n${body}\n${indent}}`;
|
|
369
599
|
}
|
|
600
|
+
function sharedLinesOf(shared) {
|
|
601
|
+
const lines = [];
|
|
602
|
+
flatten('--font', shared.font, lines);
|
|
603
|
+
flatten('--font-weight', shared.fontWeight, lines);
|
|
604
|
+
flatten('--font-stretch', shared.fontStretch, lines);
|
|
605
|
+
flatten('--text-transform', shared.textTransform, lines);
|
|
606
|
+
flatten('--text', shared.text, lines);
|
|
607
|
+
flatten('--leading', shared.leading, lines);
|
|
608
|
+
flatten('--tracking', shared.tracking, lines);
|
|
609
|
+
flatten('--space', shared.space, lines);
|
|
610
|
+
flatten('--radius', shared.radius, lines);
|
|
611
|
+
flatten('--border-width', shared.borderWidth, lines);
|
|
612
|
+
flatten('--motion', shared.motion, lines);
|
|
613
|
+
flatten('--size', shared.size, lines);
|
|
614
|
+
flatten('--material', shared.material, lines);
|
|
615
|
+
lines.push(` --measure: ${shared.measure};`);
|
|
616
|
+
lines.push(` --focus-ring-width: ${shared.focusRingWidth};`);
|
|
617
|
+
lines.push(` --focus-ring-offset: ${shared.focusRingOffset};`);
|
|
618
|
+
return lines;
|
|
619
|
+
}
|
|
620
|
+
function modeLinesOf(mode) {
|
|
621
|
+
const lines = [];
|
|
622
|
+
flatten('--color', mode.color, lines);
|
|
623
|
+
flatten('--elevation', mode.elevation, lines);
|
|
624
|
+
flatten('--shadow', mode.shadow, lines);
|
|
625
|
+
flatten('--material', mode.material, lines);
|
|
626
|
+
return lines;
|
|
627
|
+
}
|
|
628
|
+
function reducedMotionLinesOf(shared) {
|
|
629
|
+
return [
|
|
630
|
+
...Object.keys(shared.motion.duration).map((step)=>` --motion-duration-${step}: 0ms;`),
|
|
631
|
+
' --motion-spring-default-damping: 1;',
|
|
632
|
+
' --motion-spring-momentum-damping: 1;'
|
|
633
|
+
];
|
|
634
|
+
}
|
|
635
|
+
function customLinesOf(prefix, values) {
|
|
636
|
+
const lines = [];
|
|
637
|
+
if (null !== prefix) flatten(`--${prefix}`, values, lines);
|
|
638
|
+
return lines;
|
|
639
|
+
}
|
|
370
640
|
function tokensToCss(tokens) {
|
|
371
641
|
const sharedLines = [
|
|
372
|
-
' color-scheme: light dark;'
|
|
642
|
+
' color-scheme: light dark;',
|
|
643
|
+
...sharedLinesOf(tokens.shared)
|
|
373
644
|
];
|
|
374
|
-
|
|
375
|
-
flatten('--text', tokens.shared.text, sharedLines);
|
|
376
|
-
flatten('--leading', tokens.shared.leading, sharedLines);
|
|
377
|
-
flatten('--tracking', tokens.shared.tracking, sharedLines);
|
|
378
|
-
flatten('--space', tokens.shared.space, sharedLines);
|
|
379
|
-
flatten('--radius', tokens.shared.radius, sharedLines);
|
|
380
|
-
flatten('--border-width', tokens.shared.borderWidth, sharedLines);
|
|
381
|
-
flatten('--motion', tokens.shared.motion, sharedLines);
|
|
382
|
-
flatten('--size', tokens.shared.size, sharedLines);
|
|
383
|
-
flatten('--material', tokens.shared.material, sharedLines);
|
|
384
|
-
sharedLines.push(` --measure: ${tokens.shared.measure};`);
|
|
385
|
-
sharedLines.push(` --focus-ring-width: ${tokens.shared.focusRingWidth};`);
|
|
386
|
-
sharedLines.push(` --focus-ring-offset: ${tokens.shared.focusRingOffset};`);
|
|
387
|
-
const lightLines = [];
|
|
388
|
-
flatten('--color', tokens.themes.light.color, lightLines);
|
|
389
|
-
flatten('--elevation', tokens.themes.light.elevation, lightLines);
|
|
390
|
-
flatten('--material', tokens.themes.light.material, lightLines);
|
|
645
|
+
const lightLines = modeLinesOf(tokens.themes.light);
|
|
391
646
|
const darkLines = [
|
|
392
|
-
' color-scheme: dark;'
|
|
393
|
-
|
|
394
|
-
flatten('--color', tokens.themes.dark.color, darkLines);
|
|
395
|
-
flatten('--elevation', tokens.themes.dark.elevation, darkLines);
|
|
396
|
-
flatten('--material', tokens.themes.dark.material, darkLines);
|
|
397
|
-
const reducedMotionLines = [
|
|
398
|
-
...Object.keys(tokens.shared.motion.duration).map((step)=>` --motion-duration-${step}: 0ms;`),
|
|
399
|
-
' --motion-spring-default-damping: 1;',
|
|
400
|
-
' --motion-spring-momentum-damping: 1;'
|
|
647
|
+
' color-scheme: dark;',
|
|
648
|
+
...modeLinesOf(tokens.themes.dark)
|
|
401
649
|
];
|
|
402
650
|
return [
|
|
403
651
|
'/* Generated from packages/tokens/src/tokens.ts. Do not edit. */',
|
|
@@ -410,10 +658,42 @@ function tokensToCss(tokens) {
|
|
|
410
658
|
' color-scheme: light;'
|
|
411
659
|
]),
|
|
412
660
|
`@media (prefers-color-scheme: dark) {\n${block(':root:not([data-theme="light"])', darkLines, ' ')}\n}`,
|
|
413
|
-
`@media (prefers-reduced-motion: reduce) {\n${block(':root',
|
|
661
|
+
`@media (prefers-reduced-motion: reduce) {\n${block(':root', reducedMotionLinesOf(tokens.shared), ' ')}\n}`,
|
|
414
662
|
''
|
|
415
663
|
].join('\n\n');
|
|
416
664
|
}
|
|
665
|
+
function themeToCss(theme) {
|
|
666
|
+
if (true !== theme[themeBrand]) throw new Error('themeToCss: not a theme made by defineTheme');
|
|
667
|
+
const scope = `.${theme.className}`;
|
|
668
|
+
const { prefix } = theme.custom;
|
|
669
|
+
const lightModeLines = modeLinesOf(theme.light);
|
|
670
|
+
const customLightLines = customLinesOf(prefix, theme.custom.light);
|
|
671
|
+
const darkLines = [
|
|
672
|
+
' color-scheme: dark;',
|
|
673
|
+
...modeLinesOf(theme.dark),
|
|
674
|
+
...customLinesOf(prefix, theme.custom.dark)
|
|
675
|
+
];
|
|
676
|
+
const sections = [
|
|
677
|
+
`/* Generated by @calcifer-design/tokens themeToCss for "${scope}". Do not edit. */`,
|
|
678
|
+
block(scope, [
|
|
679
|
+
' color-scheme: light;',
|
|
680
|
+
...sharedLinesOf(theme.shared),
|
|
681
|
+
...lightModeLines,
|
|
682
|
+
...customLinesOf(prefix, theme.custom.shared),
|
|
683
|
+
...customLightLines
|
|
684
|
+
]),
|
|
685
|
+
block(`:root[data-theme="dark"] ${scope}`, darkLines),
|
|
686
|
+
`@media (prefers-color-scheme: dark) {\n${block(`:root:not([data-theme="light"]) ${scope}`, darkLines, ' ')}\n}`,
|
|
687
|
+
block(`:root ${scope}[data-theme="light"]`, [
|
|
688
|
+
' color-scheme: light;',
|
|
689
|
+
...lightModeLines,
|
|
690
|
+
...customLightLines
|
|
691
|
+
]),
|
|
692
|
+
block(`:root ${scope}[data-theme="dark"]`, darkLines),
|
|
693
|
+
`@media (prefers-reduced-motion: reduce) {\n${block(scope, reducedMotionLinesOf(theme.shared), ' ')}\n}`
|
|
694
|
+
];
|
|
695
|
+
return `${sections.join('\n\n')}\n`;
|
|
696
|
+
}
|
|
417
697
|
function channelToLinear(channel) {
|
|
418
698
|
const scaled = channel / 255;
|
|
419
699
|
return scaled <= 0.04045 ? scaled / 12.92 : ((scaled + 0.055) / 1.055) ** 2.4;
|
|
@@ -452,4 +732,98 @@ function findWidthQueryViolations(css, allowedWidths) {
|
|
|
452
732
|
}
|
|
453
733
|
return violations;
|
|
454
734
|
}
|
|
455
|
-
|
|
735
|
+
const readPattern = /var\(\s*(--[A-Za-z0-9_-]+)/g;
|
|
736
|
+
const declarationPattern = /(?:^|[\s;{])(--[A-Za-z0-9_-]+)\s*:/g;
|
|
737
|
+
function findUnknownCustomProperties(css, defined) {
|
|
738
|
+
const unknownNames = new Set();
|
|
739
|
+
for (const match of css.matchAll(readPattern)){
|
|
740
|
+
const name = match[1] ?? '';
|
|
741
|
+
if (!defined.has(name)) unknownNames.add(name);
|
|
742
|
+
}
|
|
743
|
+
return [
|
|
744
|
+
...unknownNames
|
|
745
|
+
].sort();
|
|
746
|
+
}
|
|
747
|
+
function customPropertyNamesOf(css) {
|
|
748
|
+
const names = new Set();
|
|
749
|
+
for (const match of css.matchAll(declarationPattern))names.add(match[1] ?? '');
|
|
750
|
+
return names;
|
|
751
|
+
}
|
|
752
|
+
const hexPattern = /^#[0-9A-Fa-f]{6}$/;
|
|
753
|
+
const modes = [
|
|
754
|
+
'light',
|
|
755
|
+
'dark'
|
|
756
|
+
];
|
|
757
|
+
function isHex(value) {
|
|
758
|
+
return 'string' == typeof value && hexPattern.test(value);
|
|
759
|
+
}
|
|
760
|
+
function pairKey([foreground, background]) {
|
|
761
|
+
return `${foreground}/${background}`;
|
|
762
|
+
}
|
|
763
|
+
function checkThemeContrast(theme, extraPairs = {}) {
|
|
764
|
+
if (true !== theme[themeBrand]) throw new Error('checkThemeContrast: not a theme made by defineTheme');
|
|
765
|
+
const lists = [
|
|
766
|
+
{
|
|
767
|
+
pairs: textContrastPairs,
|
|
768
|
+
required: 4.5
|
|
769
|
+
},
|
|
770
|
+
{
|
|
771
|
+
pairs: extraPairs.text ?? [],
|
|
772
|
+
required: 4.5
|
|
773
|
+
},
|
|
774
|
+
{
|
|
775
|
+
pairs: uiContrastPairs,
|
|
776
|
+
required: 3
|
|
777
|
+
},
|
|
778
|
+
{
|
|
779
|
+
pairs: extraPairs.ui ?? [],
|
|
780
|
+
required: 3
|
|
781
|
+
}
|
|
782
|
+
];
|
|
783
|
+
const exemptKeys = new Set(theme.contrastExemptions.map(pairKey));
|
|
784
|
+
const listedKeys = new Set();
|
|
785
|
+
const violations = [];
|
|
786
|
+
for (const { pairs, required } of lists)for (const pair of pairs){
|
|
787
|
+
const key = pairKey(pair);
|
|
788
|
+
listedKeys.add(key);
|
|
789
|
+
if (exemptKeys.has(key)) continue;
|
|
790
|
+
const [foreground, background] = pair;
|
|
791
|
+
for (const mode of modes){
|
|
792
|
+
const foregroundColor = theme[mode].color[foreground];
|
|
793
|
+
const backgroundColor = theme[mode].color[background];
|
|
794
|
+
if (!isHex(foregroundColor) || !isHex(backgroundColor)) {
|
|
795
|
+
violations.push({
|
|
796
|
+
mode,
|
|
797
|
+
foreground,
|
|
798
|
+
background,
|
|
799
|
+
kind: 'not-hex',
|
|
800
|
+
ratio: null,
|
|
801
|
+
required
|
|
802
|
+
});
|
|
803
|
+
continue;
|
|
804
|
+
}
|
|
805
|
+
const ratio = contrastRatio(foregroundColor, backgroundColor);
|
|
806
|
+
if (ratio < required) violations.push({
|
|
807
|
+
mode,
|
|
808
|
+
foreground,
|
|
809
|
+
background,
|
|
810
|
+
kind: 'ratio',
|
|
811
|
+
ratio,
|
|
812
|
+
required
|
|
813
|
+
});
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
for (const exemption of theme.contrastExemptions)if (!listedKeys.has(pairKey(exemption))) {
|
|
817
|
+
const [foreground, background] = exemption;
|
|
818
|
+
violations.push({
|
|
819
|
+
mode: 'light',
|
|
820
|
+
foreground,
|
|
821
|
+
background,
|
|
822
|
+
kind: 'unknown-exemption',
|
|
823
|
+
ratio: null,
|
|
824
|
+
required: null
|
|
825
|
+
});
|
|
826
|
+
}
|
|
827
|
+
return violations;
|
|
828
|
+
}
|
|
829
|
+
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[];
|