---
title: Alert Dialog for Astro
description: "Astro confirmation dialogs for actions that need an explicit decision."
seo:
    title: Alert Dialog for Astro | Shadcn & Tailwind CSS
related:
    - /components/dialog
    - /components/alert
sidebar:
    label: "Alert Dialog"
---

```astro
---
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
---

<AlertDialog>
  <AlertDialogTrigger variant="outline">
    Review draft removal
  </AlertDialogTrigger>
  <AlertDialogContent>
    <AlertDialogHeader>
      <AlertDialogTitle>Remove this draft?</AlertDialogTitle>
      <AlertDialogDescription>
        Removing this draft deletes its saved content. Export a copy first if
        you need to keep it.
      </AlertDialogDescription>
    </AlertDialogHeader>
    <AlertDialogFooter>
      <AlertDialogCancel>Cancel</AlertDialogCancel>
      <AlertDialogAction>Remove draft</AlertDialogAction>
    </AlertDialogFooter>
  </AlertDialogContent>
</AlertDialog>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/alert-dialog
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

Install the component packages:

```sh
bun add @data-slot/alert-dialog
```

#### src/components/ui/alert-dialog/alert-dialog-action.astro

```astro
---
import type { ComponentProps } from "astro/types"

import { Button } from "../button"

type Props = ComponentProps<typeof Button>

const { ...props } = Astro.props
---

<Button data-slot="alert-dialog-action" {...props}>
  <slot />
</Button>
```

#### src/components/ui/alert-dialog/alert-dialog-cancel.astro

```astro
---
import type { ComponentProps } from "astro/types"

import { Button } from "../button/index.ts"

type Props = ComponentProps<typeof Button>

const { variant = "outline", ...props } = Astro.props
---

<Button data-slot="alert-dialog-cancel" variant={variant} {...props}>
  <slot />
</Button>
```

#### src/components/ui/alert-dialog/alert-dialog-content.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"
import AlertDialogOverlay from "./alert-dialog-overlay.astro"
import AlertDialogPortal from "./alert-dialog-portal.astro"
import type { AlertDialogContentProps } from "./types.ts"

type Props = HTMLAttributes<"div"> & AlertDialogContentProps

const { class: className, size = "default", style, ...props } = Astro.props
---

<AlertDialogPortal>
  <AlertDialogOverlay />
  <div
    data-slot="alert-dialog-content"
    data-size={size}
    hidden
    class={cn("@container/dialog fixed top-1/2 left-1/2 z-50 flex w-full max-w-[calc(100%-2rem)] -translate-x-1/2 -translate-y-1/2 flex-col rounded-2xl text-popover-foreground outline-none data-[size=sm]:max-w-xs data-[size=default]:sm:max-w-lg border bg-popover not-dark:bg-clip-padding shadow-lg/5 before:pointer-events-none before:absolute before:inset-0 before:shadow-[0_1px_--theme(--color-black/4%)] dark:before:shadow-[0_-1px_--theme(--color-white/6%)] before:rounded-[calc(var(--radius-2xl)-1px)] transition-[scale,opacity] duration-200 data-ending-style:scale-98 data-starting-style:scale-98 data-ending-style:opacity-0 data-starting-style:opacity-0 group/alert-dialog-content", className)}
    style={`z-index: calc(var(--alert-dialog-content-stack-index, 0) * 2 + 101);${style ? ` ${style}` : ""}`}
    {...props}
  >
    <slot />
  </div>
</AlertDialogPortal>
```

#### src/components/ui/alert-dialog/alert-dialog-description.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"p">

const { class: className, ...props } = Astro.props
---

<p
  data-slot="alert-dialog-description"
  class={cn("text-muted-foreground text-sm", className)}
  {...props}
>
  <slot />
