Skip to content

fix(type-scale): drive raw tokens from base+ratio, semantic tokens as var() refs - #682

Merged
cixzhang merged 7 commits into
mainfrom
navi/feat/type-scale-raw-tokens-v2
Mar 20, 2026
Merged

cixzhang merged 7 commits into
mainfrom
navi/feat/type-scale-raw-tokens-v2

Conversation

@ernestt

@ernestt ernestt commented Mar 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Restructures the type scale system so that { base, ratio } drives the raw size tokens, with semantic tokens becoming var() references for sizes and hardcoded computed values for line heights. This fixes the core problem with PR #638: semantic tokens were computed from the ratio but the 47 components using raw textSizeVars (Button, Badge, Table, etc.) were left static and out of sync.

Named leading tokens (--leading-tight through --leading-relaxed) are not modified — they remain as intent-based ratios for component use.

Architecture

typeScale: { base, ratio }
       │
       ▼
┌─────────────────────────────────────────────────┐
│  Layer 1: Raw Size Tokens (rem)                 │
│  --text-xsm … --text-4xl                       │
│  Geometric: round(base × ratio^step) / 16      │
├─────────────────────────────────────────────────┤
│  Layer 2: Semantic Tokens                       │
│  Sizes → var(--text-*)                          │
│  Line heights → hardcoded computed values       │
│  Weights → var(--font-weight-*) (unchanged)     │
└─────────────────────────────────────────────────┘

Named leading tokens (--leading-tight … --leading-relaxed)
are NOT part of the type scale system — untouched.

Step-to-Token Mapping

Step Size Token Heading Text Type
-2 --text-xsm h6
-1 --text-sm h5 supporting
0 --text-base h4 body, label, code
+1 --text-lg h3 large
+2 --text-xl h2
+3 --text-2xl h1
+4 --text-3xl
+5 --text-4xl

Line Height Computation

Tiered target ratio based on font size, then 4px grid snap:

targetRatio = fontSize < 20 ? 1.5 : fontSize < 32 ? 1.4 : 1.25;
snappedLh = max(round(fontSize * targetRatio / 4) * 4, ceil((fontSize + 4) / 4) * 4);

Default scale (14/1.2) verification

Element Font Target LH px Ratio 4px ✓ ≥f+4 ✓
h1 24px 1.4 32px 1.3333 ✓ ✓
h2 20px 1.4 28px 1.4 ✓ ✓
h3 17px 1.5 24px 1.4118 ✓ ✓
h4 / body 14px 1.5 20px 1.4286 ✓ ✓
h5 / supporting 12px 1.5 20px 1.6667 ✓ ✓
h6 10px 1.5 16px 1.6 ✓ ✓

Raw CSS Output

@scope ([data-xds-theme="default"]) to ([data-xds-theme]) {
  :scope {
    /* Layer 1: Raw size tokens (geometric: round(14 × 1.2^step) / 16) */
    --text-xsm: 0.625rem;     /* step -2: 10px */
    --text-sm: 0.75rem;        /* step -1: 12px */
    --text-base: 0.875rem;     /* step  0: 14px (anchor) */
    --text-lg: 1.0625rem;      /* step +1: 17px */
    --text-xl: 1.25rem;        /* step +2: 20px */
    --text-2xl: 1.5rem;        /* step +3: 24px */
    --text-3xl: 1.8125rem;     /* step +4: 29px */
    --text-4xl: 2.1875rem;     /* step +5: 35px */

    /* Layer 2: Semantic tokens */
    /* Sizes are var() refs to Layer 1 */
    /* Line heights are computed: tiered target, 4px grid snap */
    --heading-1-size: var(--text-2xl);
    --heading-1-weight: var(--font-weight-semibold);
    --heading-1-leading: 1.3333;   /* 24px → 32px */
    --heading-2-size: var(--text-xl);
    --heading-2-weight: var(--font-weight-semibold);
    --heading-2-leading: 1.4;      /* 20px → 28px */
    --heading-3-size: var(--text-lg);
    --heading-3-weight: var(--font-weight-semibold);
    --heading-3-leading: 1.4118;   /* 17px → 24px */
    --heading-4-size: var(--text-base);
    --heading-4-weight: var(--font-weight-semibold);
    --heading-4-leading: 1.4286;   /* 14px → 20px */
    --heading-5-size: var(--text-sm);
    --heading-5-weight: var(--font-weight-semibold);
    --heading-5-leading: 1.6667;   /* 12px → 20px */
    --heading-6-size: var(--text-xsm);
    --heading-6-weight: var(--font-weight-semibold);
    --heading-6-leading: 1.6;      /* 10px → 16px */
    --text-body-size: var(--text-base);
    --text-body-weight: var(--font-weight-normal);
    --text-body-leading: 1.4286;   /* 14px → 20px */
    --text-large-size: var(--text-lg);
    --text-large-weight: var(--font-weight-semibold);
    --text-large-leading: 1.4118;  /* 17px → 24px */
    --text-label-size: var(--text-base);
    --text-label-weight: var(--font-weight-medium);
    --text-label-leading: 1.4286;  /* 14px → 20px */
    --text-code-size: var(--text-base);
    --text-code-weight: var(--font-weight-normal);
    --text-code-leading: 1.4286;   /* 14px → 20px */
    --text-supporting-size: var(--text-sm);
    --text-supporting-weight: var(--font-weight-normal);
    --text-supporting-leading: 1.6667; /* 12px → 20px */
  }
  /* Component overrides follow... */
}

