ToggleGroup
A set of toggle buttons that read as one control: text alignment, a view switcher, text styles. ToggleGroup.Root has role="group", and each ToggleGroup.Item is a secondary Button.Root with aria-pressed. The group takes one stop in the Tab order, on the pressed item in single mode and on the first item in multiple mode, and the arrow keys move focus between items without pressing them.
On this page
In the default single mode, one item is pressed at most, and pressing it again clears the value to null. Pass required and that press leaves the item pressed. onValueChange is not called.
import { ToggleGroup } from '@tounsoo/todae';
import { useState } from 'react';
export default function ToggleGroupAlign() {
const [align, setAlign] = useState<string | null>('left');
return (
<ToggleGroup.Root aria-label="Alignment" value={align} onValueChange={setAlign}>
<ToggleGroup.Item value="left">Left</ToggleGroup.Item>
<ToggleGroup.Item value="center">Center</ToggleGroup.Item>
<ToggleGroup.Item value="right">Right</ToggleGroup.Item>
</ToggleGroup.Root>
);
}
import { ToggleGroup } from '@tounsoo/todae';
import { useState } from 'react';
export default function ToggleGroupRequired() {
const [align, setAlign] = useState('left');
return (
<ToggleGroup.Root
aria-label="Alignment"
required
value={align}
onValueChange={(next) => {
if (next !== null) setAlign(next);
}}
>
<ToggleGroup.Item value="left">Left</ToggleGroup.Item>
<ToggleGroup.Item value="center">Center</ToggleGroup.Item>
<ToggleGroup.Item value="right">Right</ToggleGroup.Item>
</ToggleGroup.Root>
);
}
With selectionMode="multiple", any number can be pressed, and the value is an array.
import { ToggleGroup } from '@tounsoo/todae';
export default function ToggleGroupStyles() {
return (
<ToggleGroup.Root aria-label="Text style" selectionMode="multiple" defaultValue={['bold']}>
<ToggleGroup.Item value="bold">Bold</ToggleGroup.Item>
<ToggleGroup.Item value="italic">Italic</ToggleGroup.Item>
<ToggleGroup.Item value="underline">Underline</ToggleGroup.Item>
</ToggleGroup.Root>
);
}
Name the group with aria-label or aria-labelledby. Horizontal items join like Button.Group and wrap onto another line when the row runs out of room. Each wrapped line keeps the joined ends of a single row. Its borders stay on the group's edge, and they do not cover the line above. Inside a Toolbar, the toolbar owns the arrow keys and the tab stop.
import { ToggleGroup } from '@tounsoo/todae';
const DAYS = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] as const;
export default function ToggleGroupWrap() {
return (
<div style={{ maxInlineSize: '16rem' }}>
<ToggleGroup.Root aria-label="Days" defaultValue="Monday">
{DAYS.map((day) => (
<ToggleGroup.Item key={day} value={day}>
{day}
</ToggleGroup.Item>
))}
</ToggleGroup.Root>
</div>
);
}
Segmented look
A horizontal group of secondary items, the default, can be restyled as a segmented control with tokens alone, with no prop. togglegroup.bg, togglegroup.border, togglegroup.border-width, togglegroup.padding, togglegroup.radius and togglegroup.gap draw the track. A segment inherits button.* except where it differs. togglegroup.item.radius and togglegroup.item.padding.y size it, togglegroup.item.bg.default, hover and active color the resting segment, and togglegroup.item.border.default colors its border. togglegroup.item.bg.pressed, fg.pressed, border.pressed and shadow.pressed color the pressed one. togglegroup.item.pressed-ring-width is the inset ring. Focus keeps the resting fill and uses Button's focus ring. togglegroup.item.border.default follows the secondary button border, so an unpressed segment keeps that border until a theme sets the token. By default the track is empty and the items look like secondary buttons joined in a row that wraps when it runs out of room. The track and these colors apply only to secondary items, so a row of ghost icon toggles stays bare. A vertical group does not draw the track. It stacks with toolbar.gap. The Playground's Anvil preset sets the segmented tokens.
Accessible composition
Todae handles: ToggleGroup.Root is a role="group". Each ToggleGroup.Item is a Button.Root whose aria-pressed comes from the group's value, with a native disabled when the item or the group is disabled. On its own, the group keeps one tab stop and moves focus with the arrow keys; inside a Toolbar, it leaves both to the toolbar. A pressed item keeps an inset ring as well as its colors.
You supply: A name for the group with aria-label or aria-labelledby; the type does not require one. A name for each item: text children, or aria-label on an icon-only item, which Button.Root also shows as its tooltip. Labels that stay the same when pressed: aria-pressed carries the state, so "Bold" stays "Bold".
Watch for: role="radio" on items in single mode: they are toggle buttons with aria-pressed, and the group's role is fixed. Restyling the pressed state as a fill change only, which leaves it on color alone. Wrapping each item in an element that drops the props it is given, such as aria-label.
When to use
When: Options that change how something shows, where the pressed state is visible at a glance and changes take effect at once.
When not: A choice in a form that is submitted → Radio or Checkbox. One setting on or off → Switch, or a single toggle Button.Root. Switching between panels of content → Tabs.
Props
| Name | Type | Description |
|---|---|---|
aria-label | string | Names the group, such as "Alignment". |
selectionMode | "single" | "multiple" | Single presses one item at most. Pressing the pressed item clears it, unless required. Multiple presses any number. Default: "single" |
required | boolean | In single mode, pressing the pressed item keeps it pressed and does not call onValueChange. Default: false |
value | string | null | readonly string[] | Controlled pressed value: a value or null for single, an array for multiple. |
defaultValue | string | null | readonly string[] | Starting pressed value when uncontrolled. |
onValueChange | ((value: string | null) => void)| ((value: string[]) => void) | Called with the new pressed value when an item is pressed. |
orientation | "horizontal" | "vertical" | Layout, and the arrow keys that move focus outside a toolbar. Horizontal items join like Button.Group. Default: "horizontal", or the toolbar's |
disabled | boolean | Disables every item. Default: false |
children | React.ReactNode | ToggleGroup.Item elements. |
| Name | Type | Description |
|---|---|---|
value Required | string | Identifies the item in the group value. |
disabled | boolean | Disables this item. Default: false |
children | React.ReactNode | Label, or a Button.Icon with aria-label. |