Split action button
A primary button joined to a dropdown of alternatives; picking one swaps the main action and the menu remembers it.
"use client";
import { useEffect, useId, useRef, useState, type KeyboardEvent } from "react";
import "./split-button.css";
export type SplitAction = { id: string; label: string; hint?: string };
type SplitButtonProps = {
actions: SplitAction[];
onAction?: (action: SplitAction) => void;
theme?: "light" | "dark";
};
/**
* SplitButton
* - Left half runs the current action. Right half opens a menu of alternatives.
* - Choosing an alternative makes it the new main action (like "Publish" -> "Schedule").
* - Menu follows the WAI-ARIA menu button pattern: arrows move, Enter picks, Escape closes.
*/
export function SplitButton({
actions,
onAction,
theme = "light",
}: SplitButtonProps) {
const id = useId();
const [current, setCurrent] = useState(actions[0]);
const [open, setOpen] = useState(false);
const [status, setStatus] = useState("");
const toggle = useRef<HTMLButtonElement>(null);
const items = useRef<(HTMLButtonElement | null)[]>([]);
const root = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!open) return;
items.current[Math.max(0, actions.indexOf(current))]?.focus();
const close = (e: PointerEvent) =>
!root.current?.contains(e.target as Node) && setOpen(false);
window.addEventListener("pointerdown", close);
return () => window.removeEventListener("pointerdown", close);
}, [open, actions, current]);
const run = (a: SplitAction) => {
onAction?.(a);
setStatus(`${a.label}: done`);
};
const onMenuKey = (e: KeyboardEvent) => {
const i = items.current.indexOf(
document.activeElement as HTMLButtonElement,
);
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
e.preventDefault();
const next =
(i + (e.key === "ArrowDown" ? 1 : -1) + actions.length) %
actions.length;
items.current[next]?.focus();
} else if (e.key === "Home" || e.key === "End") {
e.preventDefault();
items.current[e.key === "Home" ? 0 : actions.length - 1]?.focus();
} else if (e.key === "Escape" || e.key === "Tab") {
setOpen(false);
if (e.key === "Escape") toggle.current?.focus();
}
};
return (
<div ref={root} className="gu-split" data-theme={theme}>
<div className="gu-split__group">
<button
type="button"
className="gu-split__main"
onClick={() => run(current)}
>
<span key={current.id} className="gu-split__label">
{current.label}
</span>
</button>
<button
ref={toggle}
type="button"
className="gu-split__toggle"
aria-haspopup="menu"
aria-expanded={open}
aria-controls={`${id}-menu`}
aria-label="More actions"
onClick={() => setOpen((o) => !o)}
onKeyDown={(e) =>
e.key === "ArrowDown" && (e.preventDefault(), setOpen(true))
}
>
<svg viewBox="0 0 12 12" aria-hidden="true">
<path d="m3 4.5 3 3 3-3" />
</svg>
</button>
</div>
<div
id={`${id}-menu`}
role="menu"
className="gu-split__menu"
data-open={open || undefined}
onKeyDown={onMenuKey}
hidden={!open}
>
{actions.map((a, i) => (
<button
key={a.id}
ref={(el) => {
items.current[i] = el;
}}
role="menuitemradio"
aria-checked={a.id === current.id}
tabIndex={-1}
type="button"
onClick={() => {
setCurrent(a);
setOpen(false);
toggle.current?.focus();
}}
>
<span className="gu-split__check" aria-hidden="true" />
<span>
<strong>{a.label}</strong>
{a.hint && <span>{a.hint}</span>}
</span>
</button>
))}
</div>
<p className="gu-split__status" role="status">
{status}
</p>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Split action button
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A split button: the left part runs the current action, the right part (a chevron) opens a menu of alternative actions. Choosing an alternative replaces the main action, and its label slides in.
## Layout and look
- Two joined parts, 52px tall, orange #ff5a1f with dark #0e0e10 text (weight 800), radius 14px on the outer corners, a 1.5px dark divider between, an orange glow shadow underneath.
- Main part min width 170px; toggle part 52px square with a chevron that flips when open.
- Menu: 280px card (#fbf8f2, dark #1f1f23), radius 16px, soft shadow, rows of 44px+ with a radio circle (filled orange ring when selected), bold title and a muted hint line.
- A status line under the button.
## Behavior and motion
1. Clicking the main part runs the current action and updates the status line.
2. The toggle opens the menu with a 350ms scale-and-drop from the top-left; focus moves to the selected item.
3. Choosing an item sets it as the main action; the new label enters with an 8px rise and fade.
4. Click outside closes.
## Touch
Large tap targets; the chevron half is a separate 52px button.
## Accessibility
- Toggle: aria-haspopup="menu", aria-expanded, aria-controls, aria-label "More actions"; ArrowDown opens.
- Menu: role="menu" with role="menuitemradio" and aria-checked. Up/Down/Home/End move, Escape closes and returns focus, Tab closes.
- prefers-reduced-motion: no menu or label animations.
## Props
actions ({ id, label, hint? }[]), onAction(action), theme.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### SplitButton.tsx
```tsx
"use client";
import { useEffect, useId, useRef, useState, type KeyboardEvent } from "react";
import "./split-button.css";
export type SplitAction = { id: string; label: string; hint?: string };
type SplitButtonProps = {
actions: SplitAction[];
onAction?: (action: SplitAction) => void;
theme?: "light" | "dark";
};
/**
* SplitButton
* - Left half runs the current action. Right half opens a menu of alternatives.
* - Choosing an alternative makes it the new main action (like "Publish" -> "Schedule").
* - Menu follows the WAI-ARIA menu button pattern: arrows move, Enter picks, Escape closes.
*/
export function SplitButton({
actions,
onAction,
theme = "light",
}: SplitButtonProps) {
const id = useId();
const [current, setCurrent] = useState(actions[0]);
const [open, setOpen] = useState(false);
const [status, setStatus] = useState("");
const toggle = useRef<HTMLButtonElement>(null);
const items = useRef<(HTMLButtonElement | null)[]>([]);
const root = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!open) return;
items.current[Math.max(0, actions.indexOf(current))]?.focus();
const close = (e: PointerEvent) =>
!root.current?.contains(e.target as Node) && setOpen(false);
window.addEventListener("pointerdown", close);
return () => window.removeEventListener("pointerdown", close);
}, [open, actions, current]);
const run = (a: SplitAction) => {
onAction?.(a);
setStatus(`${a.label}: done`);
};
const onMenuKey = (e: KeyboardEvent) => {
const i = items.current.indexOf(
document.activeElement as HTMLButtonElement,
);
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
e.preventDefault();
const next =
(i + (e.key === "ArrowDown" ? 1 : -1) + actions.length) %
actions.length;
items.current[next]?.focus();
} else if (e.key === "Home" || e.key === "End") {
e.preventDefault();
items.current[e.key === "Home" ? 0 : actions.length - 1]?.focus();
} else if (e.key === "Escape" || e.key === "Tab") {
setOpen(false);
if (e.key === "Escape") toggle.current?.focus();
}
};
return (
<div ref={root} className="gu-split" data-theme={theme}>
<div className="gu-split__group">
<button
type="button"
className="gu-split__main"
onClick={() => run(current)}
>
<span key={current.id} className="gu-split__label">
{current.label}
</span>
</button>
<button
ref={toggle}
type="button"
className="gu-split__toggle"
aria-haspopup="menu"
aria-expanded={open}
aria-controls={`${id}-menu`}
aria-label="More actions"
onClick={() => setOpen((o) => !o)}
onKeyDown={(e) =>
e.key === "ArrowDown" && (e.preventDefault(), setOpen(true))
}
>
<svg viewBox="0 0 12 12" aria-hidden="true">
<path d="m3 4.5 3 3 3-3" />
</svg>
</button>
</div>
<div
id={`${id}-menu`}
role="menu"
className="gu-split__menu"
data-open={open || undefined}
onKeyDown={onMenuKey}
hidden={!open}
>
{actions.map((a, i) => (
<button
key={a.id}
ref={(el) => {
items.current[i] = el;
}}
role="menuitemradio"
aria-checked={a.id === current.id}
tabIndex={-1}
type="button"
onClick={() => {
setCurrent(a);
setOpen(false);
toggle.current?.focus();
}}
>
<span className="gu-split__check" aria-hidden="true" />
<span>
<strong>{a.label}</strong>
{a.hint && <span>{a.hint}</span>}
</span>
</button>
))}
</div>
<p className="gu-split__status" role="status">
{status}
</p>
</div>
);
}
```
### split-button.css
```css
/* SplitButton: change these colors to match your brand. */
.gu-split {
--btn: #ff5a1f;
--btn-text: #0e0e10;
--divider: rgb(14 14 16 / 0.25);
--menu: #fbf8f2;
--text: #0e0e10;
--muted: #57534b;
--hover: rgb(14 14 16 / 0.06);
--line: rgb(14 14 16 / 0.12);
--focus: #2b35ff;
--ease: cubic-bezier(0.16, 1, 0.3, 1);
position: relative;
display: inline-grid;
justify-items: start;
font-family: system-ui, sans-serif;
color: var(--text);
}
.gu-split[data-theme="dark"] {
--menu: #1f1f23;
--text: #f3eee3;
--muted: #aea89c;
--hover: rgb(243 238 227 / 0.07);
--line: rgb(243 238 227 / 0.12);
--focus: #d7ff3a;
}
.gu-split button {
font: inherit;
color: inherit;
cursor: pointer;
}
.gu-split button:focus-visible {
outline: 2px solid var(--focus);
outline-offset: 2px;
}
.gu-split__group {
display: inline-flex;
border-radius: 14px;
box-shadow: 0 12px 24px -14px rgb(255 90 31 / 0.8);
}
.gu-split__main,
.gu-split__toggle {
min-height: 52px;
border: 0;
background: var(--btn);
color: var(--btn-text) !important;
font-weight: 800 !important;
transition: filter 0.2s;
}
.gu-split__main {
min-width: 170px;
padding: 0 22px;
border-radius: 14px 0 0 14px;
text-align: left;
}
.gu-split__toggle {
display: grid;
place-items: center;
width: 52px;
border-left: 1.5px solid var(--divider);
border-radius: 0 14px 14px 0;
}
.gu-split__main:hover,
.gu-split__toggle:hover {
filter: brightness(1.06);
}
.gu-split__toggle svg {
width: 14px;
height: 14px;
fill: none;
stroke: currentColor;
stroke-width: 1.8;
stroke-linecap: round;
transition: transform 0.3s var(--ease);
}
.gu-split__toggle[aria-expanded="true"] svg {
transform: rotate(180deg);
}
/* The label slides in whenever the main action changes */
.gu-split__label {
display: inline-block;
animation: gu-split-in 0.4s var(--ease);
}
@keyframes gu-split-in {
from {
opacity: 0;
transform: translateY(8px);
}
}
.gu-split__menu {
position: absolute;
top: 60px; /* just under the 52px button */
left: 0;
z-index: 5;
display: grid;
min-width: 280px;
padding: 6px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--menu);
box-shadow: 0 24px 48px -24px rgb(14 14 16 / 0.5);
transform-origin: top left;
animation: gu-split-menu 0.35s var(--ease);
}
.gu-split__menu[hidden] {
display: none;
}
@keyframes gu-split-menu {
from {
opacity: 0;
transform: translateY(-6px) scale(0.96);
}
}
.gu-split__menu button {
display: flex;
align-items: center;
gap: 12px;
min-height: 44px;
padding: 8px 12px;
border: 0;
border-radius: 10px;
background: none;
text-align: left;
}
.gu-split__menu button:hover,
.gu-split__menu button:focus-visible {
background: var(--hover);
outline: none;
}
.gu-split__menu strong {
display: block;
font-size: 0.92rem;
}
.gu-split__menu strong + span {
display: block;
color: var(--muted);
font-size: 0.8rem;
}
.gu-split__check {
flex: none;
width: 18px;
height: 18px;
border: 1.5px solid var(--line);
border-radius: 50%;
}
.gu-split__menu [aria-checked="true"] .gu-split__check {
border: 5px solid var(--btn);
}
.gu-split__status {
min-height: 1.2em;
margin: 12px 0 0;
color: var(--muted);
font-size: 0.85rem;
}
@media (prefers-reduced-motion: reduce) {
.gu-split__label,
.gu-split__menu {
animation: none;
}
}
```
You might also want
Next piece: Spotlight cardsRadial action menu
Components, intermediate
Hold to confirm
Components, beginner
Mega menu
Components, advanced