# Button group

Icon 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.

Tier: free. Type: component. Category: actions. Tags: keyboard, chat.

## Install

```bash
npx shadcn@latest add @pixilui/button-group
```

One-time setup: add the registry to `components.json`:

```json
{
  "registries": {
    "@pixilui": "https://pixilui.com/r/{name}.json"
  }
}
```

Files: `components/pixilui/button-group.tsx`.

## Preview

Live preview: https://pixilui.com/components/button-group

Knobs:

- `show`: `styles`, `more`, `sizes`, `states`

Signature: One glass tray of icon buttons whose tooltips wait once, then slide along the row with their key hints.

Styles: `quiet` (default), `lively`, set with `data-style` on <html>. The item reads the style through the foundation; it needs no prop. Standalone files: quiet https://pixilui.com/r/standalone/button-group.tsx, lively https://pixilui.com/r/standalone/button-group.lively.tsx.

## Code

`components/pixilui/button-group.tsx`

```tsx
"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>
  );
});
```

## Usage

When to use:

- A composer's tools: attach, mention, dictate, then send.
- An editor's or a canvas's small set of icon actions that belong together.

When not to use:

- 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.

```tsx
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>
  );
}
```

## Examples

None.

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `aria-label` (required) | `string` |  | What the row acts on, for screen readers ("Editor"). |
| `actions` (required) | `ButtonGroupAction[]` |  | The buttons, in order. Keep the primary last. |
| `size` | `ButtonSize` | `"sm"` | Height of every button: xs 24, sm 28 (default), md 32, lg 36, xl 44. |
| `labels` | `boolean` | `false` | Show each action's label as its text (an attached group: Preview, Diff, Logs) instead of icon-only. |

## Keyboard

- `Tab`: Moves focus into the row and on out of it.
- `ArrowLeft` + `ArrowRight`: Moves between the row's buttons, wrapping at the ends; mirrored in right to left.
- `Enter`: Presses the focused button.
- `Esc`: Closes the open tooltip and leaves focus where it is.

## 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.

## Responsive

- Sizes to its buttons; never wider than its container. Small buttons keep a 40px hit area on desktop and 44 on touch.

## Performance

- One tooltip provider for the row; each tooltip mounts its panel only while open.

## Notes for AI

- Pass actions as an array: { label, icon, tooltip?, shortcut?, variant?, onClick?, disabled? }. label is the accessible name; tooltip defaults to it.
- Keep one variant="primary" action, last (Send). The rest stay Secondary 2.
- Pass icons as nodes (Lucide at 1.5px); never import an icon library inside the component.
- Size defaults to sm (28); use md on roomier surfaces.

## Related

- [Button](https://pixilui.com/components/button/markdown): The PixilUI button in four main styles and four more, five sizes from 24 to 44, with icons, loading, disabled, a count and a shortcut.
- [Tooltip](https://pixilui.com/components/tooltip/markdown): A short text label for an icon button, with its keyboard shortcut, that waits 400ms and then opens neighbours instantly while you scan a toolbar.
- [Message actions](https://pixilui.com/components/message-actions/markdown): The row under an assistant's answer: copy, regenerate, good and bad feedback and more, as quiet icon buttons with tooltips, where copy draws a check and feedback fills its glyph.
