When to use it#
- Use a button when the user does something: sends, saves, deletes or opens.
- Write the label as a verb that says what will happen: “Send application”, not “OK”.
- Use one primary button per page, for the main next step.
- Before an action that can’t be undone, such as deleting, ask the user to confirm, for example in an AlertDialog.
- Not for going to another page: use a Link.
Example#
import { Button } from '@kvirn-ui/react'
import { useButtonTexts } from './texts.ts'
export function DefaultButton() {
const { texts } = useButtonTexts()
return <Button>{texts.saveDraft}</Button>
}Use cases#
One main action next to the others
A page has one main next step. Mark it primary, leave the rest secondary, and mark an action that deletes something as danger.
Delete buttons always need a confirmation step. They are shown together here only so you can compare them.
import { Button, ButtonGroup } from '@kvirn-ui/react'
import { useButtonTexts } from './texts.ts'
export function Variants() {
const { texts } = useButtonTexts()
return (
<>
<ButtonGroup>
<Button className="kv-button--primary">{texts.sendApplication}</Button>
<Button>{texts.saveDraft}</Button>
<Button className="kv-button--danger">{texts.deleteDraft}</Button>
</ButtonGroup>
<p>{texts.dangerNote}</p>
</>
)
}Seeing which button has the keyboard
Someone using only a keyboard needs to see which button is ready to press. In the default theme, a button with keyboard focus shows a ring around it, at least 2px wide and with 3:1 contrast against its background (2.4.7 Focus Visible). Every kind of button gets the same ring, and a disabled button that can still be focused keeps it. A natively disabled button is skipped by Tab, so it never gets one.
import { Button, Fieldset, Grid, Icon, Stack } from '@kvirn-ui/react'
import { useButtonTexts } from './texts.ts'
const kinds = [
{ kind: 'primary', className: 'kv-button--primary' },
{ kind: 'secondary', className: undefined },
{ kind: 'danger', className: 'kv-button--danger' },
] as const
// `data-focus-visible` is what the Button sets itself after a Tab. Passing it here shows the ring
// without moving focus. On your own page, press Tab to see the same ring on a real button.
export function FocusStates() {
const { texts } = useButtonTexts()
return (
<Stack className="kv-stack--gap-4">
{kinds.map(({ kind, className }) => (
<Fieldset.Root key={kind}>
<Fieldset.Legend>{texts.kinds[kind]}</Fieldset.Legend>
<Grid.Root className="kv-grid--fit-sm kv-grid--gap-4">
<div>
<p>{texts.states.rest}</p>
<Button className={className}>{texts.sendApplication}</Button>
</div>
<div>
<p>{texts.states.keyboardFocus}</p>
<Button className={className} data-focus-visible="">
{texts.sendApplication}
</Button>
</div>
<div>
<p>{texts.states.disabledFocusable}</p>
<Button className={className} disabled focusableWhenDisabled data-focus-visible="">
{texts.sendApplication}
</Button>
</div>
</Grid.Root>
</Fieldset.Root>
))}
<Fieldset.Root>
<Fieldset.Legend>{texts.kinds.iconOnly}</Fieldset.Legend>
<Grid.Root className="kv-grid--fit-sm kv-grid--gap-4">
<div>
<p>{texts.states.rest}</p>
<Button className="kv-button--icon-only" aria-label={texts.closeLabel}>
<Icon name="close" />
</Button>
</div>
<div>
<p>{texts.states.keyboardFocus}</p>
<Button
className="kv-button--icon-only"
aria-label={texts.closeLabel}
data-focus-visible=""
>
<Icon name="close" />
</Button>
</div>
</Grid.Root>
</Fieldset.Root>
</Stack>
)
}Props in this example:
disabledon ButtonfocusableWhenDisabledon Button
Warning: Don't forget
The ring is forced here with data-focus-visible, which the Button sets by itself after a Tab. Press Tab on a real button to see the same ring. A mouse click does not show it. If you restyle buttons, keep a visible ring (2.4.7).
Disabled with a reason
You can’t avoid disabling the button, for example until the form is complete. Keep it reachable with the keyboard and link the reason to it with aria-describedby, so people using a keyboard or a screen reader (software that reads the screen aloud) find it and learn why.
Fill in all required fields before you send.
import { Button } from '@kvirn-ui/react'
import { useId } from 'react'
import { useButtonTexts } from './texts.ts'
export function DisabledWithReason() {
const { texts } = useButtonTexts()
const reasonId = useId()
return (
<>
<p id={reasonId}>{texts.disabledReason}</p>
<Button
className="kv-button--primary"
disabled
focusableWhenDisabled
aria-describedby={reasonId}
>
{texts.sendApplication}
</Button>
</>
)
}Props in this example:
disabledon ButtonfocusableWhenDisabledon Button
Warning: Don't forget
Prefer to let people press the button and then explain what’s missing. If you do disable it, say why in text next to it, as in What you need to do.
Sending a form
A Button is type="button" unless you say otherwise. Give the one button that sends the form type="submit", and Enter in a text field sends it too.
import { Button } from '@kvirn-ui/react'
import { useId } from 'react'
import { useButtonTexts } from './texts.ts'
export function SubmitForm() {
const { texts } = useButtonTexts()
const nameId = useId()
return (
<form
onSubmit={(event) => {
// A real form sends the data from here.
event.preventDefault()
}}
>
<p>
<label htmlFor={nameId}>{texts.applicantName}</label>{' '}
<input id={nameId} name="name" autoComplete="name" />
</p>
<Button type="submit" className="kv-button--primary">
{texts.sendApplication}
</Button>
</form>
)
}Props in this example:
typeon Button
Information: Did you know?
A focusable disabled submit button also blocks Enter in a text field, so the form isn’t sent through it.
A save that takes a moment
Set busy while the save runs. The button shows a spinner, ignores every press and keeps keyboard focus, and its name stays the same. It is never natively disabled, which would drop focus to the page body. The button says nothing by itself, so put a Progress with the words beside it, and no Progress.Indicator.
import { Button, ButtonGroup, Progress } from '@kvirn-ui/react'
import { useEffect, useState } from 'react'
import { useButtonTexts } from './texts.ts'
export function Saving() {
const { texts } = useButtonTexts()
const [isSaving, setIsSaving] = useState(false)
useEffect(() => {
if (!isSaving) {
return
}
const timer = setTimeout(() => setIsSaving(false), 3000)
return () => clearTimeout(timer)
}, [isSaving])
return (
<ButtonGroup>
<Button busy={isSaving} onClick={() => setIsSaving(true)}>
{texts.saveDraft}
</Button>
{isSaving ? (
<Progress.Root label={texts.saving}>
<Progress.Label />
</Progress.Root>
) : null}
</ButtonGroup>
)
}Props in this example:
Warning: Don't forget
The spinner keeps moving for as long as the wait lasts. When a wait can run longer than 5 seconds beside other content, offer a visible control that stops it (2.2.2 Pause, Stop, Hide). See Moving indicators.
A button with only an icon
Use it where there is room for an icon and not a word, such as closing a panel. The name comes from an aria-label you take from your own translations.
import { Button, Icon } from '@kvirn-ui/react'
import { useButtonTexts } from './texts.ts'
export function IconOnly() {
const { texts } = useButtonTexts()
return (
<Button className="kv-button--icon-only" aria-label={texts.closeLabel}>
<Icon name="close" />
</Button>
)
}Warning: Don't forget
Without a name, a screen reader says only “button” (4.1.2 Name, Role, Value). Keep the icon decorative and put the name on the Button. While you build, a warning in the browser console tells you when an icon-only button has no name.
<Button className="kv-button--icon-only" aria-label={messages.close}>
<Icon name="close" />
</Button>Accessibility#
What it does for you#
- APG pattern: Button
- Deviations: none
- Native elements used:
<button>. Activation, the Tab stop and form submission are the browser's own.
A Button performs an action. Navigation is a Link. Button defaults to type="button", so it never submits a form by accident. A disabled button can stay focusable (focusableWhenDisabled), so keyboard and screen-reader users can still find it and read why it's disabled.
| Part | Element / role | ARIA | Notes |
|---|---|---|---|
| Button | <button> → button | none by default | type="button" unless type is given. Name from its content |
| disabled | native disabled | Not focusable, not activatable. data-disabled | |
| disabled and focusable | aria-disabled="true", no disabled attribute | Stays in the Tab order. Click, Enter, Space and implicit form submission are blocked: the onClick is not called, and the default (form submission) is prevented. data-disabled. aria-disabled can't be passed directly: it's set only through disabled with focusableWhenDisabled, or through busy | |
| busy | aria-disabled="true", no disabled attribute | The action is running (busy). Stays in the Tab order and keeps focus when busy turns on (2.4.3). Click, Enter, Space and implicit submission are blocked like a focusable disabled button. data-busy, no data-disabled. The name never changes (2.5.3). No aria-busy. disabled wins when both are set. While busy a decorative <span class="kv-spinner" aria-hidden="true"> is the first child (it shows at once, and it loops while busy, resting under reduced motion: see the 2.2.2 point under What you need to do). The Button announces nothing: pair it with a Progress beside it, without a Progress.Indicator | |
| keyboard focus | none | data-focus-visible while the button matches :focus-visible | |
| no accessible name | – | A dev warning when the button has no aria-label, aria-labelledby, title or <label>, and no content outside aria-hidden: an icon-only button with only a decorative Icon (4.1.2) |
useButton gives the same buttonProps for your own <button>. Pass your click handler as useButton({ onClick }), so it is blocked while disabled.
It announces nothing.
What you need to do#
- Give the button a visible text label, or an
aria-labelfrom your own translations for an icon-only button (4.1.2, 2.5.3). Keep the icon decorative:<Button className="kv-button--icon-only" aria-label={messages.close}><Icon name="close" /></Button>. - Use
type="submit"for the button that submits a form. - When a button is disabled, tell users why in text near it, and link that text with
aria-describedbyif the button is focusable. Prefer not disabling at all: let the user submit and show the errors (advice from the GOV.UK Design System; 3.3.1 Error Identification). - Pass your click handler to
Buttonor touseButton({ onClick }). Don't merge your ownonClickon top ofbuttonProps: it wouldn't be blocked while disabled. - Disable a button that may have focus with
focusableWhenDisabled. A natively disabled button loses focus, and focus drops tobody(2.4.3). This matters most for a button that disables itself when pressed. - Style
[data-disabled]and[data-focus-visible](or:focus-visible). Never remove the focus ring without a replacement. - 2.2.2 Pause, Stop, Hide (A). The indicators (spinner, bar sheen, busy Button, job Toast, FileUpload track, Table busy sweep) move for as long as the wait lasts, which can be more than 5 s. The theme honours
prefers-reduced-motion: reduceand shows a still shape, but that setting alone is not a control on the page. An indicator that can keep moving for more than 5 seconds alongside other content needs a visible control on the page that stops or hides it. One that is the only content during a wait where nothing else can be done is exempt. The library ships no such control, so the app offers its own and applies the CSS that stops the indicators. Moving indicators and WCAG 2.2.2. The Progress beside the button carries the wait.
Visual and modes#
- Focus indicator: headless. The browser's native ring by default. The default theme restyles it to at least 2px at 3:1 (2.4.7 Focus Visible, level AA; 2.4.13 Focus Appearance, level AAA). The ring's contrast against its surroundings is checked in every theme.
- Target size: headless. The default theme makes buttons at least 24 × 24 CSS px (2.5.8), in both densities, and a story asserts that threshold.
- forced-colors behaviour: a native
<button>, so the system'sButtonText/ButtonFaceapply. The disabled state isn't conveyed only by colour in the default theme: it has a dashed edge (1.4.1). The forced-colours sweep checks that every button keeps a visible edge and shows a focus indicator when it has focus (1.4.11). - Compact (
kv-button--compact): at least 24 × 24 CSS px at every width, so it meets 2.5.8 only (not the 44px of the AAA target size). It is shorter on wide screens (from 64rem) and taller on narrow ones, where a touch is likelier. A story asserts the 24px threshold. - Link look (
kv-button--link): at least 24px tall at every width (2.5.8), the normal focus ring (2.4.7), underlined on hover only. Disabled istext-mutedwith a dashed underline as the non-colour cue (1.4.1); on a filled top bar it is the bar's on-colour with the dashed underline. In forced colours it takes the system link colour, and the forced-colours sweep checks its edge and focus indicator like any other button. A story asserts the 24px threshold. - reduced-motion behaviour: the button itself has no motion. The spinner of a busy button rests under reduced motion, and the reduced-motion sweep checks that no animation keeps looping (2.3.3).
- Reflow and text spacing: the reflow sweep runs every story at 320 CSS px and fails on horizontal overflow (1.4.10 Reflow, AA). The text-spacing sweep applies the 1.4.12 spacing and fails when a box clips its text.
Warning: Don't forget
If you restyle buttons, keep every button at least 24 × 24 CSS pixels (2.5.8 Target Size (Minimum)), so people with a tremor or large fingers can hit it.
Focus management#
- Initial focus: not moved. Button never moves focus.
- Trap: no.
- Restore to: not applicable. After an activation attempt on a focusable disabled button, focus stays on it.
- Never obscured by: Button renders no overlay.
WCAG success criteria#
- 2.1.1 Keyboard (A): native
<button>, Enter and Space (Keyboard rows above). - 2.4.3 Focus Order (A), 2.4.7 Focus Visible (AA): native Tab order,
data-focus-visible. - 2.5.3 Label in Name (A): while busy, the name stays the visible text.
- 2.5.8 Target Size (Minimum) (AA): the default theme makes buttons at least 24 × 24 CSS px. Keep that if you restyle.
- 3.2.2 On Input (A): a Button never sends a form unless you give it
type="submit", so a stray button inside a form doesn't send it. - 4.1.2 Name, Role, Value (A): role
button, name from content,disabled/aria-disabledexposed (button.test.tsx).
Keyboard#
- Focus
- native
- Selection follows focus
- n/a
- Arrows wrap
- n/a
- Shortcuts
- none
A native <button>: one Tab stop, in DOM order, with no tabindex. Enter and Space are native.
| Key | Where | What happens |
|---|---|---|
| Tab | Button | Moves focus to the button Test: button.test.tsx › keyboard › Tab moves focus to the button |
| Shift+Tab | Button | Moves focus to the previous focusable element, and off the button Test: button.test.tsx › keyboard › Shift+Tab moves focus off the button |
| Tab | Disabled button | Skips the button Test: button.test.tsx › disabled › is natively disabled, marked with data-disabled and skipped by Tab |
| Tab | Focusable disabled button | Moves focus to the button, which is announced as unavailable Test: button.test.tsx › focusable when disabled › uses aria-disabled instead of disabled and stays in the Tab order |
| Enter | Button | Activates. Focus stays on the button Test: button.test.tsx › rendering › Enter and Space activate the button and keep focus on it |
| Enter | Focusable disabled button | Blocked. Focus stays on the button Test: button.test.tsx › focusable when disabled › click, Enter and Space call no handler, and focus stays |
| Space | Button | Activates on key up, not on key down. Focus stays on the button Test: button.test.tsx › keyboard › Space activates the button on key up |
| Space | Focusable disabled button | Blocked. Focus stays on the button Test: button.test.tsx › focusable when disabled › click, Enter and Space call no handler, and focus stays |
| Enter | Submit button in a form | Submits the form Test: button.test.tsx › keyboard › Enter on a submit button submits the form |
| Enter | Default button in a form | Activates the button without submitting the form (type="button")Test: button.test.tsx › keyboard › Enter on a default button does not submit the form |
| Tab | Busy button | Moves focus to the button, which is read as unavailable Test: button.test.tsx › busy › Tab lands on a busy button |
| Enter | Busy button | Blocked: no handler, no submit. Focus stays on the button Test: button.test.tsx › busy › click, Enter and Space call no handler, and focus stays |
| Space | Busy button | Blocked: no handler, no submit. Focus stays on the button Test: button.test.tsx › busy › click, Enter and Space call no handler, and focus stays |
| No key | busy turns on while focused | Focus stays on the button Test: button.test.tsx › busy › focus stays on the button when busy turns on and off |
| No key | Busy submit, implicit submission | Blocked, like a click Test: button.test.tsx › busy › a busy submit button submits nothing, not even by implicit submission |
| No key | Pointer | A click on a disabled button, focusable or not, calls no handler Test: button.test.tsx › disabled › …, button.test.tsx › focusable when disabled › … |
| No key | Form, implicit submission | Enter in a text field doesn't submit through a focusable disabled submit Test: button.test.tsx › focusable when disabled › blocks implicit submission from a text field |
| No key | Handler while disabled | A focusable disabled button blocks the onClick prop on click, Enter, Space and implicit submissionTest: button.test.tsx › handlers on a focusable disabled button › … |
Escape, arrow keys and Home / End are not handled.
API reference#
import { Button, useButton } from '@kvirn-ui/react'Button
Renders <button type="button"> with the role button. It takes every attribute of a <button> and passes ref to it. The default theme is described on Theming.
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | false | Blocks activation. Without focusableWhenDisabled the button is natively disabled and skipped by Tab. |
focusableWhenDisabled | boolean | false | With disabled, keeps the button in the Tab order with aria-disabled="true" so users can find it and read why. Activation stays blocked. |
busy | boolean | false | A running action: aria-disabled="true" and data-busy, never native disabled, so focus stays and every press is blocked. It shows a decorative spinner as its first child, and the name does not change. Put a Progress with the words beside it, without a Progress.Indicator: one indicator per wait. disabled wins when both are set. |
type | 'button' | 'submit' | 'reset' | 'button' | Use "submit" for the one button that sends the form. |
onClick | MouseEventHandler<HTMLButtonElement> | – | Called on click, Enter and Space, and never while the button is disabled or busy. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-button | always | The part class. The default theme styles it. |
kv-button--primary | class you add | The one main action on the page. Theme variant. |
kv-button--danger | class you add | An action that deletes something. Theme variant. |
kv-button--link | class you add | Reads as a link in a row of links. Only for an action that changes something in place, such as a toggle. Never for sending, going on a step or deleting. Theme option. |
kv-button--icon-only | class you add | A square button with only an icon. Give it an aria-label. Theme option. |
kv-button--compact | class you add | As tall as a compact input (32px) from 64rem, and 44px below it. For staff tools. Theme option. |
data-disabled | present or absent | The button is disabled, natively or focusable. |
data-busy | present or absent | Set while busy and not disabled. The theme shows the spinner at once. |
data-focus-visible | present or absent | The button has keyboard focus (:focus-visible). |
aria-disabled | "true" or absent | Set when disabled and focusableWhenDisabled are both on, and while busy. Not passed directly. |
useButton
Options
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | false | Mirrors HTML disabled. |
focusableWhenDisabled | boolean | false | Keeps a disabled button in the Tab order with aria-disabled="true". |
busy | boolean | false | The action is running. Renders aria-disabled="true", keeps focus on the button, blocks a second press, and never uses native disabled. Disabled wins. |
type | 'button' | 'submit' | 'reset' | 'button' | The button type. |
onClick | MouseEventHandler<HTMLButtonElement> | – | Called on activation, and never while disabled or busy. Pass it here, not on top. |
Result
| Prop | Type | Default | Description |
|---|---|---|---|
buttonProps | ButtonPartProps | – | Spread on a <button>: class, type, disabled state, handlers. |
isBusy | boolean | – | Whether the button is busy (busy is set and the button is not disabled). |
isDisabled | boolean | – | Whether the button is disabled. |
isFocusVisible | boolean | – | Whether the button has keyboard (:focus-visible) focus. |
Known issues#
- WebKit is not automated. Keyboard rows run in Vitest browser mode on Chromium. WebKit is not covered by the automated tests, so check Safari with your own users and assistive technology. Safari doesn't focus buttons on click, which doesn't affect this contract.
Testing#
The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.
| AT + browser + OS | Agent check | Human check | Result | Notes |
|---|---|---|---|---|
| Automated (re-runnable tests) | ||||
| axe, WCAG 2.2 A and AA, every story, 4 themes | pass | n/a | pass | button.stories.tsx, every story, 4 themes |
| Markup and ARIA (roles, names, ids, states) | pass | n/a | pass | button.test.tsx (axe and role, name and state): uses aria-disabled instead of disabled and stays in the Tab order |
| Keyboard contract rows (Vitest browser) | pass | n/a | pass | 18 rows, each names a test in button.test.tsx |
| Focus order and management | pass | n/a | pass | button.test.tsx: Enter and Space activate the button and keep focus on it |
| Display modes (forced colours, 320px reflow, text spacing, reduced motion) | pass | n/a | pass | the four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI |
| Core (required for beta) | ||||
| NVDA + Chrome + Windows | pending | pending | pending | |
| VoiceOver + Safari + iOS | pending | pending | pending | |
| Release (before 1.0 and each minor) | ||||
| JAWS + Chrome + Windows | pending | pending | pending | |
| TalkBack + Chrome + Android | pending | pending | pending | |
| VoiceOver + Safari + macOS | pending | pending | pending |
What to check#
With a screen reader and the keyboard, try:
- Tab to the button: the focus ring shows, and the name and "button" are read.
- Press Enter, then Space. Each runs the action once, and focus stays on the button.
- A focusable disabled button is reached and read as unavailable. Enter and Space do nothing.
- A busy button keeps focus and its name. Presses do nothing.
- An icon-only button reads its
aria-label.