Skip to content

refactor: shared theme CSS generation + remove XDSFontWrapper - #665

Merged
cixzhang merged 9 commits into
mainfrom
navi/fix/theme-prose-typography
Mar 18, 2026
Merged

cixzhang merged 9 commits into
mainfrom
navi/fix/theme-prose-typography

Conversation

@cixzhang

@cixzhang cixzhang commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Refactors theme CSS generation so runtime and build use an identical shared path through generateThemeRules(). Also removes XDSFontWrapper, adds prose HTML element rules, prop-level color overrides, and outputs type scale sizes in rem.

Architecture

defineTheme(input)           → XDSDefinedTheme (intermediary)
  ├─ expandTypeScale()       → computed rem tokens
  ├─ generateTypeScaleComponents() → .xds-heading/.xds-text rules
  └─ merge explicit overrides

generateThemeRules(theme)    → CSS rule strings (shared)
  ├─ Token :scope block
  ├─ Component .xds-* overrides
  ├─ Prose h1-h6, p, small, code, hr
  └─ Prop-level color overrides

Runtime: @scope wrapper → useInsertionEffect
Build:   @layer xds.theme { @scope } → theme.css

Changes

  1. Remove XDSFontWrapper — was <div className='xds-typography'>. Migration: remove wrapper, render children.
  2. generateThemeRules() — shared intermediary producing identical CSS for both paths.
  3. Build uses shared functions — build-theme.mjs calls defineTheme() + generateThemeRules() from core.
  4. Color class on Text/Heading — xdsClassName includes color prop for theme specificity.
  5. rem output — type scale sizes in rem (e.g. 1.5rem) instead of px. Respects user browser font preferences.
  6. Build output tests — 14 tests validating rules, consistency, and weight overrides.

Theme Input (default)

/**
 * XDS Default Theme
 *
 * The reference theme — provides the standard XDS visual language.
 * Uses defineTheme for CSS-based theming (no StyleX build required for consumers).
 *
 * Typography sizing is driven by typeScale, which auto-generates both
 * the token values (--heading-1-size, --text-body-size, etc.) and the
 * component override rules (.xds-heading.level-1 { font-size: ... }).
 *
 * Only non-typography component overrides need to be specified manually.
 */

import {defineTheme} from '@xds/core/theme';
import {defaultIconRegistry} from './icons';

export const defaultTheme = defineTheme({
  name: 'default',

  // Type scale: base=14px, ratio=1.2, h4 anchored to base.
  // Generates all heading + text tokens AND component overrides automatically.
  typeScale: {base: 14, ratio: 1.2},

  // The default theme uses the built-in token defaults from tokens.stylex.ts.
  // No additional token overrides needed.
  tokens: {},

  components: {
    // =========================================================================
    // Button
    // =========================================================================
    button: {
      'variant:secondary': {
        backgroundColor:
          'light-dark(rgba(5, 54, 89, 0.1), rgba(223, 226, 229, 0.2))',
      },
    },

    // Heading and text component overrides are auto-generated by typeScale.
    // No manual heading: { ... } or text: { ... } blocks needed.
  },

  icons: defaultIconRegistry,
});

Runtime Output (generateThemeCSS → <style> tag)

