Toggle Group
Compound toggle button group with keyboard navigation for single or multiple selection.
Installation
dgadd ships in @diffgazer/add on npm: pnpm add -D @diffgazer/add puts it on pnpm exec, and npx @diffgazer/add runs it without the install.
UI components require Tailwind CSS v4. Local copy mode imports src/styles/styles.css; package mode uses @diffgazer/ui CSS from the installed package.
Usage
Examples
Variants
With Counts
Multiple Selection
Sizes
Vertical
Disabled
API Reference
ToggleGroup
ToggleGroup.Item
Data attributes
Accessibility
Keyboard Navigation
Arrow keys move focus between toggle items with wrapping. Single mode follows focus; multiple mode and allow-deselect single mode use button semantics and toggle with Enter or Space.
| Key | Action |
|---|---|
| ArrowRight / ArrowDown | Moves focus to the next enabled item; wraps when wrap is true. |
| ArrowLeft / ArrowUp | Moves focus to the previous enabled item; wraps when wrap is true. |
| Home / End | Moves focus to the first or last enabled item. |
| Enter / Space | Toggles the focused item in multiple mode or allow-deselect single mode; native radio-style single mode changes selection during arrow navigation. |
With keyboard navigation
Notes
Requires @diffgazer/keys (package mode)
ToggleGroup's arrow-key navigation imports from the required @diffgazer/keys peer. Package consumers must install @diffgazer/keys with @diffgazer/ui; install both from npm (npm install @diffgazer/ui @diffgazer/keys), or take the component from the live registry instead (npx shadcn add https://r.b4r7.dev/r/ui/toggle-group.json). Importing @diffgazer/ui/components/toggle-group without keys fails at module load with an error naming the missing @diffgazer/keys package. Copy/dgadd consumers do not need the package — copy mode rewrites the keyboard hooks to local source.
Compound Component
ToggleGroup uses compound sub-components (ToggleGroup.Item) rather than a data array. Each item accepts children for the label and an optional count prop that renders as [label count].
Composition Contract
Use ToggleGroup.Item as an explicit child in the ToggleGroup JSX tree. Custom item UI belongs inside ToggleGroup.Item. Components that create items internally from an opaque wrapper are not part of the current public contract.
Keyboard Navigation
Arrow keys move roving focus with wrapping by default. In single mode, selection follows focus unless allowDeselect is enabled; in multiple mode, arrow keys move focus and Enter/Space toggles the focused item. Highlight state can be controlled externally via highlighted and onHighlightChange props. Use onNavigationBoundaryReached for composite focus handoff when wrap is false.
Role semantics
Single mode without allowDeselect renders as radiogroup/radio with aria-checked and allows at most one active choice. Initial selection is optional: defaultValue=null starts with no radio checked. Multiple mode and allow-deselect single mode render as group plus button-style items with aria-pressed because each item is independently toggleable.
Source
Install via CLI: pnpm exec dgadd add ui/toggle-group. Keyboard hooks are included as standalone copies. For the full experience, use --integration keys.
Highlighted source loads after this disclosure opens. Browse the source repository.