COMPONENT · 05NEW

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

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.

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.

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.

Vertical

orientation="vertical" stacks the items.

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.

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.json

Usage

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

PropTypeDefaultDescription
ButtonGroup selection"single" | "multiple"Required. single switches between options with a sliding indicator; multiple toggles several items at once.
ButtonGroup aria-labelstringRequired. Names the group for screen readers and agents.
ButtonGroup valuestring | null (single), string[] (multiple)The chosen items, when controlled. Pair it with onValueChange. null or [] means nothing chosen, which lights ButtonGroupAll.
ButtonGroup defaultValuestring | null (single), string[] (multiple)null / []The chosen items at first, when uncontrolled.
ButtonGroup onValueChange(value) => voidRuns 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 disabledbooleanfalseDisables every item.
ButtonGroup namestringsingle only. Submits the chosen value with a form. With ButtonGroupAll, nothing chosen submits nothing.
ButtonGroup requiredbooleanfalsesingle only. Blocks form submission until an item is chosen. With ButtonGroupAll it has no effect: All is a valid choice.
ButtonGroupItem valuestringRequired. What the group's value holds when this item is chosen.
ButtonGroupItem iconReactNodeRenders an icon-only item. Name it with tooltip or aria-label.
ButtonGroupItem startIcon / endIconReactNodeIcons beside the label. An item with an icon has no LED.
ButtonGroupItem tooltipReactNodeWraps 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 disabledbooleanfalseKeeps the item from being chosen.
ButtonGroupAllButtonGroupItem props, without valueOptional, in both modes. Lit while nothing is chosen; choosing it clears the group.