Sub topic 9
Capture decisions as design tokens and components
This chapter assumes you have a low-fidelity wireframe, a spacing scale, a type scale, and a color palette. It also assumes you can read basic CSS. You will turn those decisions into design tokens and reusable components, so the mockup and the final code stay consistent.
What a design token is
A design token is a named value that stores one design decision. Instead of typing #0b5fff in forty places, you create a token called color-accent and use that name everywhere. When the decision changes, you edit one value.
Tokens usually cover four groups:
- Spacing, such as the steps of your spacing scale.
- Color, such as text, background, and accent.
- Type, such as font sizes for body text and headings.
- Other repeated values, such as corner radius or shadow.
In Figma, tokens appear as variables or styles. In CSS, they appear as custom properties. The names are the same idea in both places, and that shared naming is what makes handoff smooth.
Name tokens by role, not by value
A token name should say what the value is for. A name like blue-500 describes the value. A name like color-accent describes the job. If the brand color changes to green, color-accent is still correct, while blue would now be wrong.
| Weak name | Better name | Why |
|---|---|---|
| `blue` | `color-accent` | Survives a rebrand |
| `gap-24` | `space-3` | Does not repeat the pixel value |
| `big-text` | `font-size-h1` | Ties the size to a heading level |
Tokens in code
The CSS below stores the homepage decisions as custom properties. Elements read them with var().
:root {
--space-1: 0.5rem;
--space-2: 1rem;
--space-3: 1.5rem;
--space-4: 2.5rem;
--color-text: #1a1a1a;
--color-bg: #ffffff;
--color-accent: #0b5fff;
--font-size-body: 1rem;
--font-size-h2: 1.75rem;
--font-size-h1: 2.5rem;
}
body {
color: var(--color-text);
background: var(--color-bg);
font-size: var(--font-size-body);
}
h1 { font-size: var(--font-size-h1); }
h2 { font-size: var(--font-size-h2); }Changing --color-accent once updates every element that uses it.
From tokens to components
A component is a reusable piece of interface built from tokens. The homepage repeats several pieces: the primary button, the feature card, and the testimonial. Design each one once, then place copies of it.
In a design tool, a component has a main version and instances. When you edit the main version, every instance updates. Components can also have variants, such as a button in a default, hover, and disabled state.
A button component might use these tokens:
- Background:
color-accent - Text:
color-bg - Padding:
space-2vertical andspace-3horizontal - Font size:
font-size-body
.button {
display: inline-block;
padding: var(--space-2) var(--space-3);
background: var(--color-accent);
color: var(--color-bg);
font-size: var(--font-size-body);
text-decoration: none;
}The mockup button and this CSS now draw from the same names, so a developer can match them by reading.
Common mistakes
- Using a raw value in one place. A single hard-coded color breaks the link to the token, and it will not change with the rest.
- Making too many tokens. Four to six spacing steps are enough. Extra near-duplicates such as
space-2andspace-2bhide the scale. - Building components too early. Wait until a pattern appears at least twice before you turn it into a component.
Exercise
Your brand color changes from blue to green. Why is color-accent a better token name than blue, and what do you edit to update the button and the feature card?
Show answer
color-accent describes the role, so it stays accurate after the change, while blue would be misleading. You edit only the value of the color-accent token, and every component that uses it updates.