This documentation is under construction. Pages can be missing or change at any time. Follow it on GitHub.

Skip to content

Dialog

A popup that opens on top of the entire page.

Examples#

State#

By default, Dialog is an uncontrolled component: the browser keeps its state. The defaultOpen prop is the state that the server renders: a render with a new value opens or closes the dialog, and the controller keeps the value of the browser in data-phem-dialog-open-value.

A <form method="dialog"> closes the dialog when it submits, and the state follows. Here, the form closes the dialog once the data is valid.

Open from a menu#

In order to open a dialog using a menu, make the menu item a detached trigger of the dialog: give it data-phem-dialog-for with the handle of the dialog. The click closes the menu and opens the dialog.

Nested dialogs#

You can nest dialogs within one another normally.

Use the [data-nested-dialog-open] selector and the var(--nested-dialogs) CSS variable to customize the styling of the parent dialog. The ::backdrop of a child dialog ([data-nested]) is transparent here, so that you can present the parent dialog in a clean way behind the one on top of it.

Close confirmation#

This example shows a nested confirmation dialog that opens if the text entered in the parent dialog is going to be discarded.

To implement this, give the parent dialog the confirmClose prop with the handle of the confirmation dialog. When the user changed a field, a request to close opens the confirmation in place: when the user clicks the backdrop, presses the Esc key, or clicks a close button. The DialogDiscard button closes the two dialogs and resets the fields.

Custom focus management#

You can control where the focus goes when the dialog opens and closes. The autofocus attribute gives the first focus to a field of the dialog, and the finalFocus prop of Dialog is a CSS selector of the element that gets the focus after the close. Without finalFocus, the focus goes back to the trigger. Here, it goes to the "Final focus" button, which has no click action.

Outside scroll#

The dialog can be made scrollable by using DialogViewport as an outer scrollable container for DialogPopup while the popup can extend past the bottom edge.

Inside scroll#

The dialog can be made scrollable by making an inner container scrollable while the popup stays fully on screen. DialogViewport is used as a positioning container for DialogPopup.

Placing elements outside the popup#

When adding elements that should appear "outside" the colored popup area, continue to place them inside DialogPopup, but create a child element that has the popup styles. This ensures they are kept in the tab order and announced correctly by screen readers.

DialogPopup has pointer-events: none, while inner content (the colored popup and close button) has pointer-events: auto so clicks on the backdrop continue to be registered.

Detached triggers#

A dialog can be controlled by a trigger located either inside or outside the Dialog component. For simple, one-off interactions, place the DialogTrigger inside Dialog, as shown in the example at the top of this page.

However, if defining the dialog's content next to its trigger is not practical, you can use a detached trigger. This involves placing the DialogTrigger outside of Dialog and linking them with the same handle. The root gets the handle as its id, and the trigger gets data-phem-dialog-for.

Multiple triggers#

A single dialog can be opened by multiple trigger elements. You can achieve this by using the same handle for several detached triggers, or by placing multiple DialogTrigger components inside a single Dialog.

The dialog can render different content depending on which trigger opened it. Give each trigger a payload: the DialogPayload part shows the payload of the trigger that opened the dialog.

Controlled mode#

You can control the dialog's open state with the defaultOpen and triggerId props of Dialog: a render with new values opens the dialog for the trigger with that id. The controller keeps the values of the browser in data-phem-dialog-open-value and data-phem-dialog-trigger-id-value.

Here, the button is in the root, and opens the dialog without a request with the phem-dialog#open action and a triggerId param.

API reference#

Import the components of the family:

use Phem\Ui\Dialog;
use Phem\Ui\DialogViewport;
use function Phem\Ui\{DialogTrigger, DialogPopup, DialogTitle, DialogDescription, DialogClose, DialogPayload, DialogDiscard};

Dialog#

A modal dialog: Base UI Dialog.Root, on the phem-dialog controller.

The popup is a native <dialog> in the top layer, so it needs no Portal. Its ::backdrop is the Backdrop of Base UI: the DialogPopup classes style it with the backdrop: variant.

A handle links the root to detached triggers: a DialogTrigger with the same handle, anywhere in the page, opens this dialog. The DialogPayload shows the payload of the trigger that opened it, and triggerId is the id of this trigger. confirmClose is the handle of an AlertDialog in the popup: a request to close opens it when the user changed a field. Its DialogDiscard closes the two dialogs. finalFocus is a CSS selector of the element that gets the focus after the close. For the first focus, give a field the autofocus attribute.

PropTypeDefault
handle?stringnull
defaultOpenboolfalse
triggerId?stringnull
dismissiblebooltrue
disablePointerDismissalboolfalse
modalbooltrue
confirmClose?stringnull
finalFocus?stringnull
children?Htmlnull
...attrsarray[]

DialogPopup#

The box of the dialog. In a DialogViewport, it is a div with the same border and background, and the classes of the caller place it.

PropTypeDefault
children?Htmlnull
...restmixed—

DialogViewport#

The container that places the popup, as the Viewport of Base UI: a <dialog> that fills the screen. A tap on it outside the popup closes the dialog. Its ::backdrop is the Backdrop. With z-50, a dialog that is not modal (not in the top layer) is above the page.

PropTypeDefault
children?Htmlnull
...attrsarray[]

DialogTrigger#

The button that opens the dialog. With a handle, it is a detached trigger of the Dialog with the same handle: it can be anywhere in the page. The payload is the text of the DialogPayload while this trigger opened the dialog.

PropTypeDefault
handle?stringnull
payload?stringnull
disabledboolfalse
children?Htmlnull
...restmixed—

DialogTitle#

The heading of the dialog. It names the dialog (aria-labelledby).

PropTypeDefault
children?Htmlnull
...restmixed—

DialogDescription#

The text that describes the dialog (aria-describedby).

PropTypeDefault
children?Htmlnull
...restmixed—

DialogClose#

A button that closes the dialog.

PropTypeDefault
disabledboolfalse
children?Htmlnull
...restmixed—

DialogPayload#

The text of the payload of the trigger that opened the dialog, or its children.

PropTypeDefault
children?Htmlnull
...restmixed—

DialogDiscard#

A button of the AlertDialog of the confirmClose of a Dialog: it closes the two dialogs, and the fields of the Dialog get their first values.

PropTypeDefault
children?Htmlnull
...restmixed—

The source of the examples: examples/ui/Dialog.phem.

Phem
Esc
↑↓ to navigate↵ to openEsc to close