Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(theme): drop the base field #5562 added to the public ThemeCSSOutput (#5566)

Cindy Zhang9a61177d8b16
diff --git a/.changeset/theme-css-output-no-base-field.md b/.changeset/theme-css-output-no-base-field.md
new file mode 100644
index 0000000000..5ee1aaaf50
--- /dev/null
+++ b/.changeset/theme-css-output-no-base-field.md
@@ -0,0 +1,8 @@
+---
+'@astryxdesign/core': patch
+'@astryxdesign/cli': patch
+---
+
+[fix] `generateThemeCSS` returns the two scoped blocks it has always returned — `prose` and `component`. The `--color-data-*` defaults it briefly also returned as a third `base` field are theme-independent, so they are not part of a theme's CSS: the `<Theme>` runtime keeps emitting them at `:root` in `@layer astryx-base`, and `astryx theme build` now formats the same block from the already-public `dataTokenDefaults` export. Output is byte-identical on both paths and no stable release ever carried the field.
+
+@cixzhang
diff --git a/packages/cli/api/theme/build/build.mjs b/packages/cli/api/theme/build/build.mjs
index be7edacb65..4ee127bdcd 100644
--- a/packages/cli/api/theme/build/build.mjs
+++ b/packages/cli/api/theme/build/build.mjs
@@ -62,14 +62,14 @@ import {
/** @type {any} */ let _defineTheme = null;
/** @type {any} */ let _generateThemeRulesSplit = null;
/** @type {any} */ let _generateOnMediaCSS = null;
-/** @type {any} */ let _generateThemeCSS = null;
+/** @type {any} */ let _dataTokenDefaults = null;
/** @type {any} */ let _coreImportError = null;
try {
const coreTheme = await import('@astryxdesign/core/theme');
_defineTheme = coreTheme.defineTheme;
_generateThemeRulesSplit = coreTheme.generateThemeRulesSplit;
_generateOnMediaCSS = coreTheme.generateOnMediaCSS;
- _generateThemeCSS = coreTheme.generateThemeCSS;
+ _dataTokenDefaults = coreTheme.dataTokenDefaults;
} catch (e) {
// Capture the reason so the theme action can surface a precise, actionable
// error. We don't throw here: this module is imported eagerly by the CLI
@@ -1195,14 +1195,17 @@ export async function themeBuild(
return null;
}
// The data-token defaults are theme-independent and go in @layer
- // astryx-base, below the theme's own overrides. Taken from the runtime's
- // own generator so the two paths cannot emit different bytes.
+ // astryx-base, below the theme's own overrides. Formatted here from the
+ // public `dataTokenDefaults` export, byte for byte as the `<Theme>`
+ // runtime emits it — build-theme.data-tokens.test.mjs is the drift guard.
// Placed after the reset block and before the theme block: a layer's order
// is fixed by where it is first declared, so emitting it anywhere else in
// the file would invert reset < astryx-base < astryx-theme for a consumer
// who imports this stylesheet on its own.
- const baseCss = _generateThemeCSS
- ? _generateThemeCSS(resolvedTheme).base
+ const baseCss = _dataTokenDefaults
+ ? `:root {\n${Object.entries(_dataTokenDefaults)
+ .map(([name, value]) => ` ${name}: ${value};`)
+ .join('\n')}\n}`
: '';
if (baseCss) {
cssParts.splice(
diff --git a/packages/cli/clients/cli/commands/build-theme.data-tokens.test.mjs b/packages/cli/clients/cli/commands/build-theme.data-tokens.test.mjs
index f1b848ed55..e0739d41af 100644
--- a/packages/cli/clients/cli/commands/build-theme.data-tokens.test.mjs
+++ b/packages/cli/clients/cli/commands/build-theme.data-tokens.test.mjs
@@ -12,12 +12,21 @@
*
* Building requires a compiled @astryxdesign/core, so this suite builds core
* once in beforeAll via the shared ensureCoreBuilt() helper.
+ *
+ * The build formats the defaults block itself, from the public
+ * `dataTokenDefaults` export; core formats it for the runtime. This suite is
+ * what keeps those two independent formattings byte-identical.
*/
import {describe, it, expect, beforeAll, beforeEach, afterEach} from 'vitest';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
+// Core's own generator, imported from source rather than from the package: it
+// is deliberately not part of `@astryxdesign/core/theme`'s public surface, and
+// this suite is the guard that the build's independent formatting matches it.
+import {generateDataTokenDefaultsCSS} from '../../../../core/src/theme/generateThemeRules';
+import {dataTokenDefaults as sourceDataTokenDefaults} from '../../../../core/src/theme/domainTokens/dataTokens';
import {ensureCoreBuilt} from './ensure-core-built.mjs';
import {runCli} from '../../../test-utils/run-cli.mjs';
@@ -100,16 +109,36 @@ describe('theme build data token output', () => {
});
it('emits the bytes the runtime generator emits', async () => {
- const tokens = {'--color-accent': '#0077B6'};
- const css = await buildTheme(tmpDir, 'charts-parity', {tokens});
+ const css = await buildTheme(tmpDir, 'charts-parity', {
+ tokens: {'--color-accent': '#0077B6'},
+ });
- const {defineTheme, generateThemeCSS} = await import(
+ // The expectation below comes from core's SOURCE; the stylesheet came from
+ // core's dist. ensureCoreBuilt() only checks that dist exists, never that
+ // it is current, so a dist older than the source would fail the byte
+ // comparison with a colour mismatch that reads as formatter drift. Name
+ // that case first.
+ const {dataTokenDefaults: builtDataTokenDefaults} = await import(
'@astryxdesign/core/theme'
);
- const {base} = generateThemeCSS(
- defineTheme({name: 'charts-parity', tokens}),
+ expect(
+ builtDataTokenDefaults,
+ 'packages/core/dist is stale — run `pnpm -F @astryxdesign/core build`',
+ ).toEqual(sourceDataTokenDefaults);
+
+ // The build formats this block itself, from the public `dataTokenDefaults`
+ // export; the runtime formats it in core. Nothing else holds the two
+ // together, so compare the bytes — a changed indent or separator in either
+ // one fails here.
+ const runtimeBase = generateDataTokenDefaultsCSS();
+ const marker = '@layer astryx-base {\n';
+ const start = css.indexOf(marker);
+ expect(start).toBeGreaterThanOrEqual(0);
+
+ const bodyStart = start + marker.length;
+ expect(css.slice(bodyStart, bodyStart + runtimeBase.length)).toBe(
+ runtimeBase,
);
-
- expect(css).toContain(`@layer astryx-base {\n${base}\n}`);
+ expect(css.slice(bodyStart + runtimeBase.length)).toMatch(/^\n\}/);
});
});
diff --git a/packages/core/src/theme/Theme.tsx b/packages/core/src/theme/Theme.tsx
index 3faecf5334..3be95080c2 100644
--- a/packages/core/src/theme/Theme.tsx
+++ b/packages/core/src/theme/Theme.tsx
@@ -42,6 +42,7 @@ import * as stylex from '@stylexjs/stylex';
import type {ThemeMode} from './types';
import {colorVars, typographyVars} from './tokens.stylex';
import {generateThemeCSS, type DefinedTheme} from './defineTheme';
+import {generateDataTokenDefaultsCSS} from './generateThemeRules';
import {registerTheme} from './themeRegistry';
import {dataAttr} from '../naming';
import {ThemeContext} from './useTheme';
@@ -141,7 +142,8 @@ function useThemeStyleInjection(theme: DefinedTheme): void {
`the built artifacts.`,
);
- const {prose, component, base} = generateThemeCSS(theme);
+ const {prose, component} = generateThemeCSS(theme);
+ const base = generateDataTokenDefaultsCSS();
injectedThemes.add(themeKey);
const cleanups: (() => void)[] = [() => injectedThemes.delete(themeKey)];
diff --git a/packages/core/src/theme/defineTheme.test.ts b/packages/core/src/theme/defineTheme.test.ts
index 0c4fd668dd..a2aec99d0c 100644
--- a/packages/core/src/theme/defineTheme.test.ts
+++ b/packages/core/src/theme/defineTheme.test.ts
@@ -116,26 +116,24 @@ describe('generateThemeCSS', () => {
expect(css).toContain('font-family: var(--font-family-heading)');
});
- it('declares the data palette for a theme that never mentions it', () => {
- const {base, component} = generateThemeCSS(
- defineTheme({name: 'chartless'}),
- );
- expect(base).toContain('--color-data-categorical-blue:');
- expect(base).toContain('--color-data-gray-1:');
+ it('keeps the data palette out of a theme that never mentions it', () => {
+ // That the palette IS declared is covered by `seeds the whole palette
+ // once, at :root` in generateThemeRules.test.ts. It is theme-independent,
+ // so there is nothing about it a theme can be used to assert.
+ const {component} = generateThemeCSS(defineTheme({name: 'chartless'}));
expect(component).not.toContain('--color-data-');
});
- it('keeps the rest of the data palette when one data token is overridden', () => {
+ it('puts only the named data token in the theme block, not its siblings', () => {
const theme = defineTheme({
name: 'brand-charts',
tokens: {'--color-data-categorical-blue': '#00A3FF'},
});
- const {base, component} = generateThemeCSS(theme);
+ const {component} = generateThemeCSS(theme);
expect(component).toContain('--color-data-categorical-blue: #00A3FF;');
- // The siblings the theme did not name stay in the shared base block, so a
- // nested theme inherits the parent's override instead of the default.
+ // Leaving the siblings out of the theme's own block is what lets a nested
+ // theme inherit a parent's override instead of shadowing it.
expect(component).not.toContain('--color-data-categorical-orange');
- expect(base).toContain('--color-data-categorical-orange:');
// Defaults reach the stylesheet without entering the theme's own tokens,
// which are what `astryx theme build` reports as overrides.
expect(theme.tokens['--color-data-categorical-orange']).toBeUndefined();
diff --git a/packages/core/src/theme/generateThemeRules.test.ts b/packages/core/src/theme/generateThemeRules.test.ts
index 766e2e8288..a5cc05e5f9 100644
--- a/packages/core/src/theme/generateThemeRules.test.ts
+++ b/packages/core/src/theme/generateThemeRules.test.ts
@@ -780,25 +780,23 @@ describe('data visualization tokens', () => {
expect(block).not.toContain('--color-data-categorical-orange');
});
- it('carries the palette in the base block, not the scoped stylesheet', () => {
- const {base, component, prose} = generateThemeCSS(
+ it('keeps the palette out of the scoped stylesheet', () => {
+ // The palette's own contents are asserted once, against
+ // `dataTokenDefaults`, in `seeds the whole palette once, at :root` above.
+ const {component, prose} = generateThemeCSS(
defineTheme({name: 'data-css'}),
);
- expect(base).toContain('--color-data-neutral:');
- expect(base).toContain('--color-data-blue-3:');
expect(component).not.toContain('--color-data-');
expect(prose).not.toContain('--color-data-');
});
- it('gives every theme the same base block', () => {
- expect(generateThemeCSS(defineTheme({name: 'data-a'})).base).toBe(
- generateThemeCSS(
- defineTheme({
- name: 'data-b',
- tokens: {'--color-data-categorical-blue': '#00A3FF'},
- }),
- ).base,
- );
+ it('keeps generateThemeCSS to its two scoped blocks', () => {
+ // The defaults are theme-independent, so they are not part of the theme
+ // CSS contract: `astryx theme build` formats them from the public
+ // `dataTokenDefaults` export instead.
+ expect(
+ Object.keys(generateThemeCSS(defineTheme({name: 'data-shape'}))).sort(),
+ ).toEqual(['component', 'prose']);
});
});
diff --git a/packages/core/src/theme/generateThemeRules.ts b/packages/core/src/theme/generateThemeRules.ts
index 9d239b3f05..9aa54f9a25 100644
--- a/packages/core/src/theme/generateThemeRules.ts
+++ b/packages/core/src/theme/generateThemeRules.ts
@@ -125,7 +125,7 @@ export interface ThemeRulesSplit {
}
/**
- * Output from generateThemeCSS — three CSS blocks for different layers.
+ * Output from generateThemeCSS — two CSS blocks for different layers.
*/
export interface ThemeCSSOutput {
/**
@@ -140,13 +140,6 @@ export interface ThemeCSSOutput {
* theme component overrides take effect. Empty string if no rules.
*/
component: string;
- /**
- * The `--color-data-*` defaults at `:root`, identical for every theme.
- * Should be injected into @layer astryx-base — where StyleX puts the core
- * token defaults — so a theme's own value outranks it and a nested theme
- * inherits its parent's override.
- */
- base: string;
}
// =============================================================================
@@ -778,7 +771,10 @@ export function generateOnMediaCSS(theme: DefinedTheme): string {
* specificity. Seeding it per theme scope instead re-declares the default
* inside every nested theme, which is the shadowing this shape avoids.
*
- * @internal Reached from outside `packages/core` as `generateThemeCSS().base`.
+ * @internal Not exported from `@astryxdesign/core/theme`: the `<Theme>`
+ * runtime is the only caller. `astryx theme build` formats the same block from
+ * the public `dataTokenDefaults` export, and a CLI test asserts the two are
+ * byte-identical.
*/
export function generateDataTokenDefaultsCSS(): string {
const declarations = Object.entries(dataTokenDefaults)
@@ -790,14 +786,16 @@ export function generateDataTokenDefaultsCSS(): string {
/**
* Generate layered CSS for a theme — runtime path.
*
- * Returns three CSS blocks for injection into different layers:
+ * Returns two CSS blocks for injection into different layers:
* - `prose`: @scope'd element defaults → inject into @layer reset
- * - `base`: the `:root` data-token defaults → inject into @layer astryx-base
* - `component`: @scope'd token + .astryx-* overrides → inject into @layer astryx-theme
*
* This separation ensures prose defaults (what bare HTML looks like in a theme)
* sit at reset-layer priority where any class-based style wins, while component
* overrides sit above StyleX so themes can restyle components intentionally.
+ *
+ * The theme-independent `--color-data-*` defaults are not part of this output:
+ * see `generateDataTokenDefaultsCSS`.
*/
export function generateThemeCSS(theme: DefinedTheme): ThemeCSSOutput {
const {component, prose} = generateThemeRulesSplit(theme);
@@ -825,9 +823,5 @@ export function generateThemeCSS(theme: DefinedTheme): ThemeCSSOutput {
: onMediaCss;
}
- return {
- prose: proseCss,
- component: componentCss,
- base: generateDataTokenDefaultsCSS(),
- };
+ return {prose: proseCss, component: componentCss};
}