# Hold to Confirm

> A button that fills while held, so destructive actions can never happen by accident.

Category: Buttons · Free · Updated 2026-10-20 · Live demo: https://www.motionkitui.com/components/hold-button

## When to use it

Anything that should never happen by accident: deleting a project, cancelling a subscription or sending a payment. It replaces the "Are you sure?" dialog with a gesture that is impossible to trigger by mistake.

## How it works

Pressing starts a linear Motion animation that fills a bar across the button over the duration you set. Let go early and the fill drains back in 0.3 seconds. Hold to the end and onConfirm fires. Space and Enter count as holding too, and screen readers hear a hint telling them how long to hold.

## Installation

```bash
npx shadcn@latest add https://www.motionkitui.com/r/hold-button.json
```

Or copy `components/motionkit/hold-button.tsx` below. Requires react, motion and Tailwind CSS v4. Free for personal projects; commercial use needs MotionKit Plus (https://www.motionkitui.com/pricing).

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `duration` | `number` | `1.2` | Seconds the button must be held. |
| `onConfirm` | `() => void` |  | Called once the hold completes. |
| `color` | `string` | `"#8C3216"` | Fill colour while holding. |
| `confirmedLabel` | `string` | `"Done"` | Shown briefly after confirming. |

## Accessibility

- **Keyboard: Pass.** A native <button>. Holding Space or Enter fills it just like holding the pointer, and letting go early drains it.
- **Screen readers: Pass.** The hold instruction is read as the button's description, and a live region announces when it confirms.
- **Reduced motion: Pass.** The fill is a progress indicator, not decoration, so it stays. Nothing else moves.
- **Colour contrast: Pass.** Label measures 13.6:1 on the button and 7.2:1 on the fill.

## Source

```tsx
"use client";

import { useEffect, useId, useRef, useState, type KeyboardEvent, type ReactNode } from "react";
import { animate, motion, useMotionValue, type AnimationPlaybackControls } from "motion/react";

type HoldButtonProps = {
  children: ReactNode;
  /** Called once the hold completes. */
  onConfirm?: () => void;
  /** Seconds the button must be held. */
  duration?: number;
  /** Fill colour while holding. */
  color?: string;
  /** Shown briefly after confirming. */
  confirmedLabel?: string;
  className?: string;
};

export function HoldButton({
  children,
  onConfirm,
  duration = 1.2,
  color = "#8C3216",
  confirmedLabel = "Done",
  className = "",
}: HoldButtonProps) {
  const progress = useMotionValue(0);
  const controls = useRef<AnimationPlaybackControls | null>(null);
  const holding = useRef(false);
  const [confirmed, setConfirmed] = useState(false);
  const hintId = useId();

  useEffect(() => {
    if (!confirmed) return;
    const timer = window.setTimeout(() => {
      setConfirmed(false);
      progress.set(0);
    }, 1600);
    return () => window.clearTimeout(timer);
  }, [confirmed, progress]);

  function start() {
    if (holding.current || confirmed) return;
    holding.current = true;
    controls.current?.stop();
    controls.current = animate(progress, 1, {
      duration: duration * (1 - progress.get()),
      ease: "linear",
      onComplete: () => {
        holding.current = false;
        setConfirmed(true);
        onConfirm?.();
      },
    });
  }

  function cancel() {
    if (!holding.current) return;
    holding.current = false;
    controls.current?.stop();
    // drain back quickly so an accidental tap never confirms
    controls.current = animate(progress, 0, { duration: 0.3, ease: "easeOut" });
  }

  // Space and Enter work too: holding the key counts as holding the button
  function handleKeyDown(event: KeyboardEvent<HTMLButtonElement>) {
    if (event.key !== " " && event.key !== "Enter") return;
    event.preventDefault();
    if (!event.repeat) start();
  }

  function handleKeyUp(event: KeyboardEvent<HTMLButtonElement>) {
    if (event.key === " " || event.key === "Enter") cancel();
  }

  return (
    <>
      <button
        type="button"
        onPointerDown={start}
        onPointerUp={cancel}
        onPointerLeave={cancel}
        onKeyDown={handleKeyDown}
        onKeyUp={handleKeyUp}
        onContextMenu={(event) => event.preventDefault()}
        aria-describedby={hintId}
        className={`relative isolate inline-flex select-none items-center justify-center overflow-hidden rounded-xl border border-white/10 bg-[#1D2727] px-5 py-3 text-sm font-medium text-[#F6F1E8] [touch-action:none] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[#5FAEB4] ${className}`}
      >
        <motion.span
          aria-hidden
          className="absolute inset-0 -z-10 origin-left"
          style={{ scaleX: progress, backgroundColor: color }}
        />
        <span className="inline-flex items-center gap-2">{confirmed ? confirmedLabel : children}</span>
      </button>
      <span id={hintId} className="sr-only">
        Press and hold for {duration} seconds to confirm.
      </span>
      <span className="sr-only" aria-live="polite">
        {confirmed ? confirmedLabel : ""}
      </span>
    </>
  );
}
```
