Buttons allow users to perform an action. They have multiple styles for various needs, and are ideal for calling attention to where a user needs to do something in order to move forward in a flow.
Events
Use the onPress prop to handle interactions via mouse, keyboard, and touch. The onPressStart, onPressEnd, and onPressChange events are also emitted as the user interacts with the button.
import {Button} from '@react-spectrum/s2/Button';
import {useState} from 'react';
function Example() {
let [count, setCount] = useState(0);
return (
<Button onPress={() => setCount(c => c + 1)}>
{`${count} Edits`}
</Button>
);
}
Pending
Use the isPending prop to display a pending state. Pending buttons remain focusable, but are otherwise disabled. After a 1 second delay, an indeterminate spinner will be displayed in place of the button label and icon.
import {Button} from '@react-spectrum/s2/Button';
import {useState} from 'react';
function PendingButton() {
let [isPending, setPending] = useState(false);
return (
<Button
variant="primary"
isPending={isPending}
onPress={() => {
setPending(true);
setTimeout(() => {
setPending(false);
}, 5000);
}}>
Save
</Button>
);
}
API
<Button>
<Icon />
<Text />
</Button>
Button
| Name | Type | Default |
|---|---|---|
children | ReactNode | Default: — |
The content to display in the Button. | ||
isPending | boolean | Default: — |
Whether the button is in a pending state. This disables press and hover events while retaining focusability, and announces the pending state to screen readers. | ||
isDisabled | boolean | Default: — |
Whether the button is disabled. | ||
styles | StylesProp | Default: — |
Spectrum-defined styles, returned by the | ||
variant | 'primary'
| 'secondary'
| 'accent'
| 'negative'
| 'premium'
| 'genai' | Default: 'primary'
|
The visual style of the button. | ||
fillStyle | 'fill' | 'outline' | Default: 'fill'
|
The background style of the Button. | ||
size | 'S'
| 'M'
| 'L'
| 'XL' | Default: 'M'
|
The size of the Button. | ||
staticColor | 'white'
| 'black'
| 'auto' | Default: — |
The static color style to apply. Useful when the Button appears over a color background. | ||
onPress | | Default: — |
Handler that is called when the press is released over the target. | ||
onPressStart | | Default: — |
Handler that is called when a press interaction starts. | ||
onPressEnd | | Default: — |
Handler that is called when a press interaction ends, either over the target or when the pointer leaves the target. | ||
onPressChange | | Default: — |
Handler that is called when the press state changes. | ||
onPressUp | | Default: — |
Handler that is called when a press is released over the target, regardless of whether it started on the target or not. | ||
onFocus | | Default: — |
Handler that is called when the element receives focus. | ||
onBlur | | Default: — |
Handler that is called when the element loses focus. | ||
onFocusChange | | Default: — |
Handler that is called when the element's focus status changes. | ||
onKeyDown | | Default: — |
Handler that is called when a key is pressed. | ||
onKeyUp | | Default: — |
Handler that is called when a key is released. | ||
name | string | Default: — |
Submitted as a pair with the button's value as part of the form data. | ||
value | string | Default: — |
The value associated with the button's name when it's submitted with the form data. | ||
type | 'button'
| 'submit'
| 'reset' | Default: 'button'
|
The behavior of the button when used in an HTML form. | ||
form | string | Default: — |
The | ||
formTarget | string | Default: — |
Overrides the target attribute of the button's form owner. | ||
formNoValidate | boolean | Default: — |
Indicates that the form is not to be validated when it is submitted. | ||
formMethod | string | Default: — |
Indicates the HTTP method used to submit the form. | ||
formEncType | string | Default: — |
Indicates how to encode the form data that is submitted. | ||
formAction | ButtonHTMLAttributes | Default: — |
The URL that processes the information submitted by the button. Overrides the action attribute of the button's form owner. | ||
autoFocus | boolean | Default: — |
Whether the element should receive focus on render. | ||
id | string | Default: — |
The element's unique identifier. See MDN. | ||
excludeFromTabOrder | boolean | Default: — |
Whether to exclude the element from the sequential tab order. If true, the element will not be focusable via the keyboard by tabbing. This should be avoided except in rare scenarios where an alternative means of accessing the element or its functionality via the keyboard is available. | ||
preventFocusOnPress | boolean | Default: — |
Whether to prevent focus from moving to the button when pressing it. Caution, this can make the button inaccessible and should only be used when alternative keyboard interaction is provided, such as ComboBox's MenuTrigger or a NumberField's increment/decrement control. | ||
aria-disabled | boolean
| 'true'
| 'false' | Default: — |
Indicates whether the element is disabled to users of assistive technology. | ||
aria-expanded | boolean
| 'true'
| 'false' | Default: — |
Indicates whether the element, or another grouping element it controls, is currently expanded or collapsed. | ||
aria-haspopup | boolean
| 'menu'
| 'listbox'
| 'tree'
| 'grid'
| 'dialog'
| 'true'
| 'false' | Default: — |
Indicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element. | ||
aria-controls | string | Default: — |
Identifies the element (or elements) whose contents or presence are controlled by the current element. | ||
aria-pressed | boolean
| 'true'
| 'false'
| 'mixed' | Default: — |
Indicates the current "pressed" state of toggle buttons. | ||
aria-current | boolean
| 'true'
| 'false'
| 'page'
| 'step'
| 'location'
| 'date'
| 'time' | Default: — |
Indicates whether this element represents the current item within a container or set of related elements. | ||
aria-label | string | Default: — |
Defines a string value that labels the current element. | ||
aria-labelledby | string | Default: — |
Identifies the element (or elements) that labels the current element. | ||
aria-describedby | string | Default: — |
Identifies the element (or elements) that describes the object. | ||
aria-details | string | Default: — |
Identifies the element (or elements) that provide a detailed, extended description for the object. | ||
UNSAFE_className | UnsafeClassName | Default: — |
Sets the CSS className
for the element. Only use as a last resort. Use the | ||
UNSAFE_style | CSSProperties | Default: — |
Sets inline style for the
element. Only use as a last resort. Use the | ||
slot | string | null | Default: — |
A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit | ||