Sub topic 8
Documenting Component States
This chapter assumes you can build components and variants in a design tool such as Figma. It also assumes you know basic CSS selectors and custom properties, and that you have read the chapter on design tokens. The goal is to document every state of a component so the developer does not have to invent the missing ones.
Why states are the most common handoff gap
A mockup usually shows a component in one condition: a button at rest, a form with tidy data, a list full of items. A real interface is rarely in that condition. A button gets hovered, focused with the keyboard, pressed, disabled, and put into a loading state. A form field can be empty, filled, or invalid. A list can have no items at all.
If the handoff shows only the resting state, the developer has three options. They can ask you, guess, or copy a default from a framework. Guessing produces inconsistencies that surface in QA, and each one costs a round trip. Documenting states up front is cheaper.
The states to cover
Most web components need a subset of the states below. The table also lists the CSS hook that usually implements each one, so you can check that your design maps to something buildable.
| State | What the user sees | Typical CSS or HTML hook |
|---|---|---|
| Default | The resting appearance | Base class |
| Hover | Pointer is over the element | `:hover` |
| Focus | Keyboard or assistive focus is on the element | `:focus-visible` |
| Active | The element is being pressed | `:active` |
| Disabled | The element cannot be used | `:disabled` or `aria-disabled` |
| Loading | An action is in progress | `aria-busy="true"` or a class |
| Error | Input or action failed | `aria-invalid="true"` |
| Empty | There is no content to show | A distinct empty-state block |
Not every component needs every row. A static card has no disabled state. A text input needs focus, error, and disabled, but loading only if it validates asynchronously. Write down which states apply and which do not. "No loading state" in the spec is information. Silence looks like an omission.
Build a state matrix
The most reliable format is a matrix: one row per component variant, one column per state. In Figma, this is a component set with variant properties such as State=Default|Hover|Focus|Active|Disabled|Loading. Lay the variants out in a grid and label each one.
Each variant should show only what changes. For a button, hover changes the background, and focus adds an outline. Link each changed value to a token, not a raw hex code. The spec then reads "background: color.brand.primary-hover" and not "#174aa6". When the brand color changes later, the spec stays correct.
The diagram below shows how a button moves between states. Use this kind of diagram when transitions are not obvious from a static grid.
From tokens to states
States should reuse the tokens you already defined. Hover and disabled need their own color tokens because they are values the design depends on, so add them to the token file. The earlier tokens produce the following CSS. The :root block is repeated here so the example runs on its own.
:root {
--color-brand-primary: #1f5fd1;
--color-brand-primary-hover: #174aa6;
--color-text-disabled: #8a8a8a;
--space-2: 8px;
--space-3: 16px;
--radius-md: 6px;
}
.button {
padding: var(--space-2) var(--space-3);
border: 0;
border-radius: var(--radius-md);
background: var(--color-brand-primary);
color: #fff;
cursor: pointer;
}
.button:hover {
background: var(--color-brand-primary-hover);
}
.button:focus-visible {
outline: 2px solid var(--color-brand-primary);
outline-offset: 2px;
}
.button:disabled {
background: transparent;
color: var(--color-text-disabled);
cursor: not-allowed;
}Each state maps to one selector. When you review the design against the code, you can tick them off one by one. If the design has a state with no selector, or a selector with no design, you have found a gap.
Details that cause bugs
Focus is not hover
Hover needs a pointer. Keyboard and screen reader users never trigger it, and touch screens have no true hover. If focus looks identical to default, keyboard users cannot see where they are. Design focus separately and specify the outline color, width, and offset. The spec above uses a 2px outline with a 2px offset so the ring does not touch the button edge.
:focus-visible shows the ring for keyboard focus but usually not for mouse clicks. Say in the spec that you want :focus-visible, so the developer does not use plain :focus and show rings on every click.
Active needs to feel different from hover
Active is the brief moment while the button is pressed. Make it distinguishable from hover with a darker color, a slight inset, or a small scale change. If you show nothing, the control feels unresponsive on slow devices. Specify the values, not just "pressed look."
Disabled has two meanings
A disabled control is not clickable, but there are two ways to build it. The HTML disabled attribute removes the element from the tab order. The aria-disabled="true" attribute keeps it focusable, which lets you show a tooltip explaining why the control is unavailable. Decide which one you want and note it. Also keep disabled text readable enough that users can still tell what the control is. Disabled elements are exempt from contrast requirements, but a label that cannot be read at all helps no one.
Loading must block repeat actions
A loading state should prevent a second click, or the user will submit twice. Specify what stays visible: the label, a spinner, or both. Specify whether the button keeps its width, so the layout does not jump when the label is replaced. The following example keeps the width by hiding the label with transparent text and drawing a spinner on top.
.button {
position: relative;
padding: 8px 16px;
border: 0;
border-radius: 6px;
background: #1f5fd1;
color: #fff;
}
.button[aria-busy="true"] {
color: transparent;
pointer-events: none;
}
.button[aria-busy="true"]::after {
content: "";
position: absolute;
inset: 0;
margin: auto;
width: 16px;
height: 16px;
border: 2px solid #fff;
border-top-color: transparent;
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
@media (prefers-reduced-motion: reduce) {
.button[aria-busy="true"]::after {
animation-duration: 2.4s;
}
}The reduced-motion rule slows the spinner for users who asked their system to limit animation. The spinner still communicates progress, but it is less jarring.
Error states need a message, not just a color
A red border alone fails users who cannot distinguish colors. Pair color with an icon or text, and write the actual message. Document where the message appears, which token colors it, and whether it replaces the helper text or sits beside it. Include real wording in the design, because a long message can wrap and change the layout.
<style>
.field input { border: 1px solid #8a8a8a; padding: 8px; border-radius: 6px; }
.field input[aria-invalid="true"] { border-color: #b3261e; }
.field-error { color: #b3261e; margin: 4px 0 0; font-size: 14px; }
</style>
<div class="field">
<label for="email">Email</label>
<input id="email" type="email" value="sam@" aria-invalid="true" aria-describedby="email-error">
<p id="email-error" class="field-error">Enter an email address like [email protected].</p>
</div>The aria-describedby link lets screen readers announce the message when the field receives focus. The visual design and the markup have to agree, so include this connection in your annotation.
Empty states are designed content
An empty list is a screen the user will see on first use, so it needs a design. Include an illustration or icon if there is one, a short heading, a sentence explaining why it is empty, and a clear next action such as "Create your first project." Also specify the empty state for search results, which differs from first-use emptiness: the advice there is to change the search, not to create something.
Combined states
States overlap in real use. The usual combinations are hover on a focused element, focus on an error field, and a loading button that is also hovered. You cannot draw every combination, so state the rules:
- Disabled wins over hover and active. A disabled control shows no hover change.
- Loading wins over hover. The background stays the same while the request runs.
- Focus is additive. The ring appears on top of whatever other state is active.
- Error and focus can coexist. Keep the error border color and add the focus ring.
Write these rules once in the component's documentation. They answer a large share of the follow-up questions you would otherwise get.
A checklist before you hand off
- Every interactive component has a variant for each applicable state.
- Each state value links to a token, and any new value was added to the token file.
- Focus is designed separately from hover, with outline width, color, and offset.
- Disabled behavior is specified: removed from tab order or still focusable.
- Loading specifies what is visible, what is blocked, and whether width is preserved.
- Error states include real message text and its placement.
- Empty states exist for first use, no results, and failed loads where relevant.
- Rules for combined states are written down.
- States that do not apply are marked as not applicable.
Exercise
A designer hands off a text input with variants for Default, Focus, and Error. The developer asks what happens when the field is Disabled, and whether the Error border should stay when the field is focused. List the two missing pieces of documentation and give a sensible answer to each.
Show answer
The missing pieces are a Disabled variant and a rule for combined states. The Disabled variant should show the background, text color, and border using tokens such as color.text.disabled, and it should say whether the input is removed from the tab order (the disabled attribute) or stays focusable (aria-disabled). For the combined case, a sensible rule is that focus is additive: the Error border color stays, and the focus ring is drawn on top with its own outline and offset. That keeps the error visible while the keyboard position is also clear.
Exercise
A "Save" button turns into a spinner while the request runs, and the button shrinks by 40px, which moves the neighboring "Cancel" button. What should the spec say to prevent this, and what extra protection does the loading state need?
Show answer
The spec should say the button keeps its width in the loading state. One way is to hide the label with transparent text and draw the spinner over it, so the text still sets the width. The loading state should also block repeat actions, for example with pointer-events: none plus aria-busy="true" and ignoring submits in code, so a second click cannot send a duplicate request. If the spinner animates, the spec should note a slower or static alternative for prefers-reduced-motion.