Alert Dialog
A dialog that requires a user response to proceed.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{AlertDialog, AlertDialogClose, AlertDialogDescription, AlertDialogPopup, AlertDialogTitle, AlertDialogTrigger};
#[Component]
function AlertDialogHeroDemo(): Node
{
return (
<AlertDialog>
<AlertDialogTrigger className="text-red-700 dark:text-red-400">Discard draft</AlertDialogTrigger>
<AlertDialogPopup>
<div className="flex flex-col gap-1">
<AlertDialogTitle>Discard draft?</AlertDialogTitle>
<AlertDialogDescription>You can’t undo this action.</AlertDialogDescription>
</div>
<div className="flex justify-end gap-3">
<AlertDialogClose>Cancel</AlertDialogClose>
<AlertDialogClose className="text-red-700 dark:text-red-400">Discard</AlertDialogClose>
</div>
</AlertDialogPopup>
</AlertDialog>
);
}
Examples#
#
In order to open an alert dialog using a menu, make the menu item a detached trigger of the alert dialog: give it data-phem-dialog-for with the handle of the alert dialog. The click closes the menu and opens the alert dialog.
Detached triggers#
An alert dialog can be controlled by a trigger located either inside or outside the AlertDialog component. For simple, one-off interactions, place the AlertDialogTrigger inside AlertDialog, as shown in the example at the top of this page.
However, if defining the alert dialog's content next to its trigger is not practical, you can use a detached trigger. This involves placing the AlertDialogTrigger outside of AlertDialog and linking them with the same handle. The root gets the handle as its id, and the trigger gets data-phem-dialog-for.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{AlertDialog, AlertDialogClose, AlertDialogDescription, AlertDialogPopup, AlertDialogTitle, AlertDialogTrigger};
#[Component]
function AlertDialogDetachedDemo(): Node
{
return (
<div>
<AlertDialogTrigger handle="discard-draft" className="text-red-700 dark:text-red-400">Discard draft</AlertDialogTrigger>
<AlertDialog handle="discard-draft">
<AlertDialogPopup>
<div className="flex flex-col gap-1">
<AlertDialogTitle>Discard draft?</AlertDialogTitle>
<AlertDialogDescription>This action cannot be undone.</AlertDialogDescription>
</div>
<div className="flex justify-end gap-3">
<AlertDialogClose>Cancel</AlertDialogClose>
<AlertDialogClose className="text-red-700 dark:text-red-400">Discard</AlertDialogClose>
</div>
</AlertDialogPopup>
</AlertDialog>
</div>
);
}
Multiple triggers#
A single alert 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 AlertDialogTrigger components inside a single AlertDialog.
The alert dialog can render different content depending on which trigger opened it. Give each trigger a payload: the AlertDialogPayload part shows the payload of the trigger that opened the alert dialog.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{AlertDialog, AlertDialogClose, AlertDialogDescription, AlertDialogPayload, AlertDialogPopup, AlertDialogTitle, AlertDialogTrigger};
#[Component]
function AlertDialogMultipleDemo(): Node
{
return (
<div className="flex gap-2">
<AlertDialogTrigger handle="multiple-alert-dialog" payload="Trigger 1">Trigger 1</AlertDialogTrigger>
<AlertDialogTrigger handle="multiple-alert-dialog" payload="Trigger 2">Trigger 2</AlertDialogTrigger>
<AlertDialog handle="multiple-alert-dialog">
<AlertDialogPopup>
<div className="flex flex-col gap-1">
<AlertDialogTitle>Alert dialog</AlertDialogTitle>
<AlertDialogDescription>Confirming <AlertDialogPayload /></AlertDialogDescription>
</div>
<div className="flex justify-end gap-3">
<AlertDialogClose>Close</AlertDialogClose>
</div>
</AlertDialogPopup>
</AlertDialog>
</div>
);
}
Controlled mode#
You can control the alert dialog's open state with the defaultOpen and triggerId props of AlertDialog: a render with new values opens the alert 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 alert dialog without a request with the phem-dialog#open action and a triggerId param. With no payload, AlertDialogPayload shows its children.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{AlertDialog, AlertDialogClose, AlertDialogDescription, AlertDialogPayload, AlertDialogPopup, AlertDialogTitle, AlertDialogTrigger};
#[Component]
function AlertDialogControlledDemo(): Node
{
return (
<AlertDialog>
<div className="flex flex-wrap justify-center gap-2">
<AlertDialogTrigger id="alert-trigger-1" payload="Discard draft?" className="text-red-700 dark:text-red-400">Discard</AlertDialogTrigger>
<AlertDialogTrigger id="alert-trigger-2" payload="Delete project?" className="text-red-700 dark:text-red-400">Delete</AlertDialogTrigger>
<AlertDialogTrigger id="alert-trigger-3" payload="Sign out?">Sign out</AlertDialogTrigger>
<button type="button" data-action="phem-dialog#open" data-phem-dialog-trigger-id-param="alert-trigger-2" className="flex h-control items-center justify-center border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 px-3 text-sm text-neutral-950 dark:text-white hover:bg-neutral-100 dark:hover:bg-neutral-800 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white">Open programmatically</button>
</div>
<AlertDialogPopup>
<div className="flex flex-col gap-1">
<AlertDialogTitle>
<AlertDialogPayload>Are you sure?</AlertDialogPayload>
</AlertDialogTitle>
<AlertDialogDescription>This action cannot be undone.</AlertDialogDescription>
</div>
<div className="flex justify-end gap-3">
<AlertDialogClose>Cancel</AlertDialogClose>
<AlertDialogClose className="text-red-700 dark:text-red-400">Confirm</AlertDialogClose>
</div>
</AlertDialogPopup>
</AlertDialog>
);
}
API reference#
Import the components of the family:
use function Phem\Ui\{AlertDialog, AlertDialogTrigger, AlertDialogPopup, AlertDialogTitle, AlertDialogDescription, AlertDialogClose, AlertDialogViewport, AlertDialogPayload};AlertDialog#
A dialog that asks the user to confirm an action: Base UI AlertDialog.Root, on the phem-dialog controller. The popup has role="alertdialog". Escape and a close button close it, and a tap on the backdrop does not.
The other props are the props of Dialog: handle links detached triggers (an AlertDialogTrigger with the same handle), and triggerId is the id of the trigger that opened it.
| Prop | Type | Default |
|---|---|---|
defaultOpen | bool | false |
children | ?Html | null |
...rest | mixed | — |
AlertDialogTrigger#
The button that opens the alert dialog. With a handle, it is a detached trigger: it can be anywhere in the page.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
AlertDialogPopup#
The box of the alert dialog: a DialogPopup with the alertdialog role. A press outside does not close it.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
AlertDialogTitle#
The heading of the alert dialog. It names the alert dialog (aria-labelledby).
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
AlertDialogDescription#
The text that describes the alert dialog (aria-describedby).
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
AlertDialogClose#
A button that closes the alert dialog, for example "Cancel" or the button of the action.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
AlertDialogViewport#
The container that places the popup, as the Viewport of Base UI (see DialogViewport).
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
AlertDialogPayload#
The text of the payload of the trigger that opened the alert dialog, or its children.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
The source of the examples: examples/ui/AlertDialog.phem.