Skip to Content

Alert Dialog

A modal dialog that interrupts the user with a confirmation and waits for an answer.

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

export function Demo() {
  return (
    <AlertDialog>
      <AlertDialogTrigger render={<Button variant="outline" />}>Delete project</AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>Delete "Q4 Planning"?</AlertDialogTitle>
          <AlertDialogDescription>
            This permanently removes the project and its 38 tasks. This cannot be undone.
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>Cancel</AlertDialogCancel>
          <AlertDialogAction variant="destructive">Delete</AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  )
}

Installation

npm i @dapplelight/signet-ui

Usage

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

<AlertDialog>
  <AlertDialogTrigger render={<Button />}>Open</AlertDialogTrigger>
  <AlertDialogContent>
    <AlertDialogHeader>
      <AlertDialogTitle>Are you sure?</AlertDialogTitle>
      <AlertDialogDescription>This cannot be undone.</AlertDialogDescription>
    </AlertDialogHeader>
    <AlertDialogFooter>
      <AlertDialogCancel>Cancel</AlertDialogCancel>
      <AlertDialogAction>Continue</AlertDialogAction>
    </AlertDialogFooter>
  </AlertDialogContent>
</AlertDialog>

Examples

Controlled

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

const [open, setOpen] = useState(false)
const [status, setStatus] = useState("Subscription active")

<AlertDialog
  open={open}
  onOpenChange={(next, details) => {
    setOpen(next)
    if (!next && details.returnValue === "confirm") setStatus("Subscription cancelled")
  }}
>
  <AlertDialogTrigger render={<Button variant="outline" />}>Cancel subscription</AlertDialogTrigger>
  <AlertDialogContent>
    <AlertDialogHeader>
      <AlertDialogTitle>Cancel your Pro plan?</AlertDialogTitle>
      <AlertDialogDescription>You keep access until 30 Nov 2026.</AlertDialogDescription>
    </AlertDialogHeader>
    <AlertDialogFooter>
      <AlertDialogCancel>Keep plan</AlertDialogCancel>
      <AlertDialogAction>Cancel plan</AlertDialogAction>
    </AlertDialogFooter>
  </AlertDialogContent>
</AlertDialog>

Async action

3 pending refunds
import { AlertDialogAction } from "@dapplelight/signet-ui/components/alert-dialog"
import { refundOrders } from "./actions"

<AlertDialogAction
  action={async () => {
    await refundOrders()
  }}
>
  Refund
</AlertDialogAction>

API Reference

AlertDialog

Takes the options of the underlying alert dialog. It is always modal and an outside press never closes it.

PropTypeDefaultDescription
openboolean-Controlled open state.
defaultOpenbooleanfalseInitial state when uncontrolled.
onOpenChange(open, details) => void-details.reason says why, details.returnValue is the value of the button that closed it ("confirm" or "cancel").
dismissiblebooleantrueWhether Escape and Android's back gesture can close it. An outside press never does.
onDismissAttempt(details) => void-Runs when a dismissal was refused.
onOpenChangeComplete(open: boolean) => void-Runs after the open or close animation ends.
pendingAnnouncementstring"Loading"What screen readers hear once an action has been pending for a second.
persistentElements(Element | string)[]-Elements that stay usable while the page is inert.
controllerAlertDialogController-Open, close and inspect it from anywhere (createAlertDialogController()).

AlertDialogTrigger

Renders a button. Pass render to use your own element, such as a Button. Also takes payload, shortcut and disabled.

AlertDialogContent

PropTypeDefaultDescription
classNamestring-Merged onto the dialog surface.
backdropClassNamestring-Merged onto the backdrop.
initialFocus"auto" | "content" | "none" | selector | element | function"auto"Where focus goes on open. "auto" picks the first Close part, the safe answer.
finalFocus"trigger" | "none" | selector | element | function"trigger"Where focus returns on close.
keepMountedbooleanfalseKeep it in the page, hidden, while closed.

AlertDialogHeader, AlertDialogFooter

PropTypeDefaultDescription
classNamestring-Merged last.

Plus every div prop. The footer stacks on phones and right-aligns from sm up.

AlertDialogTitle, AlertDialogDescription

Render an h2 and a p. They name and describe the dialog for screen readers.

AlertDialogAction

The agreeing answer. Closes with returnValue "confirm".

PropTypeDefaultDescription
variantdefault | outline | secondary | ghost | destructive | linkdefaultButton variant.
sizeButton size-Button size.
action(event, details) => unknown-Runs before closing. Return a Promise to keep the dialog open and the button pending until it settles.
valuestring"confirm"Reported as returnValue.

AlertDialogCancel

The safe answer. Put it first: it takes focus when the dialog opens. Closes with returnValue "cancel".

PropTypeDefaultDescription
variantButton variantoutlineButton variant.
sizeButton size-Button size.
action(event, details) => unknown-Runs before closing.