Alert Dialog
A modal dialog that interrupts the user with important content and expects a response.
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.
Prop | Type | Default | Description |
---|---|---|---|
defaultOpen | boolean | false | The open state of the dialog when initially rendered. |
open | boolean | - | The controlled open state of the dialog. |
onOpenChange | (open: boolean) => void | - | Event handler called when the open state changes. |
children | React.ReactNode | - | The dialog trigger and content components. |
AlertDialogTrigger
The element that opens the dialog when interacted with.
Prop | Type | Default | Description |
---|---|---|---|
asChild | boolean | false | Merge props onto child element instead of creating a button. |
children | React.ReactNode | - | The trigger element or component. |
AlertDialogContent
The container for the dialog's content.
Prop | Type | Default | Description |
---|---|---|---|
className | string | - | 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.
Prop | Type | Default | Description |
---|---|---|---|
className | string | - | Additional CSS classes for styling. |
children | React.ReactNode | - | Title and description components. |
AlertDialogTitle
The dialog's main heading.
Prop | Type | Default | Description |
---|---|---|---|
className | string | - | Additional CSS classes for styling. |
children | React.ReactNode | - | The title text content. |
AlertDialogDescription
Descriptive text providing context about the dialog's purpose.
Prop | Type | Default | Description |
---|---|---|---|
className | string | - | Additional CSS classes for styling. |
children | React.ReactNode | - | The description text content. |
AlertDialogFooter
Container for action buttons.
Prop | Type | Default | Description |
---|---|---|---|
className | string | - | Additional CSS classes for styling. |
children | React.ReactNode | - | Action and cancel buttons. |
AlertDialogAction
The primary action button that confirms the dialog.
Prop | Type | Default | Description |
---|---|---|---|
className | string | - | Additional CSS classes for styling. |
onClick | (event: MouseEvent) => void | - | Handler for action confirmation. |
children | React.ReactNode | - | Button text content. |
AlertDialogCancel
The cancel button that dismisses the dialog without action.
Prop | Type | Default | Description |
---|---|---|---|
className | string | - | Additional CSS classes for styling. |
onClick | (event: MouseEvent) => void | - | Handler for cancellation. |
children | React.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
Key | Action |
---|---|
Enter | Activates focused button |
Space | Activates focused button |
Tab | Moves focus to next interactive element |
Shift + Tab | Moves focus to previous interactive element |
Escape | Closes dialog (can be prevented) |
ARIA Attributes
role="alertdialog"
: Identifies critical user interactionaria-modal="true"
: Indicates modal behavioraria-labelledby
: Links title to dialogaria-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
Async Actions
Controlled State
Best Practices
Content Guidelines
- Clear Titles: Use action-oriented questions or statements
- Concise Descriptions: Explain consequences clearly
- Actionable Buttons: Use verbs that describe the action
- 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:
- Progressive Enhancement: Ensure critical actions work without JavaScript
- Semantic HTML: Proper heading hierarchy in content
- Accessible Content: Clear, descriptive text for all users
- Performance: Lazy load dialog content when appropriate