Skip to content

feat: Functional text type + typeScale theme shortcut #586

Closed

Description

@cixzhang

Context

PR #555 introduced an exploration for a ratio-based type scale system with an interactive prototype. The key insight: instead of maintaining separate type scale systems (default vs editorial), XDS should offer a configurable type scale where products pick their position on the density spectrum via { base, ratio }.

This issue covers the concrete implementation work to bring that exploration into the theme system.

Goals

  1. Add optional typeScale to defineTheme — a single { base, ratio } that generates all heading and text sizes for the theme
  2. Remove variant from XDSHeading — density is a theme-level decision, not a per-component one
  3. Simplify XDSFontWrapper — becomes a pure prose/typography wrapper, no variant prop

Decisions

No named variants (functional / default / editorial)

Decided after evaluating the API surface trade-off. See #632 for the vibe test that first established density as a region concern, not a component concern.

The original spec proposed three named variants (functional, default, editorial) as a first-class concept — a shared XDSTypeScaleVariant type, variant props on components, and three { base, ratio } configs per theme. After evaluation, we're removing named variants entirely:

  • 67% fewer tokens — 33 CSS custom properties instead of 99
  • 67% fewer theme override rules — 11 instead of 33
  • Zero new component props — we're actually removing variant from XDSHeading
  • Zero new types — no XDSTypeScaleVariant
  • One concept instead of five — typeScale has base and ratio, that's it

The named values live in docs as suggested starting points, not as API surface:

  • Dense/functional: { base: 12, ratio: 1.125 }
  • Default: { base: 14, ratio: 1.2 }
  • Airy/editorial: { base: 16, ratio: 1.25 }

Mixed density via theme nesting, not variant props

For pages that need different density regions (e.g. dense sidebar + airy main content), nest <XDSTheme>:

<XDSTheme theme={adminTheme}>
  <XDSTheme theme={denseSidebarTheme}>
    <Sidebar />
  </XDSTheme>
  <MainContent />
</XDSTheme>

This uses the same mechanism as brand theming and dark mode — one pattern to learn instead of two.

Remove variant from XDSHeading

variant on XDSHeading (currently supports 'default' | 'editorial') is removed. Since this hasn't shipped to external consumers, there's no migration cost. Density is controlled by the theme's typeScale, scoped via <XDSTheme> nesting.

XDSText never gets variant

Per #632 — density is not a per-component concern. XDSText stays focused on semantic text types (body, large, label, supporting, code).

Default type scale

base: 14, ratio: 1.2, with h4 anchored to base (14px). Headings scale up from h4, h5/h6 scale down.

The formula: size = base × ratio^step, where h4 is step 0.

Headings

Level Step Formula Size Line-H Previous
h1 +3 14 × 1.2³ 24px 32px 20px
h2 +2 14 × 1.2² 20px 28px 18px
h3 +1 14 × 1.2¹ 17px 24px 16px
h4 0 14 14px 20px 14px
h5 −1 14 × 1.2⁻¹ 12px 16px 14px
h6 −2 14 × 1.2⁻² 10px 16px 12px

Text types

Type Step Size Line-H Previous
large +1 17px 28px 16px
body 0 14px 24px 14px
label 0 14px 24px 14px
code 0 14px 24px 14px
supporting −1 12px 20px 12px

Scale spectrum

  24px  ← h1
  20px  ← h2
  17px  ← h3, large
  14px  ← h4, body, label, code
  12px  ← h5, supporting
  10px  ← h6

Line heights snapped to 4px grid. Heading line-height ratio ~1.3, text line-height ratio ~1.5.

typeScale in defineTheme

API design

const myTheme = defineTheme({
  name: 'admin-panel',
  typeScale: { base: 14, ratio: 1.2 },
  tokens: { ... },
  components: { ... },  // explicit overrides still merge on top
});

When typeScale is omitted, themes use the hardcoded default values (computed from { base: 14, ratio: 1.2 }). Fully backward compatible.

