The AlertDialog component extends the Base UI Alert Dialog with additional structural sub-components: AlertDialog.Header, AlertDialog.Body, and AlertDialog.Footer. While this adds to an already comprehensive component, these additions provide consistent layout patterns and better support for richer use cases beyond simple confirmation messages.
Installation
Here is the component implementation code that you can copy to your project:
The most common way to open an AlertDialog is by using the AlertDialog.Trigger component. Place it inside the AlertDialog.Root and it will automatically handle opening the dialog when clicked.
Opening with a detached Trigger
When defining the AlertDialog content next to its trigger is not practical, you can use a detached trigger with AlertDialog.createHandle(). This allows you to place the trigger button anywhere in your component tree while still controlling the same dialog instance.
Opening programmatically with state
For complete control over the dialog's visibility, use the open and onOpenChange props on AlertDialog.Root. This is useful when you need to open the dialog based on application logic, such as after an API call or in response to a menu action.
With a form and async submission
AlertDialogs can contain forms with validation. This example shows how to handle async form submission with a loading state on the submit button, keeping the dialog open until the operation completes.