Search every page in the navigation.
A set of two-state buttons that can be toggled on or off.
import { IconBold, IconItalic, IconUnderline } from "@tabler/icons-react"
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
function ToggleGroupDemo() {
return (
<ToggleGroup variant="outline" multiple>
<ToggleGroupItem value="bold" aria-label="Toggle bold">
<IconBold />
</ToggleGroupItem>
<ToggleGroupItem value="italic" aria-label="Toggle italic">
<IconItalic />
</ToggleGroupItem>
<ToggleGroupItem value="strikethrough" aria-label="Toggle strikethrough">
<IconUnderline />
</ToggleGroupItem>
</ToggleGroup>
)
}
export default ToggleGroupDemonpx shadcn@latest add @ziiz/toggle-groupimport { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"<ToggleGroup type="single">
<ToggleGroupItem value="a">A</ToggleGroupItem>
<ToggleGroupItem value="b">B</ToggleGroupItem>
<ToggleGroupItem value="c">C</ToggleGroupItem>
</ToggleGroup>Use the following composition to build a ToggleGroup:
ToggleGroup
โโโ ToggleGroupItem
โโโ ToggleGroupItemUse variant="outline" for an outline style.
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
function ToggleGroupOutline() {
return (
<ToggleGroup variant="outline" defaultValue={["all"]}>
<ToggleGroupItem value="all" aria-label="Toggle all">
All
</ToggleGroupItem>
<ToggleGroupItem value="missed" aria-label="Toggle missed">
Missed
</ToggleGroupItem>
</ToggleGroup>
)
}
export default ToggleGroupOutlineUse the size prop to change the size of the toggle group.
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
function ToggleGroupSizes() {
return (
<div className="flex flex-col gap-4">
<ToggleGroup size="sm" defaultValue={["top"]} variant="outline">
<ToggleGroupItem value="top" aria-label="Toggle top">
Top
</ToggleGroupItem>
<ToggleGroupItem value="bottom" aria-label="Toggle bottom">
Bottom
</ToggleGroupItem>
<ToggleGroupItem value="left" aria-label="Toggle left">
Left
</ToggleGroupItem>
<ToggleGroupItem value="right" aria-label="Toggle right">
Right
</ToggleGroupItem>
</ToggleGroup>
<ToggleGroup defaultValue={["top"]} variant="outline">
<ToggleGroupItem value="top" aria-label="Toggle top">
Top
</ToggleGroupItem>
<ToggleGroupItem value="bottom" aria-label="Toggle bottom">
Bottom
</ToggleGroupItem>
<ToggleGroupItem value="left" aria-label="Toggle left">
Left
</ToggleGroupItem>
<ToggleGroupItem value="right" aria-label="Toggle right">
Right
</ToggleGroupItem>
</ToggleGroup>
</div>
)
}
export default ToggleGroupSizesUse spacing to add spacing between toggle group items.
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
function ToggleGroupSpacing() {
return (
<ToggleGroup size="sm" defaultValue={["top"]} variant="outline" spacing={2}>
<ToggleGroupItem value="top" aria-label="Toggle top">
Top
</ToggleGroupItem>
<ToggleGroupItem value="bottom" aria-label="Toggle bottom">
Bottom
</ToggleGroupItem>
<ToggleGroupItem value="left" aria-label="Toggle left">
Left
</ToggleGroupItem>
<ToggleGroupItem value="right" aria-label="Toggle right">
Right
</ToggleGroupItem>
</ToggleGroup>
)
}
export default ToggleGroupSpacingUse shape="rounded" on the group to make every item a pill. With spacing={0} the group squares the seams between items and leaves the two ends round, so the group reads as one pill.
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
function ToggleGroupRounded() {
return (
<div className="flex flex-col items-center gap-4">
<ToggleGroup shape="rounded" defaultValue={["all"]}>
<ToggleGroupItem value="all">All</ToggleGroupItem>
<ToggleGroupItem value="unread">Unread</ToggleGroupItem>
<ToggleGroupItem value="mentions">Mentions</ToggleGroupItem>
</ToggleGroup>
<ToggleGroup
variant="outline"
shape="rounded"
spacing={0}
defaultValue={["day"]}
>
<ToggleGroupItem value="day">Day</ToggleGroupItem>
<ToggleGroupItem value="week">Week</ToggleGroupItem>
<ToggleGroupItem value="month">Month</ToggleGroupItem>
</ToggleGroup>
</div>
)
}
export default ToggleGroupRoundedUse orientation="vertical" for vertical toggle groups.
import { IconBold, IconItalic, IconUnderline } from "@tabler/icons-react"
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
function ToggleGroupVertical() {
return (
<ToggleGroup
multiple
orientation="vertical"
spacing={1}
defaultValue={["bold", "italic"]}
>
<ToggleGroupItem value="bold" aria-label="Toggle bold">
<IconBold />
</ToggleGroupItem>
<ToggleGroupItem value="italic" aria-label="Toggle italic">
<IconItalic />
</ToggleGroupItem>
<ToggleGroupItem value="underline" aria-label="Toggle underline">
<IconUnderline />
</ToggleGroupItem>
</ToggleGroup>
)
}
export default ToggleGroupVerticalimport { IconBold, IconItalic, IconUnderline } from "@tabler/icons-react"
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
function ToggleGroupDisabled() {
return (
<ToggleGroup disabled>
<ToggleGroupItem value="bold" aria-label="Toggle bold">
<IconBold />
</ToggleGroupItem>
<ToggleGroupItem value="italic" aria-label="Toggle italic">
<IconItalic />
</ToggleGroupItem>
<ToggleGroupItem value="strikethrough" aria-label="Toggle strikethrough">
<IconUnderline />
</ToggleGroupItem>
</ToggleGroup>
)
}
export default ToggleGroupDisabledA custom toggle group example.
Use font-normal to set the font weight.
"use client"
import * as React from "react"
import { Field, FieldDescription, FieldLabel } from "@/components/ui/field"
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
function ToggleGroupFontWeightSelector() {
const [fontWeight, setFontWeight] = React.useState("normal")
return (
<Field>
<FieldLabel>Font Weight</FieldLabel>
<ToggleGroup
value={[fontWeight]}
onValueChange={(value) => setFontWeight(value[0])}
variant="outline"
spacing={2}
size="lg"
>
<ToggleGroupItem
value="light"
aria-label="Light"
className="flex size-16 flex-col items-center justify-center rounded-xl"
>
<span className="text-2xl leading-none font-light">Aa</span>
<span className="text-xs text-gray-900">Light</span>
</ToggleGroupItem>
<ToggleGroupItem
value="normal"
aria-label="Normal"
className="flex size-16 flex-col items-center justify-center rounded-xl"
>
<span className="text-2xl leading-none font-normal">Aa</span>
<span className="text-xs text-gray-900">Normal</span>
</ToggleGroupItem>
<ToggleGroupItem
value="medium"
aria-label="Medium"
className="flex size-16 flex-col items-center justify-center rounded-xl"
>
<span className="text-2xl leading-none font-medium">Aa</span>
<span className="text-xs text-gray-900">Medium</span>
</ToggleGroupItem>
<ToggleGroupItem
value="bold"
aria-label="Bold"
className="flex size-16 flex-col items-center justify-center rounded-xl"
>
<span className="text-2xl leading-none font-bold">Aa</span>
<span className="text-xs text-gray-900">Bold</span>
</ToggleGroupItem>
</ToggleGroup>
<FieldDescription>
Use{" "}
<code className="rounded-md bg-gray-100 px-1 py-0.5 font-mono">
font-{fontWeight}
</code>{" "}
to set the font weight.
</FieldDescription>
</Field>
)
}
export default ToggleGroupFontWeightSelectorvariant, size and shape set on the group apply to every item.
| Prop | Type | Default |
|---|---|---|
variant | "default" | "outline" | "default" |
size | "default" | "sm" | "lg" | "default" |
shape | "default" | "rounded" | "default" |
spacing | number | 2 |
orientation | "horizontal" | "vertical" | "horizontal" |
See the Base UI Toggle Group documentation for the rest.
Changed the default spacing from 0 to 2 so toggle groups render with space between items by default. Use spacing={0} for connected items.