Alert Dialog
newUpdating Linux… wait, where did Linux go? This is why we ask first. A modal that interrupts with a question and waits for an answer.
Installation
Usage
<script setup lang="ts">
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from '@/components/ui/alert-dialog'
import { Button } from '@/components/ui/button'
</script>
<template>
<AlertDialog>
<AlertDialogTrigger as-child>
<Button variant="outline" color="neutral">Show dialog</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
<AlertDialogDescription>This cannot be undone.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction>Continue</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</template>
A Dialog that wants an answer. A click outside won't close it: only Escape or a button will.
Focus starts on Cancel, so a reflex Enter backs out instead of deleting everything.
AlertDialogAction and AlertDialogCancel are regular Buttons that close the dialog.
Examples
Small with an icon
size="sm" narrows the window to 320px, centres the header and gives the two answers equal width. AlertDialogMedia puts an icon above the title; tint it with a class.
From code
Like window.confirm(), but it doesn't look like it's from 1999. useConfirm() opens alert dialogs from code and waits for the answer. Set up the dialog service first.
Confirm
confirm() returns the same result as openDialog: ok is true when the action is pressed, false on Cancel or Escape.
import { useConfirm } from '@/components/ui/confirm'
const { confirm } = useConfirm()
const discard = async () => {
const result = await confirm({
title: 'Discard the draft?',
description: 'Your unsaved changes will be lost.',
action: 'Discard',
color: 'destructive',
})
if (result.ok) draft.value = ''
}
The draft is open.
Waiting for work
Pass onConfirm, and it runs before the dialog closes. Until it finishes, the action shows a spinner and nothing can close the dialog. If it throws, the dialog stays open for another try.
Alert
alert() shows a message with one button.
Prompt
prompt() asks for a line of text and gives it back as value. To reject the input, return an error message from validate, and the dialog stays open. It can be async.
Aurora
Each call returns the same handle as openDialog, so dismiss() and isOpen work too. Need more control? Open ConfirmDialog or PromptDialog with openDialog or defineDialog yourself.
API Reference
Every part accepts class, merged over its base classes.
AlertDialog
| Prop | Default | Type and description |
|---|---|---|
| open | — | booleanWhether it is open, bound with |
| defaultOpen | — | booleanWhether the dialog starts open when |
| unmountOnHide | — | booleanRemoves the window from the DOM while closed. Turn it off to keep its state between openings. |
| Event | Payload and description |
|---|---|
| update:open | [value: boolean]Fires when the dialog opens or closes. |
| Slot | Scope and description |
|---|---|
| default | { open: boolean; close: () => void; }The trigger and the content. Receives |
AlertDialogTrigger
| Prop | Default | Type and description |
|---|---|---|
| asChild | — | booleanRender the single child instead of a button, merging the trigger's attributes and behaviour onto it. |
| as | — | AsTag | ComponentThe element or component to render. |
| Slot | Scope and description |
|---|---|
| default | {}The trigger's content, or with |
AlertDialogContent
| Prop | Default | Type and description |
|---|---|---|
| forceMount | — | booleanKeeps the window mounted while closed, for animating it with a JavaScript library. |
| disableOutsidePointerEvents | — | booleanBlocks pointer events outside the window. On by default, as an alert dialog is always modal. |
| asChild | — | booleanNot supported on this part: core adds the hidden title fallback next to your content, so there is more than one node to merge onto a single child. |
| as | — | AsTag | ComponentThe element or component to render. |
| size | "md" | AlertDialogSize
|
| Event | Payload and description |
|---|---|
| escapeKeyDown | [event: KeyboardEvent]Fires on Escape. Call |
| pointerDownOutside | [event: PointerDownOutsideEvent]Fires on a pointer press outside. An outside press never closes an alert dialog. |
| focusOutside | [event: FocusOutsideEvent]Fires when focus moves outside. |
| interactOutside | [event: PointerDownOutsideEvent | FocusOutsideEvent]Fires on any press or focus outside. |
| openAutoFocus | [event: Event]Fires when focus moves into the window on open, before it moves on to |
| closeAutoFocus | [event: Event]Fires when focus returns to the trigger on close. Call |
| Slot | Scope and description |
|---|---|
| default | {}
|
AlertDialogHeader
| Slot | Scope and description |
|---|---|
| default | {}An optional |
AlertDialogMedia
| Slot | Scope and description |
|---|---|
| default | {}An icon, drawn at 24px on a muted tile. Tint the tile with a class. |
AlertDialogTitle
| Prop | Default | Type and description |
|---|---|---|
| asChild | — | booleanRender the single child instead of a wrapper, merging this part's attributes and classes onto it. |
| as | — | AsTag | ComponentThe element or component to render. |
| Slot | Scope and description |
|---|---|
| default | {}The question, which names the dialog for screen readers. |
AlertDialogDescription
| Prop | Default | Type and description |
|---|---|---|
| asChild | — | booleanRender the single child instead of a wrapper, merging this part's attributes and classes onto it. |
| as | — | AsTag | ComponentThe element or component to render. |
| Slot | Scope and description |
|---|---|
| default | {}What happens on each answer, which screen readers announce with the title. |
AlertDialogFooter
| Slot | Scope and description |
|---|---|
| default | {}
|
AlertDialogAction
| Prop | Default | Type and description |
|---|---|---|
| variant | — | "solid" | "soft" | "subtle" | "outline" | "ghost" | "link" | nullThe button's variant. |
| color | — | ButtonColor | (string & {})The button's colour, |
| size | — | ButtonSizeThe button's size. |
| Slot | Scope and description |
|---|---|
| default | {}The answer that goes ahead. Pressing it closes the dialog. |
AlertDialogCancel
| Prop | Default | Type and description |
|---|---|---|
| variant | "outline" | "outline" | "solid" | "soft" | "subtle" | "ghost" | "link" | nullThe button's variant, |
| color | "neutral" | ButtonColor | (string & {})The button's colour, |
| size | — | ButtonSizeThe button's size. |
| Slot | Scope and description |
|---|---|
| default | {}The answer that backs out. It takes focus when the dialog opens. |
ConfirmDialog
The options of confirm() and alert().
| Prop | Default | Type and description |
|---|---|---|
| title* | — | stringThe question. |
| description | — | stringWhat happens on each answer. |
| action | "OK" | stringThe action button's text. |
| cancel | — | stringThe cancel button's text. Without it there is no cancel button, as in |
| color | — | ButtonColor | (string & {})The action button's colour. |
| size | — | AlertDialogSize
|
| onConfirm | — | (() => unknown)Runs when the action is pressed, before the dialog closes. While its promise is pending the buttons are disabled and Escape is ignored; if it throws, the dialog stays open. |
PromptDialog
The options of prompt().
| Prop | Default | Type and description |
|---|---|---|
| title* | — | stringThe question. It also labels the input when there is no |
| description | — | stringText under the title. |
| label | — | stringThe input's visible label. |
| placeholder | — | stringText shown while the input is empty. |
| defaultValue | "" | stringThe input's starting value. |
| type | "text" | "text" | "email" | "password" | "search" | "tel" | "url"The input's |
| action | "OK" | stringThe submit button's text, “OK” by default. |
| cancel | "Cancel" | stringThe cancel button's text, “Cancel” by default. |
| color | — | ButtonColor | (string & {})The submit button's colour. |
| size | — | AlertDialogSize
|
| validate | — | ((value: string) => string | Promise<string | undefined> | undefined)Checks the value on submit and returns an error message to keep the dialog open, or nothing to go on. May return a promise. |
| onConfirm | — | ((value: string) => unknown)Runs with the value once it is valid, before the dialog closes. While its promise is pending the buttons are disabled and Escape is ignored; if it throws, the dialog stays open. |
Props
- open
booleanWhether it is open, bound withv-model:open.- defaultOpen
booleanWhether the dialog starts open whenv-model:openis not used.- unmountOnHide
booleanRemoves the window from the DOM while closed. Turn it off to keep its state between openings.
Emits
- update:open
[value: boolean]Fires when the dialog opens or closes.
Slots
- default
{ open: boolean; close: () => void; }The trigger and the content. Receivesopenandclose.
Props
- asChild
booleanRender the single child instead of a button, merging the trigger's attributes and behaviour onto it.- as
AsTag | ComponentThe element or component to render.
Slots
- default
{}The trigger's content, or withas-childthe element that opens the dialog.
Props
- forceMount
booleanKeeps the window mounted while closed, for animating it with a JavaScript library.- disableOutsidePointerEvents
booleanBlocks pointer events outside the window. On by default, as an alert dialog is always modal.- asChild
booleanNot supported on this part: core adds the hidden title fallback next to your content, so there is more than one node to merge onto a single child.- as
AsTag | ComponentThe element or component to render.- size
"md" AlertDialogSizemdis as wide as a dialog.smis 320px wide, centres the header and splits the footer into two equal buttons.
Emits
- escapeKeyDown
[event: KeyboardEvent]Fires on Escape. CallpreventDefault()to keep it open.- pointerDownOutside
[event: PointerDownOutsideEvent]Fires on a pointer press outside. An outside press never closes an alert dialog.- focusOutside
[event: FocusOutsideEvent]Fires when focus moves outside.- interactOutside
[event: PointerDownOutsideEvent | FocusOutsideEvent]Fires on any press or focus outside.- openAutoFocus
[event: Event]Fires when focus moves into the window on open, before it moves on toAlertDialogCancel.- closeAutoFocus
[event: Event]Fires when focus returns to the trigger on close. CallpreventDefault()to keep focus where it is.
Slots
- default
{}AlertDialogHeaderandAlertDialogFooter.
Slots
- default
{}An optionalAlertDialogMedia, the title and the description.
Slots
- default
{}An icon, drawn at 24px on a muted tile. Tint the tile with a class.
Props
- asChild
booleanRender the single child instead of a wrapper, merging this part's attributes and classes onto it.- as
AsTag | ComponentThe element or component to render.
Slots
- default
{}The question, which names the dialog for screen readers.
Props
- asChild
booleanRender the single child instead of a wrapper, merging this part's attributes and classes onto it.- as
AsTag | ComponentThe element or component to render.
Slots
- default
{}What happens on each answer, which screen readers announce with the title.
Slots
- default
{}AlertDialogCancelandAlertDialogAction. They stack, action on top, below 640px.
Props
- variant
"solid" | "soft" | "subtle" | "outline" | "ghost" | "link" | nullThe button's variant.- color
ButtonColor | (string & {})The button's colour,destructivefor an action that deletes.- size
ButtonSizeThe button's size.
Slots
- default
{}The answer that goes ahead. Pressing it closes the dialog.
Props
- variant
"outline" "outline" | "solid" | "soft" | "subtle" | "ghost" | "link" | nullThe button's variant,outlineby default.- color
"neutral" ButtonColor | (string & {})The button's colour,neutralby default.- size
ButtonSizeThe button's size.
Slots
- default
{}The answer that backs out. It takes focus when the dialog opens.
The options of confirm() and alert().
Props
- title*
stringThe question.- description
stringWhat happens on each answer.- action
"OK" stringThe action button's text.useConfirmpasses “Confirm” forconfirmand “OK” foralert.- cancel
stringThe cancel button's text. Without it there is no cancel button, as inalert.- color
ButtonColor | (string & {})The action button's colour.- size
AlertDialogSizesmormd(default), as onAlertDialogContent.- onConfirm
(() => unknown)Runs when the action is pressed, before the dialog closes. While its promise is pending the buttons are disabled and Escape is ignored; if it throws, the dialog stays open.
The options of prompt().
Props
- title*
stringThe question. It also labels the input when there is nolabel.- description
stringText under the title.- label
stringThe input's visible label.- placeholder
stringText shown while the input is empty.- defaultValue
"" stringThe input's starting value.- type
"text" "text" | "email" | "password" | "search" | "tel" | "url"The input'stype:text,email,password,search,telorurl.- action
"OK" stringThe submit button's text, “OK” by default.- cancel
"Cancel" stringThe cancel button's text, “Cancel” by default.- color
ButtonColor | (string & {})The submit button's colour.- size
AlertDialogSizesmormd(default), as onAlertDialogContent.- validate
((value: string) => string | Promise<string | undefined> | undefined)Checks the value on submit and returns an error message to keep the dialog open, or nothing to go on. May return a promise.- onConfirm
((value: string) => unknown)Runs with the value once it is valid, before the dialog closes. While its promise is pending the buttons are disabled and Escape is ignored; if it throws, the dialog stays open.
Data attributes
| data-slot | Attributes | Description |
|---|---|---|
| alert-dialog-trigger | data-state | The element that opens the dialog. |
| dialog-overlay | data-state | The scrim behind the window, shared with Dialog. |
| alert-dialog-content | data-state, data-size | The window, with role="alertdialog". data-size is sm or md. |
| alert-dialog-header | — | The media, title and description. |
| alert-dialog-media | — | The icon tile above the title. |
| alert-dialog-title | — | The question. |
| alert-dialog-description | — | The text under the title. |
| alert-dialog-footer | — | The answers. |
| alert-dialog-action | data-variant, data-color, data-size | The answer that goes ahead. |
| alert-dialog-cancel | data-variant, data-color, data-size | The answer that backs out. |
Accessibility
The dialog is named by its AlertDialogTitle and described by its AlertDialogDescription, which screen readers announce together when it opens. Always give it a title. Focus stays inside the window until it closes, then returns to the element that opened it.
| Key | Action |
|---|---|
| Tab / Shift + Tab | Moves between the answers, wrapping around inside the window. |
| Enter / Space | Presses the focused answer. Cancel has focus when the dialog opens. |
| Escape | Closes the dialog as Cancel does. While onConfirm runs, or an input method is composing text, it does nothing. |