Join our Discord Community

Alert Dialog

A modal dialog that interrupts the user with important content and expects a response.

Loading component...

Installation

npx shadcn@latest add alert-dialog

Usage

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

export function AlertDialogDemo() {
  return (
    <AlertDialog>
      <AlertDialogTrigger>Open</AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogHeader>
          <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>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>Cancel</AlertDialogCancel>
          <AlertDialogAction>Continue</AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  )
}

API Reference

AlertDialog

The root component that manages dialog state and behavior.

PropTypeDefaultDescription
defaultOpenbooleanfalseThe open state of the dialog when initially rendered.
openboolean-The controlled open state of the dialog.
onOpenChange(open: boolean) => void-Event handler called when the open state changes.
childrenReact.ReactNode-The dialog trigger and content components.

AlertDialogTrigger

The element that opens the dialog when interacted with.

PropTypeDefaultDescription
asChildbooleanfalseMerge props onto child element instead of creating a button.
childrenReact.ReactNode-The trigger element or component.

AlertDialogContent

The container for the dialog's content.

PropTypeDefaultDescription
classNamestring-Additional CSS classes for styling.
onEscapeKeyDown(event: KeyboardEvent) => void-Handler called when Escape key is pressed.
onPointerDownOutside(event: PointerDownOutsideEvent) => void-Handler for clicks outside the dialog.

AlertDialogHeader

Container for the dialog's title and description.

PropTypeDefaultDescription
classNamestring-Additional CSS classes for styling.
childrenReact.ReactNode-Title and description components.

AlertDialogTitle

The dialog's main heading.

PropTypeDefaultDescription
classNamestring-Additional CSS classes for styling.
childrenReact.ReactNode-The title text content.

AlertDialogDescription

Descriptive text providing context about the dialog's purpose.

PropTypeDefaultDescription
classNamestring-Additional CSS classes for styling.
childrenReact.ReactNode-The description text content.

AlertDialogFooter

Container for action buttons.

PropTypeDefaultDescription
classNamestring-Additional CSS classes for styling.
childrenReact.ReactNode-Action and cancel buttons.

AlertDialogAction

The primary action button that confirms the dialog.

PropTypeDefaultDescription
classNamestring-Additional CSS classes for styling.
onClick(event: MouseEvent) => void-Handler for action confirmation.
childrenReact.ReactNode-Button text content.

AlertDialogCancel

The cancel button that dismisses the dialog without action.

PropTypeDefaultDescription
classNamestring-Additional CSS classes for styling.
onClick(event: MouseEvent) => void-Handler for cancellation.
childrenReact.ReactNode-Button text content.

Implementation Details

Architecture

Built on Radix UI's AlertDialog primitive with custom styling:

  • Portal Rendering: Content renders in a React Portal at document root
  • Focus Management: Automatically traps focus within dialog when open
  • Overlay: Semi-transparent backdrop blocks interaction with page content
  • Animation: Smooth fade and zoom transitions using Tailwind animations

Component Structure

<AlertDialog>
  <AlertDialogTrigger />      // Opens the dialog
  <AlertDialogPortal>         // Portal container (rendered automatically)
    <AlertDialogOverlay />    // Background overlay (rendered automatically)
    <AlertDialogContent>      // Main dialog container
      <AlertDialogHeader>
        <AlertDialogTitle />
        <AlertDialogDescription />
      </AlertDialogHeader>
      <AlertDialogFooter>
        <AlertDialogCancel />
        <AlertDialogAction />
      </AlertDialogFooter>
    </AlertDialogContent>
  </AlertDialogPortal>
</AlertDialog>

Styling System

  • Overlay: 50% black opacity with fade animation
  • Content: Centered modal with max width constraints
  • Responsive: Stack buttons vertically on mobile, horizontally on desktop
  • Animations: Data-driven state animations for smooth transitions

Accessibility

Follows WAI-ARIA Alert Dialog pattern for maximum accessibility:

Keyboard Navigation

KeyAction
EnterActivates focused button
SpaceActivates focused button
TabMoves focus to next interactive element
Shift + TabMoves focus to previous interactive element
EscapeCloses dialog (can be prevented)

ARIA Attributes

  • role="alertdialog": Identifies critical user interaction
  • aria-modal="true": Indicates modal behavior
  • aria-labelledby: Links title to dialog
  • aria-describedby: Links description to dialog
  • Focus trap prevents tabbing outside dialog

Screen Reader Support

  • Title announced when dialog opens
  • Description provides context
  • Button labels clearly indicate actions
  • Focus management guides user flow

Examples

Destructive Action

Loading component...

Async Actions

Loading component...

Controlled State

Loading component...

Best Practices

Content Guidelines

  1. Clear Titles: Use action-oriented questions or statements
  2. Concise Descriptions: Explain consequences clearly
  3. Actionable Buttons: Use verbs that describe the action
  4. Destructive Actions: Always require confirmation for data loss

UX Considerations

  • Use for critical decisions requiring user confirmation
  • Avoid overuse - reserve for important actions
  • Provide clear consequences in the description
  • Offer a clear escape path with cancel option
  • Consider less intrusive alternatives for non-critical actions

Implementation Tips

  • Prevent accidental dismissal for critical actions
  • Handle loading states for async operations
  • Validate before closing on action confirmation
  • Consider keyboard-only users in custom implementations
  • Test with screen readers for accessibility

Customization

Styling Variants

// Warning variant
<AlertDialogContent className="border-yellow-200">
  <AlertDialogHeader>
    <AlertDialogTitle className="text-yellow-900">
      Warning
    </AlertDialogTitle>
    <AlertDialogDescription className="text-yellow-800">
      This action may have unintended consequences.
    </AlertDialogDescription>
  </AlertDialogHeader>
  {/* Footer */}
</AlertDialogContent>

Custom Animations

<AlertDialogContent className="data-[state=open]:animate-in data-[state=open]:slide-in-from-bottom-4">
  {/* Content */}
</AlertDialogContent>

Size Variants

// Small dialog
<AlertDialogContent className="sm:max-w-sm">
  {/* Content */}
</AlertDialogContent>

// Large dialog
<AlertDialogContent className="sm:max-w-2xl">
  {/* Content */}
</AlertDialogContent>

SEO Considerations

While dialogs don't directly impact SEO, consider:

  1. Progressive Enhancement: Ensure critical actions work without JavaScript
  2. Semantic HTML: Proper heading hierarchy in content
  3. Accessible Content: Clear, descriptive text for all users
  4. Performance: Lazy load dialog content when appropriate