Split button
FreeOne glass body with two hit areas: the main action and a chevron for its options, split by a hairline, each half with its own hover and press.
npx shadcn@latest add @pixilui/split-buttonPreview
import type { ReactNode } from "react";
import { IconDisclosure } from "@/components/pixilui/icons/disclosure";
import { IconEval } from "@/components/pixilui/icons/eval";
import { IconExport } from "@/components/pixilui/icons/export";
import { IconSubagent } from "@/components/pixilui/icons/subagent";
import { SplitButton } from "@/components/pixilui/split-button";
// The item page's one row, Show: Styles, More styles, Icons and sizes, States (the button pages' layout).
export const knobs = {
show: ["styles", "more", "sizes", "states"],
} as const;
const chevron = <IconDisclosure data-stroke="heavy" />;
const Row = ({ children }: { children: ReactNode }) => <div className="flex flex-wrap items-center justify-center gap-2.5">{children}</div>;
const Cap = ({ children }: { children: ReactNode }) => <p className="-mt-2 text-center text-caption text-muted">{children}</p>;
function Shown({ show }: { show: string }) {
if (show === "more")
return (
<>
<Row>
<SplitButton variant="primary" aria-label="Export" icon={<IconExport />} menuIcon={chevron} menuLabel="Export options">
Export
</SplitButton>
<SplitButton variant="secondary" aria-label="Run eval" icon={<IconEval />} menuIcon={chevron} menuLabel="Choose a dataset">
Run eval
</SplitButton>
</Row>
<Cap>With a leading icon, in each style</Cap>
</>
);
if (show === "sizes")
return (
<>
<Row>
{(["sm", "md", "lg", "xl"] as const).map((size) => (
<SplitButton key={size} variant="primary" size={size} aria-label="Deploy" menuIcon={chevron} menuLabel="Deploy options">
Deploy
</SplitButton>
))}
</Row>
<Cap>28, 32 (default), 36, 44</Cap>
</>
);
if (show === "states")
return (
<>
<Row>
<SplitButton variant="primary" aria-label="Deploy" menuIcon={chevron} menuLabel="Deploy options" menuProps={{ "aria-expanded": true, "aria-haspopup": "menu" }}>
Deploy to production
</SplitButton>
<SplitButton variant="secondary" aria-label="Branch" icon={<IconSubagent />} menuIcon={chevron} menuLabel="Switch branch">
feature/refund-retries
</SplitButton>
</Row>
<Cap>Long labels keep the chevron in reach; the menu half takes a trigger's props</Cap>
</>
);
return (
<>
<Row>
<SplitButton variant="primary" size="lg" aria-label="Deploy" menuIcon={chevron} menuLabel="Deploy options">
Deploy
</SplitButton>
<SplitButton variant="secondary" size="lg" aria-label="Branch" icon={<IconSubagent />} menuIcon={chevron} menuLabel="Switch branch">
main
</SplitButton>
</Row>
<Cap>Primary for the main action, the glass for a picker</Cap>
</>
);
}
export default function SplitButtonDemo({ show = "styles" }: KnobValues<typeof knobs>) {
return (
<div key={show} className="grid justify-items-center gap-4.5" aria-live="polite">
<Shown show={show} />
</div>
);
}When to use
Use it when
- An action people repeat with a usual choice and a few others: Deploy to production, or to staging.
- A picker that also acts: the current branch, with the switcher behind the chevron.
Use something else when
- Options that are equal: use a menu button or a segmented control.
- A single action with no options: use Button.
Installation
npx shadcn@latest add @pixilui/split-buttonAdds split-button.tsx with button.tsx, and installs clsx and tailwind-merge. First PixilUI item in this app? Add the registry once, as in Installation.
"use client";
import { forwardRef, type ComponentPropsWithoutRef, type MouseEvent, type ReactNode } from "react";
import { ButtonSplit, type ButtonSize } from "./button";
/*
One glass body, two hit areas: the action and its menu, with a hairline between. The look is
the locked split button (foundation.css section 7); this item names it and gives it a page.
*/
export type SplitButtonProps = Omit<ComponentPropsWithoutRef<"div">, "children" | "onClick"> & {
/** primary for the main action of a surface (Deploy), secondary for a picker (a branch). */
variant?: "primary" | "secondary";
/** Height, as on Button: xs 24, sm 28, md 32 (default), lg 36, xl 44. */
size?: ButtonSize;
/** The main action's label. */
children: ReactNode;
/** Leading icon of the main action. */
icon?: ReactNode;
/** The menu half's icon, usually a chevron. */
menuIcon: ReactNode;
/** The menu half's accessible name ("Deploy options"). */
menuLabel: string;
/** Runs the main action. */
onAction?: (event: MouseEvent<HTMLButtonElement>) => void | Promise<void>;
/** Opens the menu. Spread menuProps instead when a popover or menu trigger owns the button. */
onMenu?: (event: MouseEvent<HTMLButtonElement>) => void | Promise<void>;
/** Extra props for the menu half (a popover trigger's props, aria-expanded). */
menuProps?: ComponentPropsWithoutRef<"button">;
};
/** An action with its options behind a chevron: Deploy and its targets, a branch and the switcher. Each half has its own hover and press. */
export const SplitButton = forwardRef<HTMLDivElement, SplitButtonProps>(function SplitButton({ onAction, onMenu, ...rest }, ref) {
return (
<ButtonSplit
ref={ref}
onAction={onAction ? (event) => void onAction(event) : undefined}
onMenu={onMenu ? (event) => void onMenu(event) : undefined}
{...rest}
/>
);
});
It uses Button: add it the same way. Then npm i clsx tailwind-merge, and add the foundation once per app, as in Installation.
Usage
import { SplitButton } from "@/components/pixilui/split-button";
<SplitButton
menuIcon={menuIcon}
menuLabel={menuLabel}
onAction={onAction}
onMenu={onMenu}
>Save changes</SplitButton>Sizes to its label and never grows past its container. Both halves keep a 40px hit area on desktop and 44 on touch.
API reference
childrenRequiredReactNode–The main action's label.menuIconRequiredReactNode–The menu half's icon, usually a chevron.menuLabelRequiredstring–The menu half's accessible name ("Deploy options").variant"primary" | "secondary"–primary for the main action of a surface (Deploy), secondary for a picker (a branch).sizeButtonSize–Height, as on Button: xs 24, sm 28, md 32 (default), lg 36, xl 44.iconReactNode–Leading icon of the main action.onAction(event: MouseEvent<HTMLButtonElement>) => void | Promise<void>–Runs the main action.onMenu(event: MouseEvent<HTMLButtonElement>) => void | Promise<void>–Opens the menu. Spread menuProps instead when a popover or menu trigger owns the button.menuPropsComponentPropsWithoutRef<"button">–Extra props for the menu half (a popover trigger's props, aria-expanded).Also takes every native <div> attribute except onClick, and a ref.
Keyboard
Accessibility
- A group of two native buttons; name the group with aria-label ("Deploy").
- The menu half needs menuLabel ("Deploy options"); its icon is aria-hidden.
- When a popover or menu owns the menu half, pass its trigger props through menuProps so aria-expanded and aria-controls land on that button.
Motion
- Each half presses on its own: scale .96 in 150ms ease-out, with the glass setting in.
- Hover in 150ms lights only the half under the pointer.
- Reduced motion: no press scale; colour changes stay.