@scope ([data-xds-theme="default"]) to ([data-xds-theme]) {
  :scope {
    --heading-1-size: 1.5rem;
    --heading-1-weight: var(--font-weight-semibold);
    --heading-1-leading: 1.3333;
    --heading-2-size: 1.25rem;
    --heading-2-weight: var(--font-weight-semibold);
    --heading-2-leading: 1.4;
    --heading-3-size: 1.0625rem;
    --heading-3-weight: var(--font-weight-semibold);
    --heading-3-leading: 1.4118;
    --heading-4-size: 0.875rem;
    --heading-4-weight: var(--font-weight-semibold);
    --heading-4-leading: 1.4286;
    --heading-5-size: 0.75rem;
    --heading-5-weight: var(--font-weight-semibold);
    --heading-5-leading: 1.3333;
    --heading-6-size: 0.625rem;
    --heading-6-weight: var(--font-weight-semibold);
    --heading-6-leading: 1.6;
    --text-body-size: 0.875rem;
    --text-body-weight: var(--font-weight-normal);
    --text-body-leading: 1.7143;
    --text-large-size: 1.0625rem;
    --text-large-weight: var(--font-weight-semibold);
    --text-large-leading: 1.6471;
    --text-label-size: 0.875rem;
    --text-label-weight: var(--font-weight-medium);
    --text-label-leading: 1.4286;
    --text-code-size: 0.875rem;
    --text-code-weight: var(--font-weight-normal);
    --text-code-leading: 1.7143;
    --text-supporting-size: 0.75rem;
    --text-supporting-weight: var(--font-weight-normal);
    --text-supporting-leading: 1.6667;
  }

  .xds-heading.level-1 {
    font-family: var(--font-heading);
    font-size: var(--heading-1-size);
    font-weight: var(--heading-1-weight);
    line-height: var(--heading-1-leading);
  }

  .xds-heading.level-2 {
    font-family: var(--font-heading);
    font-size: var(--heading-2-size);
    font-weight: var(--heading-2-weight);
    line-height: var(--heading-2-leading);
  }

  .xds-heading.level-3 {
    font-family: var(--font-heading);
    font-size: var(--heading-3-size);
    font-weight: var(--heading-3-weight);
    line-height: var(--heading-3-leading);
  }

  .xds-heading.level-4 {
    font-family: var(--font-heading);
    font-size: var(--heading-4-size);
    font-weight: var(--heading-4-weight);
    line-height: var(--heading-4-leading);
  }

  .xds-heading.level-5 {
    font-family: var(--font-heading);
    font-size: var(--heading-5-size);
    font-weight: var(--heading-5-weight);
    line-height: var(--heading-5-leading);
  }

  .xds-heading.level-6 {
    font-family: var(--font-heading);
    font-size: var(--heading-6-size);
    font-weight: var(--heading-6-weight);
    line-height: var(--heading-6-leading);
  }

  .xds-text.body {
    font-family: var(--font-body);
    font-size: var(--text-body-size);
    font-weight: var(--text-body-weight);
    line-height: var(--text-body-leading);
  }

  .xds-text.large {
    font-family: var(--font-body);
    font-size: var(--text-large-size);
    font-weight: var(--text-large-weight);
    line-height: var(--text-large-leading);
  }

  .xds-text.label {
    font-family: var(--font-body);
    font-size: var(--text-label-size);
    font-weight: var(--text-label-weight);
    line-height: var(--text-label-leading);
  }

  .xds-text.code {
    font-family: var(--font-code);
    font-size: var(--text-code-size);
    font-weight: var(--text-code-weight);
    line-height: var(--text-code-leading);
  }

  .xds-text.supporting {
    font-family: var(--font-body);
    font-size: var(--text-supporting-size);
    font-weight: var(--text-supporting-weight);
    line-height: var(--text-supporting-leading);
  }

  .xds-button.secondary {
    background-color: light-dark(rgba(5, 54, 89, 0.1), rgba(223, 226, 229, 0.2));
  }

  :is(h1, h2, h3, h4, h5, h6) {
    font-family: var(--font-heading);
    color: var(--color-text-primary);
  }

  h1 {
    font-size: 1.5rem;
    font-weight: var(--font-weight-semibold);
    line-height: 1.3333;
  }

  h2 {
    font-size: 1.25rem;
    font-weight: var(--font-weight-semibold);
    line-height: 1.4;
  }

  h3 {
    font-size: 1.0625rem;
    font-weight: var(--font-weight-semibold);
    line-height: 1.4118;
  }

  h4 {
    font-size: 0.875rem;
    font-weight: var(--font-weight-semibold);
    line-height: 1.4286;
  }

  h5 {
    font-size: 0.75rem;
    font-weight: var(--font-weight-semibold);
    line-height: 1.3333;
  }

  h6 {
    font-size: 0.625rem;
    font-weight: var(--font-weight-semibold);
    line-height: 1.6;
  }

  p {
    font-family: var(--font-heading);
    font-size: 0.875rem;
    font-weight: var(--font-weight-normal);
    line-height: 1.7143;
    color: var(--color-text-primary);
  }

  small {
    font-size: 0.75rem;
    font-weight: var(--font-weight-normal);
    line-height: 1.6667;
    color: var(--color-text-secondary);
  }

  code, pre {
    font-family: var(--font-code);
    font-size: 0.875rem;
    line-height: 1.7143;
  }

  hr {
    border: none;
    border-top: 1px solid var(--color-divider);
  }

  .xds-text.primary { color: var(--color-text-primary); }

  .xds-heading.primary { color: var(--color-text-primary); }

  .xds-text.secondary { color: var(--color-text-secondary); }

  .xds-heading.secondary { color: var(--color-text-secondary); }

  .xds-text.disabled { color: var(--color-text-disabled); }

  .xds-heading.disabled { color: var(--color-text-disabled); }

  .xds-text.placeholder { color: var(--color-text-placeholder); }

  .xds-heading.placeholder { color: var(--color-text-placeholder); }

  .xds-text.active { color: var(--color-accent); }

  .xds-heading.active { color: var(--color-accent); }
}