How it works

  1. defineTheme takes the optional typeScale object ({ base: number, ratio: number })
  2. h4 is anchored to base. Headings h1–h3 scale up (base × ratio^1..3), h5–h6 scale down (base × ratio^-1..-2)
  3. Text types: body/label/code at base, large at base × ratio, supporting at base / ratio
  4. Line heights snapped to 4px grid (headings ~1.3× ratio, text ~1.5× ratio)
  5. Explicit components overrides deep-merge on top for fine-tuning

Generated tokens

Naming convention:

--heading-{level}-{size|weight|leading}
--text-{type}-{size|weight|leading}

Example output from { base: 14, ratio: 1.2 }:

{
  '--heading-1-size': '24px',     // 14 × 1.2³
  '--heading-1-leading': '32px',
  '--heading-2-size': '20px',     // 14 × 1.2²
  '--heading-2-leading': '28px',
  '--heading-3-size': '17px',     // 14 × 1.2¹
  '--heading-4-size': '14px',     // base (anchor)
  '--heading-5-size': '12px',     // 14 × 1.2⁻¹
  '--heading-6-size': '10px',     // 14 × 1.2⁻²
  // ... weights and leading for each

  '--text-body-size': '14px',     // base
  '--text-large-size': '17px',    // 14 × 1.2¹
  '--text-supporting-size': '12px', // 14 × 1.2⁻¹
  // ... weights and leading for each
}

Total: 33 tokens (11 entries × 3 properties).

Prose / FontWrapper coverage

Components and typography.css both reference the same tokens:

/* typography.css */
.xds-typography h1 {
  font-size: var(--heading-1-size);
  font-weight: var(--heading-1-weight);
  line-height: var(--heading-1-leading);
}

This guarantees XDSHeading, XDSText, and XDSFontWrapper prose all read from the same source of truth. A theme's typeScale setting flows through to all three automatically.

