Skip to content

Exploration: Ratio-based type scale system - #555

Closed
ernestt wants to merge 10 commits into
mainfrom
navi/exploration/ratio-based-type-scale
Closed

ernestt wants to merge 10 commits into
mainfrom
navi/exploration/ratio-based-type-scale

Conversation

@ernestt

@ernestt ernestt commented Mar 9, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

XDS currently maintains two parallel type scale systems — default (dense, for internal tools) and editorial (larger, for content-heavy pages). This creates confusion:

  • "When do I use editorial vs regular?"
  • "Do I need to support both in my component?"
  • "What if I want something in between?"

Products have varying density needs, but today they must choose between two fixed scales or manually override individual tokens.

Proposal

Replace explicit font size tokens with a ratio-based type scale inspired by typescale.com. Instead of defining 10+ individual size tokens, a theme defines just two values:

typeScale: {
  base: 14,        // Base font size in px
  ratio: 1.125,    // Major Second scale
}

The system generates all sizes mathematically (base × ratio^n) and snaps line heights to a 4px grid for vertical rhythm.

Theme Presets

Preset Base Ratio Character
Functional 12px 1.125 (Major Second) VS Code, Photoshop, Figma — maximum information density
Default 14px 1.125 (Major Second) Balanced — matches current XDS base
Editorial 16px 1.25 (Major Third) Spacious, content-focused

Same semantic tokens (--text-md, --text-lg), different computed output. One system instead of two.

The spectrum — Functional → Default → Editorial — maps naturally to how products think about their UI density. Functional makes things. Editorial tells stories.

Trade-offs

✅ Advantages

  • Eliminates editorial vs default confusion — one scale, themed by product
  • Simpler theming — 2 values instead of 10+ individual tokens
  • Mathematical harmony — sizes feel inherently related
  • Density spectrum — products can land anywhere between functional and editorial
  • Responsive scaling — change base at breakpoints, everything adjusts

⚠️ Concerns

  • Fractional pixels — ratio^n rarely lands on whole pixels, requires rounding
  • Loss of fine control — can't specify "exactly 14px" without breaking the system
  • Ratio × grid tension — ratios are multiplicative, grids are additive; they don't naturally align
  • Semantic drift — --text-sm means different px values in different themes (intentional but requires mental model shift)

Decisions

Question Decision
Collision handling Enforce 1px minimum step between adjacent sizes
Token naming Keep semantic names (--text-xs, --text-sm, etc.) — consistent with current XDS
Build vs runtime Generate at build time — static CSS, no runtime calc() overhead

What's Included

Exploration Document

.context/explorations/ratio-based-type-scale.md — full analysis including:

  • Computed examples for all three presets
  • Line height snapping algorithm
  • 1px minimum step collision enforcement
  • Decisions and remaining open questions

Interactive Prototype

Sandbox page at /pages/type-scale-explorer/ with:

  • Adjustable base size (10–24px slider)
  • Scale ratio selector (Minor Second through Golden Ratio)
  • Line height grid selector (2px, 4px, 8px)
  • Functional / Default / Editorial preset buttons
  • Computed scale table with collision warnings
  • 1px minimum step enforcement between adjacent sizes
  • Live typography preview
  • Side-by-side preset comparison

Open Questions

  1. Escape hatch — How do products specify exact sizes when the ratio doesn't produce what they need?
  2. Migration path — How do existing consumers migrate from explicit tokens to ratio-based?

Screenshots

Run the sandbox app and navigate to Type Scale Explorer to see the interactive prototype.

Add exploration document and interactive prototype for a ratio-based
type scale that replaces explicit font size tokens with a base + ratio
configuration. This would unify the default and editorial type scales
into a single themeable system.

Includes:
- .context/explorations/ratio-based-type-scale.md — full analysis with
  trade-offs, computed examples, and open questions
- Sandbox page (Type Scale Explorer) — interactive prototype with:
  - Adjustable base size, ratio, and line height grid
  - Dense/Default/Editorial presets
  - Live preview with real typography
  - Side-by-side preset comparison
  - Collision detection warnings

Co-authored-by: navi <[email protected]>
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 9, 2026
@github-actions

github-actions Bot commented Mar 9, 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 9.3KB 14.9KB 2.3KB

Accessibility Audit

Status: No accessibility violations detected.


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

ernestt and others added 9 commits March 9, 2026 22:32
Updated presets:
- Dense: 12px base, 1.125 (Major Second)
- Default: 14px base, 1.125 (Major Second)
- Editorial: 16px base, 1.25 (Major Third)

This aligns with XDS's existing 14px base for internal tools while
providing editorial as a step up rather than a dramatic jump.

Co-authored-by: navi <[email protected]>
Functional better describes the category of UI this preset targets:
VS Code, Photoshop, Figma — workbench-style tools where information
density is a feature, not a compromise.

Spectrum: Functional → Default → Editorial

Co-authored-by: navi <[email protected]>
Decisions:
- Collision handling: enforce 1px minimum step between adjacent sizes
- Token naming: keep semantic names (--text-xs, --text-sm, etc.)
- Build vs runtime: generate at build time (static CSS)

Still open:
- Escape hatch for exact sizes
- Migration path from explicit tokens

Co-authored-by: navi <[email protected]>
Reworked the prototype to combine font size, line height, and weight
into semantic type roles (h1-h6, body-lg, body, body-sm, label,
supporting, caption). Each role maps to a scale exponent and weight.

Added three tabs:
- Type Scale: split table for headings vs body/supporting with
  live preview panel
- Landing Page: realistic product page showing the type scale
  in context (nav, hero, features grid)
- Preset Comparison: side-by-side across all three presets

