Sub topic 2
Naming Tokens So They Stay Maintainable
This chapter assumes you know CSS selectors, the cascade, specificity, inheritance, and basic HTML. A design token is a named value for a shared design decision, such as a color or spacing step. Names let components depend on meaning instead of repeated raw values.
Naming tokens consistently
Use lowercase names with hyphens. Group names by category, then describe their role:
--color-blue-500identifies a raw color value.--color-textidentifies the color used for text.--button-padding-inlineidentifies a component-specific value.
Avoid names tied to temporary appearances, such as --blue-text or --large-gap. A theme or redesign can make those names inaccurate. Prefer names that describe purpose.
:root {
--color-blue-500: #2563eb;
--color-gray-900: #111827;
--space-2: 0.5rem;
--space-4: 1rem;
--color-accent: var(--color-blue-500);
--color-text: var(--color-gray-900);
}
.button {
--button-padding-inline: var(--space-4);
background: var(--color-accent);
color: white;
padding-inline: var(--button-padding-inline);
}Primitive, semantic, and component tokens
Tokens commonly have three tiers:
- Primitive tokens store raw values, such as a color or spacing unit.
- Semantic tokens assign meaning to those values, such as text, surface, or accent.
- Component tokens customize one component while keeping its default connected to the shared system.
This separation limits change. If the accent color changes, update the primitive or its semantic mapping instead of searching through every component.
Theming by overriding tokens in a scope
Custom properties follow the cascade and inherit into descendants. A theme can therefore redefine semantic tokens on a container. Every component inside that container uses the new values without changing its own rules.
:root {
--color-surface: #ffffff;
--color-text: #111827;
--color-accent: #2563eb;
}
[data-theme="dark"] {
--color-surface: #0b1020;
--color-text: #e5e7eb;
--color-accent: #60a5fa;
}
.card {
background: var(--color-surface);
color: var(--color-text);
}A scoped override affects only descendants. Check the element’s computed styles when a token appears to have the wrong value.
Exercise
Why should a button usually use --color-accent instead of --color-blue-500?
Show answer
The semantic token expresses the button's role and can map to a different value when the theme or design changes.