Specification
- Catalogue
- No. 69 of 110
- Section
- Buttons and toggles
- Interaction
- Press, Feedback
- Element
- Button
- Preview
- Live component
- Imports
- react 19.3.1
- Added
Notes
A destructive action that asks for a one second hold instead of a second dialog. The fill grows with a slow ease-in while the button is held and drains back fast when it is released early.
Completion is detected with transitionend on the fill, so the timing lives in one place: the CSS duration. The label swaps with a short blur and rise, then the button resets after a moment.
It works with Space and Enter held down as well as with a pointer. Pointer capture keeps the hold going if the finger drifts off the edge.
Code
React and plain CSS"use client";
import { useEffect, useRef, useState, type KeyboardEvent, type TransitionEvent } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import styles from "./hold-to-confirm.module.css";
type Phase = "idle" | "holding" | "done";
/** Card demo: each phase and how long it lasts. */
const DEMO: Array<[Phase, number]> = [
["idle", 500],
["holding", 1500],
["done", 1400],
];
export default function HoldToConfirm({ active, context }: LivePreviewProps) {
const [phase, setPhase] = useState<Phase>("idle");
const [step, setStep] = useState(0);
const resetTimer = useRef<number | undefined>(undefined);
const demo = active && context === "card";
// Each time the demo starts, it starts from the first step.
const [demoRunning, setDemoRunning] = useState(demo);
if (demoRunning !== demo) {
setDemoRunning(demo);
setStep(0);
}
useEffect(() => {
if (!demo) return;
let n = 0;
let id = window.setTimeout(function next() {
n += 1;
setStep(n);
id = window.setTimeout(next, DEMO[n % DEMO.length]?.[1] ?? 500);
}, DEMO[0]?.[1] ?? 500);
return () => window.clearTimeout(id);
}, [demo]);
useEffect(() => () => window.clearTimeout(resetTimer.current), []);
const shown: Phase = context === "card" ? (demo ? (DEMO[step % DEMO.length]?.[0] ?? "idle") : "idle") : phase;
const start = () => {
if (phase === "idle") setPhase("holding");
};
const release = () => setPhase((p) => (p === "holding" ? "idle" : p));
function onFillEnd(event: TransitionEvent<HTMLSpanElement>) {
if (event.propertyName !== "transform" || phase !== "holding") return;
setPhase("done");
window.clearTimeout(resetTimer.current);
resetTimer.current = window.setTimeout(() => setPhase("idle"), 1600);
}
function onKeyDown(event: KeyboardEvent<HTMLButtonElement>) {
if (event.key !== " " && event.key !== "Enter") return;
event.preventDefault();
if (!event.repeat) start();
}
return (
<div className={styles.root}>
<button
type="button"
className={styles.button}
data-phase={shown}
tabIndex={context === "card" ? -1 : 0}
aria-describedby="hold-hint"
onPointerDown={(e) => {
e.currentTarget.setPointerCapture(e.pointerId);
start();
}}
onPointerUp={release}
onPointerCancel={release}
onKeyDown={onKeyDown}
onKeyUp={release}
onContextMenu={(e) => e.preventDefault()}
>
<span className={styles.fill} onTransitionEnd={onFillEnd} aria-hidden="true" />
<span className={styles.labels}>
<span className={styles.label} data-visible={shown !== "done" || undefined}>
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path d="M4 7h16M10 11v6M14 11v6M6 7l1 12a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2l1-12M9 7V4h6v3" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
</svg>
Hold to delete
</span>
<span className={styles.label} data-visible={shown === "done" || undefined}>
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path d="m5 12.5 4.5 4.5L19 7.5" stroke="currentColor" strokeWidth="2.6" strokeLinecap="round" strokeLinejoin="round" />
</svg>
Deleted
</span>
</span>
</button>
<p id="hold-hint" className={styles.hint} role="status">
{shown === "done" ? "Draft deleted." : "Press and hold for a second to confirm."}
</p>
</div>
);
}