Implementation notes

  • typeScale is syntactic sugar — it generates token overrides, not component CSS (avoids the CSS layer specificity problem)
  • The computation (base × ratio^n, rounding, line-height snapping) should be a pure expandTypeScale() function that's easy to test
  • Collision handling: enforce 1px minimum step between adjacent sizes (from PR Exploration: Ratio-based type scale system #555's exploration)
  • Follows the same pattern as color tokens — theme sets token values, components consume via var()

Tasks

Type scale computation

  • Implement expandTypeScale({ base, ratio }) pure function — h4 anchored to base, sizes + line heights
  • Add optional typeScale field to XDSDefineThemeInput
  • Implement typeScale expansion in defineTheme (merges into token overrides)
  • Ensure explicit components overrides deep-merge on top of generated tokens

Tokens

  • Add 33 typography tokens to tokens.stylex.ts with defaults from { base: 14, ratio: 1.2 }
  • Component overrides in default theme reference new tokens (var(--heading-1-size), etc.)
  • Component overrides in neutral theme reference new tokens

Component changes

  • Remove variant prop from XDSHeading (currently 'default' | 'editorial')
  • Remove variant prop from XDSFontWrapper (simplify to prose wrapper)
  • Remove XDSHeadingVariant type
  • Update XDSHeading to read sizing from tokens instead of variant-keyed overrides
  • Update XDSFontWrapper — remove variant classes (xds-typography--editorial, etc.), single prose class only

typography.css

  • Refactor prose rules to reference new tokens (var(--heading-1-size), etc.)
  • Remove variant-specific classes (xds-typography--editorial, xds-typography--default)

Stories & Docs

  • Add Storybook story showing typeScale theme switching (dense → default → airy)
  • Add Storybook story showing mixed density via nested <XDSTheme>
  • Update docs (Heading.doc.mjs, FontWrapper.doc.mjs, CLI docs)
  • Document suggested { base, ratio } starting points in theme docs

Tests

  • Tests for expandTypeScale() computation function
  • Tests for defineTheme typeScale expansion
  • Tests for heading rendering with token-based sizing
  • Tests for text rendering with token-based sizing
  • Tests for FontWrapper prose rendering with token-based sizing
  • Tests for mixed density via nested themes

References

Activity

  1. added
    enhancement
    on Mar 12, 2026
  2. cixzhang commented on Mar 12, 2026

    @cixzhang
    ContributorAuthor

    Yeah let's add variant to XDSText, too.

  3. cixzhang commented on Mar 12, 2026

    @cixzhang
    ContributorAuthor

    Typescale API needs to be like this:

    typeScale: {
      functional: {base: 14, ratio: 1.125},
      default: {base: 14, ratio: 1.125},
      editorial: {base: 14, ratio: 1.125},
    }
    

    All 3 variants are available always. We're defining how each one translates into CSS class overrides.

  4. cixzhang commented on Mar 12, 2026

    @cixzhang
    ContributorAuthor

    Updated the spec based on your feedback:

    1. Text gets variant too — added variant?: 'functional' | 'default' | 'editorial' to XDSText, mirroring headings. Included a proposed text scale table and the component changes needed (XDSTextVariant type, class emission, theme overrides).

    2. typeScale is an object with all 3 variants — changed from a string selector to:

    typeScale: {
      functional: { base: 12, ratio: 1.125 },
      default:    { base: 14, ratio: 1.125 },
      editorial:  { base: 16, ratio: 1.25  },
    }

    All three are always available in every theme. The config just controls what values each one maps to. Explicit components still deep-merge on top.

  5. cixzhang commented on Mar 12, 2026

    @cixzhang
    ContributorAuthor

    Looks good in the latest spec iteration. Make sure that things like components.heading also preserves updating the prose HTML elements like h1, etc even with this shortcut form of typescale configuration.

  6. cixzhang commented on Mar 12, 2026

    @cixzhang
    ContributorAuthor

    Good catch — added a dedicated section on prose/FontWrapper coverage.

    Currently typography.css hardcodes the heading styles for each variant (.xds-typography--default h1, etc.). The typeScale expansion needs to generate overrides for these too, not just the XDSHeading/XDSText component classes.

    Three options sketched out in the spec (Option A: fontwrapper component key, Option B: refactor to CSS custom properties, Option C: extra rules in generateThemeCSS). Also added tasks for XDSFontWrapperVariant getting 'functional' and the new xds-typography--functional class + styles.

  7. cixzhang commented on Mar 15, 2026

    @cixzhang
    ContributorAuthor

    CSS Layer Specificity Problem for Typography Theming

    While testing the example apps as external consumers, we discovered a fundamental specificity conflict between theme overrides and component prop overrides that's especially relevant to this issue's design.

    The problem

    XDS has three levels of styling that need to cascade correctly:

    1. Base component styles (StyleX stylex.create) — the defaults
    2. Theme overrides (@scope CSS in @layer xds.theme) — theme-level customization
    3. Prop overrides (e.g. <XDSText color="secondary">) — explicit user intent, should always win

    The conflict: both #1 and #3 come from StyleX (same CSS priority layers), while #2 is in a separate @layer xds.theme. For the theme layer to override base styles, it needs higher priority than the StyleX layers. But then prop overrides (also in StyleX layers) can't beat the theme.

    Example of the bug

    A theme defines heading styles in @layer xds.theme:

    @scope ([data-xds-theme="custom"]) {
      .xds-text.body { color: navy; }
    }

    A consumer uses <XDSText color="secondary"> which applies a StyleX class for color: var(--color-text-secondary). But if xds.theme has higher layer priority than the StyleX priority layers, the theme's navy wins over the prop's secondary — the prop has no effect.

    Which components are affected?

    We analyzed which components have props that conflict with theme-overridable properties:

    Component Conflicting props Theme-overridable properties
    XDSText color, weight, type color, font-size, font-weight, line-height
    XDSHeading color color, font-size, font-weight, line-height
    XDSLink color color

    Components like Button, Badge, Card, Divider use discrete variants where the theme maps 1:1 to the variant — no cross-cutting prop conflicts.

    How this affects typeScale design

    The typeScale feature in this issue would generate theme CSS for all heading levels and text types across three variants (functional, default, editorial). This significantly expands the surface area of the conflict. If the theme defines font-size for .xds-text.body, and a consumer uses <XDSText type="large">, which font-size wins?

    Possible approaches

    A. Higher-level typography tokens instead of component overrides

    Instead of themes overriding individual component styles (font-size on .xds-text.body), themes define typography as a token system — the type scale ratios, base sizes, and weight mappings become tokens. Components reference these tokens via StyleX, so there's no layer conflict. The typeScale shortcut in defineTheme would generate token overrides, not component CSS.

    This aligns naturally with the ratio-based approach from PR #555 — the theme picks a base size and ratio, and all text/heading sizes derive from tokens.

    B. Emit prop-override rules in the theme layer

    When a theme overrides color on .xds-text.body, the builder also emits .xds-text.secondary { color: var(--color-text-secondary) } in the same theme layer so prop variants still win by specificity/source order. Downside: theme CSS grows, and old themes need rebuilding when new variants are added.

    C. Restrict what themes can override on typography

    Only allow themes to customize typography through tokens (font family, scale, weights) — not through component-level property overrides. If a theme can't set color on .xds-text.body, the conflict doesn't exist. The color prop in StyleX always wins because there's nothing in the theme layer competing with it.

    Recommendation

    Option A feels right for typography — the typeScale design in this issue is already heading in that direction. Themes should define the type system at the token level (base, ratio, weight mappings), and the build-theme command translates that into token overrides. Component-level @scope overrides would be reserved for truly component-specific visuals (button backgrounds, card borders) where there's no prop-level conflict.

  8. cixzhang commented on Mar 15, 2026

    @cixzhang
    ContributorAuthor

    Correction to my previous comment

    The recommendation isn't to replace component overrides with raw tokens. The idea is a structured type system definition in defineTheme that the builder expands into the correct CSS — including prop-level override rules.

    Example:

    defineTheme({
      name: 'admin',
      typeScale: {
        base: 12,
        scale: 1.2,
        colors: {
          supporting: 'var(--color-text-secondary)',
          // ...
        },
      },
    });

    The build-theme command would then generate the full cascade:

    .xds-text.supporting { color: var(--color-text-secondary); font-size: ...; }
    .xds-text.supporting.primary { color: var(--color-text-primary); }
    .xds-text.supporting.secondary { color: var(--color-text-secondary); }
    /* ... all color prop variants */

    The theme author thinks at the type system level — base size, scale ratio, semantic colors. The builder knows the full set of prop variants (primary, secondary, disabled, etc.) and generates the override rules needed for correct specificity. When a new color variant is added to XDSText, the builder automatically includes it in the output.

    This solves the layer specificity problem because the prop-level rules live in the same @layer xds.theme as the base theme overrides, and the builder ensures they're complete. The theme author doesn't manually manage which CSS rules are needed — the structured input guarantees correctness.

  9. cixzhang commented on Mar 15, 2026

    @cixzhang
    ContributorAuthor

    Correction on the example

    The typeScale values are the actual design values, not CSS variable references:

    defineTheme({
      name: 'admin',
      typeScale: {
        base: 12,
        scale: 1.2,
        colors: {
          supporting: ['#4E606F', '#AAAFB5'],  // [light, dark]
          // ...
        },
      },
    });

    The builder generates the CSS from these values:

    .xds-text.supporting { color: light-dark(#4E606F, #AAAFB5); font-size: ...; }
    .xds-text.supporting.primary { color: light-dark(#0A1317, #DFE2E5); }
    .xds-text.supporting.secondary { color: light-dark(#4E606F, #AAAFB5); }

    The theme defines the actual colors. The builder expands them into the complete set of component rules with correct specificity.

  10. ernestt commented on Mar 16, 2026

    @ernestt
    Contributor

    I think of scale ratio as a early stage guidance value instead of a build time component level overrides. So I think my mental model aligned more with:

    A. Higher-level typography tokens instead of component overrides

    Instead of themes overriding individual component styles (font-size on .xds-text.body), themes define typography as a token system — the type scale ratios, base sizes, and weight mappings become tokens. Components reference these tokens via StyleX, so there's no layer conflict. The typeScale shortcut in defineTheme would generate token overrides, not component CSS.

    This aligns naturally with the ratio-based approach from PR #555 — the theme picks a base size and ratio, and all text/heading sizes derive from tokens.

  11. ernestt commented on Mar 16, 2026

    @ernestt
    Contributor

    API Proposal: Token-Based typeScale

    Building on @ernestt's comment about the token-based approach (Option A) — here's a concrete API proposal. The core idea: typeScale generates token overrides, not component CSS. This follows the same pattern XDS already uses for colors (--color-text-primary is a semantic token over raw color values).


    Where should variant live?

    The issue spec proposes adding variant to both XDSText and XDSHeading. I'd argue against putting it on XDSText:

    Density is a product/region decision, not a text decision. A product picks its position on the functional → default → editorial spectrum, and the theme enforces consistency. If individual text components can opt into different variants inline, you get inconsistency by default — developers mixing functional headings and editorial body text on the same page with no guardrail. The theme becomes advisory rather than authoritative.

    It's the wrong abstraction level. variant controls density, which is a layout/region concern. It belongs on something that scopes a subtree — XDSFontWrapper or XDSTheme — not on leaf text components. Putting it on every <XDSText> and <XDSHeading> buries a region-level decision inside individual elements.

    Prop surface area for a rare use case. Most consumers will never touch variant — they'll use whatever their theme defines. Adding it to XDSText means every instance carries an option most people shouldn't use.

    The alternative for mixed density: XDSFontWrapper already has variant and scopes a region. For the case where a dashboard needs a dense sidebar alongside default-density main content, nesting <XDSFontWrapper variant="functional"> around that section is explicit and visible. It says "this region is denser" at the layout level.

    Backwards compat note: XDSHeading already has variant today for editorial. We could deprecate it in favor of the region-based approach, but that's a separate migration. The important thing is not extending the pattern to XDSText.


    New semantic typography tokens

    Add a typeScaleDefaults token group to tokens.stylex.ts with the naming convention:

    --heading-{variant}-{level}-{size|weight|leading}
    --text-{variant}-{type}-{size|weight|leading}
    

    For example:

    export const typeScaleDefaults = {
      // Heading tokens — default variant
      '--heading-default-1-size': 'var(--text-2xl)',     // 20px
      '--heading-default-1-weight': 'var(--font-weight-semibold)',
      '--heading-default-1-leading': '1.2',
      '--heading-default-2-size': 'var(--text-xl)',      // 18px
      '--heading-default-2-weight': 'var(--font-weight-semibold)',
      '--heading-default-2-leading': '1.333',
      // ... levels 3-6
    
      // Heading tokens — functional variant
      '--heading-functional-1-size': 'var(--text-lg)',   // 16px
      '--heading-functional-1-weight': 'var(--font-weight-semibold)',
      '--heading-functional-1-leading': '1.25',
      // ... levels 2-6
    
      // Heading tokens — editorial variant
      '--heading-editorial-1-size': 'var(--text-4xl)',   // 32px
      // ...
    
      // Text tokens — default variant
      '--text-default-body-size': 'var(--text-base)',    // 14px
      '--text-default-body-weight': 'var(--font-weight-normal)',
      '--text-default-body-leading': 'var(--leading-base)',
      '--text-default-large-size': 'var(--text-lg)',
      // ...
    
      // Text tokens — functional variant
      '--text-functional-body-size': 'var(--text-xsm)',  // 12px
      // ...
    
      // Text tokens — editorial variant
      '--text-editorial-body-size': 'var(--text-lg)',    // 16px
      // ...
    } as const;

    This gives us ~90 tokens (3 variants × {6 heading levels + 5 text types} × 3 properties). The defaults reference the existing --text-* size palette, so out of the box everything resolves to the same values we have today.

    Components, theme overrides, and typography.css reference the new tokens

    Theme component overrides become thin wrappers over token references:

    // defaultTheme.ts
    heading: {
      'level:1': {
        fontSize: 'var(--heading-default-1-size)',
        fontWeight: 'var(--heading-default-1-weight)',
        lineHeight: 'var(--heading-default-1-leading)',
        // ...
      },
      'variant:editorial+level:1': {
        fontSize: 'var(--heading-editorial-1-size)',
        fontWeight: 'var(--heading-editorial-1-weight)',
        lineHeight: 'var(--heading-editorial-1-leading)',
      },
      'variant:functional+level:1': {
        fontSize: 'var(--heading-functional-1-size)',
        fontWeight: 'var(--heading-functional-1-weight)',
        lineHeight: 'var(--heading-functional-1-leading)',
      },
      // ... same pattern for levels 2-6
    }

    And typography.css does the same:

    .xds-typography h1,
    .xds-typography--default h1 {
      font-size: var(--heading-default-1-size);
      font-weight: var(--heading-default-1-weight);
      line-height: var(--heading-default-1-leading);
    }
    
    .xds-typography--functional h1 {
      font-size: var(--heading-functional-1-size);
      font-weight: var(--heading-functional-1-weight);
      line-height: var(--heading-functional-1-leading);
    }

    This guarantees XDSHeading, XDSText, and XDSFontWrapper prose all read from the same source of truth.

    typeScale in defineTheme — syntactic sugar for token overrides

    The typeScale shortcut computes concrete values from { base, ratio } and writes them as token overrides — no component CSS generated:

    const adminTheme = defineTheme({
      name: 'admin-panel',
      typeScale: {
        functional: { base: 12, ratio: 1.125 },
        default:    { base: 14, ratio: 1.125 },
        editorial:  { base: 16, ratio: 1.25 },
      },
      tokens: {
        '--color-accent': ['#1A73E8', '#8AB4F8'],
      },
    });

    Internally, defineTheme calls a pure expandTypeScale() function that generates token overrides:

    // expandTypeScale() output (merged into tokens):
    {
      '--heading-default-1-size': '1.25rem',   // 14 × 1.125^5 ≈ 20px
      '--heading-default-1-leading': '1.2',
      '--heading-default-2-size': '1.125rem',  // 14 × 1.125^4 ≈ 18px
      '--text-default-body-size': '0.875rem',  // base 14px
      // ...
    }

    These land in the :scope {} block alongside color and spacing overrides. Explicit components overrides still deep-merge on top for things outside the type scale system (e.g. letterSpacing, custom weight on a single level).

    Why this solves the specificity problem

    The cascade becomes:

    1. Token layer — theme sets --heading-default-1-size: 1.5rem via :scope
    2. Component CSS (theme overrides) — .xds-heading.level-1 { font-size: var(--heading-default-1-size) } — reads the token
    3. StyleX props — <XDSHeading color="secondary"> applies color: var(--color-text-secondary)

    No conflict: the theme's type scale influence flows through custom properties. StyleX props control orthogonal properties (color, weight overrides) directly. They never compete over the same declaration in different layers.

    This is the same pattern as colors: a theme overrides --color-accent (token), components reference var(--color-accent) (consumption), and StyleX handles prop-driven overrides (color="secondary") without specificity fights.

    Summary of changes

    Area Change
    tokens.stylex.ts Add ~90 typeScaleDefaults tokens
    defineTheme.ts Add optional typeScale field; expandTypeScale() merges into tokens
    defaultTheme.ts Component overrides reference var(--heading-*-*-*) tokens
    typography.css Heading/text rules reference new tokens; add --functional variant rules
    XDSFontWrapper.tsx Widen variant type to XDSTypeScaleVariant, add xds-typography--functional class
    XDSHeading.tsx Add 'functional' to existing variant prop (already has variant for backwards compat)
    XDSText.tsx No variant prop — density controlled at region level via XDSFontWrapper
    Shared type XDSTypeScaleVariant = 'functional' | 'default' | 'editorial'

    XDSText stays focused on semantic text types (body, label, supporting, etc.) and doesn't take on density variant responsibility. That's the theme and XDSFontWrapper's job.

    Tradeoffs

    Pros:

    • No specificity conflict — type scale is tokens, props are StyleX
    • typography.css and component overrides read the same tokens — guaranteed consistency
    • typeScale is pure syntactic sugar over the existing token system — no new CSS mechanism
    • expandTypeScale() is a pure, testable function
    • Follows the established color token pattern — consistent mental model

    Cons:

    • ~90 new CSS custom properties (lightweight, but worth noting)
    • Two layers of indirection for typography (--text-base → --heading-default-1-size → component) — same as colors though (#0064E0 → --color-accent → component)

    — via Navi on behalf of @ernestt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions