A modal dialog that interrupts the user with important content and expects a response.
Loading...
Installation
Install the following dependencies:
Copy and paste the following code into your project.
Loading...
Update the import paths to match your project setup.
Composition
AlertDialog ├── AlertDialogTrigger └── AlertDialogContent ├── AlertDialogHeader │ ├── AlertDialogMedia (optional) │ ├── AlertDialogTitle │ └── AlertDialogDescription └── AlertDialogFooter ├── AlertDialogCancel └── AlertDialogAction
Examples
Default
Loading...
Destructive
Loading...
Small With Media
Loading...
Small
Loading...
With Media
Loading...
API Reference
AlertDialog
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | undefined | generated | Unique identifier used to connect the dialog parts. |
ids | ElementIds | undefined | — | Overrides generated identifiers for individual parts. |
aria-label | string | undefined | — | Accessible label used when no visible title labels the dialog. |
open | boolean | undefined | — | Open state when controlled. Supports Svelte binding. |
defaultOpen | boolean | undefined | false | Initial open state when uncontrolled. |
modal | boolean | undefined | true | Enables modal behavior. |
trapFocus | boolean | undefined | true | Keeps keyboard focus inside the open dialog. |
preventScroll | boolean | undefined | true | Prevents document scrolling while the dialog is open. |
restoreFocus | boolean | undefined | true | Restores focus after the dialog closes. |
initialFocusEl | (() => MaybeElement | false) | undefined | — | Returns the element that should receive initial focus. |
finalFocusEl | (() => MaybeElement) | undefined | — | Returns the element that should receive focus after closing. |
closeOnInteractOutside | boolean | undefined | true | Closes the dialog after an allowed outside interaction. |
closeOnEscape | boolean | undefined | true | Closes the dialog when Escape is pressed. |
role | "dialog" | "alertdialog" | undefined | "dialog" | Sets the role used by the underlying dialog machine. |
persistentElements | Array<() => Element | null> | undefined | — | Elements that remain interactive while the modal is open. |
onEscapeKeyDown | ((event: KeyboardEvent) => void) | undefined | — | Runs when Escape is pressed. |
onPointerDownOutside | ((event: PointerDownOutsideEvent) => void) | undefined | — | Runs when a pointer interaction starts outside the dialog. |
onFocusOutside | ((event: FocusOutsideEvent) => void) | undefined | — | Runs when focus moves outside the dialog. |
onInteractOutside | ((event: InteractOutsideEvent) => void) | undefined | — | Runs for pointer or focus interaction outside the dialog. |
onRequestDismiss | ((event: LayerDismissEvent) => void) | undefined | — | Runs when another dismissable layer requests that the dialog close. |
onOpenChange | ((details: OpenChangeDetails) => void) | undefined | — | Runs when the open state changes. |
triggerValue | string | null | undefined | — | Active trigger value when controlled. Supports Svelte binding. |
defaultTriggerValue | string | null | undefined | null | Initial active trigger value when uncontrolled. |
onTriggerValueChange | ((details: TriggerValueChangeDetails) => void) | undefined | — | Runs when the active trigger value changes. |
lazyMount | boolean | undefined | false | Mounts the dialog content only after it is first opened. |
unmountOnExit | boolean | undefined | false | Removes the dialog content from the DOM after it closes. |
skipAnimationOnMount | boolean | undefined | false | Skips the initial presence animation. |
onEnterComplete | VoidFunction | undefined | — | Runs after the enter animation completes. |
onExitComplete | VoidFunction | undefined | — | Runs after the exit animation completes. |
Use bind:open or bind:triggerValue for two-way binding.
AlertDialogTrigger
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | undefined | — | Value used to identify this trigger when activated. |
AlertDialogContent
| Prop | Type | Default | Description |
|---|---|---|---|
size | "default" | "sm" | undefined | "default" | Controls the dialog panel size. |
AlertDialogAction
| Prop | Type | Default | Description |
|---|---|---|---|
variant | "default" | "destructive" | "ghost" | "link" | "outline" | "secondary" | undefined | "default" | Controls the action button style. |
size | "default" | "icon" | "icon-lg" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "sm" | "xl" | "xs" | undefined | "default" | Controls the action button size. |
AlertDialogCancel
| Prop | Type | Default | Description |
|---|---|---|---|
variant | "default" | "destructive" | "ghost" | "link" | "outline" | "secondary" | undefined | "outline" | Controls the cancel button style. |
size | "default" | "icon" | "icon-lg" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "sm" | "xl" | "xs" | undefined | "default" | Controls the cancel button size. |
Context, Hooks, and Types
| Export | API |
|---|---|
AlertDialogContext | Render-prop component that exposes component state and actions. |
AlertDialogRootProvider | Provides an externally created API to the component tree. |
useAlertDialog | Creates or accesses the component API. |
useAlertDialogContext | Returns the nearest component API. |
AlertDialogOpenChangeDetails | Type for the details object passed to the matching change callback. |
Angular
React
Solid
Svelte
Vue