Hold to confirm
Hold to fill. Let go early and it drains.
Installation
Run the CLI
Run
pnpm dlx asta-ui@latest initonce, then this. The file lands incomponents/astabyte/and the colour tokens go into your CSS.$ pnpm dlx asta-ui@latest add hold-to-confirmOr install the package
Don’t want the file? Import from
asta-ui/hold-to-confirmand update withpnpm update asta-ui. Add the stylesheet once. More.$ pnpm add asta-uiimport { HoldToConfirm } from "asta-ui/hold-to-confirm";Or copy it by hand
Copy this file. Keep the paths, or fix the
@/imports. Then add the tokens fromglobals.css."use client"; import { useEffect, useRef, useState } from "react"; const DURATION = 1400; const TICKS = 24; /** * Destructive actions ask for commitment. Hold (pointer, Space or Enter) to fill: the label flips colour * exactly at the fill edge, a ruler of ticks lights up, and the button trembles harder near the end. * Let go early and it drains away. */ export default function HoldToConfirm() { const [p, setP] = useState(0); const [done, setDone] = useState(false); const [shake, setShake] = useState(0); const raf = useRef(0); const reset = useRef<ReturnType<typeof setTimeout>>(undefined); const holding = useRef(false); const progress = useRef(0); const loop = (dir: 1 | -1) => { // Reduced motion: the fill still tracks the hold, but the button doesn't tremble. const still = matchMedia("(prefers-reduced-motion: reduce)").matches; let last = performance.now(); const step = (t: number) => { const dt = t - last; last = t; progress.current = Math.max(0, Math.min(1, progress.current + (dir === 1 ? dt / DURATION : -dt / 380))); setP(progress.current); setShake(dir === 1 && !still ? Math.sin(t / 22) * progress.current ** 2 * 1.8 : 0); if (dir === 1 && progress.current >= 1) { holding.current = false; setDone(true); setShake(0); navigator.vibrate?.(24); reset.current = setTimeout(() => { setDone(false); progress.current = 0; setP(0); }, 1800); return; } if (dir === -1 && progress.current <= 0) return; raf.current = requestAnimationFrame(step); }; cancelAnimationFrame(raf.current); raf.current = requestAnimationFrame(step); }; const press = () => { if (done || holding.current) return; holding.current = true; loop(1); }; const release = () => { if (!holding.current) return; holding.current = false; loop(-1); }; // Let go when the tab is hidden mid-hold. Stop the loop and the reset timer on unmount. useEffect(() => { const hide = () => document.hidden && release(); document.addEventListener("visibilitychange", hide); return () => { document.removeEventListener("visibilitychange", hide); cancelAnimationFrame(raf.current); clearTimeout(reset.current); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const label = ( <> {done ? ( <svg viewBox="0 0 24 24" className="h-[18px] w-[18px]" fill="none" stroke="currentColor" strokeWidth="2.4" aria-hidden> <path d="M5 12.5l4.5 4.5L19 7.5" strokeDasharray="24" className="animate-[hold-draw_.5s_cubic-bezier(.16,1,.3,1)_both]" /> </svg> ) : ( <svg viewBox="0 0 24 24" className="h-[18px] w-[18px]" fill="none" stroke="currentColor" strokeWidth="1.8" aria-hidden> <path d="M4 7h16M10 11v6M14 11v6M6 7l1 12h10l1-12M9 7V4h6v3" /> </svg> )} {done ? "Deleted" : p > 0.02 ? "Keep holding" : "Hold to delete"} </> ); return ( <div className="flex h-full w-full flex-col items-center justify-center gap-5"> <style href="asta-holdtoconfirm" precedence="asta">{`@keyframes hold-draw { from { stroke-dashoffset: 24 } to { stroke-dashoffset: 0 } }`}</style> <button type="button" onPointerDown={press} onPointerUp={release} onPointerLeave={release} onPointerCancel={release} onKeyDown={(e) => { if ((e.key === " " || e.key === "Enter") && !e.repeat) { e.preventDefault(); press(); } }} onKeyUp={(e) => (e.key === " " || e.key === "Enter") && release()} onBlur={release} onContextMenu={(e) => e.preventDefault()} aria-label={done ? "Deleted" : "Hold to delete"} className="relative h-[60px] w-60 select-none overflow-hidden bg-paper-2 text-[15px] font-medium tracking-[-0.01em] text-ink shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_14%,transparent)] outline-none [touch-action:none] focus-visible:shadow-[inset_0_0_0_2px_var(--color-signal)]" style={{ transform: `translateX(${shake}px) scale(${1 - p * 0.04})` }} > <span className="absolute inset-0 flex items-center justify-center gap-2.5">{label}</span> {/* Same label in the fill colour, clipped to the fill: the colour change sits exactly on the edge. */} <span aria-hidden className={`absolute inset-0 flex items-center justify-center gap-2.5 bg-signal text-on-signal ${done ? "animate-[hold-flash_.6s_ease-out]" : ""}`} style={{ clipPath: `inset(0 ${(1 - p) * 100}% 0 0)` }} > {label} </span> <style href="asta-holdtoconfirm-2" precedence="asta">{`@keyframes hold-flash { 0% { filter: brightness(1.6) } 100% { filter: none } }`}</style> </button> <div className="flex items-start gap-[3px]" aria-hidden> {Array.from({ length: TICKS }, (_, i) => { const lit = p * TICKS > i; const major = i % 6 === 5; // Full height always, grown with scaleY from the top: no layout work per frame. const full = major ? 14 : 10; const h = lit ? full : major ? 10 : 6; return ( <span key={i} className="w-[3px] origin-top transition-[transform,background-color] duration-150 motion-reduce:transition-none" style={{ height: full, transform: `scaleY(${h / full})`, background: lit ? "var(--color-signal)" : "color-mix(in srgb, var(--color-ink) 18%, transparent)" }} /> ); })} </div> </div> ); }Or ask your AI agent
Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.
Add the Astabyte UI Hold to confirm component to my project. Hold to fill. Let go early and it drains. It fills its parent, so give the wrapper a size. It is designed on a dark surface: put it inside an element with the `dark` class. Install it: ```sh pnpm dlx asta-ui@latest add hold-to-confirm --radix ``` Or with the shadcn CLI: ```sh pnpm dlx shadcn@latest add https://ui.astabyte.com/r/hold-to-confirm.json ``` If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4). Import it like this: ```tsx import HoldToConfirm from "@/components/astabyte/hold-to-confirm"; ``` Usage example and props: https://ui.astabyte.com/hold-to-confirm Full catalogue for context: https://ui.astabyte.com/llms.txt Rules: - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS. - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback. - Keep keyboard, focus and ARIA behaviour. - Use fake data and fake brand names in examples. Use it to: [describe where it goes and what it should do]
Usage
It fills its parent. Give the wrapper a size.
import HoldToConfirm from "@/components/astabyte/hold-to-confirm";
export default function Page() {
// Hold to confirm
return (
<div className="relative h-96 w-full">
<HoldToConfirm />
</div>
);
}