Alert Dialog

new

Updating Linux… wait, where did Linux go? This is why we ask first. A modal that interrupts with a question and waits for an answer.

Demo

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.

Small alert dialog

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 = ''
}
Confirm demo

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.

Confirm with async work
Aurora
Borealis

Alert

alert() shows a message with one button.

Alert from code

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.

Prompt from code

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.

Data attributes

data-slotAttributesDescription
alert-dialog-triggerdata-stateThe element that opens the dialog.
dialog-overlaydata-stateThe scrim behind the window, shared with Dialog.
alert-dialog-contentdata-state, data-sizeThe 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-actiondata-variant, data-color, data-sizeThe answer that goes ahead.
alert-dialog-canceldata-variant, data-color, data-sizeThe 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.

KeyAction
Tab / Shift + TabMoves between the answers, wrapping around inside the window.
Enter / SpacePresses the focused answer. Cancel has focus when the dialog opens.
EscapeCloses the dialog as Cancel does. While onConfirm runs, or an input method is composing text, it does nothing.

Changelog

  1. registry 0.22.0

    d6571f8 — The footer of an sm AlertDialog splits into as many equal columns as it has buttons, so alert({ size: "sm" }) gives its lone OK button the whole width instead of half.

    ca08048 — A Dialog (or AlertDialog, or a useConfirm prompt) opened over a Drawer, or a Drawer opened over a Dialog, dims the page once: the lower overlay turns transparent as it already did for a dialog over a dialog.

  2. registry 0.21.0

    1ac32b3 — AlertDialogMedia is centred again in an AlertDialogContent with size="sm": AlertDialogHeader centres its children at sm, as the footer keeps its two-column grid.

  3. registry 0.19.0

    adf96cd — Parts no longer style themselves from their root's size through group-data-[size=…] variants, which beat any plain class of yours. size is the density of the root, and a class on a part wins:

    - Card, Empty, Alert and Stepper: CardTitle, CardDescription, EmptyTitle, EmptyDescription, AlertTitle, AlertDescription, StepperTitle and StepperDescription keep one text size at every root size (title-md/body-md, title-sm/body-md for Alert, title-sm/body-sm for Stepper). Empty's icon and gaps and Stepper's indicator number still follow the size, through --empty-icon, --empty-header-gap, --empty-content-gap and --stepper-text on the root. - Menu and DrawerMenu labels, Menubar triggers and the Calendar heading, weekday cells and week numbers keep scaling with the size, through --menu-label, --menubar-text, --calendar-heading and --calendar-label (each with a -leading twin) set by the root. - AlertDialog size="sm" centres its text through the content, so text-start on AlertDialogHeader wins; the footer keeps its two-column grid.

  4. registry 0.13.0

    93e57a0 — Every size on the control scale is named xs to xl; default and the bare icon are gone. To move over:

    - Button: size="default" becomes size="md", or leave size out; size="icon" becomes size="icon-md". The full set is xs to xl and icon-xs to icon-xl. Toggle, ToggleGroup, ToggleGroupItem, the Toolbar parts, AlertDialogAction, AlertDialogCancel and ToastAction take the same names; Button, Toggle and ToggleGroup default to md, the Toolbar parts keep sm and ToastAction keeps xs. InputGroupButton keeps its own xs, sm, icon-xs and icon-sm, 24 and 32px to fit inside the group, xs by default. Pagination hands its md to Button as it is. Button, Toggle and ToggleGroupItem set data-variant, data-color and data-size even when you leave them at their defaults. - AlertDialogContent: size="default" becomes size="md", still the default; sm is unchanged. The size option of useConfirm's confirm, alert and prompt takes the same names. - Table and DataTable: density becomes size, data-density becomes data-size, and the TableDensity type is now TableSize. sm, md and lg keep their 36, 44 and 52px rows; xs (28px) and xl (60px) are new, and DataTable's virtual row estimates cover them. The cells' padding above and below the text is now --table-cell-py on the table: 8px as before, 3px at xs. At xs, DataTable's sort buttons and expand and group toggles shrink to fit the row. The table-density example is now table-sizes. - DrawerMenu: size takes xs to xl like Menu and reads the control scale, plus room for a thumb. sm, md and lg keep their 40, 48 and 56px rows, padding and icons; xs is 36px and xl 64px. Overriding a --control-* token on :root now resizes DrawerMenu rows as well. - Select and Combobox: the list opens at the trigger's or anchor's size instead of always at md, so <SelectTrigger size="xl"> opens an xl list. size on SelectContent or ComboboxList picks another. Behind an as-child button, set size on ComboboxAnchor to match the button. The Select list now sets its menu variables on SelectContent instead of its viewport, so a class such as [--menu-item-height:2.5rem] on SelectContent reaches the items. - Input, Textarea, InputFloating, SelectTrigger, ComboboxAnchor and Separator set data-size with the default filled in, and the anchor sets data-variant too. With as-child, the anchor leaves the button's own data-variant and data-size alone.

  5. registry 0.9.0

    f3e1bd4 — New AlertDialog: shadcn's parts over Reka UI's AlertDialog on Dialog's window and overlay, with AlertDialogMedia, a sm size, and AlertDialogAction / AlertDialogCancel that take Button's variant, color and size. New confirm item: useConfirm() returns confirm, alert and prompt, which open alert dialogs from code and resolve with a DialogResult; onConfirm holds the dialog with a spinner while it runs and keeps it open if it throws, and prompt validates its value.

  6. core 0.8.0

    e60d032 — @kappa-ui/core/dialog exports AlertDialogContent: Reka UI's alert dialog content with the duties of DialogContent, so an alert dialog opened with openDialog reports "escape" and "close-button", holds while loading is on, ignores Escape during IME composition and gets the hidden title and description fallback. DialogContent now shares that code.