shadcn.io is an independent project built on shadcn/ui
Components
Free
Toggle Group for React Aria
Toggle buttons grouped into one toolbar control for React on React Aria, with one pressed at a time or several, and arrow keys between them.
On this page9 sections
A toggle group is a row of toggle buttons that act as one control: text alignment, where one is pressed at a time, or bold, italic and underline, where several can be. It belongs in a toolbar or above a list, where a form control would look out of place.
For a required choice in a form, with a visible label on every option, use a Radio Group. For one button on its own that stays pressed, use a Toggle.
Installation
Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.
With one choice, the default, the group has the radiogroup role and each item the radio role with aria-checked. With selectionMode set to multiple, it's a toolbar of buttons with aria-pressed. The arrow keys follow orientation and stop at the first and last item. Give an icon-only item an aria-label, and give the group one too.
KeyAction
Tab
Moves focus into the group, to the item that last had focus or else the first one, and the next Tab leaves the group.
Space
Presses or releases the focused item.
Enter
Presses or releases the focused item.
Arrow Right
In a horizontal group, moves focus to the next item, stopping at the last.
Arrow Left
In a horizontal group, moves focus to the previous item, stopping at the first.
Arrow Down
In a vertical group, moves focus to the next item, stopping at the last.
Arrow Up
In a vertical group, moves focus to the previous item, stopping at the first.
Demos
Outline
A two-button filter, All and Missed, like the one above a call log, with variant set to outline and All pressed to start. Only one can be pressed at a time, and pressing it again releases it, leaving neither. Use the outline when the group sits on a plain surface and needs an edge to read as one control.
Sizes
Two rows of the same four positions, Top, Bottom, Left and Right, with Top pressed in each. The first row sets size to sm on the group and the second leaves it at the default md, and every item follows its group. Use the small row in a crowded toolbar, and the default where the group is the main control on the screen.
Spacing
The four positions again, small and outlined, with spacing set to 0, so the buttons join into one segmented bar: they share their borders, and only the two ends keep rounded corners. Any other number is the gap between items in Tailwind spacing steps, and 2, the default, is what the Sizes rows use. Join them for a single choice such as text alignment.
Vertical
Bold, italic and underline stacked in a column, with orientation set to vertical, a spacing of 1, and bold and italic pressed to start. Several can be on at once, the way text styles combine. Use a column for a narrow tool palette beside a canvas, where Up and Down move between the items.
Disabled
The same three formatting buttons with isDisabled on the group, so every item fades and ignores clicks. Use it while formatting can't apply, such as in a read-only document, so the toolbar keeps its layout. To retire one action only, disable that item and leave the rest working.
Font weight selector
Four tiles, Light, Normal, Medium and Bold, each showing Aa in its weight, with a line under them naming the Tailwind class for the pressed one. The group is controlled, with size set to lg, and its change handler falls back to Normal when the pressed tile is clicked again, so a weight is always chosen. Use tiles like these when seeing the option beats reading its name.
RTL
A view switcher, list, grid and cards, labeled in Arabic, with list pressed and dir set to rtl. The first item sits at the right end of the row, and the outlined buttons follow it. Set the direction on the group or a wrapper when the toolbar sits in a page that otherwise reads left to right.
FAQ
How do I allow one or several selections in a Toggle Group in React?
One pressed item at a time is the default, and pressing the pressed item again releases it. Set selectionMode to multiple to let several be pressed together, as the Vertical demo does for bold, italic and underline.
Use one at a time for choices that exclude each other, like alignment, and several for styles that combine.
How do I control the value of a Toggle Group?
Give each ToggleGroupItem an id, then pass selectedKeys and update it in onSelectionChange, which receives a Set of the pressed ids. Use defaultSelectedKeys instead when the group can keep its own state.
How do I keep at least one item pressed in a Toggle Group?
Add disallowEmptySelection to ToggleGroup, and pressing the last pressed item leaves it pressed.
The Font weight selector demo handles it in its change handler, falling back to Normal when the pressed tile is clicked. That suits a setting where some value must always apply, like a font weight or a view mode.
How do I join Toggle Group items into one segmented control?
Set spacing to 0 on ToggleGroup. With variant set to outline, the items then share their borders and read as one bar, the way an alignment control in a text editor does.
The Spacing demo shows the joined bar. Any other number is the gap between the items in Tailwind spacing steps, and 2 is the default. variant and size on the group apply to every item, so you don't repeat them.
Is the Toggle Group accessible with the keyboard?
Yes. The group is one Tab stop, the arrow keys along its orientation move between items and stop at the ends, and Space or Enter presses the focused one. React Aria has no Home or End shortcut here.
The Accessibility section lists each key. In a vertical group, use Up and Down. Left and Right do nothing there.
How do I label an icon-only Toggle Group?
Give every item an aria-label that names the action, such as Bold or Align left, and mark its icon aria-hidden="true". Give the group a name as well, with an aria-label such as Text formatting, so a screen reader says what the set of buttons is for.
Sighted people don't see any of that, so add a Tooltip to each item when the icons aren't standard. Bold and italic are, while a custom layout icon usually isn't.
Does a Toggle Group submit its value with a form?
No. React Aria renders the items as plain buttons with no form input behind them, so a form never sees which ones are pressed.
Keep the value in state and send it yourself, or render a hidden input whose value you update from the change callback. For a choice that belongs in a form, a Radio Group or a set of Checkbox fields submits natively and reads as a form question.
Should I use a Toggle Group or a Radio Group?
Use a toggle group for toolbar buttons that change what's on screen right away, like text alignment, formatting or a list and grid switch. Use a Radio Group for a choice in a form, where each option needs a visible label and the answer is submitted.
A single-choice toggle group is already announced as a set of radio buttons, so for screen reader users the difference is mostly where it sits and whether it submits.
API Reference
ToggleGroup
PropType
defaultSelectedKeys?Iterable<Key>
The initial selected keys in the collection (uncontrolled).
Type
Iterable<Key>
disallowEmptySelection?boolean
Whether the collection allows empty selection.
isDisabled?boolean
Whether all items are disabled.
onSelectionChange?(keys: Set<Key>) → void
Handler that is called when the selection changes.
Type
(keys: Set<Key>) → void
Parameters
keys – Set<Key>
Returns
void
orientation?"horizontal" | "vertical"
Horizontal or vertical: which arrow keys move between items.
Type
"horizontal" | "vertical"
Default
"horizontal"
selectedKeys?Iterable<Key>
The currently selected keys in the collection (controlled).
Type
Iterable<Key>
selectionMode?"single" | "multiple"
Whether single or multiple selection is enabled.
Type
"single" | "multiple"
Default
"single"
slot?string | null
A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit null value indicates that the local props completely override all
props received from a parent.
Type
string | null
spacing?number
The gap between items, in spacing units. At 0 they join into one bar.
Default
2
style?StyleOrFunction<T>
The inline [style](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) for the
element. A function may be provided to compute the style based on component state.
Type
StyleOrFunction<T>
render?DOMRenderFunction<E, T>
Overrides the default DOM element with a custom render function.
This allows rendering existing components with built-in styles and behaviors
such as router links, animation libraries, and pre-styled components.
Requirements:
- You must render the expected element type (e.g. if <button> is expected, you cannot render an
<a>).
- Only a single root DOM element can be rendered (no fragments).
- You must pass through props and ref to the underlying DOM element, merging with your own prop
as appropriate.
Type
DOMRenderFunction<E, T>
className?string
Merged with the part's classes.
AttributeType
data-disabled?string
Present when disabled is true.
data-focus?string
Present when focus.
data-orientationstring
Mirrors the orientation prop.
CSS variableType
--gapstring
ToggleGroupItem
data-slot="toggle-group-item"
PropType
children?ReactNode
The content to display in the button.
defaultSelected?boolean
Whether the element should be selected (uncontrolled).
isDisabled?boolean
Whether the button is disabled.
isSelected?boolean
Whether the element should be selected (controlled).
onChange?(isSelected: boolean) → void
Handler that is called when the element's selection state changes.