Toggle Group
A set of toggles that selects one option, or several.
Installation
npx shadcn@latest add @oakoss/toggle-groupUsage
import {
ToggleGroup,
ToggleGroupItem,
} from '#/components/ui/inputs/toggle-group';
<ToggleGroup aria-label="Alignment">
<ToggleGroupItem id="start">Start</ToggleGroupItem>
<ToggleGroupItem id="center">Center</ToggleGroupItem>
<ToggleGroupItem id="end">End</ToggleGroupItem>
</ToggleGroup>;Examples
Multiple
selectionMode="multiple" lets any number of items be on, as in a text
formatting toolbar.
Joined
joined sets the items edge to edge, sharing their borders. Here
disallowEmptySelection keeps one view always chosen.
Accessibility
React Aria renders single selection, the default, as a radiogroup of
radio items, and multiple selection as a toolbar of buttons with
aria-pressed. Name the group with aria-label or aria-labelledby.
- Focus: the group is one Tab stop. Arrow keys move focus between items without changing the selection, and stop at the ends.
- Deselecting: in single selection, pressing the selected item clears it,
which a radio group normally can't. Set
disallowEmptySelectionto keep one selected. - Names: an item can't see its group's size, so an icon-only item's name
isn't checked in the type. Give each one an
aria-label. - Target size: items sit 8px apart, closer than their 44px targets would allow, so the targets are off inside a group.
- Disabled:
isDisableddisables every item and marks the grouparia-disabled.
| Key | Action |
|---|---|
Tab | Moves focus into or out of the group |
ArrowLeft / ArrowRight | Moves focus between items when horizontal; reversed right to left |
ArrowUp / ArrowDown | Moves focus between items when vertical |
Space / Enter | Selects or toggles the focused item |
Follows the WAI-ARIA radio group and toolbar patterns.
API
ToggleGroup
Extends React Aria's
ToggleButtonGroup.
Props
Prop
Type
Also accepts all
React Aria ToggleButtonGroup props.
Data attributes
| Attribute | Present when |
|---|---|
data-slot | toggle-group, unless you pass your own |
data-orientation | Always; horizontal or vertical |
data-size | Always; the items' default size |
data-variant | Always; the items' default look |
data-joined | joined is set |
data-disabled | isDisabled is set |
ToggleGroupItem
Extends React Aria's
ToggleButton, styled as a
Toggle.
Props
Prop
Type
Also accepts all
React Aria ToggleButton props.
id is required: it's the key the group selects, and an item without one
isn't part of the group's selection.
Data attributes
Toggle's, with data-slot set to toggle-group-item.