Skip to content

Button Group

Joins related buttons, inputs and menus into one connected control with shared edges.

Installation

pnpm dlx shadcn@latest add @silkware/button-group

To copy the component by hand, install its dependencies:

pnpm add class-variance-authority cn

Then copy the source into components/ui/button-group.tsx. It also needs Separator:

components/ui/button-group.tsx
import { cva, type VariantProps } from "class-variance-authority"import { cn } from "cn"
import { Separator } from "@/components/ui/separator"
const buttonGroupVariants = cva(  "flex w-fit items-stretch *:focus-visible:relative *:focus-visible:z-10 has-[>[data-slot=button-group]]:gap-2 [&>[data-slot=select-trigger]:has(+select[aria-hidden=true]:last-child)]:rounded-r-md [&>[data-slot=select-trigger]:not([class*='w-'])]:w-fit [&>input]:flex-1",  {    variants: {      orientation: {        horizontal:          "**:data-slot:rounded-r-none [&_[data-slot]~[data-slot]]:rounded-l-none [&_[data-slot]~[data-slot]]:border-l-0 [&>[data-slot]:not(:has(~[data-slot]))]:rounded-r-md!",        vertical:          "flex-col **:data-slot:rounded-b-none [&_[data-slot]~[data-slot]]:rounded-t-none [&_[data-slot]~[data-slot]]:border-t-0 [&>[data-slot]:not(:has(~[data-slot]))]:rounded-b-md!",      },    },    defaultVariants: {      orientation: "horizontal",    },  })
function ButtonGroup({  className,  orientation,  ...props}: React.ComponentProps<"div"> & VariantProps<typeof buttonGroupVariants>) {  return (    <div      role="group"      data-slot="button-group"      data-orientation={orientation}      className={cn(buttonGroupVariants({ orientation }), className)}      {...props}    />  )}
function ButtonGroupText({  className,  render,  ...props}: React.ComponentProps<"div"> & {  render?: (props: React.HTMLAttributes<HTMLElement>) => React.ReactNode}) {  if (render) {    const renderProps = {      "data-slot": "button-group-text",      className: cn(        "flex items-center gap-2 rounded-md border bg-muted px-2.5 text-sm font-medium shadow-xs [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4",        className      ),      ...props,    }
    return render(renderProps)  }
  return (    <div      data-slot="button-group-text"      className={cn(        "flex items-center gap-2 rounded-md border bg-muted px-2.5 text-sm font-medium shadow-xs [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4",        className      )}      {...props}    />  )}
function ButtonGroupSeparator({  className,  orientation = "vertical",  ...props}: React.ComponentProps<typeof Separator>) {  return (    <Separator      data-slot="button-group-separator"      orientation={orientation}      className={cn(        "relative self-stretch bg-input data-horizontal:mx-px data-horizontal:w-auto data-vertical:my-px data-vertical:h-auto",        className      )}      {...props}    />  )}
export {  ButtonGroup,  ButtonGroupSeparator,  ButtonGroupText,  buttonGroupVariants,}

Usage

import {  ButtonGroup,  ButtonGroupSeparator,  ButtonGroupText,} from "@/components/ui/button-group"
<ButtonGroup aria-label="Order actions">  <Button variant="outline">Approve</Button>  <Button variant="outline">Reject</Button></ButtonGroup>

ButtonGroup is a layout wrapper with role="group". It removes the inner radii and the inner border between the children so they read as one control. It does not hold any state: each button keeps its own press handler. To switch between options that stay selected, use a Toggle Group.

The component has no React Aria primitive behind it, so the page has no primitive link. It styles any element that sets a data-slot, which every component in this set does, including Button, Input and Select.

Examples

Orientation

orientation="vertical" stacks the children and rounds the first and last ones on the top and bottom.

Size

The group takes the size from its buttons: set size on each one. Keep it the same across the group.

Separator

Buttons with a fill, such as secondary, have no border between them. Add a ButtonGroupSeparator to draw a line. Outline buttons already have one.

Split button

A main action and a menu of variations of it: a button, a separator and a Dropdown Menu trigger. The icon button needs an aria-label because it has no text.

With input

An Input takes the remaining width of the group, so a search field and its button read as one control. Label the input with an aria-label or a visible label.

With text

ButtonGroupText shows a fixed prefix or suffix next to an input, such as a code or a unit. It is not interactive. Use its render prop to render another element in its place.

PO-

Nested groups

Place a ButtonGroup inside another to put a gap between sets of related buttons.

Accessibility

KeyAction
TabMoves focus to the next control in the group. Each button is a separate tab stop.
ShiftTabMoves focus to the previous control in the group.
SpacePresses the focused button.
EnterPresses the focused button.

ButtonGroup adds no keyboard behaviour. The keys are those of the buttons, inputs and menus inside it.

What the component provides:

  • role="group", which tells assistive technology that the controls belong together.
  • A focused child that rises above its neighbours, so its focus ring is not clipped by the shared edge.

You provide:

  • An aria-label or aria-labelledby on the group that names what the controls are for ("Order actions").
  • A visible label or an aria-label on every button and input inside it, and an aria-label on icon-only buttons.
  • Separate controls that are actually related. A group makes them look like one thing.

Guidelines

When to use

  • For two to four actions on the same object, such as approve and reject.
  • For a split button: a main action with a menu of variations.
  • For an input joined to its action, such as a search field and its button.
  • For a fixed prefix or suffix next to an input.

When not to use

  • For options that stay selected. Use a Toggle Group or Tabs.
  • For an input with icons or buttons inside it. Use an Input Group.
  • For navigation between pages. Use Pagination.
  • For unrelated actions, or more than about four. Space them apart, or group the rest in a Dropdown Menu.

Content

  • Start every button label with a verb and name the object, as in Button.
  • Keep labels about the same length so the group looks even.
  • Use one variant and one size inside a group.
Do

Group actions that are two answers to the same question.

Don’t

Don’t join unrelated actions. A group suggests they belong together, and a destructive action beside safe ones invites a mistake.

API reference

ButtonGroup

Renders a <div role="group"> that joins its children. It accepts every prop of a div.

PropTypeDefaultDescription
orientation?"horizontal" | "vertical""horizontal"The direction of the group. Vertical stacks the children.
aria-label?stringNoneThe accessible name of the group.
className?stringNoneClasses merged after the group classes. Set a width here to size an input inside it.

ButtonGroupSeparator

A Separator styled for a group. It is vertical by default, to sit between buttons in a row, and accepts the props of the React Aria Separator.

PropTypeDefaultDescription
orientation?"horizontal" | "vertical""vertical"The direction of the line. Use horizontal in a vertical group.

ButtonGroupText

A bordered, muted block of text that fits into the group. It renders a <div>.

PropTypeDefaultDescription
render?(props: HTMLAttributes<HTMLElement>) => ReactNodeNoneRenders a custom element with the text styles, for example a label.
className?stringNoneClasses merged after the text classes.

buttonGroupVariants

The class function behind ButtonGroup, from class-variance-authority: buttonGroupVariants({ orientation: "vertical" }).