Overlays
Alert Dialog A modal dialog for critical confirmations that requires an explicit user action to proceed or cancel.
npm pnpm yarn bun
npm install @morphos/overlays pnpm add @morphos/overlays yarn add @morphos/overlays bun add @morphos/overlays
import {
AlertDialog,
AlertDialogTrigger,
AlertDialogContent,
AlertDialogTitle,
AlertDialogDescription,
AlertDialogAction,
AlertDialogCancel,
} from '@morphos/overlays'
@ 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 >
</>
)
}
}
Component Description 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().
Prop Type Default Description 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.
Method Description openDialog()Opens the dialog. Emits onOpenChange(true). closeDialog()Closes the dialog. Emits onOpenChange(false). toggle()Toggles the open state. Emits onOpenChange.
Prop Type Default Description alertDialogAlertDialog— The AlertDialog instance this trigger controls. childrenChildren— The trigger element content. classstring— CSS class. idstring— HTML id.
Prop Type Default Description 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).
Prop Type Default Description as"h1" | "h2" | "h3" | "h4" | "h5" | "h6""h2"The HTML heading element to render. childrenChildren— Title text. classstring— CSS class. idstring— HTML id.
Prop Type Default Description childrenChildren— Description text. classstring— CSS class. idstring— HTML id.
Prop Type Default Description alertDialogAlertDialog— The AlertDialog instance this action button belongs to. childrenChildren— Button content. classstring— CSS class. idstring— HTML id. onClick() => void— Called before the dialog closes. Use this to run the destructive action.
Prop Type Default Description alertDialogAlertDialog— The AlertDialog instance this cancel button belongs to. childrenChildren— Button content. classstring— CSS class. idstring— HTML id. onClick() => void— Called before the dialog closes.
Attribute Element When 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.
[ 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 : 26 rem ;
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 );
}