# Hold to confirm

> A deliberate action, held for a moment: the ring fills, the label changes, and confirmation settles into a check.

Give an important action a little intention without opening another dialog. Press and hold while a fine progress ring traces around the icon. Release early and it returns to rest. Cross the threshold and the same button moves through pending, success or error, with a morphing label and drawn check. The action runs once, and an error can be retried with another hold.

## Install

```bash
npx shadcn@latest add @hairline/hold-to-confirm
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: `lucide-react`
- Hairline UI components it needs (the CLI installs them too): `text-morph`
- Docs and live preview: https://hairlineui.com/ui/hold-to-confirm
- Registry item (source): https://hairlineui.com/r/hold-to-confirm.json

## Usage

```tsx
import { HoldToConfirm } from "@/components/hold-to-confirm";

<HoldToConfirm
  labels={{ idle: "Hold to archive", pending: "Archiving", success: "Archived" }}
  onConfirm={async () => { await archiveProject(); }}
/>
```

## Connect a deliberate action

The callback owns the actual operation. Reject its promise to show an error; the component never invents a successful result.

```tsx
"use client";
import { HoldToConfirm } from "@/components/hold-to-confirm";

export function ArchiveProject({ id }: { id: string }) {
  return <HoldToConfirm labels={{ idle: "Hold to archive", success: "Archived" }}
    onConfirm={async () => {
      const response = await fetch("/api/projects/" + id + "/archive", { method: "POST" });
      if (!response.ok) throw new Error("Archive failed");
    }} />;
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `onConfirm` | `() => void \| Promise<void>` | – | Runs once after the hold threshold. Resolves to success or rejects to error. |
| `duration` | `number` | `1100` | Required hold in milliseconds, clamped to 300–5000. |
| `resetAfter` | `number` | `2200` | Delay before returning to idle. Set 0 to keep the result visible. |
| `labels` | `Partial<Record<HoldStatus, string>>` | – | Labels for idle, holding, pending, success and error. |
| `onStatusChange` | `(status: HoldStatus) => void` | – | Every change of state, e.g. to move something else in step with the hold. |
| `icon` | `ReactNode` | – | Idle and holding icon. Defaults to a fingerprint. |
| `disabled` | `boolean` | `false` | Disables the action and cancels an active hold. |

## States

- **hold**: The icon ring fills linearly over the configured threshold (1100ms by default). A short press does not confirm.
- **release**: Early release, Escape, focus loss or pointer interruption cancels the hold. The ring resets over 240ms.
- **pending**: After the threshold, the callback runs once. The label morphs and the progress icon gives way to a spinner.
- **success / error**: A resolved callback draws a check; a rejected callback shows an alert and offers another hold. The button resets after 2200ms by default.

## What it handles

A confirm dialog interrupts the page, and people click through it without reading. Holding a button asks for intention without opening anything.

- **Intent without a dialog**: Press and hold while a fine ring fills round the icon. The hold takes 1.1 seconds by default, between 0.3 and 5.
- **Letting go cancels**: Releasing early, pressing Escape, leaving the button or losing focus resets the ring.
- **It runs once**: The callback runs once after the hold, and a normal click never triggers it.
- **It shows the result**: The label morphs, a spinner shows while the promise runs, then a drawn check or an alert.
- **Retry with another hold**: If the callback fails, the button shows the error and the next hold tries again.
- **Works from the keyboard**: Hold Space or Enter. Key repeat doesn’t restart the timer, and announcements live outside the button.

## Replaces

a confirm dialog, hold timer code, a progress ring, pending and error states, cancel-on-release logic

## Where it fits

- **Destructive actions**: Delete a project, a workspace or an account.
- **Payments and transfers**: Send a payment or a payout that can’t easily be called back.
- **Publishing and deploys**: Publish to production or send a campaign to everyone.
- **Bulk actions**: Archive hundreds of records in one go.

## Notes

- Hold the pointer, Space or Enter to confirm. Key repeat does not restart the timer, and a normal click never triggers the callback.
- Releasing early, leaving the button, losing focus, Escape or a hidden browser tab cancels an incomplete hold. A completed action continues through its promise.
- State announcements live outside the button. The button retains focus while pending or showing its result.
- Reduced motion removes icon movement and spinning. The functional progress ring still communicates the hold duration.
- Use clear action labels and provide undo in your application where appropriate.

License: MIT (https://hairlineui.com/license#components)
