Actions
Button Group
Joined buttons sharing one border — view switches, ranges and formatting toolbars. Three sizes.
react-aria-components
Preview
"use client";
import { AlignCenter, AlignLeft, AlignRight, CalendarDays, LayoutGrid, List } from "lucide-react";
import { ButtonGroup } from "@/components/ui/button-group";
export default function Example() {
return (
<div className="flex flex-col items-center gap-4">
<ButtonGroup
aria-label="Range"
defaultValue={["30d"]}
items={[
{ id: "today", label: "Today" },
{ id: "7d", label: "7 days" },
{ id: "30d", label: "30 days" },
{ id: "custom", label: "Custom", icon: <CalendarDays /> },
]}
/>
<ButtonGroup
aria-label="View"
size="xs"
defaultValue={["list"]}
items={[
{ id: "list", icon: <List />, label: "List" },
{ id: "board", icon: <LayoutGrid />, label: "Board" },
]}
/>
<ButtonGroup
aria-label="Alignment"
size="2xs"
defaultValue={["left"]}
items={[
{ id: "left", icon: <AlignLeft />, "aria-label": "Align left" },
{ id: "center", icon: <AlignCenter />, "aria-label": "Align center" },
{ id: "right", icon: <AlignRight />, "aria-label": "Align right" },
]}
/>
</div>
);
}Installation
Components are copied into your project — no runtime package.
Install the following dependencies:
terminalnpm install react-aria-componentsCreate a
button-group.tsxfile and paste the following code into it.src/components/ui/button-group.tsx"use client"; import type { ReactNode } from "react"; import { type Key, ToggleButton, ToggleButtonGroup } from "react-aria-components"; import { cx } from "@/utils/cx"; export interface ButtonGroupItem { id: string; label?: ReactNode; icon?: ReactNode; "aria-label"?: string; } export interface ButtonGroupProps { items: ButtonGroupItem[]; /** Selected ids. Omit `selectionMode` for plain action buttons. */ value?: string[]; defaultValue?: string[]; onChange?: (ids: string[]) => void; selectionMode?: "single" | "multiple"; size?: "2xs" | "xs" | "sm"; "aria-label": string; className?: string; } const sizes = { "2xs": "h-6 px-2 text-sm", xs: "h-8 px-2.5 text-base", sm: "h-9 px-3 text-base" }; /** Joined buttons sharing one border — for view switches, toolbars and formatting. */ export function ButtonGroup({ items, value, defaultValue, onChange, selectionMode = "single", size = "sm", className, ...props }: ButtonGroupProps) { return ( <ToggleButtonGroup aria-label={props["aria-label"]} selectionMode={selectionMode} selectedKeys={value} defaultSelectedKeys={defaultValue} onSelectionChange={(keys: Set<Key>) => onChange?.([...keys].map(String))} className={cx("inline-flex divide-x divide-line overflow-hidden rounded-md bg-surface shadow-button", className)} > {items.map((it) => ( <ToggleButton key={it.id} id={it.id} aria-label={it["aria-label"]} className={cx( "inline-flex cursor-default items-center gap-1.5 font-medium text-fg-secondary outline-none transition-colors [&_svg]:size-4", "data-hovered:bg-subtle data-hovered:text-fg data-selected:bg-muted data-selected:text-fg data-focus-visible:relative data-focus-visible:z-10 data-focus-visible:shadow-[inset_0_0_0_2px_var(--color-accent)]", sizes[size], )} > {it.icon} {it.label} </ToggleButton> ))} </ToggleButtonGroup> ); }Add the theme tokens and the
cxhelper from Installation if you haven’t yet, then update the import paths to match your project setup.