Build Output (xds theme build → theme.css)

@layer xds.theme {
  :root { color-scheme: light dark; }

@scope ([data-xds-theme="default"]) to ([data-xds-theme]) {
  :scope {
    --heading-1-size: 1.5rem;
    --heading-1-weight: var(--font-weight-semibold);
    --heading-1-leading: 1.3333;
    --heading-2-size: 1.25rem;
    --heading-2-weight: var(--font-weight-semibold);
    --heading-2-leading: 1.4;
    --heading-3-size: 1.0625rem;
    --heading-3-weight: var(--font-weight-semibold);
    --heading-3-leading: 1.4118;
    --heading-4-size: 0.875rem;
    --heading-4-weight: var(--font-weight-semibold);
    --heading-4-leading: 1.4286;
    --heading-5-size: 0.75rem;
    --heading-5-weight: var(--font-weight-semibold);
    --heading-5-leading: 1.3333;
    --heading-6-size: 0.625rem;
    --heading-6-weight: var(--font-weight-semibold);
    --heading-6-leading: 1.6;
    --text-body-size: 0.875rem;
    --text-body-weight: var(--font-weight-normal);
    --text-body-leading: 1.7143;
    --text-large-size: 1.0625rem;
    --text-large-weight: var(--font-weight-semibold);
    --text-large-leading: 1.6471;
    --text-label-size: 0.875rem;
    --text-label-weight: var(--font-weight-medium);
    --text-label-leading: 1.4286;
    --text-code-size: 0.875rem;
    --text-code-weight: var(--font-weight-normal);
    --text-code-leading: 1.7143;
    --text-supporting-size: 0.75rem;
    --text-supporting-weight: var(--font-weight-normal);
    --text-supporting-leading: 1.6667;
  }

  .xds-heading.level-1 {
    font-family: var(--font-heading);
    font-size: var(--heading-1-size);
    font-weight: var(--heading-1-weight);
    line-height: var(--heading-1-leading);
  }

  .xds-heading.level-2 {
    font-family: var(--font-heading);
    font-size: var(--heading-2-size);
    font-weight: var(--heading-2-weight);
    line-height: var(--heading-2-leading);
  }

  .xds-heading.level-3 {
    font-family: var(--font-heading);
    font-size: var(--heading-3-size);
    font-weight: var(--heading-3-weight);
    line-height: var(--heading-3-leading);
  }

  .xds-heading.level-4 {
    font-family: var(--font-heading);
    font-size: var(--heading-4-size);
    font-weight: var(--heading-4-weight);
    line-height: var(--heading-4-leading);
  }

  .xds-heading.level-5 {
    font-family: var(--font-heading);
    font-size: var(--heading-5-size);
    font-weight: var(--heading-5-weight);
    line-height: var(--heading-5-leading);
  }

  .xds-heading.level-6 {
    font-family: var(--font-heading);
    font-size: var(--heading-6-size);
    font-weight: var(--heading-6-weight);
    line-height: var(--heading-6-leading);
  }

  .xds-text.body {
    font-family: var(--font-body);
    font-size: var(--text-body-size);
    font-weight: var(--text-body-weight);
    line-height: var(--text-body-leading);
  }

  .xds-text.large {
    font-family: var(--font-body);
    font-size: var(--text-large-size);
    font-weight: var(--text-large-weight);
    line-height: var(--text-large-leading);
  }

  .xds-text.label {
    font-family: var(--font-body);
    font-size: var(--text-label-size);
    font-weight: var(--text-label-weight);
    line-height: var(--text-label-leading);
  }

  .xds-text.code {
    font-family: var(--font-code);
    font-size: var(--text-code-size);
    font-weight: var(--text-code-weight);
    line-height: var(--text-code-leading);
  }

  .xds-text.supporting {
    font-family: var(--font-body);
    font-size: var(--text-supporting-size);
    font-weight: var(--text-supporting-weight);
    line-height: var(--text-supporting-leading);
  }

  .xds-button.secondary {
    background-color: light-dark(rgba(5, 54, 89, 0.1), rgba(223, 226, 229, 0.2));
  }

  :is(h1, h2, h3, h4, h5, h6) {
    font-family: var(--font-heading);
    color: var(--color-text-primary);
  }

  h1 {
    font-size: 1.5rem;
    font-weight: var(--font-weight-semibold);
    line-height: 1.3333;
  }

  h2 {
    font-size: 1.25rem;
    font-weight: var(--font-weight-semibold);
    line-height: 1.4;
  }

  h3 {
    font-size: 1.0625rem;
    font-weight: var(--font-weight-semibold);
    line-height: 1.4118;
  }

  h4 {
    font-size: 0.875rem;
    font-weight: var(--font-weight-semibold);
    line-height: 1.4286;
  }

  h5 {
    font-size: 0.75rem;
    font-weight: var(--font-weight-semibold);
    line-height: 1.3333;
  }

  h6 {
    font-size: 0.625rem;
    font-weight: var(--font-weight-semibold);
    line-height: 1.6;
  }

  p {
    font-family: var(--font-heading);
    font-size: 0.875rem;
    font-weight: var(--font-weight-normal);
    line-height: 1.7143;
    color: var(--color-text-primary);
  }

  small {
    font-size: 0.75rem;
    font-weight: var(--font-weight-normal);
    line-height: 1.6667;
    color: var(--color-text-secondary);
  }

  code, pre {
    font-family: var(--font-code);
    font-size: 0.875rem;
    line-height: 1.7143;
  }

  hr {
    border: none;
    border-top: 1px solid var(--color-divider);
  }

  .xds-text.primary { color: var(--color-text-primary); }

  .xds-heading.primary { color: var(--color-text-primary); }

  .xds-text.secondary { color: var(--color-text-secondary); }

  .xds-heading.secondary { color: var(--color-text-secondary); }

  .xds-text.disabled { color: var(--color-text-disabled); }

  .xds-heading.disabled { color: var(--color-text-disabled); }

  .xds-text.placeholder { color: var(--color-text-placeholder); }

  .xds-heading.placeholder { color: var(--color-text-placeholder); }

  .xds-text.active { color: var(--color-accent); }

  .xds-heading.active { color: var(--color-accent); }
}
}