Changes

File What changed
expandTypeScale.ts Removed dual target constants. Added targetLeadingRatio() tiered function + pxToRem(). Now emits 41 tokens: 8 raw size (rem) + 33 semantic (sizes as var refs, leadings as computed values).
tokens.stylex.ts textSizeDefaults aligned to geometric scale in rem. typeScaleDefaults sizes changed to var() refs, leadings updated to match tiered computation. lineHeightDefaults unchanged.
expandTypeScale.test.ts 20 tests covering both layers, 4px grid alignment across 4 scales, tiered target boundaries.
defineTheme.test.ts Updated assertions for var() ref format and token count (33→41).
generateThemeRules.test.ts Updated to expect var() refs for semantic sizes, rem for raw tokens.
TYPE_SCALE_REFERENCE.md Full computation tables, architecture diagram, raw CSS output.

All 1617 tests pass.


@ernestt
ernestt requested a review from cixzhang as a code owner March 18, 2026 18:32
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 18, 2026
@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.

No new or modified components detected.

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@xds/core 10.7KB 17.0KB 2.6KB

Accessibility Audit

Status: No accessibility violations detected.


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

@ernestt
ernestt force-pushed the navi/feat/type-scale-raw-tokens-v2 branch from a1e8d5f to 187d34d Compare March 18, 2026 19:01
@ernestt ernestt changed the title feat(type-scale): drive raw tokens from base+ratio, semantic tokens as var() refs fix(type-scale): drive raw tokens from base+ratio, semantic tokens as var() refs Mar 18, 2026
Comment on lines +267 to +271
'--text-4xs': '0.5rem', // 8px - citation
'--text-3xs': '0.625rem', // 10px - micro
'--text-2xs': '0.6875rem', // 11px - small micro
'--text-xsm': '0.75rem', // 12px - supporting, badge
'--text-sm': '0.8125rem', // 13px - secondary text
'--text-base': '0.875rem', // 14px - body text (XDS default)
'--text-lg': '1rem', // 16px - large body
'--text-xl': '1.125rem', // 18px - h2
'--text-2xl': '1.25rem', // 20px - h1
'--text-3xl': '1.5rem', // 24px - editorial h2
'--text-4xl': '2rem', // 32px - editorial h1, data viz
// Geometric scale: round(14 × 1.2^step) / 16, default base=14, ratio=1.2
'--text-xsm': '0.625rem', // step -2: 10px (14 × 1.2⁻² ≈ 9.72 → 10)

@cixzhang cixzhang Mar 18, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the awkward part here is that --text-2xs is bigger than --text-xsm in the new geometric scale, but I guess we need to deprecate those. Probably nothing to action on here if we want to delete these.

