ToggleButtons allow users to toggle a selection on or off, for example switching between two states or modes.
Selection
Use the isSelected or defaultSelected prop to set the selection state, and onChange to handle selection changes.
Not starred
import {ToggleButton} from '@react-spectrum/s2/ToggleButton';
import {useState} from 'react';
import Star from '@react-spectrum/s2/icons/Star';
function Example(props) {
let [selected, setSelection] = useState(false);
return (
<>
<ToggleButton
{...props}
aria-label="Star"
isSelected={selected}
onChange={setSelection}>
<Star />
</ToggleButton>
<p>{selected ? 'Starred' : 'Not starred'}</p>
</>
);
}
API
<ToggleButton>
<Icon /> or <Avatar />
<Text />
</ToggleButton>
ToggleButton
| Name | Type | Default |
|---|---|---|
children | ReactNode | Default: — |
The content to display in the button. | ||
isEmphasized | boolean | Default: — |
Whether the button should be displayed with an emphasized style. | ||
id | Key | Default: — |
When used in a ToggleButtonGroup, an identifier for the item in | ||
isSelected | boolean | Default: — |
Whether the element should be selected (controlled). | ||
defaultSelected | boolean | Default: — |
Whether the element should be selected (uncontrolled). | ||
isDisabled | boolean | Default: — |
Whether the button is disabled. | ||
styles | StylesProp | Default: — |
Spectrum-defined styles, returned by the | ||
size | 'XS'
| 'S'
| 'M'
| 'L'
| 'XL' | Default: 'M'
|
The size of the ActionButton. | ||
staticColor | 'black'
| 'white'
| 'auto' | Default: — |
The static color style to apply. Useful when the ActionButton appears over a color background. | ||
isQuiet | boolean | Default: — |
Whether the button should be displayed with a quiet style. | ||
onChange | | Default: — |
Handler that is called when the element's selection state changes. | ||
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. | ||
autoFocus | boolean | Default: — |
Whether the element should receive focus on render. | ||
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-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 | ||