# Split button

One glass body with two hit areas: the main action and a chevron for its options, split by a hairline, each half with its own hover and press.

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

## Install

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

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

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

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

## Preview

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

Knobs:

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

Signature: One glass body, two hit areas: a hairline splits the action from its chevron, and each half lights and presses on its own.

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/split-button.tsx, lively https://pixilui.com/r/standalone/split-button.lively.tsx.

## Code

`components/pixilui/split-button.tsx`

```tsx
"use client";

import { forwardRef, type ComponentPropsWithoutRef, type MouseEvent, type ReactNode } from "react";
import { ButtonSplit, type ButtonSize } from "./button";

/*
  One glass body, two hit areas: the action and its menu, with a hairline between. The look is
  the locked split button (foundation.css section 7); this item names it and gives it a page.
*/

export type SplitButtonProps = Omit<ComponentPropsWithoutRef<"div">, "children" | "onClick"> & {
  /** primary for the main action of a surface (Deploy), secondary for a picker (a branch). */
  variant?: "primary" | "secondary";
  /** Height, as on Button: xs 24, sm 28, md 32 (default), lg 36, xl 44. */
  size?: ButtonSize;
  /** The main action's label. */
  children: ReactNode;
  /** Leading icon of the main action. */
  icon?: ReactNode;
  /** The menu half's icon, usually a chevron. */
  menuIcon: ReactNode;
  /** The menu half's accessible name ("Deploy options"). */
  menuLabel: string;
  /** Runs the main action. */
  onAction?: (event: MouseEvent<HTMLButtonElement>) => void | Promise<void>;
  /** Opens the menu. Spread menuProps instead when a popover or menu trigger owns the button. */
  onMenu?: (event: MouseEvent<HTMLButtonElement>) => void | Promise<void>;
  /** Extra props for the menu half (a popover trigger's props, aria-expanded). */
  menuProps?: ComponentPropsWithoutRef<"button">;
};

/** An action with its options behind a chevron: Deploy and its targets, a branch and the switcher. Each half has its own hover and press. */
export const SplitButton = forwardRef<HTMLDivElement, SplitButtonProps>(function SplitButton({ onAction, onMenu, ...rest }, ref) {
  return (
    <ButtonSplit
      ref={ref}
      onAction={onAction ? (event) => void onAction(event) : undefined}
      onMenu={onMenu ? (event) => void onMenu(event) : undefined}
      {...rest}
    />
  );
});
```

## Usage

When to use:

- An action people repeat with a usual choice and a few others: Deploy to production, or to staging.
- A picker that also acts: the current branch, with the switcher behind the chevron.

When not to use:

- Options that are equal: use a menu button or a segmented control.
- A single action with no options: use Button.

