Button group
Joins buttons that belong together into one shape: undo and redo, a split button, a row of formatting toggles. It only draws the joins; each button still acts on its own. For a choice where one option is selected, use the toggle group or the segmented control.
Install
npx shadcn@latest add https://thrumui.dev/r/button-group.jsonAdds components/ui/button-group.tsx. Nothing else in your project changes.
import { ButtonGroup } from "@/components/ui/button-group";Props
| Prop | Type | Default | Description |
|---|---|---|---|
ButtonGroup: orientation | "horizontal" | "vertical" | Default "horizontal" | The direction the buttons run in: a row or a column. |
ButtonGroup: aria-label | string | – | The name of the group, read before its buttons. Or point aria-labelledby at a visible heading. |
ButtonGroup: children | ReactNode | – | The buttons, as direct children. Each child must be one element: a Button, an IconButton, a Toggle, a link with the button classes. |
ButtonGroup: className | string | – | Extra classes, merged last. w-full with [&>*]:flex-1 shares the width between the buttons. |
ButtonGroup: ...props | ComponentProps<"div"> | – | Every other attribute is passed to the group element. |
ButtonGroupSeparator | ComponentProps<"span"> | – | A 1 pixel line in the page colour, for the seam between two filled buttons. Change its colour with a class such as bg-border. |
Motion and sound
Motion
- The group itself does not move. Each button keeps its own hover and press transitions.
Sound
Silent. This component makes no sound.
Accessibility
- The element has the group role. Name it with an aria-label or aria-labelledby, so a screen reader says what the buttons have in common.
- It adds no keyboard behaviour: the buttons stay in the tab order one by one. A set that should be one tab stop with arrow keys is a toggle group or a toolbar.
- The focused button is drawn above its neighbours, so its focus ring is never cut by the next button.
- Outline buttons share a border line. Filled buttons have none, so put a ButtonGroupSeparator between them: the split must not rest on a colour change.
- In a container narrower than the row, the buttons shrink and clip their labels. Keep labels short, or set the group vertical.
| Keys | Action |
|---|---|
| Tab | Move from button to button. Every button in the group is its own tab stop. |
| Enter / Space | Press the focused button. |
Source
"use client";
import type { ComponentProps } from "react";
import { cn } from "@/lib/utils";
export type ButtonGroupProps = ComponentProps<"div"> & {
/** The direction the buttons run in. */
orientation?: "horizontal" | "vertical";
};
/**
* Joins related buttons into one shape: the outer corners stay round, the inner ones turn square, and neighbouring borders share one line.
* It styles its direct children, so buttons, icon buttons and toggles all work, and so does anything else that is a single element.
* Name it with an `aria-label`: it has the group role. Each button keeps its own tab stop.
*/
export function ButtonGroup({ orientation = "horizontal", className, ...props }: ButtonGroupProps) {
return (
<div
data-slot="button-group"
data-orientation={orientation}
role="group"
className={cn(
// Buttons give way in a narrow container instead of pushing out of it: a label is cut at the button's padding.
// The focused button comes to the front, so its ring is whole.
"group/button-group inline-flex max-w-full [&>*]:min-w-0 [&>*]:shrink [&>*]:overflow-hidden [&>*]:rounded-none [&>*:focus-visible]:z-10 [&>*>span]:min-w-0 [&>*>span]:overflow-hidden",
orientation === "horizontal"
? "[&>*:first-child]:rounded-s-md [&>*:last-child]:rounded-e-md [&>*:not(:first-child)]:-ms-px"
: "flex-col [&>*:first-child]:rounded-t-md [&>*:last-child]:rounded-b-md [&>*:not(:first-child)]:-mt-px",
className,
)}
{...props}
/>
);
}
/**
* A one pixel line between two filled buttons, which have no border of their own to share: the split in a split button.
* It is drawn over the seam in the page colour, so it adds no width. Outline buttons do not need it.
*/
export function ButtonGroupSeparator({ className, ...props }: ComponentProps<"span">) {
return (
<span
data-slot="button-group-separator"
aria-hidden="true"
className={cn("relative z-[1] w-px self-stretch bg-background group-data-[orientation=vertical]/button-group:h-px group-data-[orientation=vertical]/button-group:w-auto", className)}
{...props}
/>
);
}Files and dependencies
- Adds
components/ui/button-group.tsx- Packages
- None
- Brings along
- Nothing
Changelog
- 1.0.0 · 2026-12-03First release.