Hold to Confirm
Fills as a confirmation is held, then changes to a check. Includes a standard two-step button.
pnpm dlx shadcn@latest add @haze/hold-to-confirm---
import { Check, ChevronDown, Lock } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
import Button from "./button.astro";
interface Props {
id: string;
label?: string;
holdLabel?: string;
confirmedLabel?: string;
alternateLabel?: string;
hint?: string;
holdingHint?: string;
duration?: number;
form?: string;
name?: string;
value?: string;
class?: string;
}
const {
id, label = "Revoke link", holdLabel = `Hold to ${label.charAt(0).toLowerCase()}${label.slice(1)}`, confirmedLabel = "Link revoked", alternateLabel = "Confirm with a button",
duration = 1200, form, name, value, class: className,
} = Astro.props;
const holdTime = Number.isFinite(duration) ? Math.max(500, Math.round(duration)) : 1200;
const { hint = `Press and hold for ${holdTime / 1000} s`, holdingHint = "Keep holding…" } = Astro.props;
---
<div id={id} data-haze-hold-to-confirm data-duration={holdTime} data-confirmed-label={confirmedLabel} data-holding-hint={holdingHint} class={cn("grid min-w-0 justify-items-start gap-2", className)}>
<button type="button" data-hold-trigger hidden aria-describedby={`${id}-hint`}
class="group relative inline-grid min-h-10 max-w-full touch-manipulation cursor-pointer items-center overflow-hidden rounded-lg border border-frame-strong bg-field text-[0.8125rem] font-medium text-foreground select-none not-data-[done]:hover:bg-accent focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring aria-disabled:cursor-default data-[done]:border-success-border data-[done]:bg-success/10 data-[done]:text-success motion-safe:transition-[scale,background-color,color,border-color] motion-safe:duration-200 motion-safe:ease-snappy motion-safe:data-[holding]:scale-[0.97] [&[hidden]]:hidden">
<span class="flex min-w-0 items-center justify-center gap-2 px-4 py-2 wrap-anywhere">
<Lock aria-hidden="true" class="size-4 shrink-0 text-destructive group-data-[done]:hidden" />
<Check aria-hidden="true" class="hidden size-4 shrink-0 motion-safe:animate-pop group-data-[done]:block" />
<span data-hold-label>{holdLabel}</span>
</span>
{/* The fill is a solid copy of the label, revealed from the start edge as the hold progresses, so the text flips colour where the fill has passed. */}
<span data-hold-fill aria-hidden="true"
class="absolute inset-0 flex items-center justify-center gap-2 bg-destructive px-4 py-2 text-destructive-foreground wrap-anywhere [clip-path:inset(0_calc(100%_-_var(--hold-progress,0)_*_100%)_0_0)] rtl:[clip-path:inset(0_0_0_calc(100%_-_var(--hold-progress,0)_*_100%))] motion-safe:transition-[clip-path,opacity] motion-safe:duration-300 motion-safe:ease-snappy group-data-[holding]:duration-0 group-data-[done]:opacity-0">
<Lock class="size-4 shrink-0" />
<span>{holdLabel}</span>
</span>
</button>
<p id={`${id}-hint`} data-hold-hint hidden class="text-[0.8125rem]/5 text-muted-foreground [&[hidden]]:hidden">
<span data-hold-hint-text>{hint}</span><span class="sr-only">. With a keyboard, hold Space or Enter. Press Escape to cancel.</span>
</p>
<details class="group/alt max-w-full [&[hidden]]:hidden">
<summary class="flex min-h-6 w-fit cursor-pointer list-none items-center gap-1 rounded-sm text-[0.8125rem]/5 text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring [&::-webkit-details-marker]:hidden">
{alternateLabel}<ChevronDown aria-hidden="true" class="size-3.5 shrink-0 transition-[rotate] duration-200 ease-snappy group-open/alt:rotate-180" />
</summary>
<Button data-hold-action type={form ? "submit" : "button"} form={form} name={name} value={value} variant="destructive" class="mt-2 max-w-full whitespace-normal">{label}</Button>
</details>
<span data-hold-status role="status" class="sr-only"></span>
</div>
<script>
function enhance(root: HTMLElement) {
if (root.hasAttribute("data-enhanced")) return;
const trigger = root.querySelector<HTMLButtonElement>("[data-hold-trigger]");
const action = root.querySelector<HTMLButtonElement>("[data-hold-action]");
const alternate = root.querySelector("details");
const fill = root.querySelector<HTMLElement>("[data-hold-fill]");
const label = root.querySelector<HTMLElement>("[data-hold-label]");
const status = root.querySelector<HTMLElement>("[data-hold-status]");
const hint = root.querySelector<HTMLElement>("[data-hold-hint]");
const hintText = root.querySelector<HTMLElement>("[data-hold-hint-text]");
if (!trigger || !action || !alternate || !fill || !label || !status) return;
const duration = Number(root.dataset.duration);
const idleHint = hintText?.textContent ?? "";
let frame = 0;
let hold: number | string | null = null;
let done = false;
// stop() ends a hold but leaves the fill where it is; cancel() also empties it.
const stop = () => {
cancelAnimationFrame(frame);
const current = hold;
hold = null;
if (typeof current === "number" && trigger.hasPointerCapture(current)) trigger.releasePointerCapture(current);
trigger.removeAttribute("data-holding");
if (hintText) hintText.textContent = idleHint;
};
const cancel = () => {
stop();
fill.style.setProperty("--hold-progress", "0");
};
const start = (input: number | string) => {
if (done || hold !== null) return;
hold = input;
const since = performance.now();
trigger.setAttribute("data-holding", "");
if (hintText) hintText.textContent = root.dataset.holdingHint ?? "";
const tick = (now: number) => {
const progress = Math.min(1, Math.max(0, (now - since) / duration));
fill.style.setProperty("--hold-progress", String(progress));
if (progress < 1) frame = requestAnimationFrame(tick);
else {
stop();
action.click();
}
};
frame = requestAnimationFrame(tick);
};
action.addEventListener("click", event => {
if (done) { event.preventDefault(); return; }
if (action.form && !action.form.noValidate && !action.form.reportValidity()) {
event.preventDefault();
cancel();
return;
}
done = true;
stop();
trigger.setAttribute("data-done", "");
trigger.setAttribute("aria-disabled", "true");
label.textContent = root.dataset.confirmedLabel ?? "";
status.textContent = label.textContent;
if (alternate.contains(document.activeElement)) trigger.focus();
alternate.hidden = true;
if (hint) hint.hidden = true;
});
trigger.addEventListener("pointerdown", event => {
if (!event.isPrimary || event.button !== 0 || done || hold !== null) return;
trigger.setPointerCapture(event.pointerId);
start(event.pointerId);
});
trigger.addEventListener("pointermove", event => {
if (hold !== event.pointerId) return;
const { left, right, top, bottom } = trigger.getBoundingClientRect();
const slop = event.pointerType === "touch" ? 16 : 4;
if (event.clientX < left - slop || event.clientX > right + slop || event.clientY < top - slop || event.clientY > bottom + slop) cancel();
});
for (const type of ["pointerup", "pointercancel", "lostpointercapture"] as const) {
trigger.addEventListener(type, event => { if (hold === event.pointerId) cancel(); });
}
trigger.addEventListener("contextmenu", event => { if (hold !== null) event.preventDefault(); });
trigger.addEventListener("keydown", event => {
if (event.key === "Escape" && hold !== null) {
event.preventDefault();
cancel();
} else if (event.key === " " || event.key === "Enter") {
event.preventDefault();
if (!event.repeat) start(event.key);
}
});
trigger.addEventListener("keyup", event => { if (hold === event.key) cancel(); });
trigger.addEventListener("blur", cancel);
fill.style.setProperty("--hold-progress", "0");
trigger.hidden = false;
if (hint) hint.hidden = false;
root.setAttribute("data-enhanced", "");
}
const init = () => document.querySelectorAll<HTMLElement>("[data-haze-hold-to-confirm]").forEach(enhance);
init();
if (!document.documentElement.hasAttribute("data-haze-hold-to-confirm-listener")) {
document.documentElement.setAttribute("data-haze-hold-to-confirm-listener", "");
document.addEventListener("astro:page-load", init);
}
</script>When to use
- A consequential action is easy to trigger by mistake.
- The effect of revoking access is already explained in the surrounding UI.
When not to use
- A dialog is needed to explain the consequences or ask for more information.
Installation
Add the Haze registry to components.json once, then install Hold to Confirm with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/hold-to-confirmnpx shadcn@latest add @haze/hold-to-confirmyarn dlx shadcn@latest add @haze/hold-to-confirmbunx --bun shadcn@latest add @haze/hold-to-confirmFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | – | Unique ID for the control and its keyboard instructions. |
| label | string | Revoke link | Label on the standard confirmation button. |
| holdLabel | string | Hold to revoke link | Label on the press-and-hold button. Provide a translation with label. |
| confirmedLabel | string | Link revoked | Text and live announcement after confirmation. |
| alternateLabel | string | – | Label for the standard two-step confirmation disclosure. |
| hint | string | Press and hold for 1.2 s | Visible hint under the button. The default states the hold time. |
| holdingHint | string | Keep holding… | Hint shown while a hold is in progress. |
| duration | number | 1200 | Hold time in milliseconds, with a 500 ms minimum. |
| form | string | – | ID of a form to submit. Otherwise, listen for the click on the element marked data-hold-action. |
| name | string | – | Native submit button name. |
| value | string | – | Native submit button value. |
| class | string | – | Classes on the wrapper. |
Keyboard interactions
- Hold EnterSpace
- Confirm after the indicated hold time.
- Escape
- Cancel an active hold.
Accessibility
- Confirmation requires one uninterrupted hold. The disclosure lets people confirm with a standard button, including when scripts are disabled.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Hold to Confirm" with `npx shadcn@latest add @haze/hold-to-confirm`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import HoldToConfirm from "@/components/haze/ui/hold-to-confirm.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/hold-to-confirm.md
Customize with real content
Adapt the Haze hold-to-confirm ui for our product. Replace the demo values of these props with our real content: id, label, holdLabel, confirmedLabel, alternateLabel, hint, holdingHint, duration, form, name, value, class. Keep the layout, the heading level rules and these accessibility details: Confirmation requires one uninterrupted hold. The disclosure lets people confirm with a standard button, including when scripts are disabled. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using hold-to-confirm, check it fits: use it when a consequential action is easy to trigger by mistake.; the effect of revoking access is already explained in the surrounding ui.. Avoid it when a dialog is needed to explain the consequences or ask for more information.. Alternatives: @haze/alert-dialog, @haze/stateful-button.
Dependencies
- npm: @lucide/astro
- @haze/button
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.