</p>
```

#### src/components/ui/alert-dialog/alert-dialog-footer.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="alert-dialog-footer"
  class={cn(
    "flex flex-col-reverse gap-2 px-6 sm:flex-row sm:justify-end sm:rounded-b-[calc(var(--radius-2xl)-1px)] border-t bg-muted/72 py-4",
    className,
  )}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/alert-dialog/alert-dialog-header.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="alert-dialog-header"
  class={cn("flex flex-col gap-2 p-6 text-center max-sm:pb-4 sm:text-left", className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/alert-dialog/alert-dialog-media.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="alert-dialog-media"
  class={cn("bg-muted mb-2 inline-flex size-16 items-center justify-center rounded-md @min-sm/dialog:group-data-[size=default]/alert-dialog-content:row-span-2 *:[svg:not([class*='size-'])]:size-8", className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/alert-dialog/alert-dialog-overlay.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, style, ...props } = Astro.props
---

<div
  data-slot="alert-dialog-overlay"
  hidden
  class={cn("fixed inset-0 z-50 bg-black/32 backdrop-blur-sm transition-all duration-200 data-ending-style:opacity-0 data-starting-style:opacity-0", className)}
  style={`z-index: calc(var(--alert-dialog-overlay-stack-index, 0) * 2 + 100);${style ? ` ${style}` : ""}`}
  {...props}
></div>
```

#### src/components/ui/alert-dialog/alert-dialog-portal.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div data-slot="alert-dialog-portal" class={cn(className)} {...props}>
  <slot />
</div>
```

#### src/components/ui/alert-dialog/alert-dialog-title.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"h2">

const { class: className, ...props } = Astro.props
---

<h2
  data-slot="alert-dialog-title"
  class={cn("font-heading font-semibold text-xl leading-none", className)}
  {...props}
>
  <slot />
</h2>
```

#### src/components/ui/alert-dialog/alert-dialog-trigger.astro

```astro
---
import type { ComponentProps } from "astro/types"

import { Button } from "../button/index.ts"

type Props = ComponentProps<typeof Button>

const { ...props } = Astro.props
---

<Button data-slot="alert-dialog-trigger" {...props}>
  <slot />
</Button>
```

#### src/components/ui/alert-dialog/alert-dialog.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import type { AlertDialogProps } from "./types.ts"

type Props = HTMLAttributes<"div"> & AlertDialogProps

const {
  class: className,
  defaultOpen,
  closeOnClickOutside,
  closeOnEscape,
  lockScroll,
  ...props
} = Astro.props
---

<div
  data-slot="alert-dialog"
  data-default-open={defaultOpen}
  data-close-on-click-outside={closeOnClickOutside}
  data-close-on-escape={closeOnEscape}
  data-lock-scroll={lockScroll}
  class={className}
  {...props}
>
  <slot />
</div>

<script>
  import type { AlertDialogController } from "@data-slot/alert-dialog"
  import { create } from "@data-slot/alert-dialog"

  // Destroy bindings before a view transition so the next page-load binds once.
  const controllers: AlertDialogController[] = []
  const initialize = () => controllers.push(...create())
  initialize()
  document.addEventListener("astro:page-load", initialize)
  document.addEventListener("astro:before-swap", () => {
    for (const controller of controllers) controller.destroy()
    controllers.length = 0
  })
</script>
```

#### src/components/ui/alert-dialog/index.ts

```ts
export { default as AlertDialog } from './alert-dialog.astro'
export { default as AlertDialogAction } from './alert-dialog-action.astro'
export { default as AlertDialogCancel } from './alert-dialog-cancel.astro'
export { default as AlertDialogContent } from './alert-dialog-content.astro'
export { default as AlertDialogDescription } from './alert-dialog-description.astro'
export { default as AlertDialogFooter } from './alert-dialog-footer.astro'
export { default as AlertDialogHeader } from './alert-dialog-header.astro'
export { default as AlertDialogMedia } from './alert-dialog-media.astro'
export { default as AlertDialogOverlay } from './alert-dialog-overlay.astro'
export { default as AlertDialogPortal } from './alert-dialog-portal.astro'
export { default as AlertDialogTitle } from './alert-dialog-title.astro'
export { default as AlertDialogTrigger } from './alert-dialog-trigger.astro'
export type { AlertDialogProps, AlertDialogContentProps } from './types.ts'
```

#### src/components/ui/alert-dialog/types.ts

```ts
import type { AlertDialogOptions } from '@data-slot/alert-dialog'

