An open source design system that's fully customizable and agent ready
TypeScript
fix(theme): emit type:display-N heading rules in generateTypeScaleComponents (#4859)
Ojas Mutrejaf6871701c79c
diff --git a/.changeset/heading-display-type-theme-scale.md b/.changeset/heading-display-type-theme-scale.mdnew file mode 100644index 0000000000..562a485fda--- /dev/null+++ b/.changeset/heading-display-type-theme-scale.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] `<Heading type="display-N">` now sizes correctly under every theme, matching `Text`'s behavior. `generateTypeScaleComponents()` only emitted `level:N`-keyed CSS rules for `heading`, with no `type:display-N` counterpart — so as soon as a theme supplied `typography.scale`, the generated theme-layer CSS's `level:N` rule was the only one present and silently won regardless of `type`, discarding the prop. A theme with no typography config was unaffected, which made the bug look intermittent.++@HelloOjasMutrejadiff --git a/packages/core/src/theme/expandTypeScale.test.ts b/packages/core/src/theme/expandTypeScale.test.tsindex 3ec52fd7bc..1745b922fb 100644--- a/packages/core/src/theme/expandTypeScale.test.ts+++ b/packages/core/src/theme/expandTypeScale.test.ts@@ -305,4 +305,47 @@ describe('generateTypeScaleComponents', () => {'var(--text-body-leading)',);});++ it('generates rules for all 3 heading display types', () => {+ const components = generateTypeScaleComponents({base: 14, ratio: 1.2});+ for (const type of ['display-1', 'display-2', 'display-3']) {+ expect(components.heading).toHaveProperty(`type:${type}`);+ }+ });++ it('heading display-type rules reference the display semantic tokens', () => {+ const components = generateTypeScaleComponents({base: 14, ratio: 1.2});+ const display1 = components.heading['type:display-1'];+ expect(display1.fontFamily).toBe('var(--font-family-heading)');+ expect(display1.fontSize).toBe('var(--text-display-1-size)');+ expect(display1.lineHeight).toBe('var(--text-display-1-leading)');+ // fontWeight is intentionally omitted here, matching the `text` branch's+ // type-keyed rules — see defaultWeightByTypeStyles for how weight is+ // applied instead.+ expect(display1.fontWeight).toBeUndefined();+ });++ it('emits heading type:display-N rules after level:N rules, so type wins at equal specificity', () => {+ // Heading renders both a `level:N` and a `type:display-N` class on the+ // same element simultaneously when `type` is set (see Heading.tsx).+ // Both produce single-class-selector CSS rules of equal specificity, so+ // CSS's own "last rule wins" tiebreak is what makes `type` take+ // precedence over `level` -- which only holds if type rules are emitted+ // strictly after all level rules in this object's (and therefore the+ // generated stylesheet's) key order. This regression-tests #4829,+ // where type-keyed heading rules didn't exist at all and level always+ // won by default, silently discarding the `type` prop under any theme+ // with a typography.scale.+ const components = generateTypeScaleComponents({base: 14, ratio: 1.2});+ const keys = Object.keys(components.heading);+ const lastLevelIndex = Math.max(+ ...[1, 2, 3, 4, 5, 6].map(level => keys.indexOf(`level:${level}`)),+ );+ const firstTypeIndex = Math.min(+ ...['display-1', 'display-2', 'display-3'].map(type =>+ keys.indexOf(`type:${type}`),+ ),+ );+ expect(firstTypeIndex).toBeGreaterThan(lastLevelIndex);+ });});diff --git a/packages/core/src/theme/expandTypeScale.ts b/packages/core/src/theme/expandTypeScale.tsindex 201603f381..7efc95667b 100644--- a/packages/core/src/theme/expandTypeScale.ts+++ b/packages/core/src/theme/expandTypeScale.ts@@ -368,6 +368,23 @@ export function generateTypeScaleComponents(lineHeight: `var(--text-heading-${level}-leading)`,};}+ // `Heading` renders both a `level:N` and, when set, a `type:display-N`+ // visual-prop class simultaneously (type sizing takes precedence over+ // level sizing — see Heading.tsx's own `type ? sizeByTypeStyles[type] :+ // sizeByLevelStyles[level]`). Without a `type:display-N` rule here, the+ // `level:N` rule is the only one that matches, so it silently wins+ // regardless of `type` once a theme supplies a type scale. Emitting these+ // after the level rules keeps them later in source order, so they take+ // precedence at equal specificity, matching the component's own logic.+ // fontWeight is intentionally omitted, mirroring the `text` branch below+ // and preserving `defaultWeightByTypeStyles`.+ for (const type of ['display-1', 'display-2', 'display-3']) {+ headingRules[`type:${type}`] = {+ fontFamily: 'var(--font-family-heading)',+ fontSize: `var(--text-${type}-size)`,+ lineHeight: `var(--text-${type}-leading)`,+ };+ }components.heading = headingRules;const textRules: Record<string, Record<string, string>> = {};