Toggle group
A group of toggles, joined or segmented, on Base UI ToggleGroup.
One item is pressed at a time unless the group is multiple; value and defaultValue are arrays either way. From 768px wide, foundation draws every horizontal group as one 30px segmented track, whatever its variant, size and spacing. Those three show below that width and in vertical groups.
Single selection
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
export default function ToggleGroupSingleSelection() {
return (
<ToggleGroup aria-label="Range" defaultValue={["week"]}>
<ToggleGroupItem value="day">Day</ToggleGroupItem>
<ToggleGroupItem value="week">Week</ToggleGroupItem>
<ToggleGroupItem value="month">Month</ToggleGroupItem>
</ToggleGroup>
)
}
Multiple selection
import { BoldIcon, ItalicIcon, UnderlineIcon } from "@keyline-icons/react"
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
export default function ToggleGroupMultipleSelection() {
return (
<ToggleGroup multiple aria-label="Text style" defaultValue={["bold", "underline"]}>
<ToggleGroupItem value="bold" aria-label="Bold">
<BoldIcon />
</ToggleGroupItem>
<ToggleGroupItem value="italic" aria-label="Italic">
<ItalicIcon />
</ToggleGroupItem>
<ToggleGroupItem value="underline" aria-label="Underline">
<UnderlineIcon />
</ToggleGroupItem>
</ToggleGroup>
)
}
Vertical
import { TextAlignCenterIcon, TextAlignEndIcon, TextAlignStartIcon } from "@keyline-icons/react"
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
export default function ToggleGroupVertical() {
return (
<ToggleGroup orientation="vertical" aria-label="Alignment" defaultValue={["start"]}>
<ToggleGroupItem value="start" aria-label="Align to the start">
<TextAlignStartIcon />
</ToggleGroupItem>
<ToggleGroupItem value="center" aria-label="Align to the centre">
<TextAlignCenterIcon />
</ToggleGroupItem>
<ToggleGroupItem value="end" aria-label="Align to the end">
<TextAlignEndIcon />
</ToggleGroupItem>
</ToggleGroup>
)
}
Outline and joined
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
export default function ToggleGroupOutlineAndJoined() {
return (
<div className="flex items-start gap-6">
<ToggleGroup orientation="vertical" variant="outline" aria-label="Invoice status" defaultValue={["open"]}>
<ToggleGroupItem value="open">Open</ToggleGroupItem>
<ToggleGroupItem value="paid">Paid</ToggleGroupItem>
<ToggleGroupItem value="overdue">Overdue</ToggleGroupItem>
</ToggleGroup>
<ToggleGroup
orientation="vertical"
variant="outline"
spacing={0}
aria-label="Invoice status, joined"
defaultValue={["open"]}
>
<ToggleGroupItem value="open">Open</ToggleGroupItem>
<ToggleGroupItem value="paid">Paid</ToggleGroupItem>
<ToggleGroupItem value="overdue">Overdue</ToggleGroupItem>
</ToggleGroup>
</div>
)
}
Sizes
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
const sizes = [
{ size: "sm", label: "Small" },
{ size: "default", label: "Default" },
{ size: "lg", label: "Large" },
] as const
export default function ToggleGroupSizes() {
return (
<div className="flex items-start gap-6">
{sizes.map(({ size, label }) => (
<ToggleGroup
key={size}
orientation="vertical"
variant="outline"
size={size}
aria-label={`${label} range`}
defaultValue={["week"]}
>
<ToggleGroupItem value="day">Day</ToggleGroupItem>
<ToggleGroupItem value="week">Week</ToggleGroupItem>
<ToggleGroupItem value="month">Month</ToggleGroupItem>
</ToggleGroup>
))}
</div>
)
}
Disabled
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
export default function ToggleGroupDisabled() {
return (
<div className="flex flex-col items-center gap-3">
<ToggleGroup aria-label="Range" defaultValue={["week"]}>
<ToggleGroupItem value="day">Day</ToggleGroupItem>
<ToggleGroupItem value="week">Week</ToggleGroupItem>
<ToggleGroupItem value="year" disabled>
Year
</ToggleGroupItem>
</ToggleGroup>
<ToggleGroup disabled aria-label="Range, locked" defaultValue={["week"]}>
<ToggleGroupItem value="day">Day</ToggleGroupItem>
<ToggleGroupItem value="week">Week</ToggleGroupItem>
<ToggleGroupItem value="year">Year</ToggleGroupItem>
</ToggleGroup>
</div>
)
}
Installation
npx shadcn@latest add @khaeli/toggle-groupbunx --bun shadcn@latest add @khaeli/toggle-groupInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- @base-ui/react ^1.7.0
- class-variance-authority ^0.7.1
Keyboard
Tab- Moves into the group, to its first item or to the item that last had focus, and out of it again. The group is one tab stop. A
disabledgroup leaves the tab order. ArrowRightArrowLeft- In a horizontal group, move focus to the next or previous item, from the last round to the first. A disabled item is passed over. Nothing is pressed until you press it.
ArrowDownArrowUp- Do the same in a group with
orientation="vertical". Each pair of arrows does nothing in the other orientation. HomeEnd- Move focus to the first or last item.
SpaceEnter- Press or release the focused item. In a single-selection group pressing an item releases the one before it, and pressing the pressed item leaves none.
Accessible name
- The group is a
role="group". Give it anaria-labelthat says what is being chosen, such as Range. - Each item's name is its text; an icon-only item needs an
aria-label. Whether it is pressed isaria-pressed, set by the item.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/toggle-group.json