ToggleButtonGroup
A toggle button group allows a user to toggle multiple options, with single or multiple selection.
install | yarn add react-aria-components |
---|---|
version | 1.4.2 |
usage | import {ToggleButtonGroup} from 'react-aria-components' |
Example#
import {ToggleButtonGroup, ToggleButton} from 'react-aria-components';
<ToggleButtonGroup>
<ToggleButton id="left">Left</ToggleButton>
<ToggleButton id="center">Center</ToggleButton>
<ToggleButton id="right">Right</ToggleButton>
</ToggleButtonGroup>
import {
ToggleButton,
ToggleButtonGroup
} from 'react-aria-components';
<ToggleButtonGroup>
<ToggleButton id="left">Left</ToggleButton>
<ToggleButton id="center">Center</ToggleButton>
<ToggleButton id="right">Right</ToggleButton>
</ToggleButtonGroup>
import {
ToggleButton,
ToggleButtonGroup
} from 'react-aria-components';
<ToggleButtonGroup>
<ToggleButton id="left">
Left
</ToggleButton>
<ToggleButton id="center">
Center
</ToggleButton>
<ToggleButton id="right">
Right
</ToggleButton>
</ToggleButtonGroup>
Show CSS
.react-aria-ToggleButtonGroup {
display: flex;
> button {
border-radius: 0;
z-index: 1;
&[data-disabled] {
z-index: 0;
}
&[data-selected],
&[data-focus-visible] {
z-index: 2;
}
}
}
.react-aria-ToggleButtonGroup[data-orientation=horizontal] {
flex-direction: row;
> button {
margin-inline-start: -1px;
&:first-child {
border-radius: 4px 0 0 4px;
margin-inline-start: 0;
}
&:last-child {
border-radius: 0 4px 4px 0;
}
}
}
.react-aria-ToggleButtonGroup {
display: flex;
> button {
border-radius: 0;
z-index: 1;
&[data-disabled] {
z-index: 0;
}
&[data-selected],
&[data-focus-visible] {
z-index: 2;
}
}
}
.react-aria-ToggleButtonGroup[data-orientation=horizontal] {
flex-direction: row;
> button {
margin-inline-start: -1px;
&:first-child {
border-radius: 4px 0 0 4px;
margin-inline-start: 0;
}
&:last-child {
border-radius: 0 4px 4px 0;
}
}
}
.react-aria-ToggleButtonGroup {
display: flex;
> button {
border-radius: 0;
z-index: 1;
&[data-disabled] {
z-index: 0;
}
&[data-selected],
&[data-focus-visible] {
z-index: 2;
}
}
}
.react-aria-ToggleButtonGroup[data-orientation=horizontal] {
flex-direction: row;
> button {
margin-inline-start: -1px;
&:first-child {
border-radius: 4px 0 0 4px;
margin-inline-start: 0;
}
&:last-child {
border-radius: 0 4px 4px 0;
}
}
}
Features#
There is no built in element for toggle button groups in HTML. ToggleButtonGroup
helps achieve accessible toggle button groups that can be styled as needed.
- Accessible – Represented as an ARIA radiogroup when using single selection, or a toolbar when using multiple selection.
- Keyboard navigation – Users can navigate between buttons with the arrow keys. Selection can be toggled using the Enter or Space keys.
- Styleable – Hover, press, keyboard focus, and selection states are provided for easy styling.
Anatomy#
A toggle button group consists of a set of toggle buttons, and coordinates the selection state between them. Users can navigate between buttons with the arrow keys in either horizontal or vertical orientations.
import {ToggleButtonGroup, ToggleButton} from 'react-aria-components';
<ToggleButtonGroup>
<ToggleButton />
</ToggleButtonGroup>
import {
ToggleButton,
ToggleButtonGroup
} from 'react-aria-components';
<ToggleButtonGroup>
<ToggleButton />
</ToggleButtonGroup>
import {
ToggleButton,
ToggleButtonGroup
} from 'react-aria-components';
<ToggleButtonGroup>
<ToggleButton />
</ToggleButtonGroup>
Composed Components#
Selection#
ToggleButtonGroup supports both single and multiple selection modes. Use defaultSelectedKeys
to provide a default set of selected items (uncontrolled) and selectedKeys
to set the selected items (controlled). The value of the selected keys must match the id
prop of the items.
Single selection#
By default, the selectionMode
of a ToggleButtonGroup
is "single"
.
<ToggleButtonGroup defaultSelectedKeys={['list']}>
<ToggleButton id="grid">Grid view</ToggleButton>
<ToggleButton id="list">List view</ToggleButton>
<ToggleButton id="gallery">Gallery view</ToggleButton>
</ToggleButtonGroup>
<ToggleButtonGroup defaultSelectedKeys={['list']}>
<ToggleButton id="grid">Grid view</ToggleButton>
<ToggleButton id="list">List view</ToggleButton>
<ToggleButton id="gallery">Gallery view</ToggleButton>
</ToggleButtonGroup>
<ToggleButtonGroup
defaultSelectedKeys={[
'list'
]}
>
<ToggleButton id="grid">
Grid view
</ToggleButton>
<ToggleButton id="list">
List view
</ToggleButton>
<ToggleButton id="gallery">
Gallery view
</ToggleButton>
</ToggleButtonGroup>
Multiple selection#
Set selectionMode
prop to multiple
to allow more than one selection.
<ToggleButtonGroup selectionMode="multiple">
<ToggleButton id="bold">Bold</ToggleButton>
<ToggleButton id="italic">Italic</ToggleButton>
<ToggleButton id="underline">Underline</ToggleButton>
</ToggleButtonGroup>
<ToggleButtonGroup selectionMode="multiple">
<ToggleButton id="bold">Bold</ToggleButton>
<ToggleButton id="italic">Italic</ToggleButton>
<ToggleButton id="underline">Underline</ToggleButton>
</ToggleButtonGroup>
<ToggleButtonGroup selectionMode="multiple">
<ToggleButton id="bold">
Bold
</ToggleButton>
<ToggleButton id="italic">
Italic
</ToggleButton>
<ToggleButton id="underline">
Underline
</ToggleButton>
</ToggleButtonGroup>
Controlled selection#
The selectedKeys
prop can be used to make the selected state controlled.
import type {Key} from 'react-aria-components';
function Example() {
let [selected, setSelected] = React.useState(new Set<Key>(['bold']));
return (
<>
<ToggleButtonGroup
selectionMode="multiple"
selectedKeys={selected}
onSelectionChange={setSelected}
>
<ToggleButton id="bold">Bold</ToggleButton>
<ToggleButton id="italic">Italic</ToggleButton>
<ToggleButton id="underline">Underline</ToggleButton>
</ToggleButtonGroup>
<p>Current selections (controlled): {[...selected].join(', ')}</p>
</>
);
}
import type {Key} from 'react-aria-components';
function Example() {
let [selected, setSelected] = React.useState(
new Set<Key>(['bold'])
);
return (
<>
<ToggleButtonGroup
selectionMode="multiple"
selectedKeys={selected}
onSelectionChange={setSelected}
>
<ToggleButton id="bold">Bold</ToggleButton>
<ToggleButton id="italic">Italic</ToggleButton>
<ToggleButton id="underline">
Underline
</ToggleButton>
</ToggleButtonGroup>
<p>
Current selections (controlled):{' '}
{[...selected].join(', ')}
</p>
</>
);
}
import type {Key} from 'react-aria-components';
function Example() {
let [
selected,
setSelected
] = React.useState(
new Set<Key>([
'bold'
])
);
return (
<>
<ToggleButtonGroup
selectionMode="multiple"
selectedKeys={selected}
onSelectionChange={setSelected}
>
<ToggleButton id="bold">
Bold
</ToggleButton>
<ToggleButton id="italic">
Italic
</ToggleButton>
<ToggleButton id="underline">
Underline
</ToggleButton>
</ToggleButtonGroup>
<p>
Current
selections
(controlled):
{' '}
{[...selected]
.join(', ')}
</p>
</>
);
}
Disabled#
All buttons within a ToggleButtonGroup
can be disabled using the isDisabled
prop.
<ToggleButtonGroup isDisabled>
<ToggleButton id="grid">Grid view</ToggleButton>
<ToggleButton id="list">List view</ToggleButton>
<ToggleButton id="gallery">Gallery view</ToggleButton>
</ToggleButtonGroup>
<ToggleButtonGroup isDisabled>
<ToggleButton id="grid">Grid view</ToggleButton>
<ToggleButton id="list">List view</ToggleButton>
<ToggleButton id="gallery">Gallery view</ToggleButton>
</ToggleButtonGroup>
<ToggleButtonGroup
isDisabled
>
<ToggleButton id="grid">
Grid view
</ToggleButton>
<ToggleButton id="list">
List view
</ToggleButton>
<ToggleButton id="gallery">
Gallery view
</ToggleButton>
</ToggleButtonGroup>
Individual items can be disabled using the isDisabled
prop on each ToggleButton
.
<ToggleButtonGroup>
<ToggleButton id="grid">Grid view</ToggleButton>
<ToggleButton id="list" isDisabled>List view</ToggleButton>
<ToggleButton id="gallery">Gallery view</ToggleButton>
</ToggleButtonGroup>
<ToggleButtonGroup>
<ToggleButton id="grid">Grid view</ToggleButton>
<ToggleButton id="list" isDisabled>
List view
</ToggleButton>
<ToggleButton id="gallery">Gallery view</ToggleButton>
</ToggleButtonGroup>
<ToggleButtonGroup>
<ToggleButton id="grid">
Grid view
</ToggleButton>
<ToggleButton
id="list"
isDisabled
>
List view
</ToggleButton>
<ToggleButton id="gallery">
Gallery view
</ToggleButton>
</ToggleButtonGroup>
Orientation#
By default, toggle button groups are horizontally oriented. The orientation prop can be set to "vertical" to change the arrow key navigation behavior.
<ToggleButtonGroup orientation="vertical">
<ToggleButton id="grid">Grid</ToggleButton>
<ToggleButton id="list">List</ToggleButton>
<ToggleButton id="gallery">Gallery</ToggleButton>
</ToggleButtonGroup>
<ToggleButtonGroup orientation="vertical">
<ToggleButton id="grid">Grid</ToggleButton>
<ToggleButton id="list">List</ToggleButton>
<ToggleButton id="gallery">Gallery</ToggleButton>
</ToggleButtonGroup>
<ToggleButtonGroup orientation="vertical">
<ToggleButton id="grid">
Grid
</ToggleButton>
<ToggleButton id="list">
List
</ToggleButton>
<ToggleButton id="gallery">
Gallery
</ToggleButton>
</ToggleButtonGroup>
Show CSS
.react-aria-ToggleButtonGroup[data-orientation=vertical] {
flex-direction: column;
width: fit-content;
> button {
margin-block-start: -1px;
&:first-child {
border-radius: 4px 4px 0 0;
margin-block-start: 0;
}
&:last-child {
border-radius: 0 0 4px 4px;
}
}
}
.react-aria-ToggleButtonGroup[data-orientation=vertical] {
flex-direction: column;
width: fit-content;
> button {
margin-block-start: -1px;
&:first-child {
border-radius: 4px 4px 0 0;
margin-block-start: 0;
}
&:last-child {
border-radius: 0 0 4px 4px;
}
}
}
.react-aria-ToggleButtonGroup[data-orientation=vertical] {
flex-direction: column;
width: fit-content;
> button {
margin-block-start: -1px;
&:first-child {
border-radius: 4px 4px 0 0;
margin-block-start: 0;
}
&:last-child {
border-radius: 0 0 4px 4px;
}
}
}
Accessiblity#
A ToggleButtonGroup
can be labeled using the aria-label
or aria-labelledby
props.
<ToggleButtonGroup aria-label="Text style">
<ToggleButton id="bold">Bold</ToggleButton>
<ToggleButton id="italic">Italic</ToggleButton>
<ToggleButton id="underline">Underline</ToggleButton>
</ToggleButtonGroup>
<ToggleButtonGroup aria-label="Text style">
<ToggleButton id="bold">Bold</ToggleButton>
<ToggleButton id="italic">Italic</ToggleButton>
<ToggleButton id="underline">Underline</ToggleButton>
</ToggleButtonGroup>
<ToggleButtonGroup aria-label="Text style">
<ToggleButton id="bold">
Bold
</ToggleButton>
<ToggleButton id="italic">
Italic
</ToggleButton>
<ToggleButton id="underline">
Underline
</ToggleButton>
</ToggleButtonGroup>
Props#
ToggleButtonGroup#
Name | Type | Default | Description |
orientation | Orientation | 'horizontal' | The orientation of the the toggle button group. |
selectionMode | 'single' | 'multiple' | — | Whether single or multiple selection is enabled. |
disallowEmptySelection | boolean | — | Whether the collection allows empty selection. |
selectedKeys | Iterable<Key> | — | The currently selected keys in the collection (controlled). |
defaultSelectedKeys | Iterable<Key> | — | The initial selected keys in the collection (uncontrolled). |
isDisabled | boolean | — | Whether all items are disabled. |
children | ReactNode | (
(values: ToggleButtonGroupRenderProps
& & {}
)) => ReactNode | — | The children of the component. A function may be provided to alter the children based on component state. |
className | string | (
(values: ToggleButtonGroupRenderProps
& & {}
)) => string | — | The CSS className for the element. A function may be provided to compute the class based on component state. |
style | CSSProperties | (
(values: ToggleButtonGroupRenderProps
& & {}
)) => CSSProperties | undefined | — | The inline style for the element. A function may be provided to compute the style based on component state. |
Events
Name | Type | Description |
onSelectionChange | (
(keys: Set<Key>
)) => void | Handler that is called when the selection changes. |
Layout
Name | Type | Description |
slot | string | null | A slot name for the component. Slots allow the component to receive props from a parent component.
An explicit |
Accessibility
Name | Type | Description |
aria-label | string | Defines a string value that labels the current element. |
aria-labelledby | string | Identifies the element (or elements) that labels the current element. |
aria-describedby | string | Identifies the element (or elements) that describes the object. |
aria-details | string | Identifies the element (or elements) that provide a detailed, extended description for the object. |
ToggleButton#
Name | Type | Default | Description |
id | Key | — | When used in a ToggleButtonGroup, an identifier for the item in selectedKeys . When used standalone, a DOM id. |
isSelected | boolean | — | Whether the element should be selected (controlled). |
defaultSelected | boolean | — | Whether the element should be selected (uncontrolled). |
isDisabled | boolean | — | Whether the button is disabled. |
autoFocus | boolean | — | Whether the element should receive focus on render. |
type | 'button'
| 'submit'
| 'reset' | 'button' | The behavior of the button when used in an HTML form. |
children | ReactNode | (
(values: ToggleButtonRenderProps
& & {}
)) => ReactNode | — | The children of the component. A function may be provided to alter the children based on component state. |
className | string | (
(values: ToggleButtonRenderProps
& & {}
)) => string | — | The CSS className for the element. A function may be provided to compute the class based on component state. |
style | CSSProperties | (
(values: ToggleButtonRenderProps
& & {}
)) => CSSProperties | undefined | — | The inline style for the element. A function may be provided to compute the style based on component state. |
Events
Name | Type | Description |
onChange | (
(isSelected: boolean
)) => void | Handler that is called when the element's selection state changes. |
onPress | (
(e: PressEvent
)) => void | Handler that is called when the press is released over the target. |
onPressStart | (
(e: PressEvent
)) => void | Handler that is called when a press interaction starts. |
onPressEnd | (
(e: PressEvent
)) => void | Handler that is called when a press interaction ends, either over the target or when the pointer leaves the target. |
onPressChange | (
(isPressed: boolean
)) => void | Handler that is called when the press state changes. |
onPressUp | (
(e: PressEvent
)) => void | Handler that is called when a press is released over the target, regardless of whether it started on the target or not. |
onFocus | (
(e: FocusEvent<Target>
)) => void | Handler that is called when the element receives focus. |
onBlur | (
(e: FocusEvent<Target>
)) => void | Handler that is called when the element loses focus. |
onFocusChange | (
(isFocused: boolean
)) => void | Handler that is called when the element's focus status changes. |
onKeyDown | (
(e: KeyboardEvent
)) => void | Handler that is called when a key is pressed. |
onKeyUp | (
(e: KeyboardEvent
)) => void | Handler that is called when a key is released. |
onHoverStart | (
(e: HoverEvent
)) => void | Handler that is called when a hover interaction starts. |
onHoverEnd | (
(e: HoverEvent
)) => void | Handler that is called when a hover interaction ends. |
onHoverChange | (
(isHovering: boolean
)) => void | Handler that is called when the hover state changes. |
Layout
Name | Type | Description |
slot | string | null | A slot name for the component. Slots allow the component to receive props from a parent component.
An explicit |
Accessibility
Name | Type | Description |
excludeFromTabOrder | boolean | 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 | 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-expanded | boolean
| 'true'
| 'false' | 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' | Indicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element. |
aria-controls | string | Identifies the element (or elements) whose contents or presence are controlled by the current element. |
aria-pressed | boolean
| 'true'
| 'false'
| 'mixed' | Indicates the current "pressed" state of toggle buttons. |
aria-label | string | Defines a string value that labels the current element. |
aria-labelledby | string | Identifies the element (or elements) that labels the current element. |
aria-describedby | string | Identifies the element (or elements) that describes the object. |
aria-details | string | Identifies the element (or elements) that provide a detailed, extended description for the object. |
Styling#
React Aria components can be styled in many ways, including using CSS classes, inline styles, utility classes (e.g. Tailwind), CSS-in-JS (e.g. Styled Components), etc. By default, all components include a builtin className
attribute which can be targeted using CSS selectors. These follow the react-aria-ComponentName
naming convention.
.react-aria-ToggleButtonGroup {
/* ... */
}
.react-aria-ToggleButtonGroup {
/* ... */
}
.react-aria-ToggleButtonGroup {
/* ... */
}
A custom className
can also be specified on any component. This overrides the default className
provided by React Aria with your own.
<ToggleButtonGroup className="my-toggle-group">
{/* ... */}
</ToggleButtonGroup>
<ToggleButtonGroup className="my-toggle-group">
{/* ... */}
</ToggleButtonGroup>
<ToggleButtonGroup className="my-toggle-group">
{/* ... */}
</ToggleButtonGroup>
In addition, some components support multiple UI states (e.g. focused, placeholder, readonly, etc.). React Aria components expose states using data attributes, which you can target in CSS selectors. For example:
.react-aria-ToggleButton[data-selected] {
/* ... */
}
.react-aria-ToggleButton[data-selected] {
/* ... */
}
.react-aria-ToggleButton[data-selected] {
/* ... */
}
The className
and style
props also accept functions which receive states for styling. This lets you dynamically determine the classes or styles to apply, which is useful when using utility CSS libraries like Tailwind.
<ToggleButtonGroup
className={({ isDisabled }) => isDisabled ? 'bg-gray-100' : 'bg-gray-600'}
/>
<ToggleButtonGroup
className={({ isDisabled }) =>
isDisabled ? 'bg-gray-100' : 'bg-gray-600'}
/>
<ToggleButtonGroup
className={(
{ isDisabled }
) =>
isDisabled
? 'bg-gray-100'
: 'bg-gray-600'}
/>
Render props may also be used as children to alter what elements are rendered based on the current state. For example, you could swap an icon depending on the selection state.
<ToggleButton>
{({isSelected}) => (
<>
{isSelected ? <PinnedIcon /> : <UnpinnedIcon />}
Pin
</>
)}
</ToggleButton>
<ToggleButton>
{({isSelected}) => (
<>
{isSelected ? <PinnedIcon /> : <UnpinnedIcon />}
Pin
</>
)}
</ToggleButton>
<ToggleButton>
{(
{ isSelected }
) => (
<>
{isSelected
? (
<PinnedIcon />
)
: (
<UnpinnedIcon />
)}
Pin
</>
)}
</ToggleButton>
The states, selectors, and render props for each component used in a ToggleButtonGroup
are documented below.
ToggleButtonGroup#
A ToggleButtonGroup
can be targeted with the .react-aria-ToggleButtonGroup
CSS selector, or by overriding with a custom className
. It supports the following states and render props:
Name | CSS Selector | Description |
isDisabled | [data-disabled] | Whether the toggle button group is disabled. |
state | — | State of the toggle button group. |
ToggleButton#
A ToggleButton
can be targeted with the .react-aria-ToggleButton
CSS selector, or by overriding with a custom className
.
Name | CSS Selector | Description |
isSelected | [data-selected] | Whether the button is currently selected. |
state | — | State of the toggle button. |
isHovered | [data-hovered] | Whether the button is currently hovered with a mouse. |
isPressed | [data-pressed] | Whether the button is currently in a pressed state. |
isFocused | [data-focused] | Whether the button is focused, either via a mouse or keyboard. |
isFocusVisible | [data-focus-visible] | Whether the button is keyboard focused. |
isDisabled | [data-disabled] | Whether the button is disabled. |
Advanced customization#
Contexts#
All React Aria Components export a corresponding context that can be used to send props to them from a parent element. This enables you to build your own compositional APIs similar to those found in React Aria Components itself. You can send any prop or ref via context that you could pass to the corresponding component. The local props and ref on the component are merged with the ones passed via context, with the local props taking precedence (following the rules documented in mergeProps).
Component | Context | Props | Ref |
ToggleButtonGroup | ToggleButtonGroupContext | ToggleButtonGroupProps | HTMLDivElement |
ToggleButton | ToggleButtonContext | ToggleButtonProps | HTMLButtonElement |
State#
ToggleButtonGroup provides an ToggleGroupState
object to its children via ToggleGroupStateContext
. This can be used to access and manipulate the toggle button group's state.
This example shows a ClearButton
component that can be placed within a ToggleButtonGroup
to allow the user to clear the selected item.
import {Button, ToggleGroupStateContext} from 'react-aria-components';
function ClearButton() {
let state = React.useContext(ToggleGroupStateContext); return (
<Button onPress={() => state?.setSelectedKeys(new Set())}>
Clear
</Button>
);
}
<ToggleButtonGroup
selectionMode="multiple"
defaultSelectedKeys={['bold', 'italic']}
>
<ToggleButton id="bold">Bold</ToggleButton>
<ToggleButton id="italic">Italic</ToggleButton>
<ToggleButton id="underline">Underline</ToggleButton>
<ClearButton /></ToggleButtonGroup>
import {
Button,
ToggleGroupStateContext
} from 'react-aria-components';
function ClearButton() {
let state = React.useContext(ToggleGroupStateContext); return (
<Button
onPress={() => state?.setSelectedKeys(new Set())}
>
Clear
</Button>
);
}
<ToggleButtonGroup
selectionMode="multiple"
defaultSelectedKeys={['bold', 'italic']}
>
<ToggleButton id="bold">Bold</ToggleButton>
<ToggleButton id="italic">Italic</ToggleButton>
<ToggleButton id="underline">Underline</ToggleButton>
<ClearButton /></ToggleButtonGroup>
import {
Button,
ToggleGroupStateContext
} from 'react-aria-components';
function ClearButton() {
let state = React
.useContext(
ToggleGroupStateContext
); return (
<Button
onPress={() =>
state
?.setSelectedKeys(
new Set()
)}
>
Clear
</Button>
);
}
<ToggleButtonGroup
selectionMode="multiple"
defaultSelectedKeys={[
'bold',
'italic'
]}
>
<ToggleButton id="bold">
Bold
</ToggleButton>
<ToggleButton id="italic">
Italic
</ToggleButton>
<ToggleButton id="underline">
Underline
</ToggleButton>
<ClearButton /></ToggleButtonGroup>
Hooks#
If you need to customize things even further, such as accessing internal state, intercepting events, or customizing the DOM structure, you can drop down to the lower level Hook-based API. See useToggleButtonGroup for more details.