Line height computation is now role-aware: headings get tighter
line heights (1.2-1.3), body text gets more relaxed (1.4-1.5).

Co-authored-by: navi <[email protected]>
Display types:
- display-1 (exp +7, weight 700) — hero, splash screens
- display-2 (exp +6, weight 700)
- display-3 (exp +5, weight 600)
- Line heights are tighter (1.1-1.15×) for display text

Raw Calculations tab:
- Shows the pure mathematical output (base × ratio^n) for
  exponents -3 through +8
- Visual bar chart of relative sizes
- Highlights the base (exponent 0)
- Side-by-side role mapping table showing which exponents map
  to which semantic roles, with 'nudged' badges where collision
  handling adjusted a value
- Line height algorithm explanation

Also improved the landing page to use display-2 for the hero title
and added an overline/eyebrow pattern.

Co-authored-by: navi <[email protected]>
Type scale:
- Shift baseline from h5 (exp 0) to h4 (exp 0) — h1 is now +3,
  h2 +2, h3 +1, h4 0 (base), h5 -1, h6 -2. Display types shift
  down accordingly (display-1 is +6, display-2 +5, display-3 +4).
  Body and supporting also anchor off h4: body-lg = 0, body = -1,
  body-sm = -2, caption = -3.
- Label the base slider 'Base Size (= H4)' to make the anchor clear
- Raw calculations tab now shows exponents -4 through +7

Dashboard tab:
- Topbar with page title and date range
- 4 KPI cards (revenue, users, conversion, AOV) using h4 for values,
  caption for labels and deltas
- Bar chart placeholder showing revenue over time with month labels
- Donut chart showing traffic source split with a large h1 percentage
- Acquisition channels table with 6 rows: channel, visitors, revenue,
  conversion, trend delta — numeric columns in monospace

Co-authored-by: navi <[email protected]>
Landing page:
- Nav links: body-sm → body
- Hero eyebrow: supporting → body-sm
- Hero fine print: caption → body-sm
- Feature card descriptions: body-sm → body

Dashboard:
- KPI metric values: h4 → display-3
- KPI labels: caption → body-sm
- KPI trend deltas: caption → body-sm
- Page subheading: supporting → body-sm
- Chart axis labels: caption → body-sm
- Donut chart label: caption → body-sm
- Table headers: caption → body-sm
- View all link: caption → body-sm
- Topbar date range: caption → body-sm

Rule: use body (base) when in doubt, body-sm as the floor for
supporting text, display-2/3 for dashboard metric values.

Co-authored-by: navi <[email protected]>
Corrected exponent assignments:
- body-lg: 0 → +1 (one step above base)
- body: -1 → 0 (= base size)
- body-sm: -2 → -1 (one step below base)
- label: -1 → 0 (same as body, medium weight)
- supporting: -2 → -1 (same size as body-sm)
- caption: -3 → -2 (two steps below base)

Now body text matches the base font size, which is the intuitive
expectation. H4 and body are both at exponent 0, meaning they share
the same font size but differ in weight (600 vs 400).

Co-authored-by: navi <[email protected]>
Sits between Default (1.125) and Editorial (1.25) — same base size
as Default but with more pronounced scale steps.

Co-authored-by: navi <[email protected]>
@ernestt ernestt closed this Mar 17, 2026
ernestt added a commit that referenced this pull request Mar 17, 2026
Math.ceil always rounded UP to the next 4px increment, producing
overly generous line-heights (e.g. body 14px got 24px line-height
instead of the intended 20px).

Matches the algorithm from the exploration in PR #555 which used
Math.round (round-to-nearest). Also grid-aligns the minimum
(fontSize + 4) so the 4px rhythm is maintained even when the
minimum kicks in.

Affected defaults (base=14, ratio=1.2):
  --text-body-leading:  1.7143 (24px) → 1.4286 (20px)
  --text-large-leading: 1.6471 (28px) → 1.4118 (24px)
  --text-code-leading:  1.7143 (24px) → 1.4286 (20px)

Co-authored-by: Navi <[email protected]>
ernestt added a commit that referenced this pull request Mar 17, 2026
Math.ceil always rounded UP to the next 4px increment, producing
overly generous line-heights (e.g. body 14px got 24px line-height
instead of the intended 20px).

Matches the algorithm from the exploration in PR #555 which used
Math.round (round-to-nearest). Also grid-aligns the minimum
(fontSize + 4) so the 4px rhythm is maintained even when the
minimum kicks in.

Affected defaults (base=14, ratio=1.2):
  --text-body-leading:  1.7143 (24px) → 1.4286 (20px)
  --text-large-leading: 1.6471 (28px) → 1.4118 (24px)
  --text-code-leading:  1.7143 (24px) → 1.4286 (20px)

Co-authored-by: Navi <[email protected]>
ernestt added a commit that referenced this pull request Mar 17, 2026
Math.ceil always rounded UP to the next 4px increment, producing
overly generous line-heights (e.g. body 14px got 24px line-height
instead of the intended 20px from the exploration in PR #555).

Matches the algorithm from the exploration which used Math.round
(round-to-nearest). Also grid-aligns the minimum (fontSize + 4)
via Math.ceil so the 4px rhythm is maintained even when the
minimum kicks in.

Affected defaults (base=14, ratio=1.2):
  --text-body-leading:  1.7143 (24px) → 1.4286 (20px)
  --text-large-leading: 1.6471 (28px) → 1.4118 (24px)
  --text-code-leading:  1.7143 (24px) → 1.4286 (20px)

Co-authored-by: Navi <[email protected]>
@cixzhang
cixzhang deleted the navi/exploration/ratio-based-type-scale branch April 9, 2026 14:30
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