Morphos
Overlays

Alert Dialog

A modal dialog for critical confirmations that requires an explicit user action to proceed or cancel.

Interactive example

Open in Storybook

Installation

npm install @morphos/overlays
pnpm add @morphos/overlays
yarn add @morphos/overlays
bun add @morphos/overlays

Import

import {
  AlertDialog,
  AlertDialogTrigger,
  AlertDialogContent,
  AlertDialogTitle,
  AlertDialogDescription,
  AlertDialogAction,
  AlertDialogCancel,
} from '@morphos/overlays'

Usage

@Component()
class MyComponent extends StatefulComponent {
  @State() alertDialog = new AlertDialog()

  handleDelete() {
    // perform destructive action
  }

  render() {
    return (
      <>
        <AlertDialogTrigger alertDialog={this.alertDialog}>
          Delete account
        </AlertDialogTrigger>
        <AlertDialogContent alertDialog={this.alertDialog}>
          <AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
          <AlertDialogDescription>
            This action cannot be undone. Your account and all associated data
            will be permanently deleted.
          </AlertDialogDescription>
          <AlertDialogCancel alertDialog={this.alertDialog}>
            Cancel
          </AlertDialogCancel>
          <AlertDialogAction alertDialog={this.alertDialog} onClick={() => this.handleDelete()}>
            Delete account
          </AlertDialogAction>
        </AlertDialogContent>
      </>
    )
  }
}

Compound components

ComponentDescription
AlertDialogRoot state manager. Owns open state and exposes openDialog() / closeDialog() / toggle().
AlertDialogTrigger<button> that opens the dialog. Sets aria-haspopup="dialog", aria-expanded, and data-open.
AlertDialogContentRenders into a Portal with a sibling backdrop <div data-morphos-backdrop>. Sets role="alertdialog" and aria-modal="true". Applies focus trap and scroll lock while open.
AlertDialogTitleRenders as <h2> by default (configurable via as).
AlertDialogDescriptionRenders as <p>.
AlertDialogAction<button> that calls closeDialog() and then runs its onClick.
AlertDialogCancel<button> that calls closeDialog() and then runs its onClick.

Props — AlertDialog

PropTypeDefaultDescription
openboolean—Controlled open state. When set, the consumer owns the state.
defaultOpenbooleanfalseInitial open state in uncontrolled mode.
onOpenChange(open: boolean) => void—Called when the open state changes.
closeOnEscapebooleantrueWhether pressing Escape closes the dialog.
closeOnOutsideClickbooleantrueWhether clicking the backdrop closes the dialog.
childrenChildren—Content — typically AlertDialogTrigger and AlertDialogContent.

Methods

MethodDescription
openDialog()Opens the dialog. Emits onOpenChange(true).
closeDialog()Closes the dialog. Emits onOpenChange(false).
toggle()Toggles the open state. Emits onOpenChange.

Props — AlertDialogTrigger

PropTypeDefaultDescription
alertDialogAlertDialog—The AlertDialog instance this trigger controls.
childrenChildren—The trigger element content.
classstring—CSS class.
idstring—HTML id.

Props — AlertDialogContent

PropTypeDefaultDescription
alertDialogAlertDialog—The AlertDialog instance this content belongs to.
childrenChildren—Dialog body content.
classstring—CSS class.
idstring—HTML id. Falls back to an auto-generated id.
aria-labelstring—Accessible label for the dialog.
aria-labelledbystring—ID of the element that labels the dialog (e.g. an AlertDialogTitle).
aria-describedbystring—ID of the element that describes the dialog (e.g. an AlertDialogDescription).

Props — AlertDialogTitle

PropTypeDefaultDescription
as"h1" | "h2" | "h3" | "h4" | "h5" | "h6""h2"The HTML heading element to render.
childrenChildren—Title text.
classstring—CSS class.
idstring—HTML id.

Props — AlertDialogDescription

PropTypeDefaultDescription
childrenChildren—Description text.
classstring—CSS class.
idstring—HTML id.

Props — AlertDialogAction

PropTypeDefaultDescription
alertDialogAlertDialog—The AlertDialog instance this action button belongs to.
childrenChildren—Button content.
classstring—CSS class.
idstring—HTML id.
onClick() => void—Called after closeDialog() runs. Use this to run the destructive action.

Props — AlertDialogCancel

PropTypeDefaultDescription
alertDialogAlertDialog—The AlertDialog instance this cancel button belongs to.
childrenChildren—Button content.
classstring—CSS class.
idstring—HTML id.
onClick() => void—Called after closeDialog() runs.

data-* attributes

AttributeElementWhen present
data-openAlertDialogTriggerDialog is open
data-openAlertDialogContent rootDialog is open

Unlike Dialog, clicking the backdrop closes AlertDialogContent only when closeOnOutsideClick is true (the default). Set it to false if the destructive action must always be confirmed with AlertDialogAction or AlertDialogCancel explicitly.

AlertDialogContent renders with role="alertdialog" and aria-modal="true", and calls trapFocus() and lockScroll() from @morphos/core while open, the same way Dialog does.

Styling example

[data-morphos-backdrop] {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
}

.morphos-alert-dialog-content {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-width: 26rem;
  padding: var(--morphos-space-4);
  border-radius: var(--morphos-radius-lg);
}

.morphos-alert-dialog-action {
  background: var(--morphos-color-danger);
  color: var(--morphos-color-accent-text);
}

.morphos-alert-dialog-cancel {
  background: transparent;
  border-color: var(--morphos-color-border);
}

On this page