```tsx
import type { ReactNode } from "react";
import { IconDisclosure } from "@/components/pixilui/icons/disclosure";
import { IconEval } from "@/components/pixilui/icons/eval";
import { IconExport } from "@/components/pixilui/icons/export";
import { IconSubagent } from "@/components/pixilui/icons/subagent";
import { SplitButton } from "@/components/pixilui/split-button";

// 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 chevron = <IconDisclosure data-stroke="heavy" />;

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>
          <SplitButton variant="primary" aria-label="Export" icon={<IconExport />} menuIcon={chevron} menuLabel="Export options">
            Export
          </SplitButton>
          <SplitButton variant="secondary" aria-label="Run eval" icon={<IconEval />} menuIcon={chevron} menuLabel="Choose a dataset">
            Run eval
          </SplitButton>
        </Row>
        <Cap>With a leading icon, in each style</Cap>
      </>
    );
  if (show === "sizes")
    return (
      <>
        <Row>
          {(["sm", "md", "lg", "xl"] as const).map((size) => (
            <SplitButton key={size} variant="primary" size={size} aria-label="Deploy" menuIcon={chevron} menuLabel="Deploy options">
              Deploy
            </SplitButton>
          ))}
        </Row>
        <Cap>28, 32 (default), 36, 44</Cap>
      </>
    );
  if (show === "states")
    return (
      <>
        <Row>
          <SplitButton variant="primary" aria-label="Deploy" menuIcon={chevron} menuLabel="Deploy options" menuProps={{ "aria-expanded": true, "aria-haspopup": "menu" }}>
            Deploy to production
          </SplitButton>
          <SplitButton variant="secondary" aria-label="Branch" icon={<IconSubagent />} menuIcon={chevron} menuLabel="Switch branch">
            feature/refund-retries
          </SplitButton>
        </Row>
        <Cap>Long labels keep the chevron in reach; the menu half takes a trigger&apos;s props</Cap>
      </>
    );
  return (
    <>
      <Row>
        <SplitButton variant="primary" size="lg" aria-label="Deploy" menuIcon={chevron} menuLabel="Deploy options">
          Deploy
        </SplitButton>
        <SplitButton variant="secondary" size="lg" aria-label="Branch" icon={<IconSubagent />} menuIcon={chevron} menuLabel="Switch branch">
          main
        </SplitButton>
      </Row>
      <Cap>Primary for the main action, the glass for a picker</Cap>
    </>
  );
}

export default function SplitButtonDemo({ 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 |
| --- | --- | --- | --- |
| `variant` | `"primary" \| "secondary"` |  | primary for the main action of a surface (Deploy), secondary for a picker (a branch). |
| `size` | `ButtonSize` |  | Height, as on Button: xs 24, sm 28, md 32 (default), lg 36, xl 44. |
| `children` (required) | `ReactNode` |  | The main action's label. |
| `icon` | `ReactNode` |  | Leading icon of the main action. |
| `menuIcon` (required) | `ReactNode` |  | The menu half's icon, usually a chevron. |
| `menuLabel` (required) | `string` |  | The menu half's accessible name ("Deploy options"). |
| `onAction` | `(event: MouseEvent<HTMLButtonElement>) => void \| Promise<void>` |  | Runs the main action. |
| `onMenu` | `(event: MouseEvent<HTMLButtonElement>) => void \| Promise<void>` |  | Opens the menu. Spread menuProps instead when a popover or menu trigger owns the button. |
| `menuProps` | `ComponentPropsWithoutRef<"button">` |  | Extra props for the menu half (a popover trigger's props, aria-expanded). |

Also accepts every native `<div>` attribute except `onClick`, and a ref to it.

## Keyboard

- `Tab`: Moves focus to the action, then to the menu half.
- `Enter`: Runs the focused half: the action, or opening the menu.
- `Space`: Runs the focused half on release.

## Accessibility

- A group of two native buttons; name the group with aria-label ("Deploy").
- The menu half needs menuLabel ("Deploy options"); its icon is aria-hidden.
- When a popover or menu owns the menu half, pass its trigger props through menuProps so aria-expanded and aria-controls land on that button.

## Motion

- Each half presses on its own: scale .96 in 150ms ease-out, with the glass setting in.
- Hover in 150ms lights only the half under the pointer.
- Reduced motion: no press scale; colour changes stay.

## Responsive

- Sizes to its label and never grows past its container. Both halves keep a 40px hit area on desktop and 44 on touch.

## Performance

- The look is CSS in the foundation; the component renders two buttons and a divider.

## Notes for AI

- variant="primary" for the main action of a surface (Deploy), variant="secondary" for a picker (a branch). Size as on Button.
- onAction runs the main action. The menu half takes onMenu, or menuProps when a popover or menu trigger owns it: spread the trigger's props into menuProps.
- Pass icons as props (icon, menuIcon); add data-stroke="heavy" to the chevron.
- The props are ButtonSplit's from button.tsx; SplitButton is the same part with its own page.

## 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.
- [Button group](https://pixilui.com/components/button-group/markdown): 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.
- [Segmented control](https://pixilui.com/components/segmented-control/markdown): Two to five mutually exclusive choices on one glass track, with a thumb that glides to the one you pick and jumps at once when you arrow through them.
