@astryxdesign/core 0.4.2 → 0.4.3-canary.ac850d9
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/CHANGELOG.md +45 -0
- package/README.md +79 -45
- package/dist/Avatar/Avatar.d.ts.map +1 -1
- package/dist/Avatar/Avatar.js +3 -19
- package/dist/Banner/Banner.d.ts +6 -2
- package/dist/Banner/Banner.d.ts.map +1 -1
- package/dist/Banner/Banner.js +71 -30
- package/dist/Banner/index.d.ts +14 -12
- package/dist/Banner/index.d.ts.map +1 -1
- package/dist/Banner/index.js +10 -8
- package/dist/Chat/ChatTokenizedText.js +1 -1
- package/dist/ComplexSelector/ComplexSelector.d.ts +38 -4
- package/dist/ComplexSelector/ComplexSelector.d.ts.map +1 -1
- package/dist/ComplexSelector/ComplexSelector.js +96 -35
- package/dist/ComplexSelector/index.d.ts +2 -2
- package/dist/ComplexSelector/index.d.ts.map +1 -1
- package/dist/ComplexSelector/index.js +1 -1
- package/dist/Markdown/parser.d.ts.map +1 -1
- package/dist/Markdown/parser.js +55 -12
- package/dist/PowerSearch/PowerSearch.d.ts.map +1 -1
- package/dist/PowerSearch/PowerSearch.js +4 -1
- package/dist/PowerSearch/formatFilterValue.d.ts.map +1 -1
- package/dist/PowerSearch/formatFilterValue.js +2 -4
- package/dist/Table/columnUtils.d.ts.map +1 -1
- package/dist/Table/columnUtils.js +4 -1
- package/dist/TextArea/TextArea.d.ts +6 -3
- package/dist/TextArea/TextArea.d.ts.map +1 -1
- package/dist/TextArea/TextArea.js +17 -6
- package/dist/TreeList/TreeList.js +2 -1
- package/dist/astryx.css +6 -4
- package/dist/theme/defineTheme.d.ts +30 -7
- package/dist/theme/defineTheme.d.ts.map +1 -1
- package/dist/theme/defineTheme.js +36 -47
- package/dist/theme/expandColorScale.d.ts +17 -2
- package/dist/theme/expandColorScale.d.ts.map +1 -1
- package/dist/theme/expandColorScale.js +63 -33
- package/dist/theme/mergeComponents.d.ts +20 -0
- package/dist/theme/mergeComponents.d.ts.map +1 -0
- package/dist/theme/mergeComponents.js +56 -0
- package/dist/theme/onMediaTokens.d.ts +6 -1
- package/dist/theme/onMediaTokens.d.ts.map +1 -1
- package/dist/theme/onMediaTokens.js +11 -3
- package/dist/utils/characters.d.ts +27 -0
- package/dist/utils/characters.d.ts.map +1 -0
- package/dist/utils/characters.js +83 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -0
- package/package.json +4 -8
- package/src/Avatar/Avatar.test.tsx +6 -1
- package/src/Avatar/Avatar.tsx +3 -21
- package/src/Banner/Banner.doc.mjs +9 -7
- package/src/Banner/Banner.test.tsx +68 -0
- package/src/Banner/Banner.tsx +97 -36
- package/src/Banner/index.ts +15 -13
- package/src/Chat/ChatTokenizedText.tsx +1 -1
- package/src/ComplexSelector/ComplexSelector.doc.mjs +55 -6
- package/src/ComplexSelector/ComplexSelector.test.tsx +197 -6
- package/src/ComplexSelector/ComplexSelector.tsx +153 -32
- package/src/ComplexSelector/index.ts +3 -1
- package/src/Markdown/parser.test.ts +53 -0
- package/src/Markdown/parser.ts +53 -12
- package/src/PowerSearch/PowerSearch.test.tsx +48 -3
- package/src/PowerSearch/PowerSearch.tsx +4 -1
- package/src/PowerSearch/formatFilterValue.test.ts +22 -0
- package/src/PowerSearch/formatFilterValue.ts +2 -4
- package/src/Table/Table.test.tsx +6 -0
- package/src/Table/columnUtils.ts +3 -1
- package/src/TextArea/TextArea.doc.mjs +1 -1
- package/src/TextArea/TextArea.test.tsx +72 -0
- package/src/TextArea/TextArea.tsx +26 -8
- package/src/TreeList/TreeList.doc.mjs +2 -2
- package/src/TreeList/TreeList.tsx +1 -1
- package/src/theme/defineTheme.test.ts +127 -0
- package/src/theme/defineTheme.ts +70 -55
- package/src/theme/expandColorScale.test.ts +138 -0
- package/src/theme/expandColorScale.ts +89 -44
- package/src/theme/extensibleAxes.test.ts +365 -0
- package/src/theme/mergeComponents.ts +59 -0
- package/src/theme/onMediaTokens.ts +9 -2
- package/src/utils/characters.test.ts +141 -0
- package/src/utils/characters.ts +88 -0
- package/src/utils/index.ts +2 -0
- package/dist/astryx.umd.js +0 -147
- package/dist/astryx.umd.js.map +0 -7
package/src/theme/defineTheme.ts
CHANGED
|
@@ -72,6 +72,7 @@ import type {DomainTokenName} from './domainTokens';
|
|
|
72
72
|
import {domainTokenDefaults} from './domainTokens';
|
|
73
73
|
import type {SyntaxThemeDefinition} from './syntax';
|
|
74
74
|
import {registerTheme} from './themeRegistry';
|
|
75
|
+
import {deepMergeComponents} from './mergeComponents';
|
|
75
76
|
|
|
76
77
|
// =============================================================================
|
|
77
78
|
// Types
|
|
@@ -165,9 +166,14 @@ export interface DefineThemeInput {
|
|
|
165
166
|
name: string;
|
|
166
167
|
|
|
167
168
|
/**
|
|
168
|
-
* Base theme to extend. When provided, the new theme starts with
|
|
169
|
-
* base
|
|
170
|
-
*
|
|
169
|
+
* Base theme to extend. When provided, the new theme starts with everything
|
|
170
|
+
* the base resolved to — tokens, component overrides, icons, indicators, and
|
|
171
|
+
* its `onDark`/`onLight` surfaces — then applies this input on top. The base
|
|
172
|
+
* theme's values have lowest precedence.
|
|
173
|
+
*
|
|
174
|
+
* The result is flat: an extended theme carries its inheritance in its own
|
|
175
|
+
* resolved output, so `astryx theme build` emits one self-contained
|
|
176
|
+
* stylesheet and the base's CSS does not need to be loaded alongside it.
|
|
171
177
|
*
|
|
172
178
|
* Use this to create variant themes that customize only a few aspects
|
|
173
179
|
* (e.g. icons, accent color) without re-specifying the full theme.
|
|
@@ -242,20 +248,38 @@ export interface DefineThemeInput {
|
|
|
242
248
|
*/
|
|
243
249
|
radius?: RadiusScaleConfig;
|
|
244
250
|
/**
|
|
245
|
-
* Color scale configuration. Generates color token overrides from
|
|
246
|
-
*
|
|
251
|
+
* Color scale configuration. Generates color token overrides from an
|
|
252
|
+
* accent seed using the HCT perceptual color model.
|
|
247
253
|
*
|
|
248
254
|
* Only generates tokens derivable from the accent — status colors,
|
|
249
255
|
* categorical hues, and fixed tokens (on-dark/on-light) use defaults.
|
|
250
|
-
*
|
|
256
|
+
*
|
|
257
|
+
* `accent` accepts a single hex (same seed for both color schemes) or a
|
|
258
|
+
* `[light, dark]` tuple, matching `TokenValue`. With a tuple, the light
|
|
259
|
+
* scheme's full palette derives from the light seed and the dark
|
|
260
|
+
* scheme's from the dark seed.
|
|
251
261
|
*
|
|
252
262
|
* `accent` is optional — omit it for a neutral-only theme, which keeps
|
|
253
263
|
* the default accent tokens and only themes the neutrals.
|
|
254
264
|
*
|
|
265
|
+
* Precedence vs `tokens`: explicit `tokens` entries win over generated
|
|
266
|
+
* values, token by token. Because `--color-accent-muted`,
|
|
267
|
+
* `--color-text-accent` and `--color-icon-accent` are generated as
|
|
268
|
+
* `var(--color-accent)` references, a `tokens['--color-accent']`
|
|
269
|
+
* override re-points them at runtime. `--color-on-accent` does NOT
|
|
270
|
+
* follow: it is baked from the `color.accent` seed (a contrast
|
|
271
|
+
* computation CSS cannot express), so overriding the accent through
|
|
272
|
+
* `tokens` without also overriding `--color-on-accent` leaves the two
|
|
273
|
+
* out of sync. To re-seat the whole palette per scheme, prefer a tuple
|
|
274
|
+
* `color.accent` over the `tokens['--color-accent']` workaround.
|
|
275
|
+
*
|
|
255
276
|
* @example
|
|
256
|
-
* ```
|
|
277
|
+
* ```
|
|
257
278
|
* color: { accent: '#0064E0', neutralStyle: 'cool', contrast: 'standard' }
|
|
258
279
|
*
|
|
280
|
+
* // Per-scheme accents — light palette from the first seed, dark from the second
|
|
281
|
+
* color: { accent: ['#0064E0', '#48CAE4'] }
|
|
282
|
+
*
|
|
259
283
|
* // Neutral-only — accent tokens stay at their defaults
|
|
260
284
|
* color: { neutralStyle: 'warm' }
|
|
261
285
|
* ```
|
|
@@ -416,49 +440,6 @@ function resolveTokenValue(value: TokenValue): string {
|
|
|
416
440
|
return value;
|
|
417
441
|
}
|
|
418
442
|
|
|
419
|
-
/**
|
|
420
|
-
* Deep-merge two component style maps.
|
|
421
|
-
* Properties in `overrides` take precedence over `base`.
|
|
422
|
-
* This allows typeScale-generated rules to be overridden by explicit components.
|
|
423
|
-
*/
|
|
424
|
-
function deepMergeComponents(
|
|
425
|
-
base?: ComponentStyleMap,
|
|
426
|
-
overrides?: ComponentStyleMap,
|
|
427
|
-
): ComponentStyleMap | undefined {
|
|
428
|
-
if (!base && !overrides) {
|
|
429
|
-
return undefined;
|
|
430
|
-
}
|
|
431
|
-
if (!base) {
|
|
432
|
-
return overrides;
|
|
433
|
-
}
|
|
434
|
-
if (!overrides) {
|
|
435
|
-
return base;
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
const result: ComponentStyleMap = {};
|
|
439
|
-
|
|
440
|
-
// Start with all base entries
|
|
441
|
-
for (const [component, rules] of Object.entries(base)) {
|
|
442
|
-
result[component] = {...rules};
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
// Merge overrides on top
|
|
446
|
-
for (const [component, rules] of Object.entries(overrides)) {
|
|
447
|
-
if (!result[component]) {
|
|
448
|
-
result[component] = {...rules};
|
|
449
|
-
} else {
|
|
450
|
-
for (const [key, styles] of Object.entries(rules)) {
|
|
451
|
-
result[component][key] = {
|
|
452
|
-
...result[component][key],
|
|
453
|
-
...styles,
|
|
454
|
-
};
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
return result;
|
|
460
|
-
}
|
|
461
|
-
|
|
462
443
|
/**
|
|
463
444
|
* Resolve a FontWeight name to a var() reference.
|
|
464
445
|
* Named weights map to var(--font-weight-*); raw values pass through.
|
|
@@ -491,6 +472,24 @@ function buildFontFamily(
|
|
|
491
472
|
return quoted;
|
|
492
473
|
}
|
|
493
474
|
|
|
475
|
+
/**
|
|
476
|
+
* Describe a rejected `extends` value for the error message — enough to tell a
|
|
477
|
+
* missed import (`undefined`) from a module namespace or a plain object.
|
|
478
|
+
*/
|
|
479
|
+
function describeBadBase(value: unknown): string {
|
|
480
|
+
if (value === undefined) {
|
|
481
|
+
return 'undefined';
|
|
482
|
+
}
|
|
483
|
+
if (value === null) {
|
|
484
|
+
return 'null';
|
|
485
|
+
}
|
|
486
|
+
if (typeof value !== 'object') {
|
|
487
|
+
return typeof value;
|
|
488
|
+
}
|
|
489
|
+
const keys = Object.keys(value);
|
|
490
|
+
return `an object with keys [${keys.slice(0, 4).join(', ')}${keys.length > 4 ? ', …' : ''}]`;
|
|
491
|
+
}
|
|
492
|
+
|
|
494
493
|
/**
|
|
495
494
|
* Create an Astryx theme.
|
|
496
495
|
*
|
|
@@ -504,7 +503,19 @@ function buildFontFamily(
|
|
|
504
503
|
export function defineTheme(input: DefineThemeInput): DefinedTheme {
|
|
505
504
|
const tokens: Record<string, string> = {};
|
|
506
505
|
|
|
507
|
-
// 0. Pre-seed from base theme when `extends` is provided (lowest precedence)
|
|
506
|
+
// 0. Pre-seed from base theme when `extends` is provided (lowest precedence).
|
|
507
|
+
// A base that is not a theme is refused rather than ignored: `extends` used
|
|
508
|
+
// to inherit nothing when its value was undefined, which is what a named
|
|
509
|
+
// import silently resolving to the wrong module hands over, and the theme
|
|
510
|
+
// then built into a plausible-looking stylesheet missing everything it was
|
|
511
|
+
// supposed to inherit.
|
|
512
|
+
if ('extends' in input && !isDefinedTheme(input.extends)) {
|
|
513
|
+
throw new Error(
|
|
514
|
+
`defineTheme("${input.name}"): \`extends\` must be a theme from defineTheme(), got ${describeBadBase(input.extends)}. ` +
|
|
515
|
+
`Check that the import naming your base theme resolves to its source and exports that name — ` +
|
|
516
|
+
`a generated \`<theme>.js\` artifact sitting next to the source exports \`<name>Theme\`, not the source's own export.`,
|
|
517
|
+
);
|
|
518
|
+
}
|
|
508
519
|
const base = input.extends;
|
|
509
520
|
if (base) {
|
|
510
521
|
for (const [key, value] of Object.entries(base.tokens)) {
|
|
@@ -640,9 +651,10 @@ export function defineTheme(input: DefineThemeInput): DefinedTheme {
|
|
|
640
651
|
components = deepMergeComponents(base.components, components);
|
|
641
652
|
}
|
|
642
653
|
|
|
643
|
-
// 4. Resolve on-media token overrides (
|
|
644
|
-
|
|
645
|
-
const
|
|
654
|
+
// 4. Resolve on-media token overrides (base's resolved surface, then
|
|
655
|
+
// defaults, then this theme's own overrides)
|
|
656
|
+
const __onDark = resolveOnMedia('dark', input.onDark, base?.__onDark);
|
|
657
|
+
const __onLight = resolveOnMedia('light', input.onLight, base?.__onLight);
|
|
646
658
|
|
|
647
659
|
// 5. Merge icons — input icons override base icons
|
|
648
660
|
const icons =
|
|
@@ -663,7 +675,10 @@ export function defineTheme(input: DefineThemeInput): DefinedTheme {
|
|
|
663
675
|
components,
|
|
664
676
|
icons,
|
|
665
677
|
indicators,
|
|
666
|
-
__inputTokens:
|
|
678
|
+
__inputTokens:
|
|
679
|
+
base?.__inputTokens || input.tokens
|
|
680
|
+
? {...base?.__inputTokens, ...input.tokens}
|
|
681
|
+
: undefined,
|
|
667
682
|
__onDark,
|
|
668
683
|
__onLight,
|
|
669
684
|
};
|
|
@@ -192,6 +192,144 @@ describe('expandColorScale — neutral-only themes (#2279)', () => {
|
|
|
192
192
|
});
|
|
193
193
|
});
|
|
194
194
|
|
|
195
|
+
describe('expandColorScale — tuple accent (#2279)', () => {
|
|
196
|
+
const LIGHT_SEED = '#B7410E';
|
|
197
|
+
const DARK_SEED = '#48CAE4';
|
|
198
|
+
|
|
199
|
+
it('accepts a [light, dark] tuple and generates the accent tokens', () => {
|
|
200
|
+
const tokens = expandColorScale({accent: [LIGHT_SEED, DARK_SEED]});
|
|
201
|
+
for (const key of ACCENT_TOKENS) {
|
|
202
|
+
expect(tokens).toHaveProperty(key);
|
|
203
|
+
}
|
|
204
|
+
expect(tokens['--color-accent']).toMatch(/^light-dark\(#/);
|
|
205
|
+
expect(tokens['--color-on-accent']).toMatch(/^light-dark\(#/);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('a same-halves tuple matches the single-string expansion, token for token', () => {
|
|
209
|
+
// Pins the string path as the tuple fast path: introducing tuples must
|
|
210
|
+
// not re-color any existing single-accent theme.
|
|
211
|
+
const fromString = expandColorScale({accent: LIGHT_SEED});
|
|
212
|
+
const fromTuple = expandColorScale({accent: [LIGHT_SEED, LIGHT_SEED]});
|
|
213
|
+
expect(fromTuple).toEqual(fromString);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('derives the light palette from the light seed and the dark palette from the dark seed', () => {
|
|
217
|
+
// Every generated light-dark() pair takes its light half from the light
|
|
218
|
+
// seed's palettes and its dark half from the dark seed's, so resolving
|
|
219
|
+
// each mode must agree with the matching single-accent expansion.
|
|
220
|
+
const tuple = defineTheme({
|
|
221
|
+
name: 'tuple',
|
|
222
|
+
color: {accent: [LIGHT_SEED, DARK_SEED]},
|
|
223
|
+
});
|
|
224
|
+
const fromLight = defineTheme({name: 'light', color: {accent: LIGHT_SEED}});
|
|
225
|
+
const fromDark = defineTheme({name: 'dark', color: {accent: DARK_SEED}});
|
|
226
|
+
|
|
227
|
+
const generatedKeys = Object.keys(
|
|
228
|
+
expandColorScale({accent: [LIGHT_SEED, DARK_SEED]}),
|
|
229
|
+
);
|
|
230
|
+
const tupleLight = resolveThemeTokens(tuple, {mode: 'light'});
|
|
231
|
+
const refLight = resolveThemeTokens(fromLight, {mode: 'light'});
|
|
232
|
+
const tupleDark = resolveThemeTokens(tuple, {mode: 'dark'});
|
|
233
|
+
const refDark = resolveThemeTokens(fromDark, {mode: 'dark'});
|
|
234
|
+
for (const key of generatedKeys) {
|
|
235
|
+
expect(tupleLight[key], `${key} (light)`).toBe(refLight[key]);
|
|
236
|
+
expect(tupleDark[key], `${key} (dark)`).toBe(refDark[key]);
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it('differing seeds produce a dark half the light seed would not', () => {
|
|
241
|
+
// Regression guard: the dark half must not silently reuse the light seed.
|
|
242
|
+
const tuple = defineTheme({
|
|
243
|
+
name: 'tuple-differs',
|
|
244
|
+
color: {accent: [LIGHT_SEED, DARK_SEED]},
|
|
245
|
+
});
|
|
246
|
+
const single = defineTheme({
|
|
247
|
+
name: 'single',
|
|
248
|
+
color: {accent: LIGHT_SEED},
|
|
249
|
+
});
|
|
250
|
+
expect(
|
|
251
|
+
resolveThemeTokens(tuple, {mode: 'dark'})['--color-accent'],
|
|
252
|
+
).not.toBe(resolveThemeTokens(single, {mode: 'dark'})['--color-accent']);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it('keeps the derived accent tokens as references with a tuple accent', () => {
|
|
256
|
+
const tokens = expandColorScale({accent: [LIGHT_SEED, DARK_SEED]});
|
|
257
|
+
expect(tokens['--color-text-accent']).toBe('var(--color-accent)');
|
|
258
|
+
expect(tokens['--color-icon-accent']).toBe('var(--color-accent)');
|
|
259
|
+
expect(tokens['--color-accent-muted']).toContain('var(--color-accent)');
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it('honours neutralStyle with a tuple accent', () => {
|
|
263
|
+
const warm = expandColorScale({
|
|
264
|
+
accent: [LIGHT_SEED, DARK_SEED],
|
|
265
|
+
neutralStyle: 'warm',
|
|
266
|
+
});
|
|
267
|
+
const cool = expandColorScale({
|
|
268
|
+
accent: [LIGHT_SEED, DARK_SEED],
|
|
269
|
+
neutralStyle: 'cool',
|
|
270
|
+
});
|
|
271
|
+
expect(warm['--color-neutral']).not.toBe(cool['--color-neutral']);
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
describe('accent precedence — color config vs tokens overrides (#2279)', () => {
|
|
276
|
+
const CONFIG_ACCENT: [string, string] = ['#B7410E', '#48CAE4'];
|
|
277
|
+
const TOKEN_ACCENT: [string, string] = ['#AA0000', '#FF5555'];
|
|
278
|
+
|
|
279
|
+
it('tokens["--color-accent"] wins over the color-generated accent', () => {
|
|
280
|
+
const theme = defineTheme({
|
|
281
|
+
name: 'precedence-base',
|
|
282
|
+
color: {accent: CONFIG_ACCENT},
|
|
283
|
+
tokens: {'--color-accent': TOKEN_ACCENT},
|
|
284
|
+
});
|
|
285
|
+
expect(theme.tokens['--color-accent']).toBe(
|
|
286
|
+
`light-dark(${TOKEN_ACCENT[0]}, ${TOKEN_ACCENT[1]})`,
|
|
287
|
+
);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it('reference tokens follow the tokens override at runtime', () => {
|
|
291
|
+
const theme = defineTheme({
|
|
292
|
+
name: 'precedence-refs',
|
|
293
|
+
color: {accent: CONFIG_ACCENT},
|
|
294
|
+
tokens: {'--color-accent': TOKEN_ACCENT},
|
|
295
|
+
});
|
|
296
|
+
const light = resolveThemeTokens(theme, {mode: 'light'});
|
|
297
|
+
expect(light['--color-text-accent']).toBe(TOKEN_ACCENT[0]);
|
|
298
|
+
expect(light['--color-icon-accent']).toBe(TOKEN_ACCENT[0]);
|
|
299
|
+
const dark = resolveThemeTokens(theme, {mode: 'dark'});
|
|
300
|
+
expect(dark['--color-text-accent']).toBe(TOKEN_ACCENT[1]);
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it('baked --color-on-accent stays derived from the color.accent seed', () => {
|
|
304
|
+
// Documented divergence: a tokens override of --color-accent does not
|
|
305
|
+
// re-derive --color-on-accent, which is a baked contrast computation
|
|
306
|
+
// against the color.accent seed. Consumers who override the accent via
|
|
307
|
+
// tokens must override --color-on-accent too (or pass the tuple through
|
|
308
|
+
// color.accent so the whole palette re-derives).
|
|
309
|
+
const overridden = defineTheme({
|
|
310
|
+
name: 'precedence-on-accent',
|
|
311
|
+
color: {accent: CONFIG_ACCENT},
|
|
312
|
+
tokens: {'--color-accent': TOKEN_ACCENT},
|
|
313
|
+
});
|
|
314
|
+
const generated = defineTheme({
|
|
315
|
+
name: 'generated-on-accent',
|
|
316
|
+
color: {accent: CONFIG_ACCENT},
|
|
317
|
+
});
|
|
318
|
+
expect(overridden.tokens['--color-on-accent']).toBe(
|
|
319
|
+
generated.tokens['--color-on-accent'],
|
|
320
|
+
);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it('an explicit --color-on-accent token wins over the generated one', () => {
|
|
324
|
+
const theme = defineTheme({
|
|
325
|
+
name: 'precedence-on-accent-explicit',
|
|
326
|
+
color: {accent: CONFIG_ACCENT},
|
|
327
|
+
tokens: {'--color-on-accent': '#FFFFFF'},
|
|
328
|
+
});
|
|
329
|
+
expect(theme.tokens['--color-on-accent']).toBe('#FFFFFF');
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
|
|
195
333
|
describe('expandColorScale + defineTheme integration', () => {
|
|
196
334
|
it('explicit token overrides win over generated values', () => {
|
|
197
335
|
const theme = defineTheme({
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* @output Token overrides for derivable color tokens
|
|
7
7
|
* @position Theme utility; consumed by defineTheme.ts
|
|
8
8
|
*
|
|
9
|
-
* Generates color token overrides from
|
|
10
|
-
*
|
|
9
|
+
* Generates color token overrides from an accent seed using the HCT
|
|
10
|
+
* perceptual color model. Only produces tokens that meaningfully
|
|
11
11
|
* derive from the accent — status colors, categorical hues, and fixed
|
|
12
12
|
* tokens (on-dark/on-light) fall through to colorDefaults.
|
|
13
13
|
*
|
|
@@ -15,11 +15,19 @@
|
|
|
15
15
|
* ramp (seeded from the default accent's hue) while the accent tokens
|
|
16
16
|
* themselves fall through to colorDefaults, same as the tokens above.
|
|
17
17
|
*
|
|
18
|
+
* `accent` also accepts a `[light, dark]` tuple (matching `TokenValue` in
|
|
19
|
+
* defineTheme). A tuple seeds each color scheme from its own half: the
|
|
20
|
+
* light side of every generated `light-dark()` pair derives from the light
|
|
21
|
+
* accent's palettes, the dark side from the dark accent's. A plain string
|
|
22
|
+
* seeds both sides identically, token for token the same output as before
|
|
23
|
+
* tuples existed.
|
|
24
|
+
*
|
|
18
25
|
* WCAG contrast guarantees (asserted in expandColorScale.test.ts):
|
|
19
26
|
* - Text tones are guaranteed >= 4.5:1 against their surfaces by tone
|
|
20
27
|
* spacing alone — HCT tone is CIE L*, which fixes relative luminance
|
|
21
28
|
* regardless of hue/chroma, so the fixed tone assignments hold for any
|
|
22
|
-
* accent/neutralStyle (WCAG 1.4.3)
|
|
29
|
+
* accent/neutralStyle (WCAG 1.4.3), and each half of a tuple pairs with
|
|
30
|
+
* surfaces derived from that same seed.
|
|
23
31
|
* - --color-border-emphasized (form-control boundaries) is tone-bumped
|
|
24
32
|
* until it reaches >= 3:1 against the generated surface (WCAG 1.4.11).
|
|
25
33
|
* - --color-border, --color-skeleton, and --color-track are intentionally
|
|
@@ -46,6 +54,9 @@ import {hexToHct, hctToHex, tonalPalette, hexWithAlpha} from './hct';
|
|
|
46
54
|
* // Minimal — just a seed color
|
|
47
55
|
* { accent: '#0064E0' }
|
|
48
56
|
*
|
|
57
|
+
* // Per-scheme seeds: light palettes from '#0064E0', dark from '#48CAE4'
|
|
58
|
+
* { accent: ['#0064E0', '#48CAE4'] }
|
|
59
|
+
*
|
|
49
60
|
* // With customization
|
|
50
61
|
* { accent: '#B7410E', neutralStyle: 'warm', contrast: 'high' }
|
|
51
62
|
*
|
|
@@ -55,14 +66,18 @@ import {hexToHct, hctToHex, tonalPalette, hexWithAlpha} from './hct';
|
|
|
55
66
|
*/
|
|
56
67
|
export interface ColorScaleConfig {
|
|
57
68
|
/**
|
|
58
|
-
* Seed accent color
|
|
69
|
+
* Seed accent color. Everything derives from this.
|
|
70
|
+
*
|
|
71
|
+
* Either a single hex (#RRGGBB) used for both color schemes, or a
|
|
72
|
+
* `[light, dark]` hex tuple: the light scheme's palettes derive from
|
|
73
|
+
* the light seed and the dark scheme's palettes from the dark seed.
|
|
59
74
|
*
|
|
60
75
|
* Optional. When omitted, the neutral palettes are seeded from the
|
|
61
76
|
* default accent's hue and the accent tokens (--color-accent,
|
|
62
77
|
* --color-accent-muted, --color-on-accent) are not generated — they
|
|
63
78
|
* fall through to colorDefaults.
|
|
64
79
|
*/
|
|
65
|
-
accent?: string;
|
|
80
|
+
accent?: string | [light: string, dark: string];
|
|
66
81
|
|
|
67
82
|
/**
|
|
68
83
|
* Neutral tone warmth. Controls how much of the seed's hue bleeds
|
|
@@ -159,6 +174,11 @@ export function ensureContrastTone(
|
|
|
159
174
|
* --color-on-dark/on-light) are NOT generated — they fall through
|
|
160
175
|
* to colorDefaults.
|
|
161
176
|
*
|
|
177
|
+
* A `[light, dark]` tuple accent seeds each scheme separately: the light
|
|
178
|
+
* half of every generated `light-dark()` pair comes from the light seed's
|
|
179
|
+
* palettes, the dark half from the dark seed's. A string accent seeds both
|
|
180
|
+
* halves from the same palettes, exactly as before tuples were supported.
|
|
181
|
+
*
|
|
162
182
|
* Without an `accent`, the accent tokens join that fall-through set: the
|
|
163
183
|
* neutrals are seeded from the default accent's hue, and --color-accent,
|
|
164
184
|
* --color-accent-muted and --color-on-accent keep their colorDefaults values.
|
|
@@ -168,6 +188,9 @@ export function ensureContrastTone(
|
|
|
168
188
|
* const tokens = expandColorScale({ accent: '#0064E0' });
|
|
169
189
|
* // tokens['--color-accent'] === 'light-dark(#..., #...)'
|
|
170
190
|
*
|
|
191
|
+
* const perScheme = expandColorScale({ accent: ['#0064E0', '#48CAE4'] });
|
|
192
|
+
* // light half derives from #0064E0, dark half from #48CAE4
|
|
193
|
+
*
|
|
171
194
|
* const neutralOnly = expandColorScale({ neutralStyle: 'warm' });
|
|
172
195
|
* // neutralOnly['--color-accent'] === undefined
|
|
173
196
|
* ```
|
|
@@ -175,16 +198,33 @@ export function ensureContrastTone(
|
|
|
175
198
|
export function expandColorScale(config: ColorScaleConfig): ColorScaleTokens {
|
|
176
199
|
const {accent, neutralStyle = 'cool', contrast = 'standard'} = config;
|
|
177
200
|
|
|
178
|
-
|
|
179
|
-
|
|
201
|
+
// Normalize to per-scheme seeds. A string accent (or an absent one) uses
|
|
202
|
+
// the same seed for both halves, which keeps single-seed output identical
|
|
203
|
+
// to the pre-tuple implementation.
|
|
204
|
+
const [lightAccent, darkAccent] = Array.isArray(accent)
|
|
205
|
+
? accent
|
|
206
|
+
: [accent, accent];
|
|
207
|
+
|
|
208
|
+
const lightSeed = hexToHct(lightAccent ?? DEFAULT_ACCENT_SEED);
|
|
209
|
+
const sameSeed = darkAccent === lightAccent;
|
|
210
|
+
const darkSeed = sameSeed
|
|
211
|
+
? lightSeed
|
|
212
|
+
: hexToHct(darkAccent ?? DEFAULT_ACCENT_SEED);
|
|
180
213
|
|
|
181
|
-
const primaryChroma = Math.max(seed.chroma, 48);
|
|
182
214
|
const neutralChroma = NEUTRAL_CHROMA[neutralStyle] ?? 5;
|
|
183
215
|
const neutralVariantChroma = NEUTRAL_VARIANT_CHROMA[neutralStyle] ?? 8;
|
|
184
216
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
217
|
+
// Palette naming: *L palettes feed the light (first) half of each ld()
|
|
218
|
+
// pair below, *D palettes the dark (second) half. With a single seed the
|
|
219
|
+
// D palettes alias the L ones.
|
|
220
|
+
const PL = tonalPalette(lightSeed.hue, Math.max(lightSeed.chroma, 48));
|
|
221
|
+
const NL = tonalPalette(lightSeed.hue, neutralChroma);
|
|
222
|
+
const NVL = tonalPalette(lightSeed.hue, neutralVariantChroma);
|
|
223
|
+
const PD = sameSeed
|
|
224
|
+
? PL
|
|
225
|
+
: tonalPalette(darkSeed.hue, Math.max(darkSeed.chroma, 48));
|
|
226
|
+
const ND = sameSeed ? NL : tonalPalette(darkSeed.hue, neutralChroma);
|
|
227
|
+
const NVD = sameSeed ? NVL : tonalPalette(darkSeed.hue, neutralVariantChroma);
|
|
188
228
|
|
|
189
229
|
const isHigh = contrast === 'high';
|
|
190
230
|
|
|
@@ -209,19 +249,19 @@ export function expandColorScale(config: ColorScaleConfig): ColorScaleTokens {
|
|
|
209
249
|
const borderEmphasizedStartDark = isHigh ? 50 : 30;
|
|
210
250
|
const borderEmphasized = ld(
|
|
211
251
|
ensureContrastTone(
|
|
212
|
-
|
|
252
|
+
lightSeed.hue,
|
|
213
253
|
neutralVariantChroma,
|
|
214
254
|
borderEmphasizedStartLight,
|
|
215
255
|
-1,
|
|
216
|
-
|
|
256
|
+
NL[99],
|
|
217
257
|
NON_TEXT_MIN_CONTRAST,
|
|
218
258
|
),
|
|
219
259
|
ensureContrastTone(
|
|
220
|
-
|
|
260
|
+
darkSeed.hue,
|
|
221
261
|
neutralVariantChroma,
|
|
222
262
|
borderEmphasizedStartDark,
|
|
223
263
|
1,
|
|
224
|
-
|
|
264
|
+
ND[10],
|
|
225
265
|
NON_TEXT_MIN_CONTRAST,
|
|
226
266
|
),
|
|
227
267
|
);
|
|
@@ -234,7 +274,7 @@ export function expandColorScale(config: ColorScaleConfig): ColorScaleTokens {
|
|
|
234
274
|
// seed above, so a supplied-but-malformed accent keeps its old behavior.
|
|
235
275
|
...(accent != null
|
|
236
276
|
? {
|
|
237
|
-
'--color-accent': ld(
|
|
277
|
+
'--color-accent': ld(PL[40], PD[80]),
|
|
238
278
|
// Derived accent tokens reference --color-accent instead of baking its
|
|
239
279
|
// resolved hex, so a scoped override of the base token re-accents the
|
|
240
280
|
// whole subtree at runtime. --color-on-accent stays baked: it is a
|
|
@@ -243,65 +283,70 @@ export function expandColorScale(config: ColorScaleConfig): ColorScaleTokens {
|
|
|
243
283
|
accentWithAlpha(0.2),
|
|
244
284
|
accentWithAlpha(0.25),
|
|
245
285
|
),
|
|
246
|
-
'--color-on-accent': ld(
|
|
286
|
+
'--color-on-accent': ld(PL[100], PD[20]),
|
|
247
287
|
}
|
|
248
288
|
: null),
|
|
249
|
-
'--color-neutral': ld(hexWithAlpha(
|
|
250
|
-
'--color-background-surface': ld(
|
|
251
|
-
'--color-background-body': ld(
|
|
252
|
-
'--color-overlay': ld(hexWithAlpha(
|
|
289
|
+
'--color-neutral': ld(hexWithAlpha(NL[10], 0.1), hexWithAlpha(ND[90], 0.2)),
|
|
290
|
+
'--color-background-surface': ld(NL[99], ND[10]),
|
|
291
|
+
'--color-background-body': ld(NL[95], ND[5]),
|
|
292
|
+
'--color-overlay': ld(hexWithAlpha(NL[10], 0.4), hexWithAlpha(ND[10], 0.6)),
|
|
253
293
|
'--color-overlay-hover': ld(
|
|
254
|
-
hexWithAlpha(
|
|
255
|
-
hexWithAlpha(
|
|
294
|
+
hexWithAlpha(NL[10], 0.05),
|
|
295
|
+
hexWithAlpha(ND[100], 0.05),
|
|
256
296
|
),
|
|
257
297
|
'--color-overlay-pressed': ld(
|
|
258
|
-
hexWithAlpha(
|
|
259
|
-
hexWithAlpha(
|
|
298
|
+
hexWithAlpha(NL[10], 0.1),
|
|
299
|
+
hexWithAlpha(ND[100], 0.1),
|
|
260
300
|
),
|
|
261
301
|
'--color-background-muted': ld(
|
|
262
|
-
hexWithAlpha(
|
|
263
|
-
hexWithAlpha(
|
|
302
|
+
hexWithAlpha(NL[10], 0.05),
|
|
303
|
+
hexWithAlpha(ND[10], 0.5),
|
|
264
304
|
),
|
|
265
305
|
|
|
266
306
|
// Text
|
|
267
|
-
'--color-text-primary': ld(
|
|
307
|
+
'--color-text-primary': ld(
|
|
308
|
+
NL[textPrimaryLightTone],
|
|
309
|
+
ND[textPrimaryDarkTone],
|
|
310
|
+
),
|
|
268
311
|
'--color-text-secondary': ld(
|
|
269
|
-
|
|
270
|
-
|
|
312
|
+
NVL[textSecondaryLightTone],
|
|
313
|
+
NVD[textSecondaryDarkTone],
|
|
271
314
|
),
|
|
272
|
-
'--color-text-disabled': ld(
|
|
315
|
+
'--color-text-disabled': ld(NVL[60], NVD[40]),
|
|
273
316
|
'--color-text-accent': 'var(--color-accent)',
|
|
274
317
|
|
|
275
318
|
// Icon
|
|
276
319
|
'--color-icon-accent': 'var(--color-accent)',
|
|
277
|
-
'--color-icon-primary': ld(
|
|
320
|
+
'--color-icon-primary': ld(
|
|
321
|
+
NL[textPrimaryLightTone],
|
|
322
|
+
ND[textPrimaryDarkTone],
|
|
323
|
+
),
|
|
278
324
|
'--color-icon-secondary': ld(
|
|
279
|
-
|
|
280
|
-
|
|
325
|
+
NVL[textSecondaryLightTone],
|
|
326
|
+
NVD[textSecondaryDarkTone],
|
|
281
327
|
),
|
|
282
|
-
'--color-icon-disabled': ld(
|
|
328
|
+
'--color-icon-disabled': ld(NVL[60], NVD[40]),
|
|
283
329
|
|
|
284
330
|
// Surface variants
|
|
285
|
-
'--color-background-card': ld(
|
|
286
|
-
'--color-background-popover': ld(
|
|
287
|
-
'--color-background-inverted': ld(
|
|
331
|
+
'--color-background-card': ld(NL[99], ND[10]),
|
|
332
|
+
'--color-background-popover': ld(NL[99], ND[20]),
|
|
333
|
+
'--color-background-inverted': ld(NL[10], ND[99]),
|
|
288
334
|
|
|
289
|
-
// Border
|
|
290
335
|
// Border
|
|
291
336
|
// Decorative hairline — not a WCAG 1.4.11 boundary. High contrast
|
|
292
337
|
// doubles the alpha so structural boundaries stay perceivable.
|
|
293
338
|
'--color-border': ld(
|
|
294
|
-
hexWithAlpha(
|
|
295
|
-
hexWithAlpha(
|
|
339
|
+
hexWithAlpha(NL[10], borderSubtleAlpha),
|
|
340
|
+
hexWithAlpha(ND[95], borderSubtleAlpha),
|
|
296
341
|
),
|
|
297
342
|
'--color-border-emphasized': borderEmphasized,
|
|
298
343
|
|
|
299
344
|
// Effects
|
|
300
|
-
'--color-skeleton': ld(
|
|
345
|
+
'--color-skeleton': ld(NVL[70], NVD[30]),
|
|
301
346
|
// Channel-on-body surface (ProgressBar/Slider tracks, Switch off-state).
|
|
302
347
|
// Defaults to the same NV[70]/NV[30] ramp stop as --color-skeleton.
|
|
303
|
-
'--color-track': ld(
|
|
304
|
-
'--color-shadow': ld(hexWithAlpha(
|
|
348
|
+
'--color-track': ld(NVL[70], NVD[30]),
|
|
349
|
+
'--color-shadow': ld(hexWithAlpha(NL[0], 0.1), hexWithAlpha(ND[0], 0.3)),
|
|
305
350
|
'--color-tint-hover': ld('black', 'white'),
|
|
306
351
|
};
|
|
307
352
|
}
|