/**
 * Alert dialog configuration rendered as data attributes.
 */
export type AlertDialogProps = Pick<
    AlertDialogOptions,
    'defaultOpen' | 'closeOnClickOutside' | 'closeOnEscape' | 'lockScroll'
>

/**
 * Width of the alert dialog panel.
 */
export type AlertDialogContentProps = {
    /**
     * Panel width.
     *
     * @default "default"
     */
    size?: 'default' | 'sm'
}
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

```ts
import {
    AlertDialog,
    AlertDialogAction,
    AlertDialogCancel,
    AlertDialogContent,
    AlertDialogDescription,
    AlertDialogFooter,
    AlertDialogHeader,
    AlertDialogTitle,
    AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
```

```astro
<AlertDialog>
  <AlertDialogTrigger variant="outline">Review draft removal</AlertDialogTrigger>
  <AlertDialogContent>
    <AlertDialogHeader>
      <AlertDialogTitle>Remove this draft?</AlertDialogTitle>
      <AlertDialogDescription>
        Removing this draft deletes its saved content. Export a copy first if you need to keep it.
      </AlertDialogDescription>
    </AlertDialogHeader>
    <AlertDialogFooter>
      <AlertDialogCancel>Cancel</AlertDialogCancel>
      <AlertDialogAction>Remove draft</AlertDialogAction>
    </AlertDialogFooter>
  </AlertDialogContent>
</AlertDialog>
```

## Composition

Arrange the parts as shown for an `AlertDialog`:

```text
AlertDialog
├── AlertDialogTrigger
└── AlertDialogContent
    ├── AlertDialogHeader
    │   ├── AlertDialogMedia
    │   ├── AlertDialogTitle
    │   └── AlertDialogDescription
    └── AlertDialogFooter
        ├── AlertDialogCancel
        └── AlertDialogAction
```

## Notes

- Use `AlertDialog` for irreversible or high-risk actions, not routine inline
  confirmations.
- Keep `AlertDialogTitle` and `AlertDialogDescription` inside
  `AlertDialogContent` so screen readers announce the dialog context clearly.
- Pair the destructive path with `AlertDialogAction` and the safe path with
  `AlertDialogCancel`.
- Use `size="sm"` on `AlertDialogContent` for compact confirmations and
  `AlertDialogMedia` when the dialog needs an icon or visual cue.

## API Reference

### AlertDialogProps

<AutoTypeTable
    path="../../packages/ui/src/components/ui/alert-dialog/types.ts"
    name="AlertDialogProps"
/>

### AlertDialogContentProps

<AutoTypeTable
    path="../../packages/ui/src/components/ui/alert-dialog/types.ts"
    name="AlertDialogContentProps"
/>

See the [data-slot docs](https://github.com/bejamas/data-slot/blob/main/packages/alert-dialog/README.md) for more information on interactivity.

### Events

Dispatch these events on the alert dialog root. The root emits the outbound events.

#### Outbound events

| Event                 | Detail              |
| --------------------- | ------------------- |
| `alert-dialog:change` | `{ open: boolean }` |

#### Inbound events

| Event              | Detail              |
| ------------------ | ------------------- |
| `alert-dialog:set` | `{ open: boolean }` |

Read the [installed source](/r/alert-dialog.json) for the complete prop types.

## When to use Alert Dialog

Use Alert Dialog before an action whose effect is hard to reverse. Put the specific result in the title and explanation. Keep the cancel control available and label the action with a verb such as “Remove project”. The action button alone does not perform the operation.

## Keyboard and behavior

The existing alert-dialog controller manages modal focus and dismissal. Confirm that focus returns to the trigger after cancellation. Run the operation from your own form or script only after the user chooses the action. Test Escape and focus order in the finished page.

## Theme and layout

The surface shares dialog colors, border, and footer spacing. Keep copy short enough to fit a narrow screen and allow it to wrap. Destructive button color is useful, but the wording must still explain the outcome without color.
