Skip to content

Hold to Confirm

Hold to confirm button for destructive actions: progress fills while you press, early release cancels, Space works too.

  • Buttons
  • Developer tools
  • React only · 0 deps
  • 1 file
Full screen

Browser support · All evergreen browsers. Uses pointer events and clip-path.

Install. One command, or copy the file.

$npx shadcn@latest add https://freedesignmd.com/r/hold-to-confirm.json
  • Byte-identical to the preview
  • No Tailwind or config needed
  • SSR-safe · keyboard accessible
  • Respects reduced motion & transparency
components/buttons/hold-to-confirm.tsx
1/**
2 * HoldToConfirm, press-and-hold to run a destructive or irreversible action.
3 * Progress fills through the button; releasing early drains it back and
4 * cancels. Works with pointer, touch and keyboard (hold Space or Enter).
5 *
6 * Props: onConfirm, duration (ms, default 1500), tone ("danger" | "neutral" |
7 * "accent"), size ("md" | "lg"), label, holdingLabel, doneLabel, resetAfter
8 * (ms before it returns to idle, default 2200; 0 = stay done), onProgress,
9 * onCancel, theme, disabled. CSS vars: --fd-hold-color, --fd-hold-radius.
10 *
11 * Browser support: all evergreen browsers (clip-path inset + CSS variables).
12 * Accessibility: native <button>; a polite live region announces start,
13 * cancel and completion; instructions are linked with aria-describedby.
14 * Holding via keyboard cancels on keyup or blur. Reduced motion: no scale or
15 * shake, the fill still shows progress.
16 */
17import { useCallback, useEffect, useId, useRef, useState } from "react";
18import type { ReactNode } from "react";
19
20export interface HoldToConfirmProps {
21 onConfirm?: () => void;
22 onCancel?: () => void;
23 onProgress?: (p: number) => void;
24 duration?: number;
25 tone?: "danger" | "neutral" | "accent";
26 size?: "md" | "lg";
27 label?: ReactNode;
28 holdingLabel?: ReactNode;
29 doneLabel?: ReactNode;
30 resetAfter?: number;
31 disabled?: boolean;
32 theme?: "light" | "dark";
33 icon?: ReactNode;
34 className?: string;
35}
36
37type Phase = "idle" | "holding" | "done";
38
39export function HoldToConfirm({
40 onConfirm,
41 onCancel,
42 onProgress,
43 duration = 1500,
44 tone = "danger",
45 size = "lg",
46 label = "Hold to confirm",
47 holdingLabel = "Keep holding…",
48 doneLabel = "Confirmed",
49 resetAfter = 2200,
50 disabled,
51 theme,
52 icon,
53 className,
54}: HoldToConfirmProps) {
55 const hintId = useId();
56 const btn = useRef<HTMLButtonElement>(null);
57 const [phase, setPhase] = useState<Phase>("idle");
58 const [msg, setMsg] = useState("");
59 const st = useRef({ p: 0, dir: 0, raf: 0, t0: 0, from: 0, phase: "idle" as Phase, key: false });
60 const cb = useRef({ onConfirm, onCancel, onProgress, duration, resetAfter });
61 cb.current = { onConfirm, onCancel, onProgress, duration, resetAfter };
62 const timer = useRef(0);
63
64 const paint = (p: number) => {
65 st.current.p = p;
66 btn.current?.style.setProperty("--h-p", p.toFixed(4));
67 cb.current.onProgress?.(p);
68 };
69 const setP = (ph: Phase) => {
70 st.current.phase = ph;
71 setPhase(ph);
72 };
73
74 const loop = useCallback((t: number) => {
75 const s = st.current;
76 if (s.dir === 1) {
77 const p = Math.min(1, s.from + (t - s.t0) / cb.current.duration);
78 paint(p);
79 if (p >= 1) {
80 s.dir = 0;
81 s.raf = 0;
82 setP("done");
83 setMsg("Confirmed");
84 cb.current.onConfirm?.();
85 if (cb.current.resetAfter > 0)
86 timer.current = window.setTimeout(() => {
87 paint(0);
88 setP("idle");
89 setMsg("");
90 }, cb.current.resetAfter);
91 return;
92 }
93 } else if (s.dir === -1) {
94 const k = Math.min(1, (t - s.t0) / (260 + 240 * s.from));
95 const p = s.from * (1 - (1 - Math.pow(1 - k, 3)));
96 paint(k >= 1 ? 0 : p);
97 if (k >= 1) {
98 s.dir = 0;
99 s.raf = 0;
100 return;
101 }
102 }
103 s.raf = requestAnimationFrame(loop);
104 }, []);
105
106 const start = () => {
107 const s = st.current;
108 if (disabled || s.phase === "done") return;
109 cancelAnimationFrame(s.raf);
110 s.dir = 1;
111 s.from = s.p;
112 s.t0 = performance.now();
113 setP("holding");
114 setMsg("Hold to confirm");
115 s.raf = requestAnimationFrame(loop);
116 };
117 const stop = () => {
118 const s = st.current;
119 if (s.phase !== "holding") return;
120 cancelAnimationFrame(s.raf);
121 s.dir = -1;
122 s.from = s.p;
123 s.t0 = performance.now();
124 setP("idle");
125 setMsg("Cancelled");
126 cb.current.onCancel?.();
127 s.raf = requestAnimationFrame(loop);
128 };
129
130 useEffect(
131 () => () => {
132 cancelAnimationFrame(st.current.raf);
133 window.clearTimeout(timer.current);
134 },
135 [],
136 );
137
138 const shown = phase === "done" ? doneLabel : phase === "holding" ? holdingLabel : label;
139
140 return (
141 <span className={`fd-hold${className ? " " + className : ""}`} data-fd-theme={theme} data-tone={tone} data-size={size} data-phase={phase}>
142 <style>{css}</style>
143 <button
144 ref={btn}
145 type="button"
146 className="fd-hold-btn"
147 disabled={disabled}
148 aria-describedby={hintId}
149 onPointerDown={(e) => {
150 if (e.button !== 0) return;
151 e.currentTarget.setPointerCapture?.(e.pointerId);
152 start();
153 }}
154 onPointerUp={stop}
155 onPointerCancel={stop}
156 onLostPointerCapture={stop}
157 onKeyDown={(e) => {
158 if ((e.key === " " || e.key === "Enter") && !e.repeat) {
159 e.preventDefault();
160 st.current.key = true;
161 start();
162 }
163 }}
164 onKeyUp={(e) => {
165 if ((e.key === " " || e.key === "Enter") && st.current.key) {
166 st.current.key = false;
167 stop();
168 }
169 }}
170 onBlur={() => {
171 st.current.key = false;
172 stop();
173 }}
174 onContextMenu={(e) => e.preventDefault()}
175 >
176 <span className="fd-hold-face" data-layer="base">
177 {icon && <span className="fd-hold-icon">{icon}</span>}
178 <span className="fd-hold-text">{shown}</span>
179 </span>
180 <span className="fd-hold-fill" aria-hidden="true">
181 <span className="fd-hold-face" data-layer="on">
182 {phase === "done" ? (
183 <svg aria-hidden="true" focusable="false" className="fd-hold-check" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round"><path d="M5 12.5l4.5 4.5L19 7.5" /></svg>
184 ) : (
185 icon && <span className="fd-hold-icon">{icon}</span>
186 )}
187 <span className="fd-hold-text">{shown}</span>
188 </span>
189 </span>
190 </button>
191 <span id={hintId} className="fd-hold-sr">
192 Press and hold for {(duration / 1000).toFixed(1)} seconds, or hold the Space key.
193 </span>
194 <span className="fd-hold-sr" role="status" aria-live="polite">{msg}</span>
195 </span>
196 );
197}
198
199const css = `
200:where(.fd-hold,.fd-hold *){box-sizing:border-box;margin:0;padding:0;font:inherit;letter-spacing:inherit;color:inherit;text-decoration:none;list-style:none}
201:where(.fd-hold,.fd-hold *)::before,:where(.fd-hold,.fd-hold *)::after{box-sizing:border-box}
202:where(.fd-hold svg){display:block;flex-shrink:0}
203:where(.fd-hold button){appearance:none;-webkit-appearance:none;background:none;border:0;border-radius:0}
204.fd-hold{
205 --fd-ease-out:cubic-bezier(.22,1,.36,1);--fd-spring:cubic-bezier(.34,1.56,.64,1);--fd-dur-1:120ms;--fd-dur-2:240ms;--fd-dur-3:420ms;
206 --h-c:#E5334B;--h-soft:rgba(229,51,75,.1);--h-fg:#B3162E;--h-on:#fff;--h-line:rgba(229,51,75,.28);
207 display:inline-flex;vertical-align:middle;position:relative;
208 font:500 15px/1 ui-sans-serif,-apple-system,BlinkMacSystemFont,"SF Pro Text","Inter","Segoe UI",Roboto,sans-serif;
209}
210.fd-hold[data-tone="neutral"]{--h-c:#0B0B0F;--h-soft:rgba(11,11,15,.06);--h-fg:#0B0B0F;--h-on:#fff;--h-line:rgba(11,11,15,.14)}
211.fd-hold[data-tone="accent"]{--h-c:#007AFF;--h-soft:rgba(0,122,255,.1);--h-fg:#0058C7;--h-on:#fff;--h-line:rgba(0,122,255,.28)}
212[data-fd-theme="dark"] .fd-hold,.fd-hold[data-fd-theme="dark"]{--h-c:#FF453A;--h-soft:rgba(255,69,58,.14);--h-fg:#FF8F87;--h-on:#1A0504;--h-line:rgba(255,69,58,.32)}
213[data-fd-theme="dark"] .fd-hold[data-tone="neutral"],.fd-hold[data-tone="neutral"][data-fd-theme="dark"]{--h-c:#F5F5F7;--h-soft:rgba(255,255,255,.07);--h-fg:#F5F5F7;--h-on:#0B0B0F;--h-line:rgba(255,255,255,.14)}
214[data-fd-theme="dark"] .fd-hold[data-tone="accent"],.fd-hold[data-tone="accent"][data-fd-theme="dark"]{--h-c:#0A84FF;--h-soft:rgba(10,132,255,.16);--h-fg:#66B2FF;--h-on:#fff;--h-line:rgba(10,132,255,.36)}
215.fd-hold[data-phase="done"]{--h-c:#1FA45B;--h-soft:rgba(31,164,91,.12);--h-fg:#0F7A41;--h-on:#fff;--h-line:rgba(31,164,91,.3)}
216[data-fd-theme="dark"] .fd-hold[data-phase="done"],.fd-hold[data-fd-theme="dark"][data-phase="done"]{--h-c:#30D158;--h-soft:rgba(48,209,88,.14);--h-fg:#7BE69B;--h-on:#04210E;--h-line:rgba(48,209,88,.32)}
217.fd-hold[data-size="md"]{--h-h:40px;--h-px:18px;--h-fs:15px}
218.fd-hold[data-size="lg"]{--h-h:52px;--h-px:26px;--h-fs:15px}
219.fd-hold-btn{--h-p:0;position:relative;display:block;overflow:hidden;height:var(--h-h);min-width:176px;padding:0 var(--h-px);border-radius:var(--fd-hold-radius,9999px);
220 appearance:none;-webkit-appearance:none;border:0;outline:0;cursor:pointer;user-select:none;-webkit-user-select:none;touch-action:none;-webkit-tap-highlight-color:transparent;-webkit-touch-callout:none;
221 background:var(--h-soft);color:var(--h-fg);font-weight:500;font-size:var(--h-fs);line-height:1;font-family:inherit;letter-spacing:-.005em;
222 box-shadow:0 0 0 1px var(--h-line),0 1px 2px rgba(11,11,15,.06);
223 transform:scale(1);transition:transform var(--fd-dur-3) var(--fd-spring),box-shadow var(--fd-dur-2) var(--fd-ease-out)}
224.fd-hold[data-phase="holding"] .fd-hold-btn{transform:scale(.97);transition:transform var(--fd-dur-1) var(--fd-ease-out);box-shadow:0 0 0 1px var(--h-c),0 6px 20px -8px var(--h-c)}
225.fd-hold-btn:focus-visible{outline:2px solid rgba(10,132,255,.8);outline-offset:2px}
226.fd-hold-btn:disabled{opacity:.4;cursor:not-allowed}
227.fd-hold-face{position:absolute;inset:0;display:flex;align-items:center;justify-content:center;gap:8px;padding:0 var(--h-px);white-space:nowrap}
228.fd-hold-fill{position:absolute;inset:0;background:var(--h-c);color:var(--h-on);
229 clip-path:inset(0 calc((1 - var(--h-p)) * 100% + 0px) 0 0);will-change:clip-path}
230.fd-hold[data-phase="done"] .fd-hold-fill{clip-path:inset(0)}
231.fd-hold-icon{display:grid;place-items:center;width:18px;height:18px}
232.fd-hold-icon svg{width:100%;height:100%}
233.fd-hold-text{display:block}
234.fd-hold-check path{stroke-dasharray:24;stroke-dashoffset:24;animation:fd-hold-draw 380ms var(--fd-ease-out) 80ms forwards}
235.fd-hold-sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
236@keyframes fd-hold-draw{to{stroke-dashoffset:0}}
237@media (prefers-reduced-motion:reduce){.fd-hold-btn,.fd-hold[data-phase="holding"] .fd-hold-btn{transform:none;transition:none}.fd-hold-check path{animation:none;stroke-dashoffset:0}}
238@media (prefers-contrast:more){.fd-hold-btn{box-shadow:0 0 0 1px currentColor}}
239`;
240
241/* ───────────────────────────── demo ───────────────────────────── */
242
243const trash = (
244 <svg aria-hidden="true" focusable="false" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M4 7h16M10 11v6M14 11v6M6 7l1 12a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2l1-12M9 7V4h6v3" /></svg>
245);
246
247export function Demo() {
248 return (
249 <div className="fd-holdd" data-fd-theme="light">
250 <style>{demoCss}</style>
251 <HoldToConfirm tone="danger" size="lg" icon={trash} duration={1500} label="Hold to delete" holdingLabel="Deleting" doneLabel="Deleted" theme="light" />
252 </div>
253 );
254}
255
256const demoCss = `
257.fd-holdd,.fd-holdd *,.fd-holdd *::before,.fd-holdd *::after{box-sizing:border-box}
258.fd-holdd{display:flex;align-items:center;justify-content:center;min-height:100vh;width:100%;margin:0;padding:32px 20px;overflow:hidden;background:#f4f4f4;color:#1d1d1f;font:400 15px/1.4 ui-sans-serif,-apple-system,BlinkMacSystemFont,"SF Pro Text","Inter","Segoe UI",Roboto,sans-serif;letter-spacing:normal;-webkit-font-smoothing:antialiased}
259`;

