Morphos
Overlays

Alert Dialog

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

Interactive example

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 runs its onClick and then calls closeDialog().
AlertDialogCancel<button> that runs its onClick and then calls closeDialog().

Props — AlertDialog

PropTypeDefaultDescription
openbooleanControlled open state. When set, the consumer owns the state.
defaultOpenbooleanfalseInitial open state in uncontrolled mode.
onOpenChange(open: boolean) => voidCalled when the open state changes.
closeOnEscapebooleantrueWhether pressing Escape closes the dialog.
closeOnOutsideClickbooleantrueWhether clicking the backdrop closes the dialog.
childrenChildrenContent — 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
alertDialogAlertDialogThe AlertDialog instance this trigger controls.
childrenChildrenThe trigger element content.
classstringCSS class.
idstringHTML id.

Props — AlertDialogContent

PropTypeDefaultDescription
alertDialogAlertDialogThe AlertDialog instance this content belongs to.
childrenChildrenDialog body content.
classstringCSS class.
idstringHTML id. Falls back to an auto-generated id.
aria-labelstringAccessible label for the dialog.
aria-labelledbystringID of the element that labels the dialog (e.g. an AlertDialogTitle).
aria-describedbystringID 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.
childrenChildrenTitle text.
classstringCSS class.
idstringHTML id.

Props — AlertDialogDescription

PropTypeDefaultDescription
childrenChildrenDescription text.
classstringCSS class.
idstringHTML id.

Props — AlertDialogAction

PropTypeDefaultDescription
alertDialogAlertDialogThe AlertDialog instance this action button belongs to.
childrenChildrenButton content.
classstringCSS class.
idstringHTML id.
onClick() => voidCalled before the dialog closes. Use this to run the destructive action.

Props — AlertDialogCancel

PropTypeDefaultDescription
alertDialogAlertDialogThe AlertDialog instance this cancel button belongs to.
childrenChildrenButton content.
classstringCSS class.
idstringHTML id.
onClick() => voidCalled before the dialog closes.

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