kipluo
React7 min read4 steps

How to Build a Hold-to-Confirm Button in React

Hold-to-confirm replaces the "Are you sure?" dialog for destructive actions. The user presses and holds; a ring fills; releasing early cancels. This guide builds it with pointer and keyboard support and explains where it is and isn't a good fit.

You needReact 18+Pointer EventsSVG stroke-dasharray
Loading demo
Press and hold the button (or hold Space) until the ring fills. Let go early and it drains back.Component page

1. When hold-to-confirm is the right pattern

It suits actions that are destructive but common enough that a modal dialog becomes tedious: deleting a draft, ending a call, clearing a cart. The hold duration (around 700–1200 ms) is the friction that prevents accidents.

It is a poor fit when the action can't be undone and has serious consequences, such as deleting an account. Use a typed confirmation there. Also make sure the hold time isn't so long that users with motor impairments struggle to complete it.

2. Drive progress with requestAnimationFrame

On press, record the start time and run a loop that computes progress = elapsed / duration. When it reaches 1, fire onConfirm once. On early release, cancel the loop and animate progress back to 0.

Pointer capture keeps the hold alive if the finger drifts slightly outside the button. The same start and stop functions run on Space and Enter keydown and keyup, ignoring key repeat.

HoldButton.tsx
import { useEffect, useRef, useState } from "react";

const R = 18;
const CIRC = 2 * Math.PI * R;

export function HoldButton({
  duration = 900,
  label = "Hold to delete",
  onConfirm,
}: {
  duration?: number;
  label?: string;
  onConfirm: () => void;
}) {
  const [progress, setProgress] = useState(0);
  const [done, setDone] = useState(false);
  const raf = useRef(0);
  const holding = useRef(false);
  const value = useRef(0);

  useEffect(() => () => cancelAnimationFrame(raf.current), []);

  const animate = (toward: 0 | 1) => {
    cancelAnimationFrame(raf.current);
    let last = performance.now();
    const step = (now: number) => {
      const dt = now - last;
      last = now;
      // fill at 1x speed, drain at 3x speed
      value.current += toward === 1 ? dt / duration : (-3 * dt) / duration;
      value.current = Math.min(1, Math.max(0, value.current));
      setProgress(value.current);
      if (toward === 1 && value.current >= 1) {
        holding.current = false;
        setDone(true);
        onConfirm();
        return;
      }
      if (toward === 0 && value.current <= 0) return;
      raf.current = requestAnimationFrame(step);
    };
    raf.current = requestAnimationFrame(step);
  };

  const start = () => {
    if (done || holding.current) return;
    holding.current = true;
    animate(1);
  };
  const stop = () => {
    if (!holding.current) return;
    holding.current = false;
    animate(0);
  };

  return (
    <button
      type="button"
      aria-label={done ? "Deleted" : `${label}. Press and hold.`}
      onPointerDown={(e) => {
        e.currentTarget.setPointerCapture(e.pointerId);
        start();
      }}
      onPointerUp={stop}
      onPointerCancel={stop}
      onKeyDown={(e) => {
        if ((e.key === " " || e.key === "Enter") && !e.repeat) {
          e.preventDefault();
          start();
        }
      }}
      onKeyUp={(e) => {
        if (e.key === " " || e.key === "Enter") stop();
      }}
      onBlur={stop}
      className="inline-flex items-center gap-2 rounded-full border px-4 py-2 select-none touch-none"
    >
      <svg width="44" height="44" viewBox="0 0 44 44" aria-hidden>
        <circle cx="22" cy="22" r={R} fill="none" stroke="#e4e4e7" strokeWidth="4" />
        <circle
          cx="22"
          cy="22"
          r={R}
          fill="none"
          stroke="#dc2626"
          strokeWidth="4"
          strokeDasharray={CIRC}
          strokeDashoffset={CIRC * (1 - progress)}
          transform="rotate(-90 22 22)"
        />
      </svg>
      {done ? "Deleted" : label}
    </button>
  );
}

3. Tell people it's a hold

A tap on a hold button does nothing, which can look broken. Put "Hold" in the visible label, and if a user taps and releases quickly, briefly show a hint such as "Keep holding". The accessible name above also says "Press and hold" for screen-reader users.

4. The Kipluo version

Kipluo's free Hold to Confirm fills at a steady rate, springs the ring back when released early, squeezes the button on a spring while held, trembles slightly near the end, and plays rising ticks while holding and a chime when done. It also announces the confirmed state in a live region. Props match the hand-built version: duration, label, confirmedLabel and onConfirm.

DeleteDraft.tsx
// npx kipluo add hold-to-confirm
import { HoldToConfirm } from "@/components/kipluo/hold-to-confirm";

export function DeleteDraft({ onDelete }: { onDelete: () => void }) {
  return <HoldToConfirm duration={900} label="Hold to delete" confirmedLabel="Deleted" onConfirm={onDelete} />;
}

Accessibility checklist

Tick each one off as you check your build. Your ticks stay in this browser.

0 of 4 checked

Build it yourself or install it

Build it yourself when

One destructive button where a linear fill is fine. The component above is complete.

Install Kipluo when

You want spring release, tactile tremble and sound cues, a live-region announcement, theme styles and the Flutter HoldToConfirm widget.

Keep building