Toggle Group
A set of toggle buttons that share one value, such as the text alignment buttons of an editor. By default one item is pressed at a time; with multiple, each item presses on its own. The group is one tab stop: arrow keys move focus between its items, and Enter or Space presses one. It renders role="group"; name it with aria-label or aria-labelledby. A group that is a whole toolbar, of three controls or more, passes role="toolbar", and then also gets aria-orientation when vertical.
- Size
- 1.9 kB
Example
import { ToggleGroup } from "menhir/toggle-group";
const item =
"cursor-pointer rounded-md border border-stone-300 px-3 py-1 text-sm focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 data-pressed:border-transparent data-pressed:bg-stone-800 data-pressed:text-white dark:border-stone-700 dark:data-pressed:bg-stone-300 dark:data-pressed:text-stone-900";
export default function ToggleGroupExample() {
return (
<ToggleGroup.Root
aria-label="Text alignment"
defaultValue={["left"]}
class="inline-flex gap-1"
>
<ToggleGroup.Item value="left" class={item}>
Left
</ToggleGroup.Item>
<ToggleGroup.Item value="center" class={item}>
Center
</ToggleGroup.Item>
<ToggleGroup.Item value="right" class={item}>
Right
</ToggleGroup.Item>
</ToggleGroup.Root>
);
}
Anatomy
<ToggleGroup.Root>
<ToggleGroup.Item />
</ToggleGroup.Root>API reference
Root
<div role="group">, with data-horizontal or data-vertical, and data-disabled.
| Prop | Type | Default | Description |
|---|---|---|---|
value | string[] | — | The values of the pressed items, when controlled. Pair it with onValueChange. Nothing pressed is []. |
defaultValue | string[] | [] | The values of the items pressed at first, when uncontrolled. |
onValueChange | (value: string[]) => void | — | Called with the new values when the user presses or releases an item. A controlled group changes only if value does. |
multiple | boolean | false | Whether several items can be pressed at once. Otherwise pressing an item releases the one pressed before. |
disabled | boolean | false | Whether every item ignores the user. They cannot be focused either. |
orientation | "horizontal" | "vertical" | "horizontal" | Whether the items sit side by side, moved between with Left and Right, or stacked, moved between with Up and Down. |
render | (props) => JSX.Element | — | Renders the group with your own component. Read once. |
Item
<button type="button" aria-pressed>, with data-pressed or data-unpressed, and data-disabled. Name it with its content, or with aria-label when it shows only an icon. Its render function must render a <button>.
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | — | Identifies the item in the group's value. Required. |
disabled | boolean | false | Whether the item ignores the user. Arrow keys skip it. |
render | (props) => JSX.Element | — | Renders the item with your own button component. Read once. |
Keyboard
| Keys | Action |
|---|---|
| Tab | Moves focus into the group, to the item that last had focus or else the first, or out of it. |
| ArrowRight | Moves focus to the next item, or the first from the last. Horizontal groups; the previous item in right-to-left text. |
| ArrowLeft | Moves focus to the previous item, or the last from the first. Horizontal groups; the next item in right-to-left text. |
| ArrowDown | Moves focus to the next item, or the first from the last. Vertical groups. |
| ArrowUp | Moves focus to the previous item, or the last from the first. Vertical groups. |
| Home | Moves focus to the first item. |
| End | Moves focus to the last item. |
| Space | Presses or releases the focused item. |
| Enter | Presses or releases the focused item. |