@@ -0,0 +1,174 @@
# Type Scale Reference

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reference doc doesn't get shipped with the CLI so you might want to update the token docs in https://github.com/facebookexperimental/xds/blob/main/packages/cli/docs instead. This includes all our tokens though, so we may want to keep it dense for typescale specifically.

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good. We should update the token reference.

ernestt pushed a commit that referenced this pull request Mar 19, 2026
…LI docs

- Sub-scale tokens (--text-4xs, --text-3xs, --text-2xs) are now computed
  from base+ratio at steps -5, -4, -3 instead of being static values.
  The entire size scale (11 tokens) is now a single geometric progression.

- Default values change: 4xs 8→6px, 3xs 10→7px, 2xs 11→8px

- CLI token docs (packages/cli/docs/tokens.md) updated with:
  - New text size values showing step + geometric formula
  - Full semantic type scale tokens (heading-*, text-*-size/weight/leading)
  - Named leading tokens section clarified as separate from type scale

- TYPE_SCALE_REFERENCE.md updated to reflect the full 11-token scale

Addresses review feedback from #682.
@ernestt

ernestt commented Mar 19, 2026

Copy link
Copy Markdown
Contributor Author

Addressed both review comments in 04f9c03:

1. Sub-scale tokens now computed from base+ratio — --text-4xs, --text-3xs, --text-2xs are no longer static. They extend the geometric progression to steps -5, -4, -3. Default values shift: 4xs 8→6px, 3xs 10→7px, 2xs 11→8px. The entire 11-token size scale is now a single round(base × ratio^step) computation.

2. CLI docs updated with semantic type scale tokens — packages/cli/docs/tokens.md now includes the heading tokens (--heading-{1-6}-size/weight/leading) and text type tokens (--text-{body,large,label,code,supporting}-size/weight/leading), plus the text size table reflects the geometric scale with step numbers. Kept dense per feedback.

All 1618 tests pass.

ernestt and others added 4 commits March 19, 2026 17:38
…s var() refs

Two-layer architecture:
- Layer 1: Raw size tokens (--text-xsm…--text-4xl) from geometric progression, in rem
- Layer 2: Semantic tokens — sizes as var() refs to Layer 1,
  line heights as hardcoded computed values (tiered target: <20px→1.5,
  20-31px→1.4, ≥32px→1.25, then 4px grid snap)

Named leading tokens (--leading-tight…--leading-relaxed) are NOT modified —
they remain as intent-based ratios for component use.

This ensures all 47 components using textSizeVars cascade correctly when
typeScale is configured. Previously only semantic tokens were computed,
leaving raw tokens static and out of sync.

Co-authored-by: Navi <[email protected]>
- Type scale preview now resolves var() refs and shows px values
  for h1-h6 and large/body/supporting text types
- Added Landing Page and Dashboard preview tabs alongside
  the existing component preview
- Landing: hero, features grid, testimonial, CTA
- Dashboard: KPI cards, data table, activity feed, progress bars

Co-authored-by: Navi <[email protected]>
…b children)

The XDSTabList component takes value/onChange props with XDSTab children,
not a tabs array prop. All 3 usages were silently broken (rendering nothing).

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

- Sub-scale tokens (--text-4xs, --text-3xs, --text-2xs) are now computed
  from base+ratio at steps -5, -4, -3 instead of being static values.
  The entire size scale (11 tokens) is now a single geometric progression.

- Default values change: 4xs 8→6px, 3xs 10→7px, 2xs 11→8px

- CLI token docs (packages/cli/docs/tokens.md) updated with:
  - New text size values showing step + geometric formula
  - Full semantic type scale tokens (heading-*, text-*-size/weight/leading)
  - Named leading tokens section clarified as separate from type scale

- TYPE_SCALE_REFERENCE.md updated to reflect the full 11-token scale

Addresses review feedback from #682.
@ernestt
ernestt force-pushed the navi/feat/type-scale-raw-tokens-v2 branch from 04f9c03 to b81f2bf Compare March 19, 2026 17:42
@ernestt
ernestt requested a review from cixzhang March 19, 2026 18:00
@cixzhang

Copy link
Copy Markdown
Contributor

