Confirm Popover
An anchored confirmation card with a required phrase, fill feedback, asynchronous progress, and retry.
pnpm dlx shadcn@latest add @haze/confirm-popover---
import { Check, LoaderCircle } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
import Popover from "./popover.astro";
import Button from "./button.astro";
import Input from "./input.astro";
interface Props {
id: string;
label?: string;
title?: string;
description?: string;
phrase?: string;
action?: string;
class?: string;
}
const { id, label = "Delete draft", title = "Delete this draft?", description = "The unpublished draft and its review notes will be removed.", phrase = "DELETE", action, class: className } = Astro.props;
---
<div data-haze-confirm-popover data-phrase={phrase} class={cn("group/confirm min-w-0", className)}>
<Popover id={id} {label} class="w-80 max-w-[calc(100vw-1rem)] supports-[position-area:block-end]:m-0 supports-[position-area:block-end]:[position-area:none] supports-[position-area:block-end]:w-[min(20rem,calc(100vw-1rem))] supports-[position-area:block-end]:[inset-block-start:calc(anchor(bottom)+0.25rem)] supports-[position-area:block-end]:[inset-block-end:auto] supports-[position-area:block-end]:[inset-inline-start:clamp(0.5rem,anchor(start),calc(100vw-min(20rem,100vw-1rem)-0.5rem))] supports-[position-area:block-end]:[inset-inline-end:auto] supports-[position-area:block-end]:[position-try-fallbacks:flip-block]">
<form action={action} method={action ? "post" : "get"} role="alertdialog" aria-labelledby={`${id}-title`} aria-describedby={`${id}-description`} class="grid min-w-0 gap-3">
<p id={`${id}-title`} class="text-[0.9375rem] leading-[1.4] font-medium tracking-[-0.01em]">{title}</p>
<p id={`${id}-description`} class="text-[0.8125rem] leading-[1.4] text-muted-foreground">{description}</p>
<div class="grid min-w-0 gap-1.5 empty:hidden">
{phrase && <label for={`${id}-phrase`} class="text-[0.8125rem]/5 font-medium">Type {phrase} to confirm</label>}
{phrase && <Input id={`${id}-phrase`} name="confirmation" pattern={phrase.replace(/[.*+?^$\{\}()|[\]\\]/g, "\\$&")} required autocomplete="off" />}
<p data-confirm-status role="status" class="text-xs/5 text-muted-foreground empty:hidden"></p>
</div>
<div class="flex flex-wrap justify-end gap-2">
<Button type="button" variant="outline" popovertarget={id} popovertargetaction="hide">Cancel</Button>
<Button data-confirm-action type="submit" variant="destructive" class="group/danger relative min-w-28 overflow-hidden aria-disabled:not-data-[typing]:opacity-50 aria-disabled:data-[typing]:opacity-100">
<span data-confirm-label class="relative">Confirm deletion</span>
<LoaderCircle aria-hidden="true" class="hidden animate-spin group-data-[state=pending]/confirm:block" />
<Check aria-hidden="true" class="hidden group-data-[state=success]/confirm:block" />
<span aria-hidden="true" data-confirm-fill class="pointer-events-none absolute inset-0 flex items-center justify-center gap-2 bg-destructive px-4 text-destructive-foreground [clip-path:inset(0_calc(100%_-_var(--confirm-progress,0)_*_100%)_0_0)] rtl:[clip-path:inset(0_0_0_calc(100%_-_var(--confirm-progress,0)_*_100%))] group-data-[state=pending]/confirm:opacity-0 group-data-[state=success]/confirm:opacity-0 motion-safe:transition-[clip-path] motion-safe:duration-150 motion-safe:ease-snappy"><span data-confirm-fill-label>Confirm deletion</span></span>
</Button>
</div>
</form>
</Popover>
</div>
<script>
function enhance(root: HTMLElement) {
if (root.hasAttribute("data-enhanced")) return;
const form = root.querySelector("form");
const input = root.querySelector<HTMLInputElement>("input");
const action = root.querySelector<HTMLButtonElement>("[data-confirm-action]");
const label = root.querySelector<HTMLElement>("[data-confirm-label]");
const fill = root.querySelector<HTMLElement>("[data-confirm-fill]");
const fillLabel = root.querySelector<HTMLElement>("[data-confirm-fill-label]");
const status = root.querySelector<HTMLElement>("[data-confirm-status]");
if (!form || !action || !label || !fill || !fillLabel || !status) return;
let busy = false;
let done = false;
const phrase = root.dataset.phrase ?? "";
const sync = () => {
let count = 0;
while (count < phrase.length && input?.value[count] === phrase[count]) count++;
const valid = !phrase || input?.value === phrase;
action.setAttribute("aria-disabled", String(!valid || busy || done));
fill.style.setProperty("--confirm-progress", String(phrase ? count / phrase.length : 1));
action.toggleAttribute("data-typing", count > 0 && !busy);
};
input?.addEventListener("input", sync);
form.addEventListener("submit", async event => {
if (busy || done || (phrase && input?.value !== phrase)) { event.preventDefault(); input?.focus(); return; }
if (form.hasAttribute("action")) return;
event.preventDefault();
const tasks: Promise<unknown>[] = [];
const request = new CustomEvent("haze:confirm", { bubbles: true, cancelable: true, detail: { waitUntil: (task: Promise<unknown>) => tasks.push(Promise.resolve(task)) } });
if (!root.dispatchEvent(request)) return;
busy = true;
root.dataset.state = "pending";
form.setAttribute("aria-busy", "true");
status.textContent = "Confirming…";
sync();
try {
await Promise.all(tasks);
root.dataset.state = "success";
status.textContent = "Action confirmed.";
label.textContent = "Confirmed";
fillLabel.textContent = label.textContent;
done = true;
} catch (error) {
root.dataset.state = "error";
status.textContent = error instanceof Error ? error.message : "The action could not be completed. Try again.";
label.textContent = "Try again";
fillLabel.textContent = label.textContent;
action.focus();
} finally { busy = false; form.removeAttribute("aria-busy"); sync(); }
});
sync();
root.setAttribute("data-enhanced", "");
}
const init = () => document.querySelectorAll<HTMLElement>("[data-haze-confirm-popover]").forEach(enhance);
init();
if (!document.documentElement.hasAttribute("data-haze-confirm-popover-listener")) {
document.documentElement.setAttribute("data-haze-confirm-popover-listener", "");
document.addEventListener("astro:page-load", init);
}
</script>When to use
- A settings row needs a local confirmation with a clear consequence.
- Typing a resource name or phrase provides deliberate approval.
When not to use
- The action has several dependent steps. Use dialog.
Installation
Add the Haze registry to components.json once, then install Confirm Popover with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/confirm-popovernpx shadcn@latest add @haze/confirm-popoveryarn dlx shadcn@latest add @haze/confirm-popoverbunx --bun shadcn@latest add @haze/confirm-popoverFirst 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 popover ID and prefix for its labels. |
| label | string | – | Trigger action text. |
| title | string | – | Confirmation question. |
| description | string | – | Consequence presented before approval. |
| phrase | string | – | Exact phrase required for confirmation. Empty removes the phrase field. |
| action | string | – | Optional native POST destination. Otherwise use haze:confirm. |
| class | string | – | Additional classes on the component wrapper. |
Keyboard interactions
- EnterSpace
- Open the confirmation or activate its buttons.
- Escape
- Close the native popover and return to its trigger.
Accessibility
- The alert dialog is labelled by its title and consequence. Native pattern validation and an aria-disabled button guard the required phrase.
- Set action for native POST submission or handle the cancelable haze:confirm event and register asynchronous work with detail.waitUntil. Rejections expose a retry state.
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 "Confirm Popover" with `npx shadcn@latest add @haze/confirm-popover`. 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 ConfirmPopover from "@/components/haze/ui/confirm-popover.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/confirm-popover.md
Customize with real content
Adapt the Haze confirm-popover ui for our product. Replace the demo values of these props with our real content: id, label, title, description, phrase, action, class. Keep the layout, the heading level rules and these accessibility details: The alert dialog is labelled by its title and consequence. Native pattern validation and an aria-disabled button guard the required phrase. Set action for native POST submission or handle the cancelable haze:confirm event and register asynchronous work with detail.waitUntil. Rejections expose a retry state. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using confirm-popover, check it fits: use it when a settings row needs a local confirmation with a clear consequence.; typing a resource name or phrase provides deliberate approval.. Avoid it when the action has several dependent steps. use dialog.. Alternatives: @haze/alert-dialog, @haze/hold-to-confirm, @haze/popover.
Dependencies
- npm: @lucide/astro
- @haze/popover
- @haze/button
- @haze/input
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.