# Alert Dialog

A modal confirmation dialog built on Radix AlertDialog that interrupts the user with a title, description, media slot, and cancel/confirm actions.

- Category: Overlays & Navigation
- License: MIT
- Page: https://www.saasuji.com/components/alert-dialog
- Install: npx shadcn@latest add https://www.saasuji.com/r/alert-dialog.json

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| open (AlertDialog) | `boolean` | — | Controlled open state of the dialog. |
| defaultOpen (AlertDialog) | `boolean` | `false` | Open state on first render for an uncontrolled dialog. |
| onOpenChange (AlertDialog) | `(open: boolean) => void` | — | Called when the open state changes. |
| modal (AlertDialog) | `boolean` | `true` | Traps focus and blocks interaction with the rest of the page while open. |
| size (AlertDialogContent) | `"default" | "sm"` | `"default"` | Dialog width: max-w-lg on desktop or a compact max-w-xs. |
| variant (AlertDialogAction) | `Button variant` | `"default"` | Button variant for the confirm action, such as destructive for deletions. |
| variant (AlertDialogCancel) | `Button variant` | `"outline"` | Button variant for the cancel action. |
| size (AlertDialogAction, AlertDialogCancel) | `Button size` | `"default"` | Button size applied to both action buttons. |
| asChild (AlertDialogTrigger) | `boolean` | `false` | Renders the trigger as its child element instead of the default button. |
| className (AlertDialogOverlay, AlertDialogContent, AlertDialogHeader, AlertDialogFooter, AlertDialogMedia, AlertDialogTitle, AlertDialogDescription, AlertDialogAction, AlertDialogCancel) | `string` | — | Additional classes merged onto the part that receives it. |
| children (AlertDialog, AlertDialogTrigger, AlertDialogContent, AlertDialogHeader, AlertDialogFooter, AlertDialogMedia, AlertDialogTitle, AlertDialogDescription, AlertDialogAction, AlertDialogCancel) | `React.ReactNode` | — | Trigger label, header with title/description/media, and the cancel and confirm buttons inside the footer. |
| ...props (AlertDialog) | `React.ComponentProps<typeof AlertDialogPrimitive.Root>` | — | All remaining Radix AlertDialog root props are forwarded. |
| ...props (AlertDialogTrigger, AlertDialogAction, AlertDialogCancel) | `React.ComponentProps<typeof AlertDialogPrimitive.Trigger> | React.ComponentProps<typeof AlertDialogPrimitive.Action> | React.ComponentProps<typeof AlertDialogPrimitive.Cancel>` | — | All remaining Radix trigger, action, and cancel props are forwarded to the rendered buttons. |
| ...props (AlertDialogPortal, AlertDialogOverlay, AlertDialogContent) | `React.ComponentProps<typeof AlertDialogPrimitive.Portal> | React.ComponentProps<typeof AlertDialogPrimitive.Overlay> | React.ComponentProps<typeof AlertDialogPrimitive.Content>` | — | All remaining Radix portal, overlay, and content props are forwarded (forceMount, onOpenAutoFocus, …). |
| ...props (AlertDialogTitle, AlertDialogDescription) | `React.ComponentProps<typeof AlertDialogPrimitive.Title> | React.ComponentProps<typeof AlertDialogPrimitive.Description>` | — | All remaining Radix title and description props are forwarded. |
| ...props (AlertDialogHeader, AlertDialogFooter, AlertDialogMedia) | `React.ComponentProps<"div">` | — | All remaining native div attributes are forwarded. |

## Source

