Overview
A modal dialog that interrupts the user with important content and expects a response. Built on top of Dialog with additional features for alerts.Features
- Focus is automatically trapped within the dialog
- Can be controlled or uncontrolled
- Manages screen reader announcements with Title and Description components
- Esc key closes the dialog
- Prevents closing when clicking outside
- Scrolling is blocked on the body when open
- Always modal (cannot be changed)
- Focuses Cancel button by default
Installation
Anatomy
Import all parts and piece them together.API Reference
Root
Contains all the alert dialog component parts.The controlled open state of the alert dialog. Must be used in conjunction with
onOpenChange.The open state of the alert dialog when it is initially rendered. Use when you do not need to control its open state.
Event handler called when the open state of the alert dialog changes.
The
modal prop is not available on AlertDialog. It is always true.Trigger
The button that opens the alert dialog.Change the default rendered element for the one passed as a child, merging their props and behavior.
Portal
When used, portals your overlay and content parts into thebody.
Specify a container element to portal the content into.
Used to force mounting when more control is needed. Useful when controlling animation with React animation libraries.
Overlay
A layer that covers the inert portion of the view when the alert dialog is open.Change the default rendered element for the one passed as a child, merging their props and behavior.
Used to force mounting when more control is needed. Useful when controlling animation with React animation libraries.
Content
Contains the content to be rendered in the open alert dialog.Change the default rendered element for the one passed as a child, merging their props and behavior.
Used to force mounting when more control is needed. Useful when controlling animation with React animation libraries.
Event handler called when focus moves into the component after opening. It can be prevented by calling
event.preventDefault.Event handler called when focus moves to the trigger after closing. It can be prevented by calling
event.preventDefault.Event handler called when the escape key is down. It can be prevented by calling
event.preventDefault.Title
An accessible title to be announced when the alert dialog is opened.Change the default rendered element for the one passed as a child, merging their props and behavior.
Description
An accessible description to be announced when the alert dialog is opened.Change the default rendered element for the one passed as a child, merging their props and behavior.
Cancel
A button that closes the alert dialog. This button should be distinguished from Action buttons.Change the default rendered element for the one passed as a child, merging their props and behavior.
This button receives focus by default when the alert dialog opens.
Action
A button that closes the alert dialog and performs the main action.Change the default rendered element for the one passed as a child, merging their props and behavior.
Example
Accessibility
Adheres to the Alert and Message Dialogs WAI-ARIA design pattern.Keyboard Interactions
Space- Opens/closes the alert dialogEnter- Opens/closes the alert dialogTab- Moves focus to the next focusable elementShift + Tab- Moves focus to the previous focusable elementEsc- Closes the alert dialog and moves focus to trigger