Button group
Buttons joined into one control, with separators and text slots.
For actions that belong together, such as the buttons of one toolbar. For a choice between options use @khaeli/toggle-group or @khaeli/segmented-toggle-group. A group inside a group sits 8px from its neighbours.
Joined buttons
import { Button } from "@/components/ui/button"
import { ButtonGroup } from "@/components/ui/button-group"
export default function ButtonGroupJoinedButtons() {
return (
<ButtonGroup aria-label="Task actions">
<Button variant="outline">Archive</Button>
<Button variant="outline">Snooze</Button>
<Button variant="outline">Move</Button>
</ButtonGroup>
)
}
Vertical
import { MinusIcon, PlusIcon } from "@keyline-icons/react"
import { Button } from "@/components/ui/button"
import { ButtonGroup } from "@/components/ui/button-group"
export default function ButtonGroupVertical() {
return (
<ButtonGroup orientation="vertical" aria-label="Zoom">
<Button variant="outline" size="icon" aria-label="Zoom in">
<PlusIcon />
</Button>
<Button variant="outline" size="icon" aria-label="Zoom out">
<MinusIcon />
</Button>
</ButtonGroup>
)
}
With a separator
import { ChevronDownIcon } from "@keyline-icons/react"
import { Button } from "@/components/ui/button"
import { ButtonGroup, ButtonGroupSeparator } from "@/components/ui/button-group"
export default function ButtonGroupWithASeparator() {
return (
<ButtonGroup aria-label="Merge">
<Button variant="secondary">Merge release</Button>
<ButtonGroupSeparator />
<Button variant="secondary" size="icon" aria-label="More ways to merge">
<ChevronDownIcon />
</Button>
</ButtonGroup>
)
}
With text
import { ChevronLeftIcon, ChevronRightIcon } from "@keyline-icons/react"
import { Button } from "@/components/ui/button"
import { ButtonGroup, ButtonGroupText } from "@/components/ui/button-group"
export default function ButtonGroupWithText() {
return (
<ButtonGroup aria-label="Invoice pages">
<Button variant="outline" size="icon" aria-label="Previous page">
<ChevronLeftIcon />
</Button>
<ButtonGroupText>Page 2 of 8</ButtonGroupText>
<Button variant="outline" size="icon" aria-label="Next page">
<ChevronRightIcon />
</Button>
</ButtonGroup>
)
}
With an input
import { Button } from "@/components/ui/button"
import { ButtonGroup, ButtonGroupText } from "@/components/ui/button-group"
import { Input } from "@/components/ui/input"
export default function ButtonGroupWithAnInput() {
return (
<ButtonGroup aria-label="Invite a teammate" className="w-full max-w-sm">
<ButtonGroupText render={<label htmlFor="invite-email" />}>Invite</ButtonGroupText>
<Input id="invite-email" type="email" placeholder="name@example.com" />
<Button variant="outline">Send</Button>
</ButtonGroup>
)
}
Nested groups
import { ArchiveIcon, RedoIcon, UndoIcon } from "@keyline-icons/react"
import { Button } from "@/components/ui/button"
import { ButtonGroup } from "@/components/ui/button-group"
export default function ButtonGroupNestedGroups() {
return (
<ButtonGroup aria-label="Editor toolbar">
<ButtonGroup aria-label="History">
<Button variant="outline" size="icon" aria-label="Undo">
<UndoIcon />
</Button>
<Button variant="outline" size="icon" aria-label="Redo">
<RedoIcon />
</Button>
</ButtonGroup>
<ButtonGroup aria-label="Draft">
<Button variant="outline">
<ArchiveIcon data-icon="inline-start" />
Archive
</Button>
<Button variant="outline">Publish</Button>
</ButtonGroup>
</ButtonGroup>
)
}
Installation
npx shadcn@latest add @khaeli/button-groupbunx --bun shadcn@latest add @khaeli/button-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 to the next button. Every button is its own tab stop: the group adds no arrow-key movement.
EnterSpace- Activates the focused button.
Accessible name
- The group is a
role="group"with no name of its own. Give it anaria-labelthat says what its buttons share, such as Task actions. - An icon-only button in the group needs its own
aria-label. ButtonGroupTextis plain text. Rendered as a<label>withrenderandhtmlFor, it names the input beside it.ButtonGroupSeparatoris arole="separator"and needs no name.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/button-group.json