Alert Dialog

A modal dialog that stops the user to confirm or cancel an important action.

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

export default function AlertDialogDemo() {
  return (
    <AlertDialog>
      <AlertDialogTrigger render={<Button variant="outline" />}>Delete project</AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>Delete this project?</AlertDialogTitle>
          <AlertDialogDescription>
            Its pages, drafts and settings are removed for everyone. You can&apos;t undo this.
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>Cancel</AlertDialogCancel>
          <AlertDialogAction>Continue</AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  )
}

Installation

pnpm dlx shadcn@latest add @arene/alert-dialog

Needs the arene setup first. Also adds Button.

Usage

import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
<AlertDialog>
  <AlertDialogTrigger render={<Button variant="outline" />}>Delete project</AlertDialogTrigger>
  <AlertDialogContent>
    <AlertDialogHeader>
      <AlertDialogTitle>Delete this project?</AlertDialogTitle>
      <AlertDialogDescription>
        Its pages, drafts and settings are removed for everyone. You can't undo this.
      </AlertDialogDescription>
    </AlertDialogHeader>
    <AlertDialogFooter>
      <AlertDialogCancel>Cancel</AlertDialogCancel>
      <AlertDialogAction>Continue</AlertDialogAction>
    </AlertDialogFooter>
  </AlertDialogContent>
</AlertDialog>

An alert dialog is always modal and never closes on an outside press or on Escape. Every path out of it is a button you render.

Composition

AlertDialog
├── AlertDialogTrigger
└── AlertDialogContent     portal + overlay + viewport + popup
    ├── AlertDialogHeader
    │   ├── AlertDialogMedia
    │   ├── AlertDialogTitle
    │   └── AlertDialogDescription
    └── AlertDialogFooter
        ├── AlertDialogCancel
        └── AlertDialogAction

AlertDialogFooter is a tray, and the content above it is a panel raised out of it. Keep the footer the last child of AlertDialogContent. It bleeds to the popup's edges and draws the panel's bottom edge itself, so the panel needs no wrapper.

Size

Use the size prop on AlertDialogContent. sm stays narrow at every width and splits its footer into two equal columns.

import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
  type AlertDialogSize,
} from "@/components/ui/alert-dialog"
import { Button } from "@/components/ui/button"
import { HStack } from "@/components/ui/stack"

const sizes: AlertDialogSize[] = ["default", "sm"]

export default function AlertDialogSizeDemo() {
  return (
    <HStack gap={2}>
      {sizes.map((size) => (
        <AlertDialog key={size}>
          <AlertDialogTrigger render={<Button variant="outline" />}>{size}</AlertDialogTrigger>
          <AlertDialogContent size={size}>
            <AlertDialogHeader>
              <AlertDialogTitle>Publish this release?</AlertDialogTitle>
              <AlertDialogDescription>
                Everyone on your team will be notified.
              </AlertDialogDescription>
            </AlertDialogHeader>
            <AlertDialogFooter>
              <AlertDialogCancel>Cancel</AlertDialogCancel>
              <AlertDialogAction>Publish</AlertDialogAction>
            </AlertDialogFooter>
          </AlertDialogContent>
        </AlertDialog>
      ))}
    </HStack>
  )
}

Media

Use AlertDialogMedia for an icon above the title. At default size it moves beside the text from the md breakpoint up.

import { UserPlusIcon } from "@phosphor-icons/react/dist/csr/UserPlus"
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogMedia,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
import { Button } from "@/components/ui/button"

export default function AlertDialogMediaDemo() {
  return (
    <AlertDialog>
      <AlertDialogTrigger render={<Button variant="outline" />}>
        Add collaborator
      </AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogMedia>
            <UserPlusIcon aria-hidden="true" />
          </AlertDialogMedia>
          <AlertDialogTitle>Add Grace Hopper?</AlertDialogTitle>
          <AlertDialogDescription>
            She will be able to edit every page in this project.
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>Cancel</AlertDialogCancel>
          <AlertDialogAction>Add</AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  )
}

Destructive

Pass variant="destructive" to AlertDialogAction, and tint the media through xstyle to match.

import * as stylex from "@stylexjs/stylex"
import { TrashIcon } from "@phosphor-icons/react/dist/csr/Trash"
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogMedia,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
import { Button } from "@/components/ui/button"
import { color } from "@/theme/tokens.stylex"

const styles = stylex.create({
  media: { backgroundColor: color.buttonDestructiveBg, color: color.destructive },
})

export default function AlertDialogDestructiveDemo() {
  return (
    <AlertDialog>
      <AlertDialogTrigger render={<Button variant="destructive" />}>
        Delete branch
      </AlertDialogTrigger>
      <AlertDialogContent size="sm">
        <AlertDialogHeader>
          <AlertDialogMedia xstyle={styles.media}>
            <TrashIcon aria-hidden="true" />
          </AlertDialogMedia>
          <AlertDialogTitle>Delete branch?</AlertDialogTitle>
          <AlertDialogDescription>
            Its commits stay in history, and any open reviews on it are closed.
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>Cancel</AlertDialogCancel>
          <AlertDialogAction variant="destructive">Delete</AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  )
}

AlertDialogAction does not close the dialog. It is a plain Button, so close it from onClick or once the work it starts settles. That way a failed delete leaves the dialog open.

API reference

Pass render to replace a part's element or compose it with another component.

Every part wraps the Base UI AlertDialog part of the same name and accepts its props. See the Base UI reference for the full list. Each styled part also takes xstyle and className.

AlertDialogContent

PropTypeDefault
size"default" | "sm""default"
overlayPropsAlertDialogOverlayProps—
viewportPropsAlertDialogViewportProps—

AlertDialogContent passes size to the header, media and footer through context, so each of them changes layout without an ancestor selector.

AlertDialogAction

Takes every Button prop. variant defaults to "default".

AlertDialogCancel

Renders AlertDialog.Close as a Button and closes the dialog on press.

PropTypeDefault
variantButtonVariant"outline"
sizeButtonSize—

alertDialogStyles

KeyContents
surfaceThe shared popup surface: fill, foreground, hairline
baseThe popup box: radius, padding, grid, shadow, motion
sizesdefault and sm, each a maxWidth

Data attributes

AttributeOnEffect
data-slot="alert-dialog-content"the popupPopup slot
data-slot="alert-dialog-overlay"the scrimBackdrop surface
data-sizethe popupThe size prop, for selecting on from outside
data-starting-style, data-ending-stylepopup and overlayBase UI writes them while the dialog animates
data-open, data-closedpopup and overlayBase UI writes them