Alert Dialog
The React Alert Dialog component shows a modal window on top of the page to ask users to confirm an important action. It is used for actions like deleting data, logging out, or making permanent changes.
It opens as an overlay and blocks interaction with the background until the user takes action. For example, users must confirm before deleting an account or removing a file.
Built with React and react-aria-components, and styled using Tailwind CSS. It supports keyboard navigation, focus management, and accessible modal behavior.
Installation
Install the component using the Tailgrids CLI.
npx @tailgrids/cli add alert-dialogAnatomy
Wrap the AlertDialog component with Backdrop to create a complete alert dialog experience with backdrop behavior.
"use client";
import { AlertDialog } from "@/registry/core/alert-dialog";
import { Button } from "@/registry/core/button";
import {
DialogClose,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle
} from "@/registry/core/dialog";
import { OverlayWrapper } from "@/registry/core/overlay";
export default function AlertDialogPreview() {
return (
<OverlayWrapper>
<Button />
<AlertDialog>
<DialogHeader>
<DialogTitle />
<DialogDescription />
</DialogHeader>
<DialogFooter>
<DialogClose />
</DialogFooter>
</AlertDialog>
</OverlayWrapper>
);
}Examples
Basic
Wrap the AlertDialog component with OverlayWrapper to create a basic alert dialog component.
Controlled
Use the isOpen and onOpenChange props to control the open state of the alert dialog.
Autofocus
Pass autoFocus to desired elements to automatically focus them when the alert dialog opens.
API Reference
AlertDialog
The root component that wraps all the content and provides alert dialog behavior and accessibility features.
| Prop | Type | Default | Description |
|---|---|---|---|
children | React.ReactNode | - | The trigger and content elements |
isOpen | boolean | - | Controlled open state |
defaultOpen | boolean | - | Uncontrolled default open state |
onOpenChange | (isOpen: boolean) => void | - | Called when the open state changes |
className | string | - | Additional CSS classes |
Composition
The Alert Dialog should be composed the same as Dialog without the Backdrop component.
OverlayWrapper β A stateful wrapper that manages the open state of the alert dialog and backdrop.
βββ Button β The trigger button that opens the alert dialog.
βββ AriaDialog β The dialog panel with `alertdialog` role and accessible behavior.
βββ children β Content of the dialog.- Follow the same composition as
Dialogwithout theBackdropcomponent. - Use
DialogHeader,DialogTitle,DialogDescriptionand otherDialogsub-components to structure the content of the alert dialog.
Accessibility
- Dialog role: The
AlertDialogcomponent renders withrole="alertdialog"by default. - Focus management: The alert dialog keeps focus while open and returns focus to the trigger when it closes.
- Outside click and Keyboard support: Closing the dialog by clicking outside and
Escapekey is disabled by default. Can be enabled withisDismissableandisKeyboardDismissDisabledprops. - Screen reader support: Screen readers announce the dialog as an alertdialog, so users understand that immediate action is required.
