Skip to content

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

TypeScript

fix(core): make Text size override a themed type's font-size (#4279)

Cindy Zhang8736335ce599
diff --git a/.changeset/text-size-themed-type.md b/.changeset/text-size-themed-type.md
new file mode 100644
index 0000000000..68b1fd4e1b
--- /dev/null
+++ b/.changeset/text-size-themed-type.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Text: an explicit `size` now overrides the font-size of a themed `type`. The `size` class lived in a lower cascade layer (`astryx-base`) than a theme's per-type font-size rule (`astryx-theme`), so `<Text type="supporting" size="xsm">` silently kept the type's size. Themes now re-emit the size classes in the theme layer so `size` wins as documented.
+@ejc3
diff --git a/packages/core/src/theme/generateThemeRules.test.ts b/packages/core/src/theme/generateThemeRules.test.ts
index 4ea4955f30..b77d671143 100644
--- a/packages/core/src/theme/generateThemeRules.test.ts
+++ b/packages/core/src/theme/generateThemeRules.test.ts
@@ -190,6 +190,61 @@ describe('generateThemeRules', () => {
expect(rules.some(r => r.includes('.astryx-text.accent'))).toBe(true);
});
+ // --- Size-prop overrides (so `size` beats a themed `type`) ---
+
+ it('emits Text size-prop font-size overrides in the same layer as type rules', () => {
+ // Digit-leading sizes are prefixed (size-2xs); word sizes stay bare.
+ const sizeRule = rules.find(
+ r => r.includes('.astryx-text.size-2xs') && r.includes('font-size'),
+ );
+ expect(sizeRule).toBeDefined();
+ expect(sizeRule).toContain('var(--font-size-2xs)');
+
+ // `xsm` maps to the --font-size-xs token (matches sizeStyles).
+ const xsmRule = rules.find(r => r.includes('.astryx-text.xsm'));
+ expect(xsmRule).toBeDefined();
+ expect(xsmRule).toContain('var(--font-size-xs)');
+
+ // Overrides set only font-size — line-height/family come from `type`.
+ expect(xsmRule).not.toContain('line-height');
+ });
+
+ it('emits a size override for every TextSize value', () => {
+ const sizes = [
+ 'size-4xs',
+ 'size-3xs',
+ 'size-2xs',
+ 'xsm',
+ 'sm',
+ 'base',
+ 'lg',
+ 'xl',
+ 'size-2xl',
+ 'size-3xl',
+ 'size-4xl',
+ ];
+ for (const cls of sizes) {
+ expect(
+ rules.some(
+ r => r.includes(`.astryx-text.${cls}`) && r.includes('font-size'),
+ ),
+ ).toBe(true);
+ }
+ });
+
+ it('orders size overrides after the themed type font-size rules', () => {
+ // Source order breaks specificity ties within a layer, so the size
+ // override must come after the `.astryx-text.<type>` type rule.
+ const typeIdx = rules.findIndex(
+ r => r.includes('.astryx-text.supporting') && r.includes('font-size'),
+ );
+ const sizeIdx = rules.findIndex(
+ r => r.includes('.astryx-text.size-2xs') && r.includes('font-size'),
+ );
+ expect(typeIdx).toBeGreaterThanOrEqual(0);
+ expect(sizeIdx).toBeGreaterThan(typeIdx);
+ });
+
// --- Consistency ---
it('generateThemeCSS returns prose and component blocks with @scope', () => {
@@ -202,6 +257,16 @@ describe('generateThemeRules', () => {
expect(combined).toContain(rule);
}
});
+
+ it('routes Text size overrides into the component (astryx-theme) block', () => {
+ // The size override only beats a themed type if it lands in the same
+ // layer as the type rules (astryx-theme / component block), not the
+ // reset-tier prose block.
+ const {prose, component} = generateThemeCSS(theme);
+ expect(component).toContain('.astryx-text.size-2xs');
+ expect(component).toContain('.astryx-text.xsm');
+ expect(prose).not.toContain('.astryx-text.size-2xs');
+ });
});
describe('generateThemeRules with weight overrides', () => {
diff --git a/packages/core/src/theme/generateThemeRules.ts b/packages/core/src/theme/generateThemeRules.ts
index 5439f1c1cc..0fef62eed2 100644
--- a/packages/core/src/theme/generateThemeRules.ts
+++ b/packages/core/src/theme/generateThemeRules.ts
@@ -301,6 +301,10 @@ export function generateThemeRules(theme: DefinedTheme): string[] {
// 4. Prop-level color overrides (for text/heading/link specificity)
generateColorOverrides(theme.components || {}, parts);
+ // 5. Text `size`-prop font-size overrides (so an explicit size beats a
+ // themed type's font-size across the astryx-base → astryx-theme layers)
+ generateSizeOverrides(theme.components || {}, parts);
+
// (on-media rules are generated separately — see generateOnMediaCSS)
return parts;
@@ -500,6 +504,58 @@ function generateColorOverrides(
}
}
+/**
+ * Map a Text `size` prop value to its raw font-size token.
+ * Mirrors `sizeStyles` in `Text/text.stylex.ts` (note `xsm` → `--font-size-xs`).
+ * <!-- SYNC: packages/core/src/Text/text.stylex.ts (sizeStyles) -->
+ */
+const TEXT_SIZE_TOKEN_MAP: Record<string, string> = {
+ '4xs': 'var(--font-size-4xs)',
+ '3xs': 'var(--font-size-3xs)',
+ '2xs': 'var(--font-size-2xs)',
+ xsm: 'var(--font-size-xs)',
+ sm: 'var(--font-size-sm)',
+ base: 'var(--font-size-base)',
+ lg: 'var(--font-size-lg)',
+ xl: 'var(--font-size-xl)',
+ '2xl': 'var(--font-size-2xl)',
+ '3xl': 'var(--font-size-3xl)',
+ '4xl': 'var(--font-size-4xl)',
+};
+
+/**
+ * Generate `size`-prop font-size overrides for the Text component.
+ *
+ * The `size` prop is documented as a font-size override that wins over the
+ * size implied by `type`. Its StyleX class lives in `@layer astryx-base`, but a
+ * theme's per-type font-size rule (`.astryx-text.<type>`) lives in the higher
+ * `@layer astryx-theme`, so the layer cascade let the theme silently shadow
+ * `size` for any `type` the theme styled. Re-emitting the size classes here —
+ * same layer as the type rules, same `.astryx-text.<x>` specificity, later in
+ * source — restores `size` as a real override.
+ *
+ * Only `font-size` is overridden; line-height and other type properties are
+ * intentionally preserved, matching the prop's documented contract.
+ *
+ * Gated on the theme touching `text` (which includes the auto-generated
+ * type-scale rules) — with no theme type rule to beat, the base-layer StyleX
+ * class already wins and no override is needed.
+ */
+function generateSizeOverrides(
+ components: Record<string, unknown>,
+ parts: string[],
+): void {
+ if (!('text' in components)) {
+ return;
+ }
+ for (const [sizeName, sizeValue] of Object.entries(TEXT_SIZE_TOKEN_MAP)) {
+ const suffix = parseStyleKey(`size:${sizeName}`);
+ parts.push(
+ ` ${componentClassSelector('text', suffix)} { font-size: ${sizeValue}; }`,
+ );
+ }
+}
+
/**
* Generate theme rules split into component and prose groups.
*