More Buttons. Same file contract.

Button Core, buttons preview: React button component with 5 variants, 3 sizes, icon slots, a loading to success morph and a springy press

Button Core

React button component with 5 variants, 3 sizes, icon slots, a loading to success morph and a springy press.

Shimmer Button, buttons preview: Shimmer button for React: a dark capsule with a travelling light sweep and rotating border glow, built for…

Shimmer Button

Shimmer button for React: a dark capsule with a travelling light sweep and rotating border glow, built for one main CTA.

Magnetic Button, buttons preview: Magnetic button for React that leans toward your cursor on a spring, with the label drifting further than the…

Magnetic Button

Magnetic button for React that leans toward your cursor on a spring, with the label drifting further than the shell.

Questions about Hold to Confirm

Run npx shadcn@latest add https://freedesignmd.com/r/hold-to-confirm.json, or open the page and copy the file into your project. It has zero dependencies besides React. What you copy is the exact file that renders the live demo.

Hold to confirm button for destructive actions: progress fills while you press, early release cancels, Space works too. It is a free, MIT-licensed React buttons component from the freedesignmd library.

Yes. It is free and MIT-licensed, use, modify and ship it in personal and commercial projects without attribution.

All evergreen browsers. Uses pointer events and clip-path.

Yes. It is a plain React component with its own styles, so it runs in Next.js, Vite, Remix and Lovable projects, and it needs no Tailwind setup. It has zero dependencies besides React.