Button group
FreeIcon buttons on one glass tray, each named by a tooltip with its key hint, with arrow keys to move along the row: the toolbar under a composer or an editor.
npx shadcn@latest add @pixilui/button-groupPreview
import type { ReactNode } from "react";
import { IconAttach } from "@/components/pixilui/icons/attach";
import { IconCode } from "@/components/pixilui/icons/code";
import { IconDictate } from "@/components/pixilui/icons/dictate";
import { IconMention } from "@/components/pixilui/icons/mention";
import { IconReveal } from "@/components/pixilui/icons/reveal";
import { IconSendStop } from "@/components/pixilui/icons/send-stop";
import { IconTrace } from "@/components/pixilui/icons/trace";
import { ButtonGroup, type ButtonGroupAction } from "@/components/pixilui/button-group";
// The item page's one row, Show: Styles, More styles, Icons and sizes, States (the button pages' layout).
export const knobs = {
show: ["styles", "more", "sizes", "states"],
} as const;
const editor: ButtonGroupAction[] = [
{ label: "Attach", tooltip: "Attach files", shortcut: "⌘U", icon: <IconAttach /> },
{ label: "Mention", tooltip: "Mention a file or tool", shortcut: "@", icon: <IconMention /> },
{ label: "Dictate", icon: <IconDictate /> },
{ label: "Send", shortcut: "↵", icon: <IconSendStop />, variant: "primary" },
];
const Row = ({ children }: { children: ReactNode }) => <div className="flex flex-wrap items-center justify-center gap-2.5">{children}</div>;
const Cap = ({ children }: { children: ReactNode }) => <p className="-mt-2 text-center text-caption text-muted">{children}</p>;
function Shown({ show }: { show: string }) {
if (show === "more")
return (
<>
<Row>
<ButtonGroup
aria-label="Run output"
labels
actions={[
{ label: "Preview", icon: <IconReveal /> },
{ label: "Trace", icon: <IconTrace /> },
{ label: "Code", icon: <IconCode /> },
]}
/>
</Row>
<Cap>An attached group with words and icons</Cap>
<Row>
<ButtonGroup aria-label="Range" labels actions={[{ label: "Today" }, { label: "7 days" }, { label: "30 days" }]} />
</Row>
<Cap>Words only</Cap>
</>
);
if (show === "sizes")
return (
<>
{(["sm", "md", "lg"] as const).map((size) => (
<Row key={size}>
<ButtonGroup aria-label="Editor" size={size} actions={editor} />
</Row>
))}
<Cap>28 (default), 32, 36</Cap>
</>
);
if (show === "states")
return (
<>
<Row>
<ButtonGroup
aria-label="Editor"
actions={[
{ label: "Attach", tooltip: "Attaching is off for this workspace", icon: <IconAttach />, disabled: true },
...editor.slice(1),
]}
/>
</Row>
<Cap>A disabled tool stays focusable and its tooltip says why</Cap>
</>
);
return (
<>
<Row>
<ButtonGroup aria-label="Editor" actions={editor} />
</Row>
<Cap>Tools in Secondary 2 on one glass tray, then the primary. Arrow keys move along it.</Cap>
</>
);
}
export default function ButtonGroupDemo({ show = "styles" }: KnobValues<typeof knobs>) {
return (
<div key={show} className="grid justify-items-center gap-4.5" aria-live="polite">
<Shown show={show} />
</div>
);
}When to use
Use it when
- A composer's tools: attach, mention, dictate, then send.
- An editor's or a canvas's small set of icon actions that belong together.
Use something else when
- Choosing one of several options: use the segmented control.
- Actions with words on them: use Buttons in a row with a gap.
- More than about six tools: group them, or move the rest into a menu.
Installation
npx shadcn@latest add @pixilui/button-groupAdds button-group.tsx with button.tsx and tooltip.tsx, and installs clsx, tailwind-merge and @base-ui/react. First PixilUI item in this app? Add the registry once, as in Installation.
"use client";
import { forwardRef, type MouseEvent, type ReactNode } from "react";
import { cn } from "./lib/utils";
import { Button, ButtonToolbar, type ButtonSize, type ButtonToolbarProps } from "./button";
import { Tooltip, TooltipProvider } from "./tooltip";
/*
Icon buttons on one glass tray, each named by a tooltip with its key. The tray and its arrow-key
movement are ButtonToolbar's (button.tsx); the tooltips share one provider, so after the first
waits, the next ones open as the pointer slides along the row.
*/
export type ButtonGroupAction = {
/** Accessible name of the button ("Attach"). */
label: string;
/** The icon. Required unless the group shows labels. */
icon?: ReactNode;
/** Tooltip text; the label when left out ("Attach files"). */
tooltip?: string;
/** Key hint shown in the tooltip ("⌘U"). */
shortcut?: string;
/** secondary-2 (the default) for tools, primary for the one action the row leads to (Send). */
variant?: "secondary-2" | "primary" | "secondary";
/** Runs when pressed. */
onClick?: (event: MouseEvent<HTMLButtonElement>) => void | Promise<void>;
/** Shows the button as unavailable; it stays focusable and ignores presses. */
disabled?: boolean;
};
export type ButtonGroupProps = Omit<ButtonToolbarProps, "children"> & {
/** The buttons, in order. Keep the primary last. */
actions: ButtonGroupAction[];
/** What the row acts on, for screen readers ("Editor"). */
"aria-label": string;
/** Height of every button: xs 24, sm 28 (default), md 32, lg 36, xl 44. */
size?: ButtonSize;
/** Show each action's label as its text (an attached group: Preview, Diff, Logs) instead of icon-only. */
labels?: boolean;
};
/** An editor or composer toolbar: icon buttons on one glass tray, each with a tooltip and its key; arrow keys move along the row. */
export const ButtonGroup = forwardRef<HTMLDivElement, ButtonGroupProps>(function ButtonGroup({ actions, size = "sm", labels = false, className, ...rest }, ref) {
return (
<TooltipProvider>
<ButtonToolbar ref={ref} className={cn("font-body", className)} data-labels={labels ? "" : undefined} {...rest}>
{actions.map((action) => {
const common = {
variant: action.variant ?? "secondary-2",
size,
icon: action.icon,
"aria-disabled": action.disabled || undefined,
onClick: action.onClick ? (event: MouseEvent<HTMLButtonElement>) => void action.onClick?.(event) : undefined,
} as const;
// Icon-only actions always name themselves in a tooltip; labelled ones only when given one.
if (labels && !action.tooltip && !action.shortcut) return <Button key={action.label} {...common}>{action.label}</Button>;
const button = labels ? <Button {...common}>{action.label}</Button> : <Button {...common} aria-label={action.label} icon={action.icon} />;
return (
<Tooltip key={action.label} label={action.tooltip ?? action.label} shortcut={action.shortcut}>
{button}
</Tooltip>
);
})}
</ButtonToolbar>
</TooltipProvider>
);
});
It uses Button and Tooltip: add them the same way. Then npm i clsx tailwind-merge @base-ui/react, and add the foundation once per app, as in Installation.
Usage
import { ButtonGroup } from "@/components/pixilui/button-group";
<ButtonGroup aria-label={aria-label} actions={actions} />Sizes to its buttons; never wider than its container. Small buttons keep a 40px hit area on desktop and 44 on touch.
API reference
aria-labelRequiredstring–What the row acts on, for screen readers ("Editor").actionsRequiredButtonGroupAction[]–The buttons, in order. Keep the primary last.sizeButtonSize"sm"Height of every button: xs 24, sm 28 (default), md 32, lg 36, xl 44.labelsbooleanfalseShow each action's label as its text (an attached group: Preview, Diff, Logs) instead of icon-only.Keyboard
Accessibility
- role="toolbar" with the aria-label you pass ("Editor").
- Every button is icon-only and named by its label; keep the tooltip text close to it.
- Tooltips open on keyboard focus too, so the key hint is reachable without a mouse.
- A disabled action uses aria-disabled, so it stays in the arrow-key path and keeps focus.
Motion
- The first tooltip waits 400ms; the next ones open at once while the pointer slides along the row.
- Each button presses at .96 in 150ms ease-out; press always wins over hover.
- Reduced motion: no press scale; tooltips fade only.