Kobalte.v0.13.6

Alert Dialog

A modal dialog that interrupts the user's workflow to communicate an important message and acquire a response. Examples include action confirmation prompts and error message confirmations. The alertdialog role enables assistive technologies and browsers to distinguish alert dialogs from other dialogs so they have the option of giving alert dialogs special treatment, such as playing a system alert sound.

Import

ts
import { AlertDialog } from "@kobalte/core/alert-dialog";
// or
import { Root, Trigger, ... } from "@kobalte/core/alert-dialog";
// or (deprecated)
import { AlertDialog } from "@kobalte/core";
ts
import { AlertDialog } from "@kobalte/core/alert-dialog";
// or
import { Root, Trigger, ... } from "@kobalte/core/alert-dialog";
// or (deprecated)
import { AlertDialog } from "@kobalte/core";

Features

  • Follow the WAI ARIA Alert Dialog design pattern.
  • Supports modal and non-modal modes.
  • Provides screen reader announcements via rendered title and description.
  • Focus is trapped and scrolling is blocked while it is open.
  • Pressing Esc closes the alert dialog.
  • Can be controlled or uncontrolled.

Anatomy

The alert dialog consists of:

  • AlertDialog: Contains all the parts of a dialog.
  • AlertDialog.Trigger: The button that opens the dialog.
  • AlertDialog.Portal: Portals its children into the body when the dialog is open.
  • AlertDialog.Overlay: The layer that covers the inert portion of the view when the dialog is open.
  • AlertDialog.Content: Contains the content to be rendered when the dialog is open.
  • AlertDialog.CloseButton: The button that closes the dialog.
  • AlertDialog.Title: An accessible title to be announced when the dialog is opened.
  • AlertDialog.Description: An optional accessible description to be announced when the dialog is opened.
tsx
<AlertDialog>
<AlertDialog.Trigger />
<AlertDialog.Portal>
<AlertDialog.Overlay />
<AlertDialog.Content>
<AlertDialog.CloseButton />
<AlertDialog.Title />
<AlertDialog.Description />
</AlertDialog.Content>
</AlertDialog.Portal>
</AlertDialog>
tsx
<AlertDialog>
<AlertDialog.Trigger />
<AlertDialog.Portal>
<AlertDialog.Overlay />
<AlertDialog.Content>
<AlertDialog.CloseButton />
<AlertDialog.Title />
<AlertDialog.Description />
</AlertDialog.Content>
</AlertDialog.Portal>
</AlertDialog>

Example

Usage

Default open

An initial, uncontrolled open value can be provided using the defaultOpen prop.

tsx
<AlertDialog defaultOpen>...</AlertDialog>
tsx
<AlertDialog defaultOpen>...</AlertDialog>

Controlled open

The open prop can be used to make the open state controlled. The onOpenChange event is fired when the user presses the trigger, close button or overlay, and receives the new value.

tsx
import { createSignal } from "solid-js";
function ControlledExample() {
const [open, setOpen] = createSignal(false);
return (
<AlertDialog open={open()} onOpenChange={setOpen}>
...
</AlertDialog>
);
}
tsx
import { createSignal } from "solid-js";
function ControlledExample() {
const [open, setOpen] = createSignal(false);
return (
<AlertDialog open={open()} onOpenChange={setOpen}>
...
</AlertDialog>
);
}

API Reference

AlertDialog

AlertDialog is equivalent to the Root import from @kobalte/core/alert-dialog (and deprecated AlertDialog.Root).

PropDescription
openboolean
The controlled open state of the dialog.
defaultOpenboolean
The default open state when initially rendered. Useful when you do not need to control the open state.
onOpenChange(open: boolean) => void
Event handler called when the open state of the dialog changes.
idstring
A unique identifier for the component. The id is used to generate id attributes for nested components. If no id prop is provided, a generated id will be used.
modalboolean
Whether the dialog should be the only visible content for screen readers, when set to true:
- interaction with outside elements will be disabled.
- scroll will be locked.
- focus will be locked inside the dialog content.
- elements outside the dialog content will not be visible for screen readers.
preventScrollboolean
Whether the scroll should be locked even if the alert dialog is not modal.
forceMountboolean
Used to force mounting the dialog (portal, overlay and content) when more control is needed. Useful when controlling animation with SolidJS animation libraries.

AlertDialog.Trigger

AlertDialog.Trigger consists of Button.

Data attributeDescription
data-expandedPresent when the dialog is open.
data-closedPresent when the dialog is close.

AlertDialog.Content and AlertDialog.Overlay shares the same data-attributes.

AlertDialog.Content

PropDescription
onOpenAutoFocus(event: Event) => void
Event handler called when focus moves into the component after opening. It can be prevented by calling event.preventDefault.
onCloseAutoFocus(event: Event) => void
Event handler called when focus moves to the trigger after closing. It can be prevented by calling event.preventDefault.
onEscapeKeyDown(event: KeyboardEvent) => void
Event handler called when the escape key is down. It can be prevented by calling event.preventDefault.
onPointerDownOutside(event: PointerDownOutsideEvent) => void
Event handler called when a pointer event occurs outside the bounds of the component. It can be prevented by calling event.preventDefault.
onFocusOutside(event: FocusOutsideEvent) => void
Event handler called when the focus moves outside the bounds of the component. It can be prevented by calling event.preventDefault.
onInteractOutside(event: InteractOutsideEvent) => void
Event handler called when an interaction (pointer or focus event) happens outside the bounds of the component. It can be prevented by calling event.preventDefault.

Rendered elements

ComponentDefault rendered element
AlertDialognone
AlertDialog.Triggerbutton
AlertDialog.PortalPortal
AlertDialog.Overlaydiv
AlertDialog.Contentdiv
AlertDialog.CloseButtonbutton
AlertDialog.Titleh2
AlertDialog.Descriptionp

Accessibility

Keyboard Interactions

KeyDescription
SpaceWhen focus is on the trigger, opens/closes the dialog.
EnterWhen focus is on the trigger, opens/closes the dialog.
TabMoves focus to the next focusable element.
Shift + TabMoves focus to the previous focusable element.
EscCloses the dialog and moves focus to the trigger.