Component
Split Button
A primary action paired with an attached, keyboard-navigable menu of alternatives. The leading button fires the default action; the chevron opens a menu. A shared border and negative margin keep it one composite control.
- Component
- React
- TSX
- Tailwind CSS
- MIT
Preview
Live component
9:41 100%
devsnips.dev/library/react/components/buttons/split-button/ fluid · no fixed width 100%
Install
Add to your project
npx devsnips add React/Components/Buttons/split-button React/Components/Buttons/split-button import { useEffect, useRef, useState } from "react";
import type { ButtonHTMLAttributes, ReactNode } from "react";
/* DevSnips React — SplitButton
* Primary action + attached action menu. The leading button fires the
* default (last-chosen) action; the chevron opens a keyboard-navigable
* menu (aria-haspopup="menu", aria-expanded).
*/
export type ButtonSize = "xs" | "sm" | "md" | "lg" | "xl";
export type SplitVariant = "solid" | "outline";
export interface SplitAction {
id: string;
label: ReactNode;
icon?: string;
destructive?: boolean;
}
export interface SplitButtonProps
extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, "onClick"> {
label?: ReactNode;
actions?: SplitAction[];
onAction?: (id: string, action: SplitAction) => void;
variant?: SplitVariant;
size?: ButtonSize;
}
function cx(...parts: Array<string | false | null | undefined>): string {
return parts.filter(Boolean).join(" ");
}
const SIZES: Record<ButtonSize, string> = {
xs: "h-7 gap-1 px-2 text-xs [&_svg]:size-[14px]",
sm: "h-8 gap-1.5 px-3 text-xs [&_svg]:size-[14px]",
md: "h-9 gap-2 px-3.5 text-[13px] [&_svg]:size-4",
lg: "h-10 gap-2 px-4 text-[13px] [&_svg]:size-[18px]",
xl: "h-11 gap-2 px-5 text-sm [&_svg]:size-5",
};
const VARIANTS: Record<SplitVariant, string> = {
solid: "border-transparent bg-[var(--ds-color-primary)] text-[var(--ds-color-primary-foreground)] hover:bg-[color-mix(in_srgb,var(--ds-color-primary)_88%,#000)] active:bg-[color-mix(in_srgb,var(--ds-color-primary)_80%,#000)]",
outline: "border-[var(--ds-color-border-strong)] bg-transparent text-[var(--ds-color-foreground)] hover:bg-[var(--ds-color-surface-hover)] active:bg-[var(--ds-color-surface-active)]",
};
// Minimal stroke icon set; swap for your icon library in a real project.
function Icon({ name, className }: { name?: string; className?: string }) {
if (!name) return null;
const common = { width: "1em", height: "1em", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 1.75, strokeLinecap: "round", strokeLinejoin: "round", className, "aria-hidden": "true", focusable: "false" } as const;
const paths: Record<string, ReactNode> = {
"upload": <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />,
"duplicate": <><rect x="9" y="9" width="12" height="12" rx="2" /><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" /></>,
"trash": <><path d="M3 6h18" /><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" /><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6" /></>,
"edit": <><path d="M12 20h9" /><path d="M16.5 3.5a2.12 2.12 0 0 1 3 3L7 19l-4 1 1-4Z" /></>,
"share": <><circle cx="18" cy="5" r="3" /><circle cx="6" cy="12" r="3" /><circle cx="18" cy="19" r="3" /><path d="m8.6 13.5 6.8 4" /><path d="m15.4 6.5-6.8 4" /></>,
};
return <svg {...common}>{paths[name]}</svg>;
}
const MENU =
"absolute right-0 top-[calc(100%+4px)] z-40 min-w-[180px] rounded-[var(--ds-radius-md)] " +
"border border-[var(--ds-color-border)] bg-[var(--ds-color-surface-elevated)] p-1 " +
"shadow-[var(--ds-shadow-md)]";
const ITEM =
"flex w-full items-center gap-2 rounded-[var(--ds-radius-sm)] border-0 px-2 py-1.5 " +
"text-left font-normal text-[13px] leading-none text-[var(--ds-color-foreground)] " +
"bg-transparent transition-colors duration-150 ease-out motion-reduce:transition-none " +
"hover:bg-[var(--ds-color-surface-hover)] focus:bg-[var(--ds-color-surface-hover)] " +
"focus-visible:outline-2 focus-visible:outline-offset-[-2px] focus-visible:outline-[var(--ds-color-focus-ring)]";
function ChevronIcon({ open }: { open: boolean }) {
return (
<svg className={cx("h-[1em] w-[1em] shrink-0 transition-transform duration-150 ease-out motion-reduce:transition-none", open ? "rotate-180" : "rotate-0")} viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path d="m6 9 6 6 6-6" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" />
</svg>
);
}
export function SplitButton({
label,
actions = [],
onAction,
variant = "solid",
size = "md",
disabled,
className,
...rest
}: SplitButtonProps) {
const [open, setOpen] = useState(false);
const [active, setActive] = useState(0);
const triggerRef = useRef<HTMLButtonElement>(null);
const itemRefs = useRef<Array<HTMLButtonElement | null>>([]);
const containerRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!open) return;
function onDown(e: MouseEvent) {
if (containerRef.current && !containerRef.current.contains(e.target as Node)) setOpen(false);
}
function onKey(e: KeyboardEvent) { if (e.key === "Escape") { setOpen(false); triggerRef.current?.focus(); } }
document.addEventListener("mousedown", onDown);
document.addEventListener("keydown", onKey);
return () => { document.removeEventListener("mousedown", onDown); document.removeEventListener("keydown", onKey); };
}, [open]);
function openMenu() {
setOpen(true);
setTimeout(() => itemRefs.current[0]?.focus(), 0);
}
function choose(i: number) {
setOpen(false);
setActive(i);
onAction?.(actions[i].id, actions[i]);
triggerRef.current?.focus();
}
function onTriggerKey(e: React.KeyboardEvent) {
if (e.key === "ArrowDown" || e.key === "Enter" || e.key === " ") { e.preventDefault(); openMenu(); }
}
function onItemKey(e: React.KeyboardEvent, i: number) {
const n = actions.length;
if (e.key === "ArrowDown") { e.preventDefault(); itemRefs.current[(i + 1) % n]?.focus(); }
else if (e.key === "ArrowUp") { e.preventDefault(); itemRefs.current[(i - 1 + n) % n]?.focus(); }
else if (e.key === "Enter" || e.key === " ") { e.preventDefault(); choose(i); }
}
const current = actions[active];
return (
<div ref={containerRef} className="relative inline-flex">
<button
type="button"
ref={triggerRef}
className={cx(
"inline-flex select-none items-center justify-center whitespace-nowrap rounded-[var(--ds-radius-sm)] border font-medium leading-none transition-colors duration-150 ease-out motion-reduce:transition-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--ds-color-focus-ring)] disabled:pointer-events-none disabled:opacity-50",
VARIANTS[variant],
SIZES[size],
"rounded-r-none border-r-0",
className,
)}
disabled={disabled}
onClick={() => onAction?.(current?.id ?? "", current ?? ({} as SplitAction))}
onKeyDown={onTriggerKey}
{...rest}
>
{current?.icon ? <Icon name={current.icon} /> : null}
<span>{current?.label ?? label}</span>
</button>
<button
type="button"
className={cx(
"inline-flex select-none items-center justify-center whitespace-nowrap rounded-[var(--ds-radius-sm)] border font-medium leading-none transition-colors duration-150 ease-out motion-reduce:transition-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--ds-color-focus-ring)] disabled:pointer-events-none disabled:opacity-50",
VARIANTS[variant],
SIZES[size],
"rounded-l-none",
"px-0 [&_svg]:size-[1em]",
)}
aria-haspopup="menu"
aria-expanded={open}
aria-label="More actions"
disabled={disabled}
onClick={() => (open ? setOpen(false) : openMenu())}
>
<ChevronIcon open={open} />
</button>
{open && (
<div role="menu" className={MENU}>
{actions.map((a, i) => (
<button
key={a.id}
ref={(el) => { itemRefs.current[i] = el; }}
role="menuitem"
tabIndex={-1}
className={cx(ITEM, a.destructive && "text-[var(--ds-color-destructive)]")}
onClick={() => choose(i)}
onKeyDown={(e) => onItemKey(e, i)}
>
{a.icon ? <Icon name={a.icon} /> : <span className="w-[1em]" />}
<span className="flex-1">{a.label}</span>
{i === active && (
<svg className="h-[1em] w-[1em] shrink-0" viewBox="0 0 24 24" fill="none" aria-hidden="true"><path d="M20 6 9 17l-5-5" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" /></svg>
)}
</button>
))}
</div>
)}
</div>
);
}
export default SplitButton; /* DevSnips React — JavaScript parity build.
* Same API, behavior, and classes as code.tsx; TypeScript types removed.
* Regenerated from code.tsx — edit code.tsx and re-run the generator.
*/
import { useEffect, useRef, useState } from "react";
function cx(...parts) {
return parts.filter(Boolean).join(" ");
}
const SIZES = {
xs: "h-7 gap-1 px-2 text-xs [&_svg]:size-[14px]",
sm: "h-8 gap-1.5 px-3 text-xs [&_svg]:size-[14px]",
md: "h-9 gap-2 px-3.5 text-[13px] [&_svg]:size-4",
lg: "h-10 gap-2 px-4 text-[13px] [&_svg]:size-[18px]",
xl: "h-11 gap-2 px-5 text-sm [&_svg]:size-5"
};
const VARIANTS = {
solid: "border-transparent bg-[var(--ds-color-primary)] text-[var(--ds-color-primary-foreground)] hover:bg-[color-mix(in_srgb,var(--ds-color-primary)_88%,#000)] active:bg-[color-mix(in_srgb,var(--ds-color-primary)_80%,#000)]",
outline: "border-[var(--ds-color-border-strong)] bg-transparent text-[var(--ds-color-foreground)] hover:bg-[var(--ds-color-surface-hover)] active:bg-[var(--ds-color-surface-active)]"
};
function Icon({ name, className }) {
if (!name) return null;
const common = { width: "1em", height: "1em", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 1.75, strokeLinecap: "round", strokeLinejoin: "round", className, "aria-hidden": "true", focusable: "false" };
const paths = {
"upload": <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />,
"duplicate": <><rect x="9" y="9" width="12" height="12" rx="2" /><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" /></>,
"trash": <><path d="M3 6h18" /><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" /><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6" /></>,
"edit": <><path d="M12 20h9" /><path d="M16.5 3.5a2.12 2.12 0 0 1 3 3L7 19l-4 1 1-4Z" /></>,
"share": <><circle cx="18" cy="5" r="3" /><circle cx="6" cy="12" r="3" /><circle cx="18" cy="19" r="3" /><path d="m8.6 13.5 6.8 4" /><path d="m15.4 6.5-6.8 4" /></>
};
return <svg {...common}>{paths[name]}</svg>;
}
const MENU = "absolute right-0 top-[calc(100%+4px)] z-40 min-w-[180px] rounded-[var(--ds-radius-md)] border border-[var(--ds-color-border)] bg-[var(--ds-color-surface-elevated)] p-1 shadow-[var(--ds-shadow-md)]";
const ITEM = "flex w-full items-center gap-2 rounded-[var(--ds-radius-sm)] border-0 px-2 py-1.5 text-left font-normal text-[13px] leading-none text-[var(--ds-color-foreground)] bg-transparent transition-colors duration-150 ease-out motion-reduce:transition-none hover:bg-[var(--ds-color-surface-hover)] focus:bg-[var(--ds-color-surface-hover)] focus-visible:outline-2 focus-visible:outline-offset-[-2px] focus-visible:outline-[var(--ds-color-focus-ring)]";
function ChevronIcon({ open }) {
return <svg className={cx("h-[1em] w-[1em] shrink-0 transition-transform duration-150 ease-out motion-reduce:transition-none", open ? "rotate-180" : "rotate-0")} viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path d="m6 9 6 6 6-6" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" />
</svg>;
}
export function SplitButton({
label,
actions = [],
onAction,
variant = "solid",
size = "md",
disabled,
className,
...rest
}) {
const [open, setOpen] = useState(false);
const [active, setActive] = useState(0);
const triggerRef = useRef(null);
const itemRefs = useRef([]);
const containerRef = useRef(null);
useEffect(() => {
if (!open) return;
function onDown(e) {
if (containerRef.current && !containerRef.current.contains(e.target)) setOpen(false);
}
function onKey(e) {
if (e.key === "Escape") {
setOpen(false);
triggerRef.current?.focus();
}
}
document.addEventListener("mousedown", onDown);
document.addEventListener("keydown", onKey);
return () => {
document.removeEventListener("mousedown", onDown);
document.removeEventListener("keydown", onKey);
};
}, [open]);
function openMenu() {
setOpen(true);
setTimeout(() => itemRefs.current[0]?.focus(), 0);
}
function choose(i) {
setOpen(false);
setActive(i);
onAction?.(actions[i].id, actions[i]);
triggerRef.current?.focus();
}
function onTriggerKey(e) {
if (e.key === "ArrowDown" || e.key === "Enter" || e.key === " ") {
e.preventDefault();
openMenu();
}
}
function onItemKey(e, i) {
const n = actions.length;
if (e.key === "ArrowDown") {
e.preventDefault();
itemRefs.current[(i + 1) % n]?.focus();
} else if (e.key === "ArrowUp") {
e.preventDefault();
itemRefs.current[(i - 1 + n) % n]?.focus();
} else if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
choose(i);
}
}
const current = actions[active];
return <div ref={containerRef} className="relative inline-flex">
<button
type="button"
ref={triggerRef}
className={cx(
"inline-flex select-none items-center justify-center whitespace-nowrap rounded-[var(--ds-radius-sm)] border font-medium leading-none transition-colors duration-150 ease-out motion-reduce:transition-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--ds-color-focus-ring)] disabled:pointer-events-none disabled:opacity-50",
VARIANTS[variant],
SIZES[size],
"rounded-r-none border-r-0",
className
)}
disabled={disabled}
onClick={() => onAction?.(current?.id ?? "", current ?? {})}
onKeyDown={onTriggerKey}
{...rest}
>
{current?.icon ? <Icon name={current.icon} /> : null}
<span>{current?.label ?? label}</span>
</button>
<button
type="button"
className={cx(
"inline-flex select-none items-center justify-center whitespace-nowrap rounded-[var(--ds-radius-sm)] border font-medium leading-none transition-colors duration-150 ease-out motion-reduce:transition-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--ds-color-focus-ring)] disabled:pointer-events-none disabled:opacity-50",
VARIANTS[variant],
SIZES[size],
"rounded-l-none",
"px-0 [&_svg]:size-[1em]"
)}
aria-haspopup="menu"
aria-expanded={open}
aria-label="More actions"
disabled={disabled}
onClick={() => open ? setOpen(false) : openMenu()}
>
<ChevronIcon open={open} />
</button>
{open && <div role="menu" className={MENU}>
{actions.map((a, i) => <button
key={a.id}
ref={(el) => {
itemRefs.current[i] = el;
}}
role="menuitem"
tabIndex={-1}
className={cx(ITEM, a.destructive && "text-[var(--ds-color-destructive)]")}
onClick={() => choose(i)}
onKeyDown={(e) => onItemKey(e, i)}
>
{a.icon ? <Icon name={a.icon} /> : <span className="w-[1em]" />}
<span className="flex-1">{a.label}</span>
{i === active && <svg className="h-[1em] w-[1em] shrink-0" viewBox="0 0 24 24" fill="none" aria-hidden="true"><path d="M20 6 9 17l-5-5" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" /></svg>}
</button>)}
</div>}
</div>;
}
export default SplitButton; # Split Button
A primary action paired with an attached, keyboard-navigable menu of alternatives. The leading button fires the default action; the chevron opens a menu. A shared border and negative margin keep it one composite control.
## Installation
This component requires **React** and **Tailwind CSS**. Drop `code.tsx` (or `code.jsx` for JavaScript projects) into your project. Tailwind utility classes are included directly in the component, so no separate CSS file is required.
The component consumes the DevSnips semantic design tokens through Tailwind arbitrary values (for example `bg-[var(--ds-color-primary)]`). Define the `--ds-*` tokens once in your theme — see [React/DESIGN_TOKENS.md](../../../DESIGN_TOKENS.md) for the full token spec.
## Usage
```tsx
<SplitButton label="Create project" actions={[{id:"blank",label:"Blank project"},{id:"import",label:"Import"}} onAction={handle} />
```
## JavaScript
A `code.jsx` build is provided for projects that ship plain JSX. It exposes the same API and behavior as `code.tsx` — only the TypeScript types are removed.
## Props
| Prop | Type | Default |
|---|---|---|
| `label` | `ReactNode` | — |
| `actions` | `Array<{ id: string; label: ReactNode; icon?: string; destructive?: boolean }>` | `[]` |
| `onAction` | `(id, action) => void` | — |
| `variant` | `solid \| outline` | `solid` |
| `size` | `ButtonSize` | `md` |
| `disabled` | `boolean` | `false` |
Plus all native `ButtonHTMLAttributes<HTMLButtonElement>`.
## Variants
solid (default) · outline. The menu is always a bordered elevated surface (`surface-elevated`, `radius-md`, `shadow-md`).
## Sizes
xs (28px) · sm (32px) · **md (36px, default)** · lg (40px) · xl (44px). Horizontal padding scales 8 → 20px; icons scale 14 → 20px.
## States
default · hover · active · focus-visible · open (menu, `aria-expanded`) · disabled (reduced opacity).
## Accessibility
The chevron trigger has `aria-haspopup="menu"` + `aria-expanded`. The menu uses `role="menu"` and items `role="menuitem"`. **Keyboard**: trigger ArrowDown/Enter/Space opens; ArrowUp/Down move between items; Enter/Space activates; Escape closes and returns focus to the trigger; outside click closes. Selecting an item sets it as the new default and fires `onAction`.
## Styling
Tailwind classes are included directly in the component and consume the DevSnips semantic design tokens (`--ds-*`) via arbitrary values. The button themes with the surface automatically in light and dark mode. No component-specific CSS file is needed.
## Design Tokens
See [React/DESIGN_TOKENS.md](../../../DESIGN_TOKENS.md) for the authoritative token specification. This button uses the semantic color, radius, and motion tokens; define them once in your project theme and every button in the family stays in sync.
## Notes
Use for action variants (one default + alternatives). Don't use for navigation. The leading button fires the last-chosen action. 190 lines UTF-8 · LF · Spaces: 2
Continue browsing