# Confirm button

Destructive 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.

Tier: free. Type: component. Category: actions. Tags: motion, keyboard.

## Install

```bash
npx shadcn@latest add @pixilui/confirm-button
```

One-time setup: add the registry to `components.json`:

```json
{
  "registries": {
    "@pixilui": "https://pixilui.com/r/{name}.json"
  }
}
```

Files: `components/pixilui/confirm-button.tsx`.

## Preview

Live preview: https://pixilui.com/components/confirm-button

Knobs:

- `show`: `styles`, `more`, `sizes`, `states`

Signature: The fill sweeps across while the button is held and lets go the moment you release, so nothing goes on a slip.

Styles: `quiet` (default), `lively`, set with `data-style` on <html>. The item reads the style through the foundation; it needs no prop. Standalone files: quiet https://pixilui.com/r/standalone/confirm-button.tsx, lively https://pixilui.com/r/standalone/confirm-button.lively.tsx.

## Code

`components/pixilui/confirm-button.tsx`

```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>
  );
});
```

## Usage

When to use:

- 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.

When not to use:

- 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.

```tsx
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>
  );
}
```

## Examples

None.

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `size` | `ButtonSize` |  | 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. |
| `onConfirm` (required) | `() => void \| Promise<void>` |  | Runs once the person confirms. |
| `label` (required) | `string` |  | The label at rest: say what happens ("Hold to remove", "Delete branch"). |
| `confirmLabel` | `string` | `"Confirm"` | Two-step only: the label while armed. |
| `doneLabel` | `string` | `"Done"` | The label once confirmed, beside the drawn check in two-step. |
| `variant` | `ButtonVariant` |  | Visual weight. hold defaults to danger; two-step rests on danger-quiet and arms as danger. |
| `armWindow` | `number` | `ARM_MS` | Two-step only: how long the button stays armed, in ms. |
| `resetAfter` | `number` | `DONE_MS` | How long the done label shows before the button returns to rest, in ms. |

## Keyboard

- `Tab`: Moves focus to the button.
- `Space` + `Enter`: Hold mode: holding either key fills the button; letting go early resets it.
- `Enter`: Two-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.

## Responsive

- Never wider than its container. Pointer, touch and keyboard all hold the same way.

## Performance

- The fill is CSS; one timer per button, cleared on unmount.

## Notes for AI

- Pass onConfirm and a label that names the action; mode defaults to hold. Use mode="two-step" with confirmLabel when holding is awkward (a row of many buttons).
- For a plain destructive button with no guard, use Button with variant="danger"; for a quiet one (Revoke), variant="danger-quiet".
- Hold rests on danger; two-step rests on danger-quiet and arms as danger. Keep armWindow at about three seconds.
- Keep this distinct from the future hold-to-approve hero element, which approves agent actions with evidence.

## Related

- [Button](https://pixilui.com/components/button/markdown): The PixilUI button in four main styles and four more, five sizes from 24 to 44, with icons, loading, disabled, a count and a shortcut.
- [Action button](https://pixilui.com/components/action-button/markdown): Buttons that show their own work, so the page needs no toast: Save to Saving to Saved, Upload that fills its own glass, and Generate that becomes its own Stop with the elapsed time.
- [Approval card](https://pixilui.com/components/approval-card/markdown): An agent asks before it acts: what it will do, the rows that matter, the risk, and approve or reject with a note.
