Using Design Tokens
This guide shows developers how to consume and extend the design–token system shipped with the Astro Performance Starter.
1. Quick recap
Section titled “1. Quick recap”Token sources live in tokens/:
| File | Purpose |
|---|---|
base.json | Atomic design tokens (color, spacing, radii, motion, etc.) |
semantic.json | Light/dark semantic aliases (background, border, etc.) |
dist/ | Build output – do not edit directly |
The build script (pnpm run build:tokens) generates:
tokens/dist/tailwind-tokens.json– imported bytailwind.config.ts.tokens/dist/tokens.css– CSS variables (light +.dark).
2. In templates/components
Section titled “2. In templates/components”Tailwind utilities
Section titled “Tailwind utilities”<div class="bg-background-default text-foreground-default p-4 rounded-lg shadow-md transition-base duration-fast"> …</div>bg-background-default– maps tosemantic.background.default.shadow-md– fromshadowscale.transition-base&duration-fast– motion tokens.
CSS/SCSS
Section titled “CSS/SCSS”.card { background-color: hsl(var(--semantic-background-default)); color: hsl(var(--semantic-foreground-default)); border-radius: var(--border-radius-lg);}- Access any token via CSS custom property – wrap HSL tokens with
hsl(). - Dark-mode handled automatically via
.darkvariables.
3. Adding / updating tokens
Section titled “3. Adding / updating tokens”- Edit
tokens/base.jsonortokens/semantic.json. - Run
pnpm run build:tokensto regenerate outputs. - Commit both source and dist files.
Tip: keep scales consistent (increments of
4pxfor spacing,8msfor durations, etc.).
4. Naming rules
Section titled “4. Naming rules”- Atomic tokens: lowercase camelCase (
borderRadius.lg). - Semantic tokens: cascade from purpose → state (
background.default).
5. Dark mode strategies
Section titled “5. Dark mode strategies”- System preference: enabled automatically via
prefers-color-scheme. - Manual toggle: add
.darkclass tohtml/body.
// Example: toggleimport { useEffect } from "preact/hooks";
useEffect(() => { document.documentElement.classList.toggle("dark");});6. Lint & validation
Section titled “6. Lint & validation”pnpm run validate:contrast– ensures WCAG-AA contrast for semantic pairs.- CI fails if new tokens break contrast budgets.
Need help? Check the Design System implementation guide or open an issue.