An open source design system that's fully customizable and agent ready
TypeScript
fix(theme): emit derived accent tokens as var(--color-accent) references (#3551)
Md Shahrier Islam Arham2a94c444806e
diff --git a/.changeset/accent-tokens-var-refs.md b/.changeset/accent-tokens-var-refs.mdnew file mode 100644index 0000000000..9ea983f4be--- /dev/null+++ b/.changeset/accent-tokens-var-refs.md@@ -0,0 +1,8 @@+---+'@astryxdesign/core': patch+---++[fix] Emit derived accent tokens as var(--color-accent) references (#3495)++Generated themes now emit `--color-text-accent` and `--color-icon-accent` as `var(--color-accent)` and `--color-accent-muted` as `color-mix()` over the same reference, instead of baking resolved hex literals. Overriding `--color-accent` on any scope re-accents the whole subtree at runtime — no second theme build, no per-token overrides. `--color-on-accent` stays resolved because it is a contrast computation CSS cannot express.+@arham766diff --git a/packages/core/src/theme/expandColorScale.test.ts b/packages/core/src/theme/expandColorScale.test.tsindex 05005ff322..f37ce37399 100644--- a/packages/core/src/theme/expandColorScale.test.ts+++ b/packages/core/src/theme/expandColorScale.test.ts@@ -3,6 +3,7 @@import {describe, it, expect} from 'vitest';import {expandColorScale} from './expandColorScale';import {defineTheme} from './defineTheme';+import {generateThemeRules} from './generateThemeRules';describe('expandColorScale', () => {it('produces all expected token keys', () => {@@ -60,6 +61,19 @@ describe('expandColorScale', () => {expect(warm['--color-neutral']).not.toBe(neutral['--color-neutral']);});+ it('emits derived accent tokens as references to --color-accent', () => {+ const tokens = expandColorScale({accent: '#0064E0'});+ // Reference tokens follow a scoped --color-accent override at runtime.+ expect(tokens['--color-text-accent']).toBe('var(--color-accent)');+ expect(tokens['--color-icon-accent']).toBe('var(--color-accent)');+ expect(tokens['--color-accent-muted']).toBe(+ 'light-dark(color-mix(in srgb, var(--color-accent) 20%, transparent), color-mix(in srgb, var(--color-accent) 25%, transparent))',+ );+ // The base token and the contrast-computed on-accent stay resolved.+ expect(tokens['--color-accent']).toMatch(/^light-dark\(#/);+ expect(tokens['--color-on-accent']).toMatch(/^light-dark\(#/);+ });+it('contrast high produces different --color-text-primary than standard', () => {const standard = expandColorScale({accent: '#0064E0',@@ -81,4 +95,19 @@ describe('expandColorScale + defineTheme integration', () => {});expect(theme.tokens['--color-accent']).toBe('red');});++ it('generated theme CSS keeps the accent references (#3495)', () => {+ const theme = defineTheme({+ name: 'test-accent-refs',+ color: {accent: '#DC2626'},+ });+ const css = generateThemeRules(theme).join('\n');+ expect(css).toContain('--color-text-accent: var(--color-accent);');+ expect(css).toContain('--color-icon-accent: var(--color-accent);');+ expect(css).toContain(+ '--color-accent-muted: light-dark(color-mix(in srgb, var(--color-accent) 20%, transparent), color-mix(in srgb, var(--color-accent) 25%, transparent));',+ );+ // The base token itself stays a resolved color pair.+ expect(css).toMatch(/--color-accent: light-dark\(#/);+ });});diff --git a/packages/core/src/theme/expandColorScale.ts b/packages/core/src/theme/expandColorScale.tsindex d564fcdfcc..0c9effafd6 100644--- a/packages/core/src/theme/expandColorScale.ts+++ b/packages/core/src/theme/expandColorScale.ts@@ -77,6 +77,10 @@ function ld(light: string, dark: string): string {return `light-dark(${light}, ${dark})`;}+function accentWithAlpha(alpha: number): string {+ return `color-mix(in srgb, var(--color-accent) ${alpha * 100}%, transparent)`;+}+/*** Expand a color scale config into Astryx color token overrides.*@@ -117,10 +121,11 @@ export function expandColorScale(return {// Core semantic'--color-accent': ld(P[40], P[80]),- '--color-accent-muted': ld(- hexWithAlpha(P[40], 0.2),- hexWithAlpha(P[80], 0.25),- ),+ // Derived accent tokens reference --color-accent instead of baking its+ // resolved hex, so a scoped override of the base token re-accents the+ // whole subtree at runtime. --color-on-accent stays baked: it is a+ // contrast computation against the accent, which CSS cannot express.+ '--color-accent-muted': ld(accentWithAlpha(0.2), accentWithAlpha(0.25)),'--color-on-accent': ld(P[100], P[20]),'--color-neutral': ld(hexWithAlpha(N[10], 0.1), hexWithAlpha(N[90], 0.2)),'--color-background-surface': ld(N[99], N[10]),@@ -146,10 +151,10 @@ export function expandColorScale(NV[textSecondaryDarkTone],),'--color-text-disabled': ld(NV[60], NV[40]),- '--color-text-accent': ld(P[30], P[80]),+ '--color-text-accent': 'var(--color-accent)',// Icon- '--color-icon-accent': ld(P[40], P[80]),+ '--color-icon-accent': 'var(--color-accent)','--color-icon-primary': ld(N[textPrimaryLightTone], N[textPrimaryDarkTone]),'--color-icon-secondary': ld(NV[textSecondaryLightTone],