Alert Dialog

Interrupts to ask for a decision, such as confirming a destructive action. useConfirm() turns it into one awaited line.

Status: beta. Category: Overlays.

import { AlertDialog, AlertDialogTrigger, AlertDialogContent, AlertDialogHeader, AlertDialogTitle, AlertDialogDescription } from '@glyphui/ui'

Exports: AlertDialog, AlertDialogTrigger, AlertDialogContent, AlertDialogHeader, AlertDialogTitle, AlertDialogDescription, AlertDialogFooter, AlertDialogCancel, AlertDialogAction, alertDialogStyles, ConfirmProvider, useConfirm.

Notes

  • Cancel receives focus when it opens, so pressing Enter never confirms by accident.
  • Clicking the backdrop does not close it; the user has to choose.
  • Mount ConfirmProvider once near the root to use useConfirm() anywhere below it.

Example: useConfirm

Resolves true when confirmed, false when cancelled or dismissed.

import { Button, ConfirmProvider, toast, useConfirm } from '@glyphui/ui'

function DeleteProjectButton() {
  const confirm = useConfirm()

  async function handleDelete() {
    const confirmed = await confirm({
      title: 'Delete “Website redesign”?',
      description: 'Its 48 issues and all comments are deleted. This can’t be undone.',
      tone: 'danger',
    })
    if (confirmed) toast.success('Project deleted')
  }

  return (
    <Button variant="danger" onClick={handleDelete}>
      Delete project
    </Button>
  )
}

// Mount ConfirmProvider once at the root of your app.
export default function Example() {
  return (
    <ConfirmProvider>
      <DeleteProjectButton />
    </ConfirmProvider>
  )
}

Example: Composed

Build the dialog yourself when it needs custom content.

import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
  Button,
} from '@glyphui/ui'

export default function Example() {
  return (
    <AlertDialog>
      <AlertDialogTrigger asChild>
        <Button variant="outline">Leave page</Button>
      </AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>Discard unsaved changes?</AlertDialogTitle>
          <AlertDialogDescription>
            You edited 3 fields. Leaving now discards those edits.
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>Keep editing</AlertDialogCancel>
          <AlertDialogAction tone="danger">Discard changes</AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  )
}

Live page: https://glyphui.xyz/components/alert-dialog