Alert Dialog
Overlay
Feedback
Modal dialog for important confirmations with multiple variants.
Preview
Different dialog variants for different contexts
Variants
All available dialog variants
Default
Standard confirmation dialogs
Danger
Destructive actions like delete
Warning
Unsaved changes, data loss
Info
Updates, information prompts
Success
Successful operations
Props
Component API reference
| Prop | Type | Default | Description |
|---|---|---|---|
open* | boolean | — | Controls dialog visibility |
onOpenChange* | (open: boolean) => void | — | Open state change handler |
title* | string | — | Dialog title |
description* | string | — | Dialog description |
variant | "default" | "danger" | "warning" | "info" | "success" | "default" | Visual variant |
confirmText | string | "Confirm" | Confirm button text |
cancelText | string | "Cancel" | Cancel button text |
loading | boolean | false | Show loading spinner |
onConfirm | () => void | — | Confirm button click handler |
onCancel | () => void | — | Cancel button click handler |
icon | ReactNode | — | Custom icon component |
children | ReactNode | — | Additional dialog content |
Usage
Import and implementation examples
import { useState } from 'react'
import { AlertDialog } from '@/components/ui/alert-dialog'
import { Button } from '@/components/ui/button'
function DeleteButton() {
const [open, setOpen] = useState(false)
return (
<>
<Button variant="destructive" onClick={() => setOpen(true)}>
Delete Account
</Button>
<AlertDialog
open={open}
onOpenChange={setOpen}
title="Delete Account"
description="This will permanently delete your account. This action cannot be undone."
variant="danger"
confirmText="Delete"
cancelText="Cancel"
onConfirm={() => {
console.log('Deleted')
setOpen(false)
}}
/>
</>
)
}
// With loading state
const [loading, setLoading] = useState(false)
<AlertDialog
open={open}
onOpenChange={setOpen}
title="Processing..."
description="Please wait while we process your request."
variant="info"
loading={loading}
onConfirm={() => {
setLoading(true)
// Async operation
setTimeout(() => {
setLoading(false)
setOpen(false)
}, 2000)
}}
/>Features
Built-in functionality
- 5 variants: Default, danger, warning, info, success
- Loading states: Spinner during async operations
- Keyboard navigation: Escape to close
- Click outside: Close dialog on backdrop click
- Custom icons: Override default variant icons
- Customizable buttons: Custom confirm/cancel text
- Body scroll lock: Prevents background scroll
- Dark mode: Full dark mode support
Accessibility
Accessibility considerations
ARIA Attributes
Uses role="alertdialog" for modalaria-labelledby for titlearia-describedby for descriptionaria-modal="true" when openKeyboard Navigation
| Key | Action |
|---|---|
| Escape | Close dialog |
| Tab | Navigate between buttons |
| Enter | Activate focused button |
Notes
- Focus trapped within dialog when open
- Focus returns to trigger on close
- Screen reader announces dialog content