Differences

Runtime Build
Wrapper @scope only @layer xds.theme { @scope }
color-scheme No :root { color-scheme: light dark; }
Content Identical Identical

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 17, 2026
@cixzhang
cixzhang marked this pull request as ready for review March 18, 2026 02:20
cixzhang and others added 8 commits March 18, 2026 02:35
XDSFontWrapper was just <div className='xds-typography'>{children}</div>.
Migration: remove the wrapper, render children directly. Use className
'xds-typography' on a parent div if prose styling is needed.

Co-authored-by: Navi <[email protected]>
Both runtime and build paths now use the same generateThemeRules()
function from @xds/core to produce CSS rules. The only difference
is how the output is wrapped:
- Runtime: @scope only (no @layer), injected via useInsertionEffect
- Build: @layer xds.theme { @scope { ... } }, written to theme.css

generateThemeRules accepts { computedValues: boolean } — when true,
prose rules use computed px values from tokens (self-contained CSS);
when false, they use var() references (runtime with token :scope).

Co-authored-by: Navi <[email protected]>
The build path now calls defineTheme() + generateThemeRules() from
@xds/core/theme — the same functions the runtime uses. This ensures
typeScale expansion, component override generation, prose rules, and
prop-level color overrides are identical in both paths.

Falls back to legacy generation if core isn't built yet.

