Actions and feedback
Button
When to use
- The user has to perform an action: save, open a dialog, delete, retry a request.
- You need the shared set of
variant,tone,size,disabledand focus indication. - The action is navigation: pass a single link as
childrentogether withasChild.
When not to use
- To pick one mode, use
Segmented; for an independent set of filters, useToggleGroup. - To toggle a boolean state with the switch role, use
Toggle. - For a link that does not need to look like a button, use
Link.
Minimal example
<Button tone="accent" variant="solid" onClick={save}>
Save
</Button>
<Button asChild variant="soft">
<a href="/campaigns">To campaigns</a>
</Button>Public API
variant,tone,sizeset the look; the defaults aresoft,neutral,md.- There are four size steps:
xs,sm,md,lg.xsis 20 pixels and exists for an icon button in a list row:smis 30 with 20 for the content, and a 32 row stretches to 42.xsis not meant to have a label, so use it withiconOnly. The hit area ofxsis still 24×24: a pseudo-element extends it, because 20×20 fails WCAG 2.5.8, and enlarging the button itself would bring the stretched row back. iconadds an icon; withiconOnlythe label is not rendered. An icon-only button must have an accessible name througharia-labelortitle. The icon itself is not a hit target (pointer-events: none): otherwise theevent.targetof a click on the icon is the<svg>, not the button, and anyone who inspects the click target gets it wrong. A real-world example is anEditableListrow with an action: react-aria checks the target without walking up the ancestors, and a click on the icon triggered both the button and the row action.selectedsetsaria-pressed; it is only for a pressed button, and a link (asChild) does not get it.loadingmeans a submission is in progress: an indicator takes the icon slot, the button cannot be pressed, and the state is announced througharia-busy. There is no need to adddisabled: it is applied automatically. A button without an icon gets the slot for the duration of the submission, and the label shifts; if a button can enterloading, give it an icon from the start. The indicator is decorative and is not part of the button's accessible name.asChildpasses the styles, props and ref to the single child element. For a link,disabledbecomesaria-disabled, turns off pointer events and takes the child out of the tab order (tabIndex={-1}) — even if the child settabIndexitself: the component decides this itself, because otherwise a disabled link remained reachable from the keyboard.selectedis not passed on, becausearia-pressedis not allowed on a link.
Accessibility and semantics
- A regular button is activated with Space and Enter; a link rendered through
asChildis activated with Enter, as a link should be. - Do not nest interactive elements in
children. - Source of the contract:
src/components/actions/Button.tsx; scenarios —src/components/actions/Button.stories.tsxandtests/button-aschild.test.tsx.
TypeScript types
ButtonProps
Prop
Type
Plus DOM attributes, such as value, onChange, disabled — 288 in total; the table does not repeat them.