Button
Actions
Open in Storybook: Button (opens in a new tab)
Overview
The button for an action on the current page: submit a form, open a dialog, toggle a setting, play media, delete an item. It is always a native button, which gives Enter and Space, focus, a role and a disabled state for free. If the control takes the user somewhere else it is a link, even when the design makes it look like a button.
Primary is for the one action a view exists for, Secondary for the rest and Tertiary for the quietest. Verb first, sentence case: “Erase account”. Never two Primary buttons in one view.
Anatomy
- Button
button type="button"The one element. It holds the role, the name, the state and the focus. A hard shadow of 4px, an edge of 2px and a radius of 8.
- Icon
svg aria-hidden="true"Decorative, 24px, in the colour of the label. A check replaces it when a toggle is on, a chevron turns in a menu button, a spinner is drawn while it loads.
- Label
text
The visible words are the accessible name: verb and object. An icon-only button has an
aria-labelinstead.
Usage
Use cases
The nine of the design, each the real button, and each works.
Variants
Primary for the one action a view exists for, Secondary for the rest, Tertiary for the quietest. Hover them, tab to them, press Enter or Space.
button type="button",variantLoading and feedback
Press Save changes. While it works the button keeps its label and its focus, and the end is said once.
Nothing is saved yet.
aria-disabled="true",svg aria-hidden="true",ariaNotify()Toggle button
The label never changes, the state does: a heart while it is off, a check while it is on.
button aria-pressedSay what happens
The visible label is the accessible name: verb and object, so each button makes sense on its own.
Don’t
Generic labels. Out of context, in a list of the buttons of the page, nobody knows what they do.
Do
Verb and object. Each button makes sense on its own.
The visible text is the name
Icon only
An icon with no words is named by an
aria-label, and the same words are a tooltip on hover and on focus.Don’t
An icon with no name. A screen reader says “button”, and a trash can means delete, discard or clear.
Do
aria-label="Delete draft"on the button and the icon hidden. The same words are a tooltip on hover and on focus.button aria-label,svg aria-hidden="true",role="tooltip"Label in name
An
aria-labelthat replaces the visible words breaks voice control: “Click Send” does nothing.Don’t
aria-label="Submit the contact form"The aria-label replaces the visible words. “Click Send” in voice control does nothing.
Do
No aria-label, or aria-label="Send message"Let the visible words be the name. If you add context, start with them.
No
aria-label, or one that starts with the visible wordsRoom for the focus ring
The ring is 3px outside the button, so a parent that cuts what is outside it cuts the ring.
Don’t
outline: none, or a parent with overflow: hidden that cuts the ring. A keyboard user loses their place.
Do
The ring of the site and room around the button for it, and its shadow.
:focus-visible, nooverflow: hiddenaround
Props
| Prop | Does | Default |
|---|---|---|
children | The label, and the accessible name. Verb and object. | required |
variant | primary, secondary or tertiary. | primary |
loading | Work is running: aria-disabled, the spinner is drawn, the label and the focus stay, and the click does nothing. | false |
type | A button left to the browser submits its form. This one says button unless told otherwise. | button |
disabled, aria-disabled, onClick, ref, any native attribute | As on a native button. A click on an aria-disabled button is stopped before onClick, so it submits nothing. | — |
IconButton: icon, label | An icon with no words. label is the aria-label; the icon is hidden. Show the same words as a tooltip. | both required |
ToggleButton: icon, pressed, defaultPressed, onPressedChange | aria-pressed is the state and the label never changes. icon is shown while off, a check while on. | defaultPressed false |
MenuButton: expanded, controls | aria-expanded and aria-controls. The chevron turns with expanded. The menu is yours. | both required |
Example
loading is the whole of the loading state: do not set disabled while it works, or focus falls to body. Say the end with ariaNotify() after the page has changed.
<Button variant="primary" loading={saving} onClick={save}>
Save changes
</Button>
<IconButton icon={Trash2} label="Delete draft" />
<ToggleButton icon={Heart}>Favourite</ToggleButton>
<MenuButton expanded={open} controls={menuId} onClick={() => setOpen(!open)}>
Options
</MenuButton>
<Button aria-disabled={!accepted} aria-describedby={reasonId}>
Continue
</Button>
<p id={reasonId}>Accept the terms to continue.</p>Specs
Measurements
| Part | Property | Value |
|---|---|---|
| Target | min-block-size, min-inline-size | 2.75rem (44px). It grows with the text |
| Button | padding | 0.75rem 1.25rem (12px 20px) |
| Icon button | padding | 0.625rem (10px): 24px of icon and the edge make 48 × 48 |
| Edge | border | bw (2px), transparent where the design draws none, so the button never changes size |
| Corner | border-radius | radius (8px) |
| Shadow | box-shadow | shadow (4px 4px 0 ink) |
| Gap | gap | 0.5rem (8px) |
| Icon | size | 24px, in the colour of the label |
| Label | font | 1rem (16px), 700 |
| Focus ring | outline | focus-ring-width (4px), 3px outside, focus-ring-primary. The site’s own, not declared again |
| Press | translate | 4px 4px |
Variants
| Variant | Fill | Words | Edge | Shadow |
|---|---|---|---|---|
| Primary | accent | accent-foreground | none | shadow |
| Secondary | white | ink | ink | shadow |
| Tertiary | none | lilac | none | none |
On (a toggle with aria-pressed="true") | accent | accent-foreground | none | shadow |
States
| State | Primary | Secondary | Tertiary |
|---|---|---|---|
| Default | accent fill, white words, shadow | white fill, ink words and edge, shadow | no fill, lilac words, no shadow |
| Hover | lilac-deep fill, shadow stays | wash fill, shadow stays | wash fill, lilac-deep words |
| Focus | the ring, and no shadow | the ring, and no shadow | the ring |
| Pressed | lilac-deep fill, no shadow, moves 4px | wash fill, no shadow, moves 4px | line fill, lilac-deep words |
| Disabled | fill, ink-soft words, dashed ink-soft edge, no shadow | as Primary | as Primary, with no fill |
| Loading | its Default look and a spinner before the label | its Default look and a spinner | its Default look and a spinner |
Hover darkens the fill and is never the only cue for anything: touch and keyboard users do not see it. Pressed is feedback, not meaning. A button that is aria-disabled, loading included, answers to neither.
Kinds
| Kind | Element | Its state is | Icon |
|---|---|---|---|
| Button | button | loading, disabled or aria-disabled | none |
| Icon button | button aria-label | as Button | 24px, hidden from assistive technology |
| Toggle button | button aria-pressed | aria-pressed | a heart while off, a check while on |
| Menu button | button aria-expanded aria-controls | aria-expanded | a chevron down, up while open |
disabled and aria-disabled
| Attribute | Tab order | Click | Use it for |
|---|---|---|---|
disabled | out: Tab skips it, so a user may never learn why it is there | ignored by the browser | a control that is irrelevant right now |
aria-disabled="true" | in, and announced as dimmed | stopped by the component: no handler, no submit | a submit button or a step, with the reason in aria-describedby |
Better still: keep the button enabled, validate on submit and move focus to the first error. The disabled look is exempt from contrast rules, and ours stays readable (ink-soft on fill, 7.8 to 1) and adds a dashed edge so it is not shown by colour alone.
Markup
<button type="submit" class="bt bt--primary">Save changes</button>
<button type="button" class="bt bt--secondary bt--icon" aria-label="Delete draft">
<svg aria-hidden="true" focusable="false">…</svg>
</button>
<button type="button" class="bt bt--secondary" aria-pressed="false">
<svg aria-hidden="true" focusable="false">…</svg>Favourite
</button>
<button type="button" class="bt bt--secondary" aria-expanded="false" aria-controls="options-menu">
Options<svg aria-hidden="true" focusable="false">…</svg>
</button>
<button type="submit" class="bt bt--primary" aria-disabled="true" aria-describedby="terms-hint">Continue</button>
<p id="terms-hint">Accept the terms to continue.</p>Styling
.bt { min-block-size: 2.75rem; min-inline-size: 2.75rem; }
.bt:focus-visible { box-shadow: none; } /* the ring is the site's */
.bt:active:not(:disabled, [aria-disabled='true']) {
box-shadow: none;
translate: 4px 4px;
}
@media (prefers-reduced-motion: reduce) {
.bt:active:not(:disabled, [aria-disabled='true']) { translate: none; }
.bt__spinner { animation: none; }
}Tokens
Component tokens
| Part | Property | Token | Value |
|---|---|---|---|
| Primary | background | accent | #5e548e |
| Primary | color | accent-foreground | #ffffff |
| Primary, hover and pressed | background | lilac-deep | #3d3459 |
| Secondary | background | white | #ffffff |
| Secondary | color, border-color | ink | #16161a |
| Secondary, hover and pressed | background | wash | #edebf4 |
| Tertiary | color | lilac | #5e548e |
| Tertiary, hover | background, color | wash, lilac-deep | #edebf4, #3d3459 |
| Tertiary, pressed | background | line | #d7d7dc |
| Disabled | background | fill | #f3f3f5 |
| Disabled | color, border-color | ink-soft | #4b4b55 |
| Shadow | box-shadow | shadow | 4px 4px 0 #16161a |
| Corner | border-radius | radius | 8px |
| Edge | border-width | bw | 2px |
| Focus ring | outline | focus-ring-primary, focus-ring-width | #0066ff, 4px |
Guidelines
Button or link
| Element | Use it when | Examples |
|---|---|---|
button | it does something on the current page | submit or save a form · open or close a dialog, menu or drawer · toggle a setting (aria-pressed) · play, pause, mute · add to cart, delete, copy · show more content on the same page · export a file that has no URL yet |
a href | it goes somewhere | another page or route · a section of the same page (#id) · a file that already has a URL · an external site · open in a new tab |
Style follows the design; the element follows the behaviour. A “Shop now” that navigates is an a href styled as a button. A “Load more” that stays on the page is a button, even styled as a text link (then do not underline it).
Which variant
| Variant | Use it for |
|---|---|
| Primary | the one action a view exists for. Never two in one view |
| Secondary | the rest |
| Tertiary | the quietest: an action that sits among others or in a dense row |
| Danger | the site’s destructive button, for an action that cannot be undone, has no design on the Figma page and is not built here |
Content
| Part | Format | Example |
|---|---|---|
| Label | verb first, then the object, sentence case | Save changes · Send message |
| Icon only | an aria-label with the action and the object, and the same words as a tooltip for sighted users | Delete draft |
| Toggle | the label never changes: “Unfavourite” would be read twice and contradict itself | Favourite |
| Reason | a sentence next to an aria-disabled button, in its aria-describedby | Accept the terms to continue. |
| The end | words on the page, said once | Changes saved |
Behavior
| Event | Result |
|---|---|
| Activate | runs onClick. Focus stays on the button |
Activate while loading or aria-disabled | nothing: no handler and no submit |
| Loading starts | aria-disabled, the spinner, the label and the focus stay. disabled would drop focus to body (2.4.3) |
| Loading ends | the button is Default again, the end is words on the page and is said once with ariaNotify() |
| Toggle | aria-pressed flips, the label stays, the icon is a heart or a check |
| Menu button | aria-expanded flips. Esc closes the menu and focus returns to the button |
| Pressed | the shadow drops and the button moves 4px. Not under prefers-reduced-motion |
Fake buttons
| Markup | Why not |
|---|---|
div onclick | No role, no focus, no Enter or Space. Rebuilding that is more code and easier to break (4.1.2, 2.1.1) |
a href="#" onclick | Announced as a link, Space scrolls the page, the URL gets a #. If it does something, it is a button |
button inside a | Nested interactive elements: one is unreachable or both are announced at once. Keep them siblings |
A button with no type in a form | It defaults to submit: a “Show password” button submits the form. The component says type="button" |
Accessibility
Keyboard
| Key | Action | Native |
|---|---|---|
| Tab, Shift+Tab | moves to and from the button in DOM order. disabled is skipped, aria-disabled is not | yes |
| Enter | activates | yes |
| Space | activates, on release. A link does not respond to Space: one more reason not to fake buttons | yes |
| Esc | in a menu or dialog opened by the button, closes it and returns focus to the button | no, script |
Roles and properties
| Property | Exposed | Note |
|---|---|---|
| role | button | implicit; a toggle is announced as a toggle button |
| name | the visible label, or the aria-label of an icon button | starts with the visible words (2.5.3) |
aria-pressed | true or false | a toggle only |
aria-expanded, aria-controls | the state and the menu it opens | a menu button only |
aria-disabled | true | a loading button, or one with a reason. Still focusable |
disabled | dimmed, out of the tab order | for a control that is irrelevant |
aria-describedby | the reason | on an aria-disabled button |
type | button | the component’s default |
Screen reader
The words of the design. Listen in NVDA, JAWS and VoiceOver before relying on them: none was run.
| Moment | What is read |
|---|---|
| A toggle that is on | “Favourite, toggle button, pressed” |
| A menu button that is closed | “Options, button, collapsed” |
An aria-disabled button | dimmed or unavailable, then the reason |
| An icon button | “Delete draft, button” |
Announcements
The Figma page says <p role="status">. A live region is not used here: the words are on the page and ariaNotify() says them once, as in the Progress and the Video player.
| Event | Said by | Announced | Priority |
|---|---|---|---|
| Work started | nothing: aria-disabled and the spinner | — | — |
| Work done | ariaNotify() on the button, after the page has changed | “Changes saved” | normal |
| Toggled | the browser, from aria-pressed | the state | — |
| Menu opened or closed | the browser, from aria-expanded | the state | — |
Measured behavior
Chromium 148.0.7778.96, from the tests.
| Observation | Consequence |
|---|---|
| Space fires the click on release, not when it goes down | the pressed look is :active, which is true while it is held |
| Hovered and focused at once, the button has no shadow | :focus-visible comes after :hover and :active |
disabled is skipped by Tab and aria-disabled is focused | a button with a reason must be aria-disabled |
A native aria-disabled submit button still submits on click and Enter | the component stops the click itself |
| A line of text in the system font makes the button 46px high, not the 47 that the Figma draws in Work Sans | the target is min-block-size, never a fixed height |
Support
| Browser | Checked |
|---|---|
| Chromium 148 | yes, in the tests and the stories |
| WebKit, Firefox | not run |
WCAG criteria
| Criterion | Level | Requirement here |
|---|---|---|
| 1.4.1 Use of Color | A | disabled is a dashed edge and muted words, and a toggle that is on is a fill and a check |
| 1.4.3 Contrast (Minimum) | AA | the label is 6.7 to 1 or more in every state. Disabled is exempt and is 7.8 to 1 |
| 1.4.4 Resize Text | AA | sizes are in rem: the button grows with the text |
| 1.4.11 Non-text Contrast | AA | the ring is 4.8 to 1 against the page |
| 1.4.13 Content on Hover or Focus | AA | the tooltip of an icon button shows on hover and on focus, and Esc dismisses it |
| 2.1.1 Keyboard | A | a native button: Enter and Space |
| 2.4.3 Focus Order | A | loading never uses disabled, so focus never falls to body |
| 2.4.7 Focus Visible | AA | the ring is shown by :focus-visible |
| 2.4.11 Focus Not Obscured (Minimum) | AA | room around the button for the ring and its shadow: no overflow: hidden cuts it |
| 2.5.3 Label in Name | A | the visible text is the name, and any aria-label starts with it |
| 2.5.8 Target Size (Minimum) | AA | 24 × 24 or more, with 8px between neighbours |
| 2.5.5 Target Size (Enhanced) | AAA | 44 × 44 or more: our default |
| 4.1.2 Name, Role, Value | A | a native button with its state in aria-pressed, aria-expanded or aria-disabled |
| 4.1.3 Status Messages | AA | the end of a task is said once with ariaNotify() |
Related
This section is still in progress.