Prototyper UI

Toggle Group

group of toggles with single or multiple selection

Loading...

Installation

pnpm dlx shadcn@latest add https://prototyper-ui.com/r/toggle-group.json
npx shadcn@latest add https://prototyper-ui.com/r/toggle-group.json
yarn dlx shadcn@latest add https://prototyper-ui.com/r/toggle-group.json
bunx --bun shadcn@latest add https://prototyper-ui.com/r/toggle-group.json

This will add the following files to your project:

  • components/ui/toggle-group.tsx

Usage

import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"

<ToggleGroup defaultValue={["center"]} aria-label="Text alignment">
  <ToggleGroupItem value="left" aria-label="Align left">Left</ToggleGroupItem>
  <ToggleGroupItem value="center" aria-label="Align center">Center</ToggleGroupItem>
  <ToggleGroupItem value="right" aria-label="Align right">Right</ToggleGroupItem>
</ToggleGroup>

Anatomy

<ToggleGroup>
  <ToggleGroupItem />
  <ToggleGroupItem />
  <ToggleGroupItem />
</ToggleGroup>
Sub-componentdata-slotPurposeRequired
ToggleGrouptoggle-groupRoot container, manages selection stateYes
ToggleGroupItemtoggle-group-itemIndividual toggle button within the groupYes

Examples

Outline

Loading...

Multiple

Loading...

Vertical

Loading...

Small

Loading...

Large

Loading...

Disabled

Loading...

Styling

Data Slots

Use data-slot attributes to target specific parts of the toggle group:

Slot nameElement
toggle-groupRoot container for all toggle items
toggle-group-itemIndividual toggle button

Customization Examples

/* Style all pressed items in a toggle group */
[data-slot="toggle-group-item"][data-state="on"] {
  @apply bg-primary text-primary-foreground;
}

/* Add spacing between items */
[data-slot="toggle-group"] {
  @apply gap-1;
}
{/* Override styles via className */}
<ToggleGroup className="rounded-lg border bg-background p-1">
  <ToggleGroupItem value="a">A</ToggleGroupItem>
  <ToggleGroupItem value="b">B</ToggleGroupItem>
</ToggleGroup>

API Reference

ToggleGroup

Root container that manages which toggle items are pressed.

Prop

Type

All Base UI ToggleGroup props are forwarded via ...props.

ToggleGroupItem

An individual toggle button within the group.

Prop

Type

All Base UI Toggle props are forwarded via ...props.

Accessibility

Keyboard Interactions

KeyAction
SpaceToggles the pressed state of the focused item
EnterToggles the pressed state of the focused item
TabMoves focus into / out of the toggle group
ArrowRightMoves focus to the next item (horizontal orientation)
ArrowLeftMoves focus to the previous item (horizontal orientation)
ArrowDownMoves focus to the next item (vertical orientation)
ArrowUpMoves focus to the previous item (vertical orientation)
HomeMoves focus to the first item
EndMoves focus to the last item

ARIA Attributes

  • ToggleGroup renders as a group with aria-label describing the group purpose.
  • Each ToggleGroupItem renders as a <button> with aria-pressed attribute.
  • aria-pressed="true" when the item is in the pressed state, aria-pressed="false" when unpressed.
  • aria-disabled is set when the group or an individual item is disabled.
  • aria-orientation is set to match the orientation prop.
  • Focus management follows the roving tabindex pattern so only one item is tabbable at a time.

On this page