Let's resolve the conflicts and clean up TYPE_SCALE_REFERENCE.md‎

cixzhang and others added 2 commits March 19, 2026 21:36
Trims the reference doc to architecture + step mapping + line height formula.
Full token docs live in packages/cli/docs/tokens.md (already updated).

Co-authored-by: Navi <[email protected]>
@cixzhang
cixzhang merged commit 3332f8d into main Mar 20, 2026
11 checks passed
@cixzhang
cixzhang deleted the navi/feat/type-scale-raw-tokens-v2 branch March 20, 2026 00:50
cixzhang added a commit that referenced this pull request Apr 26, 2026
… var() refs (#682)

* feat(type-scale): drive raw tokens from base+ratio, semantic tokens as var() refs

Two-layer architecture:
- Layer 1: Raw size tokens (--text-xsm…--text-4xl) from geometric progression, in rem
- Layer 2: Semantic tokens — sizes as var() refs to Layer 1,
  line heights as hardcoded computed values (tiered target: <20px→1.5,
  20-31px→1.4, ≥32px→1.25, then 4px grid snap)

Named leading tokens (--leading-tight…--leading-relaxed) are NOT modified —
they remain as intent-based ratios for component use.

This ensures all 47 components using textSizeVars cascade correctly when
typeScale is configured. Previously only semantic tokens were computed,
leaving raw tokens static and out of sync.

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

* feat(theme-editor): resolve type scale px values, add 3 preview tabs

- Type scale preview now resolves var() refs and shows px values
  for h1-h6 and large/body/supporting text types
- Added Landing Page and Dashboard preview tabs alongside
  the existing component preview
- Landing: hero, features grid, testimonial, CTA
- Dashboard: KPI cards, data table, activity feed, progress bars

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

* fix(theme-editor): use correct XDSTabList API (value/onChange + XDSTab children)

The XDSTabList component takes value/onChange props with XDSTab children,
not a tabs array prop. All 3 usages were silently broken (rendering nothing).

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

* fix(type-scale): extend geometric scale to sub-scale tokens, update CLI docs

- Sub-scale tokens (--text-4xs, --text-3xs, --text-2xs) are now computed
  from base+ratio at steps -5, -4, -3 instead of being static values.
  The entire size scale (11 tokens) is now a single geometric progression.

- Default values change: 4xs 8→6px, 3xs 10→7px, 2xs 11→8px

- CLI token docs (packages/cli/docs/tokens.md) updated with:
  - New text size values showing step + geometric formula
  - Full semantic type scale tokens (heading-*, text-*-size/weight/leading)
  - Named leading tokens section clarified as separate from type scale

- TYPE_SCALE_REFERENCE.md updated to reflect the full 11-token scale

Addresses review feedback from #682.

* chore: clean up TYPE_SCALE_REFERENCE.md — slim to internal reference

Trims the reference doc to architecture + step mapping + line height formula.
Full token docs live in packages/cli/docs/tokens.md (already updated).

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

* chore: remove TYPE_SCALE_REFERENCE.md

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

---------

Co-authored-by: Navi <[email protected]>
Co-authored-by: cixzhang <[email protected]>
cixzhang added a commit that referenced this pull request Jun 21, 2026
… var() refs (#682)

* feat(type-scale): drive raw tokens from base+ratio, semantic tokens as var() refs

Two-layer architecture:
- Layer 1: Raw size tokens (--text-xsm…--text-4xl) from geometric progression, in rem
- Layer 2: Semantic tokens — sizes as var() refs to Layer 1,
  line heights as hardcoded computed values (tiered target: <20px→1.5,
  20-31px→1.4, ≥32px→1.25, then 4px grid snap)

Named leading tokens (--leading-tight…--leading-relaxed) are NOT modified —
they remain as intent-based ratios for component use.

This ensures all 47 components using textSizeVars cascade correctly when
typeScale is configured. Previously only semantic tokens were computed,
leaving raw tokens static and out of sync.

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

* feat(theme-editor): resolve type scale px values, add 3 preview tabs

- Type scale preview now resolves var() refs and shows px values
  for h1-h6 and large/body/supporting text types
- Added Landing Page and Dashboard preview tabs alongside
  the existing component preview
- Landing: hero, features grid, testimonial, CTA
- Dashboard: KPI cards, data table, activity feed, progress bars

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

* fix(theme-editor): use correct XDSTabList API (value/onChange + XDSTab children)

The XDSTabList component takes value/onChange props with XDSTab children,
not a tabs array prop. All 3 usages were silently broken (rendering nothing).

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

* fix(type-scale): extend geometric scale to sub-scale tokens, update CLI docs

- Sub-scale tokens (--text-4xs, --text-3xs, --text-2xs) are now computed
  from base+ratio at steps -5, -4, -3 instead of being static values.
  The entire size scale (11 tokens) is now a single geometric progression.

- Default values change: 4xs 8→6px, 3xs 10→7px, 2xs 11→8px

- CLI token docs (packages/cli/docs/tokens.md) updated with:
  - New text size values showing step + geometric formula
  - Full semantic type scale tokens (heading-*, text-*-size/weight/leading)
  - Named leading tokens section clarified as separate from type scale

- TYPE_SCALE_REFERENCE.md updated to reflect the full 11-token scale

Addresses review feedback from #682.

* chore: clean up TYPE_SCALE_REFERENCE.md — slim to internal reference

Trims the reference doc to architecture + step mapping + line height formula.
Full token docs live in packages/cli/docs/tokens.md (already updated).

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

* chore: remove TYPE_SCALE_REFERENCE.md

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

---------

Co-authored-by: Navi <[email protected]>
Co-authored-by: cixzhang <[email protected]>
cixzhang added a commit that referenced this pull request Jun 21, 2026
… var() refs (#682)

* feat(type-scale): drive raw tokens from base+ratio, semantic tokens as var() refs

Two-layer architecture:
- Layer 1: Raw size tokens (--text-xsm…--text-4xl) from geometric progression, in rem
- Layer 2: Semantic tokens — sizes as var() refs to Layer 1,
  line heights as hardcoded computed values (tiered target: <20px→1.5,
  20-31px→1.4, ≥32px→1.25, then 4px grid snap)

Named leading tokens (--leading-tight…--leading-relaxed) are NOT modified —
they remain as intent-based ratios for component use.

This ensures all 47 components using textSizeVars cascade correctly when
typeScale is configured. Previously only semantic tokens were computed,
leaving raw tokens static and out of sync.


* feat(theme-editor): resolve type scale px values, add 3 preview tabs

- Type scale preview now resolves var() refs and shows px values
  for h1-h6 and large/body/supporting text types
- Added Landing Page and Dashboard preview tabs alongside
  the existing component preview
- Landing: hero, features grid, testimonial, CTA
- Dashboard: KPI cards, data table, activity feed, progress bars


* fix(theme-editor): use correct XDSTabList API (value/onChange + XDSTab children)

The XDSTabList component takes value/onChange props with XDSTab children,
not a tabs array prop. All 3 usages were silently broken (rendering nothing).


* fix(type-scale): extend geometric scale to sub-scale tokens, update CLI docs

- Sub-scale tokens (--text-4xs, --text-3xs, --text-2xs) are now computed
  from base+ratio at steps -5, -4, -3 instead of being static values.
  The entire size scale (11 tokens) is now a single geometric progression.

- Default values change: 4xs 8→6px, 3xs 10→7px, 2xs 11→8px

- CLI token docs (packages/cli/docs/tokens.md) updated with:
  - New text size values showing step + geometric formula
  - Full semantic type scale tokens (heading-*, text-*-size/weight/leading)
  - Named leading tokens section clarified as separate from type scale

- TYPE_SCALE_REFERENCE.md updated to reflect the full 11-token scale

Addresses review feedback from #682.

* chore: clean up TYPE_SCALE_REFERENCE.md — slim to internal reference

Trims the reference doc to architecture + step mapping + line height formula.
Full token docs live in packages/cli/docs/tokens.md (already updated).


* chore: remove TYPE_SCALE_REFERENCE.md


---------

Co-authored-by: cixzhang <[email protected]>
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.

2 participants