shadcn.io is an independent project built on shadcn/ui
Components
Free
Toggle Group for React
Toggle buttons grouped into one toolbar control for React on Base UI, 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.
Each item's value is what the group reports as pressed. variant and size set on the group apply to every item in it.
Accessibility
The group has the group role, and every item is a button with aria-pressed, whether one or several can be pressed. The group is one stop in the tab order, and the arrow keys follow orientation: Left and Right in a horizontal group, Up and Down in a vertical one, wrapping at the ends. 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, and from the last one back to the first.
Arrow Left
In a horizontal group, moves focus to the previous item, and from the first one to the last.
Arrow Down
In a vertical group, moves focus to the next item, and from the last one back to the first.
Arrow Up
In a vertical group, moves focus to the previous item, and from the first one to the last.
Home
Moves focus to the first item.
End
Moves focus to the last item.
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 disabled 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. Add multiple to ToggleGroup 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?
Pass value and update it in onValueChange, whose first argument is the new array. The value is an array even when only one item can be pressed, so a single group's choice is its first entry.
How do I keep at least one item pressed in a Toggle Group?
Base UI has no option for it, so handle it in onValueChange: when the new value is empty, keep the old one or fall back to a default.
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 wrap at the ends, Home and End jump to the first and last, and Space or Enter presses the focused one.
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. Base UI 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.
Base UI announces each item as a pressed or unpressed button, even in a single-choice group, so a screen reader doesn't present it as one question with one answer.
API Reference
ToggleGroup
<button>
PropType
orientation?"horizontal" | "vertical"
Horizontal or vertical: which arrow keys move between items.
Type
"horizontal" | "vertical"
Default
"horizontal"
spacing?number
The gap between items, in spacing units. At 0 they join into one bar.