Skip to
Keyboard shortcuts

Use the up and down arrow keys to move between items. Press Alt+Shift+K to open this list again.

Alpha version out now. The API may still change.

Button

A button that does something, such as sending a form or saving a draft. It never sends a form by accident. A disabled button can stay reachable with the keyboard, so people can still find it and read why.

Package
@kvirn-ui/react
Pattern
APG Button
Testing
Automated. Core 0 of 2, Release 0 of 3

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#

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.

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.

Primary

Default

Keyboard focus

Disabled but focusable, keyboard focus

Secondary

Default

Keyboard focus

Disabled but focusable, keyboard focus

Danger

Default

Keyboard focus

Disabled but focusable, keyboard focus

Icon only

Default

Keyboard focus

Props in this example:

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.

Props in this example:

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.

Props in this example:

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.

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.

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.

PartElement / roleARIANotes
Button<button> → buttonnone by defaulttype="button" unless type is given. Name from its content
disablednative disabledNot focusable, not activatable. data-disabled
disabled and focusablearia-disabled="true", no disabled attributeStays 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
busyaria-disabled="true", no disabled attributeThe 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 focusnonedata-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-label from 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-describedby if 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 Button or to useButton({ onClick }). Don't merge your own onClick on top of buttonProps: 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 to body (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: reduce and 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's ButtonText / ButtonFace apply. 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 is text-muted with 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-disabled exposed (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.

KeyWhereWhat happens
TabButtonMoves focus to the button
Test: button.test.tsx › keyboard › Tab moves focus to the button
Shift+TabButtonMoves focus to the previous focusable element, and off the button
Test: button.test.tsx › keyboard › Shift+Tab moves focus off the button
TabDisabled buttonSkips the button
Test: button.test.tsx › disabled › is natively disabled, marked with data-disabled and skipped by Tab
TabFocusable disabled buttonMoves 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
EnterButtonActivates. Focus stays on the button
Test: button.test.tsx › rendering › Enter and Space activate the button and keep focus on it
EnterFocusable disabled buttonBlocked. Focus stays on the button
Test: button.test.tsx › focusable when disabled › click, Enter and Space call no handler, and focus stays
SpaceButtonActivates on key up, not on key down. Focus stays on the button
Test: button.test.tsx › keyboard › Space activates the button on key up
SpaceFocusable disabled buttonBlocked. Focus stays on the button
Test: button.test.tsx › focusable when disabled › click, Enter and Space call no handler, and focus stays
EnterSubmit button in a formSubmits the form
Test: button.test.tsx › keyboard › Enter on a submit button submits the form
EnterDefault button in a formActivates the button without submitting the form (type="button")
Test: button.test.tsx › keyboard › Enter on a default button does not submit the form
TabBusy buttonMoves focus to the button, which is read as unavailable
Test: button.test.tsx › busy › Tab lands on a busy button
EnterBusy buttonBlocked: no handler, no submit. Focus stays on the button
Test: button.test.tsx › busy › click, Enter and Space call no handler, and focus stays
SpaceBusy buttonBlocked: 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 keybusy turns on while focusedFocus stays on the button
Test: button.test.tsx › busy › focus stays on the button when busy turns on and off
No keyBusy submit, implicit submissionBlocked, like a click
Test: button.test.tsx › busy › a busy submit button submits nothing, not even by implicit submission
No keyPointerA click on a disabled button, focusable or not, calls no handler
Test: button.test.tsx › disabled › …, button.test.tsx › focusable when disabled › …
No keyForm, implicit submissionEnter 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 keyHandler while disabledA focusable disabled button blocks the onClick prop on click, Enter, Space and implicit submission
Test: 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.

PropTypeDefaultDescription
disabledbooleanfalseBlocks activation. Without focusableWhenDisabled the button is natively disabled and skipped by Tab.
focusableWhenDisabledbooleanfalseWith disabled, keeps the button in the Tab order with aria-disabled="true" so users can find it and read why. Activation stays blocked.
busybooleanfalseA 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.
onClickMouseEventHandler<HTMLButtonElement>–Called on click, Enter and Space, and never while the button is disabled or busy.
Class or attributeValuesMeaning
kv-buttonalwaysThe part class. The default theme styles it.
kv-button--primaryclass you addThe one main action on the page. Theme variant.
kv-button--dangerclass you addAn action that deletes something. Theme variant.
kv-button--linkclass you addReads 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-onlyclass you addA square button with only an icon. Give it an aria-label. Theme option.
kv-button--compactclass you addAs tall as a compact input (32px) from 64rem, and 44px below it. For staff tools. Theme option.
data-disabledpresent or absentThe button is disabled, natively or focusable.
data-busypresent or absentSet while busy and not disabled. The theme shows the spinner at once.
data-focus-visiblepresent or absentThe button has keyboard focus (:focus-visible).
aria-disabled"true" or absentSet when disabled and focusableWhenDisabled are both on, and while busy. Not passed directly.

useButton

Options

PropTypeDefaultDescription
disabledbooleanfalseMirrors HTML disabled.
focusableWhenDisabledbooleanfalseKeeps a disabled button in the Tab order with aria-disabled="true".
busybooleanfalseThe 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.
onClickMouseEventHandler<HTMLButtonElement>–Called on activation, and never while disabled or busy. Pass it here, not on top.

Result

PropTypeDefaultDescription
buttonPropsButtonPartProps–Spread on a <button>: class, type, disabled state, handlers.
isBusyboolean–Whether the button is busy (busy is set and the button is not disabled).
isDisabledboolean–Whether the button is disabled.
isFocusVisibleboolean–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 + OSAgent checkHuman checkResultNotes
Automated (re-runnable tests)
axe, WCAG 2.2 A and AA, every story, 4 themespassn/apassbutton.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apassbutton.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)passn/apass18 rows, each names a test in button.test.tsx
Focus order and managementpassn/apassbutton.test.tsx: Enter and Space activate the button and keep focus on it
Display modes (forced colours, 320px reflow, text spacing, reduced motion)passn/apassthe four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI
Core (required for beta)
NVDA + Chrome + Windowspendingpendingpending
VoiceOver + Safari + iOSpendingpendingpending
Release (before 1.0 and each minor)
JAWS + Chrome + Windowspendingpendingpending
TalkBack + Chrome + Androidpendingpendingpending
VoiceOver + Safari + macOSpendingpendingpending

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.