GooseUI

Components

Alert Dialog

New

A modal that interrupts the user to confirm an action that is hard to undo. It is a native dialog with role=alertdialog: the trigger is a button with Invoker Commands, Cancel and Action are command=close buttons, and Escape, the top layer, the backdrop, inert page content and focus return come from the browser. Cancel gets focus when the dialog opens, and a click on the backdrop does not close it. A tiny client script moves focus to Cancel, reports the state and wires the buttons in browsers without Invoker Commands.

Browser Support

Baseline Newly available
4 features
Details
<dialog>

Opens the modal in the top layer with a backdrop and Escape to close.

Your browser:
Invoker commands

Opens and closes the dialog from buttons without JavaScript. Without it a small script does the same.

Your browser:
@starting-style

Fades the dialog in when it opens. Without it the dialog appears at once.

Your browser:
transition-behavior

Fades the dialog out when it closes. Without it the dialog disappears at once.

Your browser:

Preview

Are you absolutely sure?

This action cannot be undone. It will permanently delete your account and remove your data from our servers.

Installation

npx shadcn@latest add @gooseui/alert-dialog
pnpm dlx shadcn@latest add @gooseui/alert-dialog
yarn dlx shadcn@latest add @gooseui/alert-dialog
bunx --bun shadcn@latest add @gooseui/alert-dialog

Usage

import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
import { Button } from "@/components/ui/button"

export default function Example() {
  return (
    <AlertDialog>
      <AlertDialogTrigger asChild>
        <Button variant="outline">Delete account</Button>
      </AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
          <AlertDialogDescription>
            This action cannot be undone.
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>Cancel</AlertDialogCancel>
          <AlertDialogAction>Continue</AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  )
}

Examples

Destructive action

With asChild a Button child keeps its own variant, so the confirmation can be red. The Action and Cancel buttons both close the dialog.

Remove this project?

All files and settings of the project will be deleted.

<AlertDialogFooter>
  <AlertDialogCancel>Cancel</AlertDialogCancel>
  <AlertDialogAction asChild>
    <Button variant="destructive">Remove</Button>
  </AlertDialogAction>
</AlertDialogFooter>

Controlled and with a handler

open and onOpenChange from a client component; the onClick of the Action runs the confirmed action. The browser still closes the dialog on its own.

Delete your account?

You will lose access to all your projects.

State: closed

"use client"

const [open, setOpen] = React.useState(false)

<AlertDialog open={open} onOpenChange={setOpen}>
  <AlertDialogTrigger asChild>
    <Button variant="destructive">Delete account</Button>
  </AlertDialogTrigger>
  <AlertDialogContent>
    …
    <AlertDialogAction asChild onClick={deleteAccount}>
      <Button variant="destructive">Delete</Button>
    </AlertDialogAction>
  </AlertDialogContent>
</AlertDialog>

Props

PropTypeDefaultDescription
openboolean-Controlled open state. Client components only
defaultOpenbooleanfalseOpen right after hydration
onOpenChange(open: boolean) => void-Called with the new state on every open and close. Client components only: functions cannot cross the server boundary
idstring-id of the dialog element. Generated when omitted
asChildbooleanfalseMerge the props into the only child, for example a Button (AlertDialogTrigger, AlertDialogAction, AlertDialogCancel)
classNamestring-Additional CSS classes

Features

  • role="alertdialog" on a native dialog, labelled by AlertDialogTitle and described by AlertDialogDescription.
  • Escape closes the dialog; a click on the backdrop does not, because an alert dialog needs an explicit answer.
  • Focus moves to Cancel on open, so Enter never confirms by accident, and returns to the trigger on close.
  • The page behind the dialog is inert and does not scroll.
  • Parts are linked without React context, so AlertDialog and its parts stay server components. A part rendered inside your own component is not reached: keep the parts in the JSX passed to AlertDialog.