Co-authored-by: Navi <[email protected]>
…ecificity

Text now emits xdsClassName('text', {type, color}) and Heading emits
xdsClassName('heading', {level, color}). This enables themes to target
color variants via CSS (.xds-text.primary, .xds-heading.secondary)
for prop-level override specificity in @layer xds.theme.

Co-authored-by: Navi <[email protected]>
Removed the computedValues option from generateThemeRules — both paths
now always use the computed values from tokens (which defineTheme
populates via expandTypeScale). The only difference between runtime and
build output is the wrapper: runtime uses bare @scope, build wraps in
@layer xds.theme with color-scheme.

Co-authored-by: Navi <[email protected]>
Font sizes from expandTypeScale now output rem values (e.g. 1.5rem
instead of 24px) based on the standard 16px root. This respects user
browser font size preferences for accessibility — rem scales with the
root font size while px is fixed.

The reset.css does not set a root font-size, following the convention
of leaving it at the browser default (typically 16px) so user
preferences work automatically.

Co-authored-by: Navi <[email protected]>
Tests cover:
- Token block structure (rem sizes, unitless line-heights, var() weights)
- Component override rules (.xds-heading.level-*, .xds-text.*)
- Prose HTML element rules (h1-h6, p, small, code, hr with computed values)
- Prop-level color overrides (.xds-text.primary, .xds-heading.secondary)
- Consistency: every rule from generateThemeRules appears in generateThemeCSS
- Weight override propagation through tokens and prose

Co-authored-by: Navi <[email protected]>
The Python script that removed FontWrapper component entries left
orphaned closing brackets in both the English and Chinese doc objects.

Co-authored-by: Navi <[email protected]>
@cixzhang
cixzhang force-pushed the navi/fix/theme-prose-typography branch from b88829e to cd0b233 Compare March 18, 2026 02:39
- Remove typography.css — prose styles are now generated by themes
  (both runtime and build paths emit h1-h6, p, small, code, hr rules)
- Remove all typography.css imports from example apps, storybook, sandbox
- Remove dist/index.css import from storybook preview (was generated from
  typography.css import in the now-deleted XDSFontWrapper)
- Fix remaining orphaned Chinese doc entry in Text.doc.mjs
- Remove typography.css export from package.json

Co-authored-by: Navi <[email protected]>
@github-actions

github-actions Bot commented Mar 18, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

Modified Components

Text · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A 335B N/A
Lines of Code N/A 484 -
Complexity N/A Very High (44) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@xds/core 10.3KB 16.4KB 2.5KB

Accessibility Audit

Status: 1 accessibility violation(s) found — 1 serious.

XDSText - 1 issue(s)
  • 🟠 serious: Ensure the contrast between foreground and background colors meets WCAG 2 AA minimum contrast ratio thresholds
    • Rule: color-contrast · Affects 1/22 stories · Learn more
    • WCAG: 1.4.3 (Level AA)

Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

@cixzhang
cixzhang merged commit 6a4254a into main Mar 18, 2026
11 checks passed
@cixzhang
cixzhang deleted the navi/fix/theme-prose-typography branch March 20, 2026 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant