Button Group
A tray of buttons that lights up the chosen ones. Switch between a few options, with a background that slides to the new choice, or turn several filters on and off at once.
- @ui-sdk/react
Example
Showing #4189, #4190, #4191, #4192, #4193
"use client"
import { useState } from "react"
import { CircleCheckIcon, CircleXIcon, ClockIcon } from "lucide-react"
import { ButtonGroup, ButtonGroupAll, ButtonGroupItem } from "@/components/ui/button-group"
const orders = [
{ id: 4189, status: "pending" },
{ id: 4190, status: "paid" },
{ id: 4191, status: "paid" },
{ id: 4192, status: "cancelled" },
{ id: 4193, status: "pending" },
]
const count = (status: string) => orders.filter((order) => order.status === status).length
export function ButtonGroupFiltersDemo() {
const [status, setStatus] = useState<string[]>([])
const shown = status.length === 0 ? orders : orders.filter((order) => status.includes(order.status))
return (
<div className="flex w-full flex-col items-center gap-4">
<ButtonGroup selection="multiple" aria-label="Order status" value={status} onValueChange={setStatus}>
<ButtonGroupAll>All · {orders.length}</ButtonGroupAll>
<ButtonGroupItem value="pending" startIcon={<ClockIcon />}>Pending · {count("pending")}</ButtonGroupItem>
<ButtonGroupItem value="paid" startIcon={<CircleCheckIcon />}>Paid · {count("paid")}</ButtonGroupItem>
<ButtonGroupItem value="cancelled" startIcon={<CircleXIcon />}>Cancelled · {count("cancelled")}</ButtonGroupItem>
</ButtonGroup>
<p className="text-sm text-muted-foreground">
Showing {shown.map((order) => `#${order.id}`).join(", ")}
</p>
</div>
)
}Press Pending and Paid to show both; press All to clear them. Each lit item gets a solid fill and its LED turns on. Move the pointer along the tray: one hover fill slides from item to item and stays on the last one until the pointer leaves the tray.
Filters
selection="multiple" lets several items stay pressed, and its value is an array of their values. Add ButtonGroupAll to give the user a way back to "no filter": it is lit while the value is empty, and pressing it clears the value. Leave it out and an empty value just lights nothing. Counters and other extras go in the item's children.
Switching
selection="single" keeps one item checked, like radios: the arrow keys move the choice, and a background slides to the new item and fades into its fill. Its value is a string, or null when nothing is checked, which checks ButtonGroupAll if there is one. Set name to submit it with a form; with ButtonGroupAll, nothing chosen submits nothing and required has no effect.
import { KanbanIcon, LayoutGridIcon, ListIcon } from "lucide-react"
import { ButtonGroup, ButtonGroupItem } from "@/components/ui/button-group"
export function ButtonGroupSwitchDemo() {
return (
<ButtonGroup selection="single" aria-label="View" defaultValue="list">
<ButtonGroupItem value="list" startIcon={<ListIcon />}>List</ButtonGroupItem>
<ButtonGroupItem value="grid" startIcon={<LayoutGridIcon />}>Grid</ButtonGroupItem>
<ButtonGroupItem value="board" startIcon={<KanbanIcon />}>Board</ButtonGroupItem>
</ButtonGroup>
)
}Icons and tooltips
icon renders an icon-only item; name it with tooltip (or aria-label). startIcon and endIcon sit beside a label. The LED shows only on text-only items: an icon already marks the item. Items are the registry Button, so their tooltip is the Button's.
import { AlignCenterIcon, AlignLeftIcon, AlignRightIcon, BoldIcon, ItalicIcon, UnderlineIcon } from "lucide-react"
import { ButtonGroup, ButtonGroupItem } from "@/components/ui/button-group"
export function ButtonGroupIconsDemo() {
return (
<div className="flex flex-wrap items-center justify-center gap-4">
<ButtonGroup selection="single" aria-label="Alignment" defaultValue="left">
<ButtonGroupItem value="left" icon={<AlignLeftIcon />} tooltip="Align left" />
<ButtonGroupItem value="center" icon={<AlignCenterIcon />} tooltip="Align center" />
<ButtonGroupItem value="right" icon={<AlignRightIcon />} tooltip="Align right" />
</ButtonGroup>
<ButtonGroup selection="multiple" aria-label="Formatting" color="info" defaultValue={["bold"]}>
<ButtonGroupItem value="bold" icon={<BoldIcon />} tooltip="Bold" />
<ButtonGroupItem value="italic" icon={<ItalicIcon />} tooltip="Italic" />
<ButtonGroupItem value="underline" icon={<UnderlineIcon />} tooltip="Underline" />
</ButtonGroup>
</div>
)
}Sizes
size scales the tray and everything in it: padding, text, icons and LEDs. "xs" fits dense toolbars and table headers, "md" is the default, and "lg" suits a main view switch.
import { ButtonGroup, ButtonGroupItem } from "@/components/ui/button-group"
export function ButtonGroupSizesDemo() {
return (
<div className="flex flex-col items-start gap-4">
<ButtonGroup selection="single" aria-label="Range, extra small" size="xs" defaultValue="week">
<ButtonGroupItem value="day">Day</ButtonGroupItem>
<ButtonGroupItem value="week">Week</ButtonGroupItem>
<ButtonGroupItem value="month">Month</ButtonGroupItem>
</ButtonGroup>
<ButtonGroup selection="single" aria-label="Range, medium" size="md" defaultValue="week">
<ButtonGroupItem value="day">Day</ButtonGroupItem>
<ButtonGroupItem value="week">Week</ButtonGroupItem>
<ButtonGroupItem value="month">Month</ButtonGroupItem>
</ButtonGroup>
<ButtonGroup selection="single" aria-label="Range, large" size="lg" defaultValue="week">
<ButtonGroupItem value="day">Day</ButtonGroupItem>
<ButtonGroupItem value="week">Week</ButtonGroupItem>
<ButtonGroupItem value="month">Month</ButtonGroupItem>
</ButtonGroup>
</div>
)
}Vertical
orientation="vertical" stacks the items.
import { ButtonGroup, ButtonGroupAll, ButtonGroupItem } from "@/components/ui/button-group"
export function ButtonGroupVerticalDemo() {
return (
<ButtonGroup selection="single" aria-label="Warehouse" orientation="vertical" defaultValue={null}>
<ButtonGroupAll>All warehouses</ButtonGroupAll>
<ButtonGroupItem value="north">North</ButtonGroupItem>
<ButtonGroupItem value="south">South</ButtonGroupItem>
<ButtonGroupItem value="east">East</ButtonGroupItem>
</ButtonGroup>
)
}States
disabled works on one item or the whole group. A disabled item still shows its tooltip on hover, so it can say why. In multiple the arrow keys still reach it; in single the keyboard skips it, like a disabled radio.
import { ButtonGroup, ButtonGroupItem } from "@/components/ui/button-group"
export function ButtonGroupStatesDemo() {
return (
<div className="flex flex-col items-center gap-4">
<ButtonGroup selection="single" aria-label="Billing period" defaultValue="monthly">
<ButtonGroupItem value="monthly">Monthly</ButtonGroupItem>
<ButtonGroupItem value="yearly">Yearly</ButtonGroupItem>
<ButtonGroupItem value="lifetime" disabled tooltip="Coming soon">Lifetime</ButtonGroupItem>
</ButtonGroup>
<ButtonGroup selection="single" aria-label="Theme" defaultValue="system" disabled>
<ButtonGroupItem value="light">Light</ButtonGroupItem>
<ButtonGroupItem value="dark">Dark</ButtonGroupItem>
<ButtonGroupItem value="system">System</ButtonGroupItem>
</ButtonGroup>
</div>
)
}Agents
The group is ready for ui-sdk agents (createAgent from @ui-sdk/react/agent); find it by its aria-label. In single, use the group's setValue action. In multiple, use the group's toggle action with the item's value, not the item's label: each item is both ui-sdk's toggle and the registry Button, so its label names two entries in the snapshot and pressing it by label is ambiguous.
await agent.act({
steps: [
{ ref: "View", action: "setValue", args: ["grid"] },
{ ref: "Status", action: "toggle", args: ["paid"] },
],
})Installation
npx shadcn@latest add https://lanzate-ui.vercel.app/r/button-group.jsonUsage
import { ButtonGroup, ButtonGroupAll, ButtonGroupItem } from "@/components/ui/button-group"
<ButtonGroup selection="multiple" aria-label="Status" value={status} onValueChange={setStatus}>
<ButtonGroupAll>All</ButtonGroupAll>
<ButtonGroupItem value="pending">Pending</ButtonGroupItem>
<ButtonGroupItem value="paid">Paid</ButtonGroupItem>
</ButtonGroup>API
| Prop | Type | Default | Description |
|---|---|---|---|
ButtonGroup selection | "single" | "multiple" | Required. single switches between options with a sliding indicator; multiple toggles several items at once. | |
ButtonGroup aria-label | string | Required. Names the group for screen readers and agents. | |
ButtonGroup value | string | null (single), string[] (multiple) | The chosen items, when controlled. Pair it with onValueChange. null or [] means nothing chosen, which lights ButtonGroupAll. | |
ButtonGroup defaultValue | string | null (single), string[] (multiple) | null / [] | The chosen items at first, when uncontrolled. |
ButtonGroup onValueChange | (value) => void | Runs when the user chooses an item, with the new value. ButtonGroupAll's internal value never appears in it. | |
ButtonGroup color | "primary" | "secondary" | "muted" | "info" | "warning" | "success" | "error" | "primary" | Color of the lit items and the indicator. |
ButtonGroup size | "xs" | "md" | "lg" | "md" | Scales the tray and its items: padding, text, icons and LEDs. |
ButtonGroup orientation | "horizontal" | "vertical" | "horizontal" | Lays the items out in a row or a column. |
ButtonGroup disabled | boolean | false | Disables every item. |
ButtonGroup name | string | single only. Submits the chosen value with a form. With ButtonGroupAll, nothing chosen submits nothing. | |
ButtonGroup required | boolean | false | single only. Blocks form submission until an item is chosen. With ButtonGroupAll it has no effect: All is a valid choice. |
ButtonGroupItem value | string | Required. What the group's value holds when this item is chosen. | |
ButtonGroupItem icon | ReactNode | Renders an icon-only item. Name it with tooltip or aria-label. | |
ButtonGroupItem startIcon / endIcon | ReactNode | Icons beside the label. An item with an icon has no LED. | |
ButtonGroupItem tooltip | ReactNode | Wraps the item in a Tooltip. A disabled item keeps its tooltip on hover; in multiple it also stays focusable with the arrow keys, in single the keyboard skips it. | |
ButtonGroupItem disabled | boolean | false | Keeps the item from being chosen. |
ButtonGroupAll | ButtonGroupItem props, without value | Optional, in both modes. Lit while nothing is chosen; choosing it clears the group. |