useToggleButtonGroup
install | yarn add react-aria |
---|---|
version | 3.35.1 |
usage | import {useToggleButtonGroup} from 'react-aria' |
API#
useToggleButtonGroup(
props: AriaToggleButtonGroupProps,
state: ToggleGroupState,
ref: RefObject<HTMLElement
| | null>
): ToggleButtonGroupAria
useToggleButtonGroupItem(
props: AriaToggleButtonGroupItemOptions<ElementType>,
state: ToggleGroupState,
ref: RefObject<any>
): ToggleButtonAria<HTMLAttributes<any>>
Features#
There is no built in element for toggle button groups in HTML. useToggleButtonGroup
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.
useToggleButtonGroup
returns props for the toggle button group:
Name | Type | Description |
groupProps | DOMAttributes | Props for the toggle button group container. |
useToggleButtonGroupItem
returns props for an individual toggle button:
Name | Type | Description |
isSelected | boolean | Whether the button is selected. |
isDisabled | boolean | Whether the button is disabled. |
buttonProps | T | Props for the button element. |
isPressed | boolean | Whether the button is currently pressed. |
Selection state is managed by the useToggleGroupState
hook in @react-stately/toggle
. The state object should be passed as an option to useToggleButtonGroup
and useToggleButtonGroupItem
.
Note: useToggleButtonGroupItem
should only be used when it is contained within a toggle button group. For a
standalone toggle button, use the useToggleButton hook instead.
Example#
import type {ToggleGroupState} from 'react-stately';
import type {AriaToggleButtonGroupItemProps, AriaToggleButtonGroupProps} from 'react-aria';
import {useToggleGroupState} from 'react-stately';
import {useToggleButtonGroup, useToggleButtonGroupItem} from 'react-aria';
interface ToggleButtonGroupProps extends AriaToggleButtonGroupProps {
children: React.ReactNode;
}
let ToggleButtonGroupContext = React.createContext<ToggleGroupState | null>(
null
);
function ToggleButtonGroup(props: ToggleButtonGroupProps) {
let state = useToggleGroupState(props);
let ref = React.useRef<HTMLDivElement | null>(null);
let { groupProps } = useToggleButtonGroup(props, state, ref);
return (
<div {...groupProps} className="toggle-group" ref={ref}>
<ToggleButtonGroupContext.Provider value={state}>
{props.children}
</ToggleButtonGroupContext.Provider>
</div>
);
}
function ToggleButton(props: AriaToggleButtonGroupItemProps) {
let ref = React.useRef<HTMLButtonElement | null>(null);
let state = React.useContext(ToggleButtonGroupContext)!;
let { buttonProps, isPressed, isSelected } = useToggleButtonGroupItem(
props,
state,
ref
);
return (
<button
{...buttonProps}
className="toggle-button"
data-pressed={isPressed}
data-selected={isSelected}
ref={ref}
>
{props.children}
</button>
);
}
<ToggleButtonGroup>
<ToggleButton id="left">Left</ToggleButton>
<ToggleButton id="center">Center</ToggleButton>
<ToggleButton id="right">Right</ToggleButton>
</ToggleButtonGroup>
import type {ToggleGroupState} from 'react-stately';
import type {
AriaToggleButtonGroupItemProps,
AriaToggleButtonGroupProps
} from 'react-aria';
import {useToggleGroupState} from 'react-stately';
import {
useToggleButtonGroup,
useToggleButtonGroupItem
} from 'react-aria';
interface ToggleButtonGroupProps
extends AriaToggleButtonGroupProps {
children: React.ReactNode;
}
let ToggleButtonGroupContext = React.createContext<
ToggleGroupState | null
>(null);
function ToggleButtonGroup(props: ToggleButtonGroupProps) {
let state = useToggleGroupState(props);
let ref = React.useRef<HTMLDivElement | null>(null);
let { groupProps } = useToggleButtonGroup(
props,
state,
ref
);
return (
<div {...groupProps} className="toggle-group" ref={ref}>
<ToggleButtonGroupContext.Provider value={state}>
{props.children}
</ToggleButtonGroupContext.Provider>
</div>
);
}
function ToggleButton(
props: AriaToggleButtonGroupItemProps
) {
let ref = React.useRef<HTMLButtonElement | null>(null);
let state = React.useContext(ToggleButtonGroupContext)!;
let { buttonProps, isPressed, isSelected } =
useToggleButtonGroupItem(props, state, ref);
return (
<button
{...buttonProps}
className="toggle-button"
data-pressed={isPressed}
data-selected={isSelected}
ref={ref}
>
{props.children}
</button>
);
}
<ToggleButtonGroup>
<ToggleButton id="left">Left</ToggleButton>
<ToggleButton id="center">Center</ToggleButton>
<ToggleButton id="right">Right</ToggleButton>
</ToggleButtonGroup>
import type {ToggleGroupState} from 'react-stately';
import type {
AriaToggleButtonGroupItemProps,
AriaToggleButtonGroupProps
} from 'react-aria';
import {useToggleGroupState} from 'react-stately';
import {
useToggleButtonGroup,
useToggleButtonGroupItem
} from 'react-aria';
interface ToggleButtonGroupProps
extends
AriaToggleButtonGroupProps {
children:
React.ReactNode;
}
let ToggleButtonGroupContext =
React.createContext<
| ToggleGroupState
| null
>(null);
function ToggleButtonGroup(
props:
ToggleButtonGroupProps
) {
let state =
useToggleGroupState(
props
);
let ref = React.useRef<
HTMLDivElement | null
>(null);
let { groupProps } =
useToggleButtonGroup(
props,
state,
ref
);
return (
<div
{...groupProps}
className="toggle-group"
ref={ref}
>
<ToggleButtonGroupContext.Provider
value={state}
>
{props.children}
</ToggleButtonGroupContext.Provider>
</div>
);
}
function ToggleButton(
props:
AriaToggleButtonGroupItemProps
) {
let ref = React.useRef<
| HTMLButtonElement
| null
>(null);
let state = React
.useContext(
ToggleButtonGroupContext
)!;
let {
buttonProps,
isPressed,
isSelected
} =
useToggleButtonGroupItem(
props,
state,
ref
);
return (
<button
{...buttonProps}
className="toggle-button"
data-pressed={isPressed}
data-selected={isSelected}
ref={ref}
>
{props.children}
</button>
);
}
<ToggleButtonGroup>
<ToggleButton id="left">
Left
</ToggleButton>
<ToggleButton id="center">
Center
</ToggleButton>
<ToggleButton id="right">
Right
</ToggleButton>
</ToggleButtonGroup>
Show CSS
.toggle-group {
display: flex;
gap: 4px;
&[aria-orientation=vertical] {
flex-direction: column;
width: fit-content;
}
}
.toggle-button {
background: lightgray;
color: black;
padding: 10px;
font-size: 16px;
user-select: none;
border: none;
&[data-pressed=true] {
background: gray;
}
&[data-selected=true] {
background: green;
color: white;
&[data-pressed=true] {
background: darkgreen;
}
}
&:disabled {
opacity: 0.5;
}
}
.toggle-group {
display: flex;
gap: 4px;
&[aria-orientation=vertical] {
flex-direction: column;
width: fit-content;
}
}
.toggle-button {
background: lightgray;
color: black;
padding: 10px;
font-size: 16px;
user-select: none;
border: none;
&[data-pressed=true] {
background: gray;
}
&[data-selected=true] {
background: green;
color: white;
&[data-pressed=true] {
background: darkgreen;
}
}
&:disabled {
opacity: 0.5;
}
}
.toggle-group {
display: flex;
gap: 4px;
&[aria-orientation=vertical] {
flex-direction: column;
width: fit-content;
}
}
.toggle-button {
background: lightgray;
color: black;
padding: 10px;
font-size: 16px;
user-select: none;
border: none;
&[data-pressed=true] {
background: gray;
}
&[data-selected=true] {
background: green;
color: white;
&[data-pressed=true] {
background: darkgreen;
}
}
&:disabled {
opacity: 0.5;
}
}
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-stately';
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-stately';
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-stately';
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>
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>