# Alert dialog

> A native confirmation dialog that starts focus on Cancel and ignores outside clicks.

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

## Use when
- People must acknowledge the consequence of discarding, revoking, or deleting something
- A short irreversible action needs distinct cancel and confirm labels

## Avoid when
- The modal collects fields or longer content. Use dialog.
- A routine notification can be read without interrupting the user. Use alert.

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

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` |  | yes | Unique dialog ID. An external trigger can target it with commandfor. |
| title | `string` | "Discard this draft?" | no | Question or brief confirmation title. |
| description | `string` | "Your unsaved edits will be lost." | no | Consequence of accepting the action. |
| trigger | `boolean` | true | no | Renders the default opener. |
| triggerLabel | `string` | "Discard draft" | no | Text on the opener. |
| cancelLabel | `string` | "Keep editing" | no | Label of the cancel action. |
| confirmLabel | `string` | "Discard draft" | no | Label of the confirm action. |
| confirmForm | `string` |  | no | ID of a form outside the dialog to submit. When omitted, confirmation closes with returnValue confirm. |
| destructive | `boolean` | true | no | Uses destructive styling for confirmation. |
| headingLevel | `2 \| 3 \| 4 \| 5 \| 6` | 2 | no | Heading level for the title. |
| default slot | `slot` |  | no | Optional content below the description. |
| class | `string` |  | no | Additional classes on the dialog. |

## Accessibility
- Initial focus is on Cancel. The native modal manages focus containment and restoration.
- Handle the close event and look for returnValue equal to confirm before accepting the result.
- Place the dialog outside its optional submission form and explain the specific consequence in the description.
- Key Enter or Space: Open the dialog or activate the focused action.
- Key Escape: Cancel and restore focus to the trigger.

## Agent prompts

### Install and use

Install the Haze UI component "Alert dialog" with `npx shadcn@latest add @haze/alert-dialog`. 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 AlertDialog from "@/components/haze/ui/alert-dialog.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/alert-dialog.md

### Customize with real content

Adapt the Haze alert-dialog ui for our product. Replace the demo values of these props with our real content: id, title, description, trigger, triggerLabel, cancelLabel, confirmLabel, confirmForm, destructive, headingLevel, default slot, class. Keep the layout, the heading level rules and these accessibility details: Initial focus is on Cancel. The native modal manages focus containment and restoration. Handle the close event and look for returnValue equal to confirm before accepting the result. Place the dialog outside its optional submission form and explain the specific consequence in the description. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using alert-dialog, check it fits: use it when people must acknowledge the consequence of discarding, revoking, or deleting something; a short irreversible action needs distinct cancel and confirm labels. Avoid it when the modal collects fields or longer content. use dialog.; a routine notification can be read without interrupting the user. use alert.. Alternatives: @haze/dialog, @haze/alert.

## Example requests that should lead here
- Confirm before discarding a draft, focus Keep editing first, and submit the discard form only on confirmation.

## Source

```astro
---
import { cn } from "../../../lib/haze/utils";
import Button from "./button.astro";
import Dialog from "./dialog.astro";

interface Props {
  id: string;
  title?: string;
  description?: string;
  trigger?: boolean;
  triggerLabel?: string;
  cancelLabel?: string;
  confirmLabel?: string;
  confirmForm?: string;
  destructive?: boolean;
  headingLevel?: 2 | 3 | 4 | 5 | 6;
  class?: string;
}

const {
  id, title = "Discard this draft?", description = "Your unsaved edits will be lost.",
  trigger = true, triggerLabel = "Discard draft", cancelLabel = "Keep editing", confirmLabel = "Discard draft",
  confirmForm, destructive = true, headingLevel = 2, class: className,
} = Astro.props;
---

<Dialog id={id} title={title} description={description} trigger={trigger} triggerLabel={triggerLabel}
  role="alertdialog" size="sm" headingLevel={headingLevel} class={cn(className)}>
  <slot />
  <form method="dialog" slot="footer" class="grid w-full gap-2 sm:flex sm:justify-end">
    <Button type="submit" value="cancel" autofocus variant="outline" class="whitespace-normal">{cancelLabel}</Button>
    <Button type="submit" form={confirmForm} value="confirm" variant={destructive ? "destructive" : "primary"} class="whitespace-normal">{confirmLabel}</Button>
  </form>
</Dialog>
```
