A React component for building accessible alert dialogs with focus management and ARIA compliance
npx @tessl/cli install tessl/npm-radix-ui--react-alert-dialog@1.1.0Radix UI Alert Dialog provides low-level, accessible React components for building alert dialogs. Alert dialogs are modal dialogs that demand user attention and action before continuing, with enhanced accessibility features including proper focus management, keyboard navigation, and screen reader support.
npm install @radix-ui/react-alert-dialogimport * as AlertDialog from "@radix-ui/react-alert-dialog";Or import specific components:
import {
AlertDialog,
AlertDialogTrigger,
AlertDialogPortal,
AlertDialogOverlay,
AlertDialogContent,
AlertDialogTitle,
AlertDialogDescription,
AlertDialogAction,
AlertDialogCancel,
} from "@radix-ui/react-alert-dialog";For CommonJS:
const AlertDialog = require("@radix-ui/react-alert-dialog");import * as AlertDialog from "@radix-ui/react-alert-dialog";
function MyAlertDialog() {
return (
<AlertDialog.Root>
<AlertDialog.Trigger asChild>
<button>Delete item</button>
</AlertDialog.Trigger>
<AlertDialog.Portal>
<AlertDialog.Overlay />
<AlertDialog.Content>
<AlertDialog.Title>Are you absolutely sure?</AlertDialog.Title>
<AlertDialog.Description>
This action cannot be undone. This will permanently delete your item.
</AlertDialog.Description>
<div style={{ display: "flex", gap: 25, justifyContent: "flex-end" }}>
<AlertDialog.Cancel asChild>
<button>Cancel</button>
</AlertDialog.Cancel>
<AlertDialog.Action asChild>
<button>Yes, delete item</button>
</AlertDialog.Action>
</div>
</AlertDialog.Content>
</AlertDialog.Portal>
</AlertDialog.Root>
);
}Alert Dialog follows a compound component pattern where each part serves a specific role:
Key differences from regular dialogs:
The root container that manages dialog state and provides context to all child components.
interface AlertDialogProps {
children?: React.ReactNode;
open?: boolean;
defaultOpen?: boolean;
onOpenChange?: (open: boolean) => void;
}
const AlertDialog: React.FC<AlertDialogProps>;Also available as:
const Root: React.FC<AlertDialogProps>;Button or element that opens the alert dialog when activated.
interface AlertDialogTriggerProps extends React.ComponentPropsWithoutRef<'button'> {
asChild?: boolean;
}
const AlertDialogTrigger: React.ForwardRefExoticComponent<
AlertDialogTriggerProps & React.RefAttributes<HTMLButtonElement>
>;Also available as:
const Trigger: React.ForwardRefExoticComponent<
AlertDialogTriggerProps & React.RefAttributes<HTMLButtonElement>
>;Renders dialog content outside the normal DOM tree, typically in document.body.
interface AlertDialogPortalProps {
children?: React.ReactNode;
container?: HTMLElement;
forceMount?: true;
}
const AlertDialogPortal: React.FC<AlertDialogPortalProps>;Also available as:
const Portal: React.FC<AlertDialogPortalProps>;Background layer that appears behind the dialog content.
interface AlertDialogOverlayProps extends React.ComponentPropsWithoutRef<'div'> {
asChild?: boolean;
forceMount?: true;
}
const AlertDialogOverlay: React.ForwardRefExoticComponent<
AlertDialogOverlayProps & React.RefAttributes<HTMLDivElement>
>;Also available as:
const Overlay: React.ForwardRefExoticComponent<
AlertDialogOverlayProps & React.RefAttributes<HTMLDivElement>
>;Main dialog container with built-in accessibility features and focus management.
interface AlertDialogContentProps extends Omit<React.ComponentPropsWithoutRef<'div'>, 'onPointerDownOutside' | 'onInteractOutside'> {
asChild?: boolean;
forceMount?: true;
onEscapeKeyDown?: (event: KeyboardEvent) => void;
onOpenAutoFocus?: (event: Event) => void;
onCloseAutoFocus?: (event: Event) => void;
}
const AlertDialogContent: React.ForwardRefExoticComponent<
AlertDialogContentProps & React.RefAttributes<HTMLDivElement>
>;Also available as:
const Content: React.ForwardRefExoticComponent<
AlertDialogContentProps & React.RefAttributes<HTMLDivElement>
>;Key behaviors:
role="alertdialog" for accessibilityonPointerDownOutside and onInteractOutside are internally prevented)Required accessible title for the alert dialog.
interface AlertDialogTitleProps extends React.ComponentPropsWithoutRef<'h2'> {
asChild?: boolean;
}
const AlertDialogTitle: React.ForwardRefExoticComponent<
AlertDialogTitleProps & React.RefAttributes<HTMLHeadingElement>
>;Also available as:
const Title: React.ForwardRefExoticComponent<
AlertDialogTitleProps & React.RefAttributes<HTMLHeadingElement>
>;Required accessible description for the alert dialog content.
interface AlertDialogDescriptionProps extends React.ComponentPropsWithoutRef<'p'> {
asChild?: boolean;
}
const AlertDialogDescription: React.ForwardRefExoticComponent<
AlertDialogDescriptionProps & React.RefAttributes<HTMLParagraphElement>
>;Also available as:
const Description: React.ForwardRefExoticComponent<
AlertDialogDescriptionProps & React.RefAttributes<HTMLParagraphElement>
>;Note: The Description component is required for accessibility. In development mode, a console warning will be logged if the description is missing or not properly linked via aria-describedby. The warning provides detailed guidance on how to add proper descriptions for screen readers.
Primary action button that closes the dialog and executes the main action.
interface AlertDialogActionProps extends React.ComponentPropsWithoutRef<'button'> {
asChild?: boolean;
}
const AlertDialogAction: React.ForwardRefExoticComponent<
AlertDialogActionProps & React.RefAttributes<HTMLButtonElement>
>;Also available as:
const Action: React.ForwardRefExoticComponent<
AlertDialogActionProps & React.RefAttributes<HTMLButtonElement>
>;Cancel button that closes the dialog without executing the main action. Receives focus when the dialog opens.
interface AlertDialogCancelProps extends React.ComponentPropsWithoutRef<'button'> {
asChild?: boolean;
}
const AlertDialogCancel: React.ForwardRefExoticComponent<
AlertDialogCancelProps & React.RefAttributes<HTMLButtonElement>
>;Also available as:
const Cancel: React.ForwardRefExoticComponent<
AlertDialogCancelProps & React.RefAttributes<HTMLButtonElement>
>;Key behavior: Automatically receives focus when the alert dialog opens.
Advanced composition utility for creating scoped alert dialog contexts.
/**
* Creates a scoped context for alert dialog components
* Used for advanced composition scenarios with multiple dialog instances
*/
function createAlertDialogScope(): CreateScope;
interface CreateScope {
scopeName: string;
(): ScopeHook;
}
type ScopeHook = (scope: Scope) => { [__scopeProp: string]: Scope };
type Scope<C = any> = { [scopeName: string]: React.Context<C>[] } | undefined;Many components support the asChild prop for composition:
// Default behavior - renders a button
<AlertDialog.Trigger>Open</AlertDialog.Trigger>
// With asChild - merges props with the child element
<AlertDialog.Trigger asChild>
<MyCustomButton>Open</MyCustomButton>
</AlertDialog.Trigger>Content and Overlay components support forceMount for animation libraries:
<AlertDialog.Overlay forceMount={shouldForceMount} />
<AlertDialog.Content forceMount={shouldForceMount}>
{/* content */}
</AlertDialog.Content><AlertDialog.Root onOpenChange={(open) => console.log("Dialog is", open ? "open" : "closed")}>
<AlertDialog.Content
onOpenAutoFocus={(event) => {
// Prevent default focus behavior if needed
event.preventDefault();
}}
onCloseAutoFocus={(event) => {
// Handle focus return when dialog closes
}}
onEscapeKeyDown={(event) => {
// Escape key is prevented by default in alert dialogs
// This handler won't typically fire unless custom behavior is needed
}}
>
{/* content */}
</AlertDialog.Content>
</AlertDialog.Root>role="alertdialog" and ARIA labeling