Skip to main content
An alert dialog is a modal dialog that interrupts the user with important content and expects a response. It should be used sparingly for critical confirmations or warnings that require explicit user acknowledgment.

Installation

npx shadcn@latest add @eo-n/alert-dialog

Usage

Import all parts and piece them together:
import {
  AlertDialog,
  AlertDialogClose,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
<AlertDialog>
  <AlertDialogTrigger>Open</AlertDialogTrigger>
  <AlertDialogContent>
    <AlertDialogTitle>Confirm Deletion</AlertDialogTitle>
    <AlertDialogDescription>
      Are you sure you want to delete this item? This action cannot be undone.
    </AlertDialogDescription>
    <AlertDialogFooter>
      <AlertDialogClose>Cancel</AlertDialogClose>
      <Button>Delete</Button>
    </AlertDialogFooter>
  </AlertDialogContent>
</AlertDialog>

Examples

Basic Alert Dialog

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

export default function AlertDialogDemo() {
  return (
    <AlertDialog>
      <AlertDialogTrigger asChild>
        <Button variant="destructive">Delete Account</Button>
      </AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogTitle>Confirm Deletion</AlertDialogTitle>
        <AlertDialogDescription>
          Are you sure you want to delete this item? This action cannot be
          undone.
        </AlertDialogDescription>
        <AlertDialogFooter>
          <AlertDialogClose>Cancel</AlertDialogClose>
          <Button variant="destructive">Delete</Button>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}

With Confirmation Callback

Handle the confirmation action with a callback:
import * as React from "react";
import {
  AlertDialog,
  AlertDialogClose,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";

export default function AlertDialogConfirmation() {
  const [open, setOpen] = React.useState(false);

  const handleConfirm = () => {
    // Perform the delete action
    console.log("Item deleted");
    setOpen(false);
  };

  return (
    <AlertDialog open={open} onOpenChange={setOpen}>
      <AlertDialogTrigger asChild>
        <Button variant="destructive">Delete</Button>
      </AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
        <AlertDialogDescription>
          This action cannot be undone. This will permanently delete your
          account and remove your data from our servers.
        </AlertDialogDescription>
        <AlertDialogFooter>
          <AlertDialogClose>Cancel</AlertDialogClose>
          <Button variant="destructive" onClick={handleConfirm}>
            Continue
          </Button>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}

Open From Menu

Control the dialog state to open it from a dropdown menu:
import * as React from "react";
import {
  AlertDialog,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Button } from "@/components/ui/button";

export default function AlertDialogFromMenu() {
  const menuTriggerRef = React.useRef<HTMLButtonElement>(null);
  const [alertDialogOpen, setAlertDialogOpen] = React.useState(false);

  return (
    <>
      <DropdownMenu>
        <DropdownMenuTrigger ref={menuTriggerRef} asChild>
          <Button>Actions</Button>
        </DropdownMenuTrigger>
        <DropdownMenuContent>
          <DropdownMenuItem onClick={() => setAlertDialogOpen(true)}>
            Delete
          </DropdownMenuItem>
        </DropdownMenuContent>
      </DropdownMenu>
      
      <AlertDialog open={alertDialogOpen} onOpenChange={setAlertDialogOpen}>
        <AlertDialogContent finalFocus={menuTriggerRef}>
          <AlertDialogTitle>Are you sure you want to proceed?</AlertDialogTitle>
          <AlertDialogDescription>
            This action may have permanent effects. Please confirm if you want
            to continue.
          </AlertDialogDescription>
        </AlertDialogContent>
      </AlertDialog>
    </>
  );
}
Make sure to use the dialog’s finalFocus prop to return focus back to the menu trigger for proper accessibility.

With Custom Actions

Customize the footer with multiple action buttons:
import {
  AlertDialog,
  AlertDialogClose,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";

export default function AlertDialogCustomActions() {
  return (
    <AlertDialog>
      <AlertDialogTrigger asChild>
        <Button>Save Changes</Button>
      </AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogTitle>Unsaved Changes</AlertDialogTitle>
        <AlertDialogDescription>
          You have unsaved changes. Would you like to save them before leaving?
        </AlertDialogDescription>
        <AlertDialogFooter>
          <AlertDialogClose>Don't Save</AlertDialogClose>
          <AlertDialogClose asChild>
            <Button variant="outline">Cancel</Button>
          </AlertDialogClose>
          <Button>Save</Button>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}

API Reference

AlertDialog

The root component that manages the alert dialog state.
open
boolean
Controls the open state of the dialog.
defaultOpen
boolean
The initial open state for uncontrolled usage.
onOpenChange
(open: boolean) => void
Callback fired when the open state changes.

AlertDialogTrigger

The button that opens the alert dialog.
asChild
boolean
default:"false"
Merge props onto the child element instead of wrapping it.

AlertDialogContent

Contains the content to be rendered in the open alert dialog.
flush
boolean
default:"false"
Removes padding and gap from the content container. Useful when using AlertDialogHeader and AlertDialogFooter with borders.
finalFocus
React.RefObject<HTMLElement>
Element to receive focus when the dialog closes.
className
string
Additional CSS classes to apply.

AlertDialogHeader

Wrapper for the alert dialog title and description.
className
string
Additional CSS classes to apply.

AlertDialogFooter

Wrapper for alert dialog action buttons.
className
string
Additional CSS classes to apply.

AlertDialogTitle

The accessible title of the alert dialog. Required for accessibility.
className
string
Additional CSS classes to apply.

AlertDialogDescription

The accessible description of the alert dialog.
className
string
Additional CSS classes to apply.

AlertDialogClose

A button that closes the alert dialog. Automatically styled as an outline button.
asChild
boolean
default:"false"
Merge props onto the child element instead of wrapping it.
className
string
Additional CSS classes to apply.

Build docs developers (and LLMs) love