Skip to content

Lesson 7 of 9 · 22m

Design Tokens and Theming Architecture

The three-tier token system (primitive/semantic/component), how to wire it in custom properties, and why 'just use the hex code' collapses at scale.

Every design system that survives contact with reality converges on the same architecture: a three-tier token system. Teams that skip it end up with #2563eb in 400 files, a dark mode that requires touching all of them, and a rebrand that takes a quarter instead of an afternoon.

The three tiers

Tier 1 — primitives. The raw palette, named for what they look like, never for what they're used for:

:root {
  --blue-600: #2563eb;
  --blue-400: #60a5fa;
  --gray-900: #111827;
  --gray-50: #f9fafb;
  --space-2: 0.5rem;
  --space-4: 1rem;
  --radius-md: 0.5rem;
}

Tier 2 — semantic. Named for intent, and this is the tier themes override:

:root {
  --color-surface: var(--gray-50);
  --color-text: var(--gray-900);
  --color-accent: var(--blue-600);
  --color-danger: #dc2626;
}
[data-theme="dark"] {
  --color-surface: var(--gray-900);
  --color-text: var(--gray-50);
  --color-accent: var(--blue-400);
}

Tier 3 — component. Optional, for components with real internal complexity:

.button {
  --button-bg: var(--color-accent);
  --button-fg: white;
  background: var(--button-bg);
  color: var(--button-fg);
}

The rule: components consume semantic or component tokens, never primitives. The moment .error-banner references --red-500 directly, dark mode and theming lose the ability to adapt it independently.

Why this works: one override point

Because custom properties resolve at computed-value time (lesson 3), flipping [data-theme="dark"] on <html> retargets every semantic token, and every component follows — zero additional selectors, zero duplication. Compare with the pre-token approach, where dark mode is hundreds of .dark .card { ... } overrides, each one a specificity fight.

// theme boot — must run before first paint or users see a flash
const theme = localStorage.getItem("theme")
  ?? (matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light");
document.documentElement.dataset.theme = theme;

Inline this in <head> before stylesheets load. A theme that flashes white-then-dark on every page load is worse than no dark mode.

Tokens as data, not just CSS

Mature systems keep tokens in a source-of-truth file (JSON or a format like the W3C Design Tokens spec) and generate CSS custom properties, TS constants, and sometimes native platform values from it. The reason: drift. If the marketing site uses --blue-600 from CSS and the docs use #2563eb copied into a config, the rebrand misses one and ships mismatched blues. Style Dictionary is the standard toolchain; even a 30-line script that regenerates tokens.css from tokens.json beats hand-copying.

Field notes

  • Don't tokenize everything. One-off values used in exactly one place are noise — tokens are for values that participate in a system.
  • Spacing scales (4px or 8px base) matter more than color scales for visual consistency; --space-3 everywhere beats magic numbers.
  • Test dark mode with real content, not the marketing page — data tables, code blocks, and form errors are where token gaps hide.

Was this lesson helpful?

Sign in to rate