Confirm button
FreeDestructive actions that a slip cannot trigger: hold until the fill crosses, or press twice while the button is armed. The plain Destructive and quiet Revoke styles sit beside them.
npx shadcn@latest add @pixilui/confirm-buttonPreview
Hold to delete fills the button and drains if you let go early.
Show
import type { ReactNode } from "react";
import { IconDelete } from "@/components/pixilui/icons/delete";
import { Button } from "@/components/pixilui/button";
import { ConfirmButton } from "@/components/pixilui/confirm-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 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>
<ConfirmButton mode="two-step" size="lg" label="Delete branch" confirmLabel="Confirm delete" doneLabel="Deleted" resetAfter={1600} onConfirm={() => {}} />
<ConfirmButton mode="two-step" label="Revoke key" confirmLabel="Confirm revoke" doneLabel="Revoked" resetAfter={1600} onConfirm={() => {}} />
</Row>
<Cap>Two-step: press once to arm it, again inside three seconds to confirm</Cap>
</>
);
if (show === "sizes")
return (
<>
<Row>
<ConfirmButton size="sm" label="Hold to remove" doneLabel="Removed" resetAfter={1800} onConfirm={() => {}} />
<ConfirmButton icon={<IconDelete />} label="Hold to delete" doneLabel="Deleted" resetAfter={1800} onConfirm={() => {}} />
<ConfirmButton size="lg" icon={<IconDelete />} label="Hold to delete run" doneLabel="Run deleted" resetAfter={1800} onConfirm={() => {}} />
</Row>
<Cap>28, 32 (default), 36, with or without an icon</Cap>
</>
);
if (show === "states")
return (
<>
<Row>
<ConfirmButton size="lg" label="Hold to delete" doneLabel="Deleted" resetAfter={1800} onConfirm={() => {}} />
</Row>
<Cap>Hold: the fill sweeps across in 1.2 seconds; let go early and it drains</Cap>
</>
);
return (
<>
<Row>
<Button variant="danger" size="lg" icon={<IconDelete />}>
Delete workspace
</Button>
<Button variant="danger-quiet" size="lg">
Revoke access
</Button>
<ConfirmButton size="lg" label="Hold to delete" doneLabel="Deleted" resetAfter={1800} onConfirm={() => {}} />
</Row>
<Cap>Destructive, Quiet danger, and Destructive held to confirm</Cap>
</>
);
}
export default function ConfirmButtonDemo({ 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
- Deleting, revoking or removing something that cannot come back, in place, without a dialog.
- A row of risky actions (remove a member, revoke a key) where a dialog per row would be heavy.
Use something else when
- Approving an agent's action with evidence: that is the coming hold-to-approve element.
- A destructive action that needs a typed confirmation or explains its consequences: use a dialog.
- Anything reversible: use a plain button and offer undo.
Installation
Terminal
npx shadcn@latest add @pixilui/confirm-buttonAdds confirm-button.tsx with button.tsx, and installs clsx and tailwind-merge. First PixilUI item in this app? Add the registry once, as in Installation.
components/pixilui/confirm-button.tsx
"use client";
import { forwardRef, useEffect, useRef, useState, type MouseEvent } from "react";
import { Button, type ButtonProps, type ButtonSize, type ButtonVariant } from "./button";
/*
A destructive action that a slip cannot trigger (the plain Destructive and quiet Revoke styles
are Button's danger and danger-quiet). Two forms, both the button's own motion
(button.tsx, foundation.css section 7):
hold the fill sweeps across while held (1200ms, linear); letting go early resets it.
two-step the first press arms it (the glass turns danger and a darker fill drains across the
window); a second press inside the window confirms.
*/
const ARM_MS = 3000;
const DONE_MS = 1700;
export type ConfirmButtonMode = "hold" | "two-step";
export type ConfirmButtonStatus = "idle" | "armed" | "done";
export type ConfirmButtonProps = Omit<ButtonProps, "children" | "armed" | "done" | "reserve" | "onHoldComplete" | "variant"> & {
/** hold (the default) confirms after a 1200ms hold; two-step confirms on a second press while armed. */
mode?: ConfirmButtonMode;
/** Runs once the person confirms. */
onConfirm: () => void | Promise<void>;
/** The label at rest: say what happens ("Hold to remove", "Delete branch"). */
label: string;
/** Two-step only: the label while armed. */
confirmLabel?: string;
/** The label once confirmed, beside the drawn check in two-step. */
doneLabel?: string;
/** Visual weight. hold defaults to danger; two-step rests on danger-quiet and arms as danger. */
variant?: ButtonVariant;
/** Height, as on Button: xs 24, sm 28, md 32 (default), lg 36, xl 44. */
size?: ButtonSize;
/** Two-step only: how long the button stays armed, in ms. */
armWindow?: number;
/** How long the done label shows before the button returns to rest, in ms. */
resetAfter?: number;
};
/** A destructive button that needs intent: hold until the fill crosses, or press twice inside a short window. */
export const ConfirmButton = forwardRef<HTMLButtonElement, ConfirmButtonProps>(function ConfirmButton(
{ mode = "hold", onConfirm, label, confirmLabel = "Confirm", doneLabel = "Done", variant, armWindow = ARM_MS, resetAfter = DONE_MS, onClick, ...rest },
ref,
) {
const [status, setStatus] = useState<ConfirmButtonStatus>("idle");
const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
useEffect(() => () => clearTimeout(timer.current), []);
const confirm = () => {
clearTimeout(timer.current);
setStatus("done");
timer.current = setTimeout(() => setStatus("idle"), resetAfter);
void onConfirm();
};
if (mode === "hold") {
return (
<Button
{...rest}
ref={ref}
onClick={onClick}
variant={variant ?? "danger"}
reserve={[label, doneLabel]}
data-status={status}
onHoldComplete={confirm}
>
{status === "done" ? doneLabel : label}
</Button>
);
}
const press = (event: MouseEvent<HTMLButtonElement>) => {
onClick?.(event);
if (event.defaultPrevented) return;
clearTimeout(timer.current);
if (status === "idle") {
setStatus("armed");
timer.current = setTimeout(() => setStatus("idle"), armWindow);
} else if (status === "armed") confirm();
};
return (
<Button
{...rest}
ref={ref}
variant={status === "armed" ? "danger" : (variant ?? "danger-quiet")}
armed={status === "armed"}
done={status === "done"}
reserve={[label, confirmLabel, { label: doneLabel, icon: true }]}
data-status={status}
onClick={press}
>
{status === "armed" ? confirmLabel : status === "done" ? doneLabel : label}
</Button>
);
});
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
example.tsx
import { ConfirmButton } from "@/components/pixilui/confirm-button";
<ConfirmButton onConfirm={onConfirm} label={label} />Never wider than its container. Pointer, touch and keyboard all hold the same way.
API reference
PropTypeDefault
onConfirmRequired() => void | Promise<void>–Runs once the person confirms.labelRequiredstring–The label at rest: say what happens ("Hold to remove", "Delete branch").sizeButtonSize–Height, as on Button: xs 24, sm 28, md 32 (default), lg 36, xl 44.mode"hold" | "two-step""hold"hold (the default) confirms after a 1200ms hold; two-step confirms on a second press while armed.confirmLabelstring"Confirm"Two-step only: the label while armed.doneLabelstring"Done"The label once confirmed, beside the drawn check in two-step.variantButtonVariant–Visual weight. hold defaults to danger; two-step rests on danger-quiet and arms as danger.armWindownumberARM_MSTwo-step only: how long the button stays armed, in ms.resetAfternumberDONE_MSHow long the done label shows before the button returns to rest, in ms.Keyboard
TabMoves focus to the button.
Space EnterHold mode: holding either key fills the button; letting go early resets it.
EnterTwo-step mode: the first press arms it, a second press inside the window confirms.
Accessibility
- A native button. The label says what happens (Hold to remove, Confirm delete, Deleted), so the state is read aloud.
- The fill and the drain are aria-hidden; the label carries the meaning.
- Danger text keeps 4.5:1 in light and dark on both the danger and the danger-quiet look.
- data-status is idle, armed or done.
Motion
- Hold: the fill sweeps across in 1200ms, linear, and lets go in 200ms when released early.
- Two-step: the button turns danger and a darker fill drains across the arm window (3000ms); when it runs out, the button returns to rest.
- Done: the label cross-fades to the done label, with a check that draws itself in two-step.
- Reduced motion: no sweep or drain; the states change at once.