A toggle button group allows a user to toggle multiple options, with single or multiple selection.
Vanilla CSS theme
--tint CSS variable used by the Vanilla CSS examples.Selection
Use the selectionMode prop to enable single or multiple selection. The selected items can be controlled via the selectedKeys prop, matching the id of each <ToggleButton>. Items can be disabled with the isDisabled prop. See the selection guide for more details.
Current selection: bold
Animation
Render a SelectionIndicator within each ToggleButton to animate selection changes.
import {ToggleButtonGroup, ToggleButton, ToggleButtonProps, SelectionIndicator, composeRenderProps} from 'react-aria-components';
import './SegmentedControl.css';
function SegmentedControlItem(props: ToggleButtonProps) {
return (
<ToggleButton {...props} className="segmented-control-item">
{composeRenderProps(props.children, (children) => (<>
<SelectionIndicator className="react-aria-SelectionIndicator button-base" data-selected />
<span>{children}</span>
</>))}
</ToggleButton>
);
}
<ToggleButtonGroup
className="segmented-control button-base"
data-variant="secondary"
aria-label="Time period"
defaultSelectedKeys={['day']}
disallowEmptySelection>
<SegmentedControlItem id="day">Day</SegmentedControlItem>
<SegmentedControlItem id="week">Week</SegmentedControlItem>
<SegmentedControlItem id="month">Month</SegmentedControlItem>
<SegmentedControlItem id="year">Year</SegmentedControlItem>
</ToggleButtonGroup>
API
<ToggleButtonGroup>
<ToggleButton>
<SelectionIndicator />
</ToggleButton>
</ToggleButtonGroup>
ToggleButtonGroup
| Name | Type | Default |
|---|---|---|
orientation | Orientation | Default: 'horizontal'
|
The orientation of the the toggle button group. | ||
isDisabled | boolean | Default: — |
Whether all items are disabled. | ||
children | ChildrenOrFunction | Default: — |
The children of the component. A function may be provided to alter the children based on component state. | ||
selectionMode | 'single' | 'multiple' | Default: 'single'
|
Whether single or multiple selection is enabled. | ||
selectedKeys | Iterable | Default: — |
The currently selected keys in the collection (controlled). | ||
defaultSelectedKeys | Iterable | Default: — |
The initial selected keys in the collection (uncontrolled). | ||
onSelectionChange | | Default: — |
Handler that is called when the selection changes. | ||
disallowEmptySelection | boolean | Default: — |
Whether the collection allows empty selection. | ||
Default className: react-aria-ToggleButtonGroup
| Render Prop | CSS Selector |
|---|---|
orientation | CSS Selector: [data-orientation="horizontal | vertical"]
|
| The orientation of the toggle button group. | |
isDisabled | CSS Selector: [data-disabled]
|
| Whether the toggle button group is disabled. | |
state | CSS Selector: — |
| State of the toggle button group. | |