Native Solid 2 fork. Original documentation and designs © WorkOS; historical posts retain their original context.

Alert Dialog

A modal dialog that interrupts the user with important content and expects a response.

Download runnable sandbox
import { AlertDialog } from "@wsagency/radix2solid-primitives";
import styles from "./styles.module.css";
const AlertDialogDemo = () => <AlertDialog.Root>
<AlertDialog.Trigger asChild>{target => target("button", {
"class": `${styles.Button} violet`,
children: <>Delete account</>
})}</AlertDialog.Trigger>
<AlertDialog.Portal>
<AlertDialog.Overlay class={styles.Overlay} />
<AlertDialog.Content class={styles.Content}>
<AlertDialog.Title class={styles.Title}>
Are you absolutely sure?
</AlertDialog.Title>
<AlertDialog.Description class={styles.Description}>
This action cannot be undone. This will permanently delete your
account and remove your data from our servers.
</AlertDialog.Description>
<div style={{ "display": "flex", "gap": "25px", "justify-content": "flex-end" }}>
<AlertDialog.Cancel asChild>{target => target("button", {
"class": `${styles.Button} muave`,
children: <>Cancel</>
})}</AlertDialog.Cancel>
<AlertDialog.Action asChild>{target => target("button", {
"class": `${styles.Button} red`,
children: <>
Yes, delete account
</>
})}</AlertDialog.Action>
</div>
</AlertDialog.Content>
</AlertDialog.Portal>
</AlertDialog.Root>;
export default AlertDialogDemo;

Features

  • Focus is automatically trapped.

  • Can be controlled or uncontrolled.

  • Manages screen reader announcements with Title and Description components.

  • Esc closes the component automatically.

Anatomy

Import all parts and piece them together.

import { AlertDialog } from "@wsagency/radix2solid-primitives";
export default () => <AlertDialog.Root>
<AlertDialog.Trigger />
<AlertDialog.Portal>
<AlertDialog.Overlay />
<AlertDialog.Content>
<AlertDialog.Title />
<AlertDialog.Description />
<AlertDialog.Cancel />
<AlertDialog.Action />
</AlertDialog.Content>
</AlertDialog.Portal>
</AlertDialog.Root>;

API Reference

Root

Contains all the parts of an alert dialog.

PropTypeDefault
defaultOpenboolean—
openboolean—
onOpenChangefunction—

Trigger

A button that opens the dialog.

PropTypeDefault
asChildbooleanfalse
Data attributeValues
[data-state]"open" | "closed"

Portal

When used, portals your overlay and content parts into the body.

PropTypeDefault
forceMountboolean—
containerHTMLElementdocument.body

Overlay

A layer that covers the inert portion of the view when the dialog is open.

PropTypeDefault
asChildbooleanfalse
forceMountboolean—
Data attributeValues
[data-state]"open" | "closed"

Content

Contains content to be rendered when the dialog is open.

AlertDialog.Content sets aria-labelledby to AlertDialog.Title and aria-describedby to AlertDialog.Description using IDs that AlertDialog.Root allocates up front. Passing either attribute to Content replaces that default, and passing it as undefined removes it. A Title or Description with its own id must be linked from Content with the same ID.

Breaking in 0.1.0-rc.2: Content no longer collects part IDs or appends them to the relationships you pass. Add Content ARIA for custom part IDs and list the title ID yourself when combining it with an external label.

PropTypeDefault
asChildbooleanfalse
forceMountboolean—
onOpenAutoFocusfunction—
onCloseAutoFocusfunction—
onEscapeKeyDownfunction—
Data attributeValues
[data-state]"open" | "closed"

Cancel

A button that closes the dialog. This button should be distinguished visually from AlertDialog.Action buttons.

PropTypeDefault
asChildbooleanfalse

Action

A button that closes the dialog. These buttons should be distinguished visually from the AlertDialog.Cancel button.

PropTypeDefault
asChildbooleanfalse

Title

An accessible name to be announced when the dialog is opened. Alternatively, you can exclude this component and name AlertDialog.Content with a nonblank aria-label or with aria-labelledby. An undefined or empty aria-label keeps the Title link. A custom id replaces the Root ID, so link it with aria-labelledby on AlertDialog.Content.

PropTypeDefault
asChildbooleanfalse

Description

An accessible description to be announced when the dialog is opened. Alternatively, you can exclude this component and give AlertDialog.Content your own aria-describedby, or aria-describedby={undefined} for no description. A custom id replaces the Root ID, so link it with aria-describedby on AlertDialog.Content.

PropTypeDefault
asChildbooleanfalse

Examples

Close after asynchronous form submission

Use the controlled props to programmatically close the Alert Dialog after an async operation has completed.

import { createSignal } from "solid-js";
import { AlertDialog } from "@wsagency/radix2solid-primitives";
const wait = () => new Promise(resolve => setTimeout(resolve, 1000));
export default () => {
const [open, setOpen] = createSignal(false);
return <AlertDialog.Root open={open()} onOpenChange={setOpen}>
<AlertDialog.Trigger>Open</AlertDialog.Trigger>
<AlertDialog.Portal>
<AlertDialog.Overlay />
<AlertDialog.Content aria-label="Submit form" aria-describedby={undefined}>
<form onSubmit={event => { wait().then(() => setOpen(false)); event.preventDefault(); }}>
{/** some inputs */}
<button type="submit">Submit</button>
</form>
</AlertDialog.Content>
</AlertDialog.Portal>
</AlertDialog.Root>;
};

Custom portal container

Customise the element that your alert dialog portals into.

import { createSignal } from "solid-js";
export default () => {
const [container, setContainer] = createSignal(null);
return <div>
<AlertDialog.Root>
<AlertDialog.Trigger />
<AlertDialog.Portal container={container()}>
<AlertDialog.Overlay />
<AlertDialog.Content>...</AlertDialog.Content>
</AlertDialog.Portal>
</AlertDialog.Root>
<div ref={setContainer} />
</div>;
};

Accessibility

Adheres to the Alert and Message Dialogs WAI-ARIA design pattern.

Keyboard Interactions

KeyDescription
Space
Opens/closes the dialog.
Enter
Opens/closes the dialog.
Tab
Moves focus to the next focusable element.
Shift + Tab
Moves focus to the previous focusable element.
Esc
Closes the dialog and moves focus to AlertDialog.Trigger.