```tsx
"use client"

import * as React from "react"
import { AlertDialog as AlertDialogPrimitive } from "radix-ui"

import { cn } from "@/lib/utils"
import { Button } from "@/components/ui/button"

function AlertDialog({
  ...props
}: React.ComponentProps<typeof AlertDialogPrimitive.Root>) {
  return <AlertDialogPrimitive.Root data-slot="alert-dialog" {...props} />
}

function AlertDialogTrigger({
  ...props
}: React.ComponentProps<typeof AlertDialogPrimitive.Trigger>) {
  return (
    <AlertDialogPrimitive.Trigger data-slot="alert-dialog-trigger" {...props} />
  )
}

function AlertDialogPortal({
  ...props
}: React.ComponentProps<typeof AlertDialogPrimitive.Portal>) {
  return (
    <AlertDialogPrimitive.Portal data-slot="alert-dialog-portal" {...props} />
  )
}

function AlertDialogOverlay({
  className,
  ...props
}: React.ComponentProps<typeof AlertDialogPrimitive.Overlay>) {
  return (
    <AlertDialogPrimitive.Overlay
      data-slot="alert-dialog-overlay"
      className={cn(
        "fixed inset-0 z-[1400] bg-black/50 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:animate-in data-[state=open]:fade-in-0",
        className
      )}
      {...props}
    />
  )
}

function AlertDialogContent({
  className,
  size = "default",
  ...props
}: React.ComponentProps<typeof AlertDialogPrimitive.Content> & {
  size?: "default" | "sm"
}) {
  return (
    <AlertDialogPortal>
      <AlertDialogOverlay />
      <AlertDialogPrimitive.Content
        data-slot="alert-dialog-content"
        data-size={size}
        className={cn(
          "group/alert-dialog-content fixed top-[50%] left-[50%] z-[1450] grid w-full max-w-[calc(100%-2rem)] translate-x-[-50%] translate-y-[-50%] gap-4 rounded-lg border bg-background p-6 shadow-lg duration-200 data-[size=sm]:max-w-xs data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95 data-[size=default]:sm:max-w-lg",
          className
        )}
        {...props}
      />
    </AlertDialogPortal>
  )
}

function AlertDialogHeader({
  className,
  ...props
}: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="alert-dialog-header"
      className={cn(
        "grid grid-rows-[auto_1fr] place-items-center gap-1.5 text-center has-data-[slot=alert-dialog-media]:grid-rows-[auto_auto_1fr] has-data-[slot=alert-dialog-media]:gap-x-6 sm:group-data-[size=default]/alert-dialog-content:place-items-start sm:group-data-[size=default]/alert-dialog-content:text-left sm:group-data-[size=default]/alert-dialog-content:has-data-[slot=alert-dialog-media]:grid-rows-[auto_1fr]",
        className
      )}
      {...props}
    />
  )
}

function AlertDialogFooter({
  className,
  ...props
}: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="alert-dialog-footer"
      className={cn(
        "flex flex-col-reverse gap-2 group-data-[size=sm]/alert-dialog-content:grid group-data-[size=sm]/alert-dialog-content:grid-cols-2 sm:flex-row sm:justify-end",
        className
      )}
      {...props}
    />
  )
}

function AlertDialogTitle({
  className,
  ...props
}: React.ComponentProps<typeof AlertDialogPrimitive.Title>) {
  return (
    <AlertDialogPrimitive.Title
      data-slot="alert-dialog-title"
      className={cn(
        "text-lg font-semibold sm:group-data-[size=default]/alert-dialog-content:group-has-data-[slot=alert-dialog-media]/alert-dialog-content:col-start-2",
        className
      )}
      {...props}
    />
  )
}

function AlertDialogDescription({
  className,
  ...props
}: React.ComponentProps<typeof AlertDialogPrimitive.Description>) {
  return (
    <AlertDialogPrimitive.Description
      data-slot="alert-dialog-description"
      className={cn("text-sm text-muted-foreground", className)}
      {...props}
    />
  )
}

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

function AlertDialogAction({
  className,
  variant = "default",
  size = "default",
  ...props
}: React.ComponentProps<typeof AlertDialogPrimitive.Action> &
  Pick<React.ComponentProps<typeof Button>, "variant" | "size">) {
  return (
    <Button variant={variant} size={size} asChild>
      <AlertDialogPrimitive.Action
        data-slot="alert-dialog-action"
        className={cn(className)}
        {...props}
      />
    </Button>
  )
}

function AlertDialogCancel({
  className,
  variant = "outline",
  size = "default",
  ...props
}: React.ComponentProps<typeof AlertDialogPrimitive.Cancel> &
  Pick<React.ComponentProps<typeof Button>, "variant" | "size">) {
  return (
    <Button variant={variant} size={size} asChild>
      <AlertDialogPrimitive.Cancel
        data-slot="alert-dialog-cancel"
        className={cn(className)}
        {...props}
      />
    </Button>
  )
}

export {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogMedia,
  AlertDialogOverlay,
  AlertDialogPortal,
  AlertDialogTitle,
  AlertDialogTrigger,
}

```
