Hold to confirm
A destructive-action button you have to press and hold: a fill sweeps across, letting go early drains it back, and finishing locks in with a check.
"use client";
import { useEffect, useId, useRef, useState } from "react";
import "./hold-button.css";
type HoldButtonProps = {
label?: string;
doneLabel?: string;
/** How long to hold, in milliseconds. */
duration?: number;
onConfirm?: () => void;
theme?: "light" | "dark";
};
/**
* HoldButton
* - Hold the pointer (or Space / Enter) for `duration` ms to confirm.
* - Progress lives in a CSS variable --p (0..1) updated every frame.
* - Releasing early drains the fill back to zero, twice as fast.
*/
export function HoldButton({
label = "Hold to delete",
doneLabel = "Deleted",
duration = 1200,
onConfirm,
theme = "light",
}: HoldButtonProps) {
const id = useId();
const btn = useRef<HTMLButtonElement>(null);
const [done, setDone] = useState(false);
const holding = useRef(false);
const progress = useRef(0);
const raf = useRef(0);
const loop = (last: number) => (now: number) => {
const dt = now - last;
progress.current += holding.current ? dt / duration : -(dt / duration) * 2;
progress.current = Math.min(1, Math.max(0, progress.current));
btn.current?.style.setProperty("--p", progress.current.toFixed(4));
if (progress.current >= 1) {
holding.current = false;
setDone(true);
onConfirm?.();
return;
}
if (progress.current > 0 || holding.current)
raf.current = requestAnimationFrame(loop(now));
};
const start = () => {
if (done) return;
holding.current = true;
cancelAnimationFrame(raf.current);
raf.current = requestAnimationFrame(loop(performance.now()));
};
const stop = () => {
holding.current = false;
};
// After confirming, reset so the demo can be tried again.
useEffect(() => {
if (!done) return;
const t = setTimeout(() => {
progress.current = 0;
btn.current?.style.setProperty("--p", "0");
setDone(false);
}, 2400);
return () => clearTimeout(t);
}, [done]);
useEffect(() => () => cancelAnimationFrame(raf.current), []);
return (
<div className="gu-hold" data-theme={theme}>
<button
ref={btn}
type="button"
className="gu-hold__btn"
data-done={done || undefined}
aria-describedby={`${id}-hint`}
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) => (e.key === " " || e.key === "Enter") && stop()}
onContextMenu={(e) => e.preventDefault()}
>
<span className="gu-hold__fill" aria-hidden="true" />
<span className="gu-hold__label">
{done ? (
<svg viewBox="0 0 20 20" aria-hidden="true">
<path d="m4 10.5 4 4 8-9" />
</svg>
) : (
<svg viewBox="0 0 20 20" aria-hidden="true">
<path d="M4 6h12M8 6V4h4v2M6 6l1 10h6l1-10" />
</svg>
)}
{done ? doneLabel : label}
</span>
</button>
<p id={`${id}-hint`} className="gu-hold__hint">
Press and hold for {duration / 1000} seconds. This cannot be undone.
</p>
<p className="gu-hold__sr" role="status">
{done ? doneLabel : ""}
</p>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Hold to confirm
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A "press and hold to confirm" button for destructive actions. While held, an orange fill sweeps from left to right; releasing early drains it back. Completing turns the button lime with a check mark.
## Layout and look
- 58px tall, min 220px wide, radius 16px, dark #0e0e10 with cream text (inverted in dark mode), weight 800, trash icon + label.
- Fill: #ff5a1f, scaled on X from the left by progress. The label color blends from cream to dark text as progress rises (color-mix by --p).
- Done: background #d7ff3a, dark text, check icon, a small pop.
- A muted hint underneath: "Press and hold for 1.2 seconds. This cannot be undone."
## Behavior and motion
1. Progress p (0..1) advances by dt / duration (1200ms) each animation frame while held, and drains at twice that speed when released.
2. p is written to a CSS variable --p: fill transform scaleX(p), button scale(1 - p × 0.04) to feel like pressing in.
3. At p = 1: call onConfirm, switch to the done state with a 500ms overshoot pop (scale 1.06 at 40%), and reset after 2.4s in the demo.
4. Pointer capture keeps the hold active if the finger slides slightly.
## Touch
touch-action: none, no text selection, no long-press context menu, so holding feels native.
## Accessibility
- A real <button>; Space or Enter held down works the same (ignores key repeat).
- aria-describedby points to the hint; a role="status" region announces the result.
- prefers-reduced-motion: keep the progress fill (essential feedback) but no squeeze and no pop.
## Props
label, doneLabel, duration (ms), onConfirm, theme.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### HoldButton.tsx
```tsx
"use client";
import { useEffect, useId, useRef, useState } from "react";
import "./hold-button.css";
type HoldButtonProps = {
label?: string;
doneLabel?: string;
/** How long to hold, in milliseconds. */
duration?: number;
onConfirm?: () => void;
theme?: "light" | "dark";
};
/**
* HoldButton
* - Hold the pointer (or Space / Enter) for `duration` ms to confirm.
* - Progress lives in a CSS variable --p (0..1) updated every frame.
* - Releasing early drains the fill back to zero, twice as fast.
*/
export function HoldButton({
label = "Hold to delete",
doneLabel = "Deleted",
duration = 1200,
onConfirm,
theme = "light",
}: HoldButtonProps) {
const id = useId();
const btn = useRef<HTMLButtonElement>(null);
const [done, setDone] = useState(false);
const holding = useRef(false);
const progress = useRef(0);
const raf = useRef(0);
const loop = (last: number) => (now: number) => {
const dt = now - last;
progress.current += holding.current ? dt / duration : -(dt / duration) * 2;
progress.current = Math.min(1, Math.max(0, progress.current));
btn.current?.style.setProperty("--p", progress.current.toFixed(4));
if (progress.current >= 1) {
holding.current = false;
setDone(true);
onConfirm?.();
return;
}
if (progress.current > 0 || holding.current)
raf.current = requestAnimationFrame(loop(now));
};
const start = () => {
if (done) return;
holding.current = true;
cancelAnimationFrame(raf.current);
raf.current = requestAnimationFrame(loop(performance.now()));
};
const stop = () => {
holding.current = false;
};
// After confirming, reset so the demo can be tried again.
useEffect(() => {
if (!done) return;
const t = setTimeout(() => {
progress.current = 0;
btn.current?.style.setProperty("--p", "0");
setDone(false);
}, 2400);
return () => clearTimeout(t);
}, [done]);
useEffect(() => () => cancelAnimationFrame(raf.current), []);
return (
<div className="gu-hold" data-theme={theme}>
<button
ref={btn}
type="button"
className="gu-hold__btn"
data-done={done || undefined}
aria-describedby={`${id}-hint`}
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) => (e.key === " " || e.key === "Enter") && stop()}
onContextMenu={(e) => e.preventDefault()}
>
<span className="gu-hold__fill" aria-hidden="true" />
<span className="gu-hold__label">
{done ? (
<svg viewBox="0 0 20 20" aria-hidden="true">
<path d="m4 10.5 4 4 8-9" />
</svg>
) : (
<svg viewBox="0 0 20 20" aria-hidden="true">
<path d="M4 6h12M8 6V4h4v2M6 6l1 10h6l1-10" />
</svg>
)}
{done ? doneLabel : label}
</span>
</button>
<p id={`${id}-hint`} className="gu-hold__hint">
Press and hold for {duration / 1000} seconds. This cannot be undone.
</p>
<p className="gu-hold__sr" role="status">
{done ? doneLabel : ""}
</p>
</div>
);
}
```
### hold-button.css
```css
/* HoldButton: change these colors to match your brand. */
.gu-hold {
--idle: #0e0e10; /* button before holding */
--idle-text: #f3eee3;
--fill: #ff5a1f; /* the sweeping fill */
--fill-text: #0e0e10;
--done: #d7ff3a; /* confirmed state */
--muted: #57534b;
--focus: #2b35ff;
display: grid;
justify-items: center;
gap: 14px;
font-family: system-ui, sans-serif;
}
.gu-hold[data-theme="dark"] {
--idle: #f3eee3;
--idle-text: #0e0e10;
--muted: #aea89c;
--focus: #d7ff3a;
}
.gu-hold__btn {
--p: 0;
position: relative;
overflow: hidden;
min-width: 220px;
min-height: 58px;
padding: 0 28px;
border: 0;
border-radius: 16px;
background: var(--idle);
color: var(--idle-text);
font: inherit;
font-weight: 800;
cursor: pointer;
user-select: none;
-webkit-user-select: none;
-webkit-touch-callout: none;
touch-action: none;
/* Squeeze a little while held, scaled by progress */
transform: scale(calc(1 - var(--p) * 0.04));
}
.gu-hold__btn:focus-visible {
outline: 3px solid var(--focus);
outline-offset: 3px;
}
.gu-hold__fill {
position: absolute;
inset: 0;
background: var(--fill);
transform: scaleX(var(--p));
transform-origin: left;
}
/* The label switches color exactly where the fill has reached (two stacked clips) */
.gu-hold__label {
position: relative;
display: inline-flex;
align-items: center;
gap: 10px;
}
.gu-hold__btn:not([data-done]) .gu-hold__label {
color: color-mix(
in srgb,
var(--fill-text) calc(var(--p) * 100%),
var(--idle-text)
);
}
.gu-hold__label svg {
width: 20px;
height: 20px;
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
}
.gu-hold__btn[data-done] {
background: var(--done);
color: #0e0e10;
animation: gu-hold-pop 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.gu-hold__btn[data-done] .gu-hold__fill {
opacity: 0;
}
@keyframes gu-hold-pop {
40% {
transform: scale(1.06);
}
}
.gu-hold__hint {
margin: 0;
color: var(--muted);
font-size: 0.85rem;
}
.gu-hold__sr {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
}
@media (prefers-reduced-motion: reduce) {
.gu-hold__btn[data-done] {
animation: none;
}
.gu-hold__btn {
transform: none;
}
}
```
You might also want
Next piece: Invoicing app landing pageRadial action menu
Components, intermediate
Smart tooltips
Components, intermediate
Split action button
Components, intermediate