Join our Discord Community

Shadcn Alert Dialog

React alert dialog for confirmation modals and destructive actions. Built with TypeScript and Tailwind CSS for Next.js applications with focus management.

Alert modal issues?

Join our Discord community for help from other developers.


Ever watched a user accidentally delete months of work with a single click? Yeah, that sick feeling in your stomach is exactly why alert dialogs exist. This shadcn/ui alert dialog component stops users in their tracks before they do something they can't undo in your Next.js application.

Confirmation dialog

Standard confirmation dialog with cancel and continue options:

Loading component...

Built on Radix UI AlertDialog with focus trapping, keyboard navigation, and full accessibility support. Works perfectly with TypeScript and styled with Tailwind CSS classes.

npx shadcn@latest add alert-dialog

Why alert dialogs actually work

Here's the thing—alert dialogs aren't just annoying popups. They're psychological speed bumps that save your users (and your reputation) from disasters.

Think about it: your brain operates on autopilot most of the time. You click, tap, and swipe without thinking. Alert dialogs snap users out of autopilot mode by forcing them to read, process, and make a deliberate choice.

The magic happens in those few seconds of interruption. Users suddenly realize what they're about to do, and most importantly, they get a clear way out. That's not just good UX—that's respecting your users' intent and protecting their data.

This React component handles all the tricky parts: focus management, keyboard navigation, and accessibility. No wrestling with JavaScript event handling or screen reader compatibility.

Common alert dialog patterns you'll use

Destructive confirmation

Confirmation for dangerous actions like deletion using Button variants:

Loading component...

Async operations

Keep the dialog open during async operations with loading states and disabled buttons:

Loading component...

Each pattern serves different use cases in your React applications. Whether you're building with Next.js or vanilla JavaScript, these alert dialog patterns handle the complex state management and accessibility requirements.

Features

This free open source alert dialog component includes everything you need:

  • Focus trapping - Keyboard focus stays within the dialog until dismissed
  • TypeScript native - Built with TypeScript, works perfectly in JavaScript projects
  • Accessibility first - Full screen reader support and ARIA compliance
  • Tailwind CSS styling - Easy customization with utility classes
  • Radix UI foundation - Battle-tested primitives for modal behavior
  • Keyboard navigation - Space, Enter, Escape, and Tab all work as expected

API Reference

AlertDialog

The root component that manages dialog state and focus.

PropTypeDefaultDescription
defaultOpenbooleanfalseInitial open state for uncontrolled usage
openboolean-Controlled open state
onOpenChange(open: boolean) => void-Callback when open state changes
childrenReact.ReactNode-Dialog components (trigger, content)

AlertDialogTrigger

The button that opens the dialog.

PropTypeDefaultDescription
asChildbooleanfalseRender as child component using Radix UI Slot
classNamestring-Additional CSS classes for styling
childrenReact.ReactNode-Trigger content (usually a button)

AlertDialogContent

The modal container with focus management and animations.

PropTypeDefaultDescription
classNamestring-Additional CSS classes for styling
onOpenAutoFocus(event: Event) => void-Custom focus behavior when opening
onCloseAutoFocus(event: Event) => void-Custom focus behavior when closing
onEscapeKeyDown(event: KeyboardEvent) => void-Handle Escape key presses
childrenReact.ReactNode-Dialog content (header, footer)

AlertDialogHeader

Container for title and description with proper spacing.

PropTypeDefaultDescription
classNamestring-Additional CSS classes for styling
childrenReact.ReactNode-Header content (title, description)

AlertDialogTitle

The dialog title announced to screen readers.

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

AlertDialogDescription

Additional context about the action and its consequences.

PropTypeDefaultDescription
classNamestring-Additional CSS classes for styling
childrenReact.ReactNode-Description content

AlertDialogFooter

Container for action buttons with proper alignment.

PropTypeDefaultDescription
classNamestring-Additional CSS classes for styling
childrenReact.ReactNode-Action buttons (cancel, confirm)

AlertDialogCancel

The cancel button that dismisses the dialog without action.

PropTypeDefaultDescription
asChildbooleanfalseRender as child component using Radix UI Slot
classNamestring-Additional CSS classes for styling
childrenReact.ReactNode-Button text content

AlertDialogAction

The confirm button that performs the action and closes the dialog.

PropTypeDefaultDescription
asChildbooleanfalseRender as child component using Radix UI Slot
classNamestring-Additional CSS classes for styling
childrenReact.ReactNode-Button text content

Keyboard Navigation

KeyAction
Space/EnterOpens dialog (when trigger focused)
EscapeCloses dialog and returns focus to trigger
TabMoves focus to next focusable element
Shift + TabMoves focus to previous focusable element

Accessibility Features

FeatureDescription
Focus trappingFocus stays within dialog until closed
Focus restorationReturns focus to trigger after closing
ARIA attributesProper roles, labels, and descriptions
Screen reader supportTitle and description announced when opened
Keyboard navigationFull keyboard accessibility

Production tips

Use alert dialogs sparingly. This free shadcn/ui component works best for truly destructive or irreversible actions. For simple confirmations, consider inline validation or Toast notifications instead.

Button hierarchy matters. Use Button variants to make dangerous actions visually distinct—destructive red buttons for deletion, default styling for safe actions.

Form integration works smoothly. You can trigger alert dialogs from Form validation or Input field changes. Just remember that the dialog will interrupt the form flow.

Test keyboard navigation thoroughly. This open source component handles Tab, Enter, and Escape automatically, but custom trigger buttons need testing.

Consider mobile experience. Alert dialogs work on touch devices, but make sure your button targets are large enough (44px minimum) and text is readable.

Integration with other components

Alert dialogs work great with Form validation workflows and Button actions throughout your React applications. Use them alongside Dialog components for different types of user interactions—alerts for confirmations, dialogs for complex input.

Questions you might have