Split button

Slant / Controls

A main action and a menu of the others, in one control.

Demo

Usage

@tuttn/slant
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

PropTypeDefaultDescription
childrenReactNode—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.
itemsSplitButtonItem[]—The other actions as data, as Menu takes them: `{ id, label, kind?, checked?, disabled?, group?, stickyGroup?, textValue?, shortcut?, keyshortcuts?, items? }` or `{ separator: true }`.
menuReactNode—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.
variantButtonProps['variant']'secondary', or the ButtonGroup'sEmphasis, shared by both halves.
toneTone'accent'Semantic colour, shared by both halves, as Button takes it.
size'sm' | 'md' | 'lg''md', or the ButtonGroup'sHeight on the control scale, shared by both halves.
disabledbooleanfalseDisables both halves: neither takes focus, and the menu closes.
actionDisabledbooleanfalseDisables only the action, with aria-disabled, so it keeps its tab stop and Alt+ArrowDown still opens the menu, which stays usable.
menuLabelstringmessages.moreActions ("More actions")The menu button's accessible name, which also names the menu.
openboolean—Whether the menu is open. Pass it with onOpenChange to control it; leave it out and the menu keeps its own state.
defaultOpenbooleanfalseWhether the menu starts open, when uncontrolled.
onOpenChange(open: boolean) => void—Called with the new state whenever the menu opens or closes.
placementMenuPlacement'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, .SeparatorMenu’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.
…restHTMLAttributes<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

Tabmoves to the action, then to the menu button: two tab stops
Enter / Spaceon 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, lettersin the menu, move as in Menu: wrapping past disabled items, jumping, and typeahead
Enter / Spacein 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
Esccloses the menu and returns focus to the menu button
Tab / Shift+Tabin 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 by menuLabel ("More actions" by default, from messages.moreActions), with aria-haspopup="menu", aria-expanded and, while open, aria-controls.
  • The menu is a role="menu" named by the menu button through aria-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 menuLabel that says whose actions they are, such as "More merge options".
  • actionDisabled sets aria-disabled on the action rather than disabled, so it keeps its tab stop and a screen reader can still find it and hear that it is unavailable; say why nearby. disabled disables 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.