Split button
A main action and a menu of the others, in one control.
Demo
Usage
import { SplitButton, type SplitButtonItem } from '@tuttn/slant'
// The action is the children and onClick; the menu is Menu's items and onSelect.
const targets: SplitButtonItem[] = [
{ id: 'finder', label: 'Reveal in Finder' },
{ id: 'terminal', label: 'Open in Terminal' },
]
<SplitButton onClick={openInEditor} items={targets} onSelect={id => open(id)}>Open in Editor</SplitButton>
// Both halves share variant, tone and size. actionDisabled holds back only the
// action, which keeps its focus; the menu still works.
<SplitButton variant="primary" menuLabel="More merge options" items={mergeOptions} onSelect={setMethod}
actionDisabled={!ready} onClick={merge}>Merge</SplitButton>
// Parts instead of items, for rich content: Menu's own parts.
<SplitButton onClick={deploy} onSelect={run} menu={<>
<SplitButton.Item id="preview">Deploy a preview</SplitButton.Item>
<SplitButton.Sub label="Roll back to">
<SplitButton.Item id="v41">v41</SplitButton.Item>
<SplitButton.Item id="v40">v40</SplitButton.Item>
</SplitButton.Sub>
</>}>Deploy</SplitButton>
// The menu keeps its own open state; pass open and onOpenChange to control it.
<SplitButton open={open} onOpenChange={setOpen} items={targets} onSelect={run}>Open</SplitButton>Props
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | — | The action's content, which also names it: "Open in Editor", "Merge". |
onClick | (event: MouseEvent<HTMLButtonElement>) => void | — | The main action, run by a click, Enter or Space on the action half. |
items | SplitButtonItem[] | — | The other actions as data, as Menu takes them: `{ id, label, kind?, checked?, disabled?, group?, stickyGroup?, textValue?, shortcut?, keyshortcuts?, items? }` or `{ separator: true }`. |
menu | ReactNode | — | The other actions as parts, after any `items`: SplitButton.Item, SplitButton.CheckboxItem, SplitButton.RadioItem, SplitButton.Group, SplitButton.Sub and SplitButton.Separator (Menu's own parts). |
onSelect | (id: string) => void | — | Called with the chosen menu item's id. Checkbox items keep the menu open; the rest close it. |
variant | ButtonProps['variant'] | 'secondary', or the ButtonGroup's | Emphasis, shared by both halves. |
tone | Tone | 'accent' | Semantic colour, shared by both halves, as Button takes it. |
size | 'sm' | 'md' | 'lg' | 'md', or the ButtonGroup's | Height on the control scale, shared by both halves. |
disabled | boolean | false | Disables both halves: neither takes focus, and the menu closes. |
actionDisabled | boolean | false | Disables only the action, with aria-disabled, so it keeps its tab stop and Alt+ArrowDown still opens the menu, which stays usable. |
menuLabel | string | messages.moreActions ("More actions") | The menu button's accessible name, which also names the menu. |
open | boolean | — | Whether the menu is open. Pass it with onOpenChange to control it; leave it out and the menu keeps its own state. |
defaultOpen | boolean | false | Whether the menu starts open, when uncontrolled. |
onOpenChange | (open: boolean) => void | — | Called with the new state whenever the menu opens or closes. |
placement | MenuPlacement | 'bottom-end' | The side of the control the menu hangs from, and the edge it lines up with: `bottom-end` lines its end edge up with the menu button's. Start and end follow the text direction, and it flips when this side lacks room. |
SplitButton.Item, .CheckboxItem, .RadioItem, .Group, .Sub, .Separator | Menu’s parts | — | The parts for `menu`: Menu.Item and the rest themselves, under the split button’s name. Read the Menu page for their props. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, other attributes and the ref go on the attached ButtonGroup around both halves (a role="group" div with .sl-split-button). The halves are .sl-split-button-action and .sl-split-button-menu. |
Keyboard
| Tab | moves to the action, then to the menu button: two tab stops |
| Enter / Space | on the action, runs it; on the menu button, opens the menu on its checked radio item, or the first |
| Alt+↓ | on the action, opens the menu on its first item (↓ alone stays with the browser) |
| ↓ / ↑ | on the menu button, open the menu on its checked radio item (or the first), or on its last |
| ↓ / ↑, Home / End, letters | in the menu, move as in Menu: wrapping past disabled items, jumping, and typeahead |
| Enter / Space | in the menu, choose the item: onSelect gets its id, and the menu closes (a checkbox item leaves it open) with focus on the menu button |
| Esc | closes the menu and returns focus to the menu button |
| Tab / Shift+Tab | in the menu, close it and move on from the menu button, as if the list were not there: Shift+Tab lands on the action |
Accessibility
- Two native buttons in a
role="group". The action is named by its content; the menu button bymenuLabel("More actions" by default, frommessages.moreActions), witharia-haspopup="menu",aria-expandedand, while open,aria-controls. - The menu is a
role="menu"named by the menu button througharia-labelledby, with Menu’s roles, keys and focus return. - Two split buttons on one page both called "More actions" are hard to tell apart: give each a
menuLabelthat says whose actions they are, such as "More merge options". actionDisabledsetsaria-disabledon the action rather thandisabled, so it keeps its tab stop and a screen reader can still find it and hear that it is unavailable; say why nearby.disableddisables both halves natively.
When to use it
For one action people take most of the time, with a few close alternatives: Open in Editor with Reveal in Finder beside it, Merge with its other methods. The action runs at once; the alternatives wait in the menu. When no action leads, use a Menu; when the actions are equal and few, a ButtonGroup.
Look
The two halves are an attached ButtonGroup’s buttons, sharing its variant, tone and size, and inside another ButtonGroup they take its size and variant as a Button would. Secondary halves share their hairline border; ghost and primary ones get a seam between them instead, in the fill’s text colour on primary. The menu half is narrower than a square icon button.
The menu
It is Menu’s own list, from items (Menu’s MenuItem, so groups, sticky groups, separators, checkbox and radio items, disabled items and submenus all work) or from parts in menu, or both, parts last. It renders in a portal, hangs from the whole control, its end edge lined up with the menu button’s by default (placement="bottom-end"), and flips above when there is no room below. Choosing an item calls onSelect, not onClick.