# Confirm Popover

> An anchored confirmation card with a required phrase, fill feedback, asynchronous progress, and retry.

- Kind: ui · Category: Overlays · Interactivity: script
- Install: `npx shadcn@latest add @haze/confirm-popover`
- File: `src/components/haze/ui/confirm-popover.astro`
- npm dependencies: @lucide/astro
- Registry dependencies: @haze/button, @haze/input, @haze/popover, @haze/utils
- Docs page: https://hazeui.com/ui/confirm-popover

## Use when
- A settings row needs a local confirmation with a clear consequence.
- Typing a resource name or phrase provides deliberate approval.

## Avoid when
- The action has several dependent steps. Use dialog.

## Alternatives
- [alert-dialog](https://hazeui.com/ui/alert-dialog.md)
- [hold-to-confirm](https://hazeui.com/ui/hold-to-confirm.md)
- [popover](https://hazeui.com/ui/popover.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` |  | yes | Unique popover ID and prefix for its labels. |
| label | `string` |  | no | Trigger action text. |
| title | `string` |  | no | Confirmation question. |
| description | `string` |  | no | Consequence presented before approval. |
| phrase | `string` |  | no | Exact phrase required for confirmation. Empty removes the phrase field. |
| action | `string` |  | no | Optional native POST destination. Otherwise use haze:confirm. |
| class | `string` |  | no | Additional classes on the component wrapper. |

## 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.
- Key Enter / Space: Open the confirmation or activate its buttons.
- Key Escape: Close the native popover and return to its trigger.

## Agent prompts

### 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": "https://hazeui.com/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: https://hazeui.com/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.

## Example requests that should lead here
- Ask people to type DELETE before removing a draft, and show asynchronous success or retry.

## Source

```astro
---
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>
```
