# Split Button

> SplitButton joins a primary Button and a menu trigger in one group, with the Menu Button keyboard model on the trigger and Alt+ArrowDown on the primary part.

Canonical: https://salmonui.dev/docs/buttons/split-button

```tsx
import { SplitButton, MenuItem } from '@salmonui/buttons/menu';
import '@salmonui/buttons/button/styles.css';
import '@salmonui/buttons/menu/styles.css';

<SplitButton
  onClick={save}
  onAction={(event) => (event.id === 'draft' ? saveDraft() : saveAs())}
  menu={
    <>
      <MenuItem id="draft">Save as draft</MenuItem>
      <MenuItem id="as">Save as…</MenuItem>
    </>
  }
>
  Save
</SplitButton>
```

`SplitButton` renders an attached `ButtonGroup` (`role="group"`,
`.salmon-button-group.salmon-split-button`) of two `Button`s: the primary part with the `children`, `iconStart` and `iconEnd`,
and an icon-only trigger part with a chevron. The
primary part takes `onClick` and the other DOM props; `ref` is the
primary `<button>`. `className` and `style` reach the group; `classNames` and
`styles` take the parts `primary`, `trigger` and `menu`.

Everything about the menu is the same as on `MenuButton`: `menu`
or `items`, `renderItem`, `open` / `defaultOpen` / `onOpenChange`, `onAction`,
`placement`, `container`, `submenuOpen`, `hoverDelay`, and the item
components, including groups. The menu is uncontrolled by default;
pass `open` with `onOpenChange` to control it. Like `MenuButton`, a closed
split button renders no menu markup, on the server or the client, and
hydrates without warnings.

`tone`, `variant` and `size` apply to both parts, and `disabled`
disables both. The trigger part carries `data-open` while the menu
is open.

## Keyboard and accessibility

- Two tab stops: the primary part, then the trigger.
- On the trigger: Enter, Space and ArrowDown open the menu on the first item,
  ArrowUp on the last. Alt+ArrowDown on the primary part opens the menu too.
- In the menu: the Menu keys (ArrowUp / ArrowDown, Home / End, typeahead,
  Enter, Escape, Alt+ArrowUp, Tab); focus returns to the trigger.
- The group is named by the primary part's content. The primary button keeps
  its own name, and only the trigger has `aria-haspopup="menu"`,
  `aria-expanded` and `aria-controls`. The trigger's name is
  `menuLabel`, `"More options"` by default.

## In a button group

`MenuButton` and `SplitButton` can be children of a `ButtonGroup`.
They take the group's `variant`, `tone`, `size` and `disabled`
unless they set their own, and an attached group joins their borders with
their neighbours. In a vertical group a `SplitButton` stays one horizontal
row that spans the group's width:

```tsx
<ButtonGroup aria-label="Clipboard" orientation="vertical">
  <Button>Copy</Button>
  <SplitButton items={pasteOptions}>Paste</SplitButton>
</ButtonGroup>
```

### Live demo: Button groups with menus and RTL

Live demo: https://salmonui.dev/demos/buttons/button-group-menus

A MenuButton and a SplitButton inside a ButtonGroup, a SplitButton in a vertical group, and a toggle group and a button group inside a right-to-left container.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { Button, ButtonGroup, ToggleButton, ToggleButtonGroup } from '@salmonui/buttons/button';
import { MenuButton, MenuItem, SplitButton } from '@salmonui/buttons/menu';

const shareItems = [
  { id: 'link', label: 'Copy link' },
  { id: 'email', label: 'Email' },
];

// MenuButton and SplitButton as ButtonGroup children, a SplitButton in a
// vertical group, and the same group under dir="rtl".
export default function ButtonGroupMenusDemo() {
  const [last, setLast] = useState('nothing yet');
  const onAction = (event: { id: string }) => setLast(event.id);
  return (
    <div style={{ display: 'grid', gap: 16 }}>
      <div style={{ display: 'flex', flexWrap: 'wrap', alignItems: 'flex-start', gap: 16 }}>
        <ButtonGroup aria-label="Document" variant="outline">
          <Button onClick={() => setLast('edit')}>Edit</Button>
          <MenuButton items={shareItems} onAction={onAction}>
            Share
          </MenuButton>
          <SplitButton
            onClick={() => setLast('print')}
            onAction={onAction}
            menu={
              <>
                <MenuItem id="print-preview">Print preview</MenuItem>
                <MenuItem id="page-setup">Page setup</MenuItem>
              </>
            }
          >
            Print
          </SplitButton>
        </ButtonGroup>
        <ButtonGroup aria-label="Release" orientation="vertical" tone="primary">
          <SplitButton onClick={() => setLast('publish')} onAction={onAction} items={[{ id: 'schedule', label: 'Schedule…' }, { id: 'publish-draft', label: 'Publish as draft' }]}>
            Publish
          </SplitButton>
          <Button onClick={() => setLast('preview')}>Preview</Button>
          <Button onClick={() => setLast('discard')}>Discard</Button>
        </ButtonGroup>
      </div>
      <div dir="rtl" lang="ar" style={{ display: 'flex', flexWrap: 'wrap', gap: 16 }}>
        <ToggleButtonGroup aria-label="المحاذاة" defaultValue="start" onValueChange={(value) => setLast(`align ${value ?? 'none'}`)}>
          <ToggleButton value="start">بداية</ToggleButton>
          <ToggleButton value="center">وسط</ToggleButton>
          <ToggleButton value="end">نهاية</ToggleButton>
        </ToggleButtonGroup>
        <ButtonGroup aria-label="تنسيق" variant="outline">
          <Button onClick={() => setLast('bold')}>عريض</Button>
          <Button onClick={() => setLast('italic')}>مائل</Button>
          <MenuButton items={[{ id: 'size-small', label: 'صغير' }, { id: 'size-large', label: 'كبير' }]} onAction={onAction}>
            الحجم
          </MenuButton>
        </ButtonGroup>
      </div>
      <p>
        Last action: <output>{last}</output>
      </p>
    </div>
  );
}
```

An icon-above-text layout is not offered: compose the content
instead.

### Live demo: Split Button

Live demo: https://salmonui.dev/demos/buttons/split-button

A primary action with a grouped menu of alternatives, a named menu trigger and a styled popup part, and a disabled SplitButton.

`Demo.tsx`:

```tsx
'use client';
import { useState } from 'react';
import { MenuGroup, MenuItem, MenuSeparator, SplitButton } from '@salmonui/buttons/menu';

export default function SplitButtonDemo() {
  const [status, setStatus] = useState('Not saved');
  return (
    <div>
      <div className="demo-toolbar">
        <SplitButton
          tone="primary"
          menuLabel="More save options"
          classNames={{ menu: 'demo-split-menu' }}
          styles={{ menu: { minInlineSize: 200 } }}
          onClick={() => setStatus('Saved')}
          onAction={(event) => setStatus(`Chose ${event.id}`)}
          menu={
            <>
              <MenuGroup label="Save">
                <MenuItem id="draft">Save as draft</MenuItem>
                <MenuItem id="copy">Save a copy</MenuItem>
              </MenuGroup>
              <MenuSeparator />
              <MenuGroup label="Export">
                <MenuItem id="pdf">Export as PDF</MenuItem>
                <MenuItem id="docx">Export as DOCX</MenuItem>
              </MenuGroup>
            </>
          }
        >
          Save
        </SplitButton>
        <SplitButton variant="outline" disabled items={[{ id: 'x', label: 'Unavailable' }]}>
          Disabled
        </SplitButton>
      </div>
      <p>
        <output>{status}</output>
      </p>
    </div>
  );
}
```

## API reference

[`SplitButton`](https://salmonui.dev/docs/reference/buttons/menu/SplitButton) and its
`SplitButtonProps` / `SplitButtonPart` types are exported from
[`@salmonui/buttons/menu`](https://salmonui.dev/docs/reference/buttons/menu); the menu props are
documented on [Menu Button and Menu](https://salmonui.dev/docs/buttons/menu-button).
