Dialog
A popup that opens on top of the entire page.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Dialog;
use function Phem\Ui\{DialogClose, DialogDescription, DialogPopup, DialogTitle, DialogTrigger};
#[Component]
function DialogHeroDemo(): Node
{
return (
<Dialog>
<DialogTrigger>View notifications</DialogTrigger>
<DialogPopup>
<div className="flex flex-col gap-1">
<DialogTitle>Notifications</DialogTitle>
<DialogDescription>You are all caught up. Good job!</DialogDescription>
</div>
<div className="flex justify-end gap-3">
<DialogClose>Close</DialogClose>
</div>
</DialogPopup>
</Dialog>
);
}
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.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Dialog;
use function Phem\Ui\{DialogClose, DialogPopup, DialogTitle, DialogTrigger};
#[Component]
function DialogStateDemo(): Node
{
return (
<Dialog>
<DialogTrigger>Open</DialogTrigger>
<DialogPopup>
<DialogTitle>Example dialog</DialogTitle>
<form method="dialog" className="flex flex-col gap-4">
<input required aria-label="Name" placeholder="Enter your name" className="w-full border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 dark:text-white placeholder:text-neutral-500 dark:placeholder:text-neutral-400 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white h-control px-2" />
<div className="flex justify-end gap-3">
<DialogClose>Close</DialogClose>
<button type="submit" 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">Save</button>
</div>
</form>
</DialogPopup>
</Dialog>
);
}
#
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.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Dialog;
use function Phem\Ui\{DialogClose, DialogDescription, DialogPopup, DialogTitle, DialogTrigger};
#[Component]
function DialogNestedDemo(): Node
{
return (
<Dialog>
<DialogTrigger>View notifications</DialogTrigger>
<DialogPopup className="top-[calc(50%+1.25rem*var(--nested-dialogs,0))] scale-[calc(1-0.1*var(--nested-dialogs,0))] transition-[top,scale,opacity] after:absolute after:inset-0 after:bg-black/5 after:opacity-0 after:transition-opacity after:duration-100 after:ease-out after:pointer-events-none data-ending-style:top-[calc(50%+0.25rem+1.25rem*var(--nested-dialogs,0))] data-ending-style:scale-[0.96] data-nested-dialog-open:after:opacity-100 data-starting-style:top-[calc(50%+0.25rem+1.25rem*var(--nested-dialogs,0))] data-starting-style:scale-[0.96] data-nested:backdrop:bg-transparent">
<div className="flex flex-col gap-1">
<DialogTitle>Notifications</DialogTitle>
<DialogDescription>You are all caught up. Good job!</DialogDescription>
</div>
<div className="flex items-center gap-3">
<Dialog>
<DialogTrigger>Customize</DialogTrigger>
<DialogPopup className="top-[calc(50%+1.25rem*var(--nested-dialogs,0))] scale-[calc(1-0.1*var(--nested-dialogs,0))] transition-[top,scale,opacity] after:absolute after:inset-0 after:bg-black/5 after:opacity-0 after:transition-opacity after:duration-100 after:ease-out after:pointer-events-none data-ending-style:top-[calc(50%+0.25rem+1.25rem*var(--nested-dialogs,0))] data-ending-style:scale-[0.96] data-nested-dialog-open:after:opacity-100 data-starting-style:top-[calc(50%+0.25rem+1.25rem*var(--nested-dialogs,0))] data-starting-style:scale-[0.96] data-nested:backdrop:bg-transparent">
<div className="flex flex-col gap-1">
<DialogTitle>Customize notifications</DialogTitle>
<DialogDescription>Review your settings here.</DialogDescription>
</div>
<div className="flex items-center justify-end gap-3">
<DialogClose>Close</DialogClose>
</div>
</DialogPopup>
</Dialog>
</div>
</DialogPopup>
</Dialog>
);
}
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.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Dialog;
use function Phem\Ui\{AlertDialog, AlertDialogClose, AlertDialogDescription, AlertDialogPopup, AlertDialogTitle, DialogClose, DialogDiscard, DialogPopup, DialogTitle, DialogTrigger};
#[Component]
function DialogCloseConfirmationDemo(): Node
{
return (
<Dialog confirmClose="discard-tweet">
<DialogTrigger>Tweet</DialogTrigger>
<DialogPopup className="top-[calc(50%+1.25rem*var(--nested-dialogs,0))] scale-[calc(1-0.1*var(--nested-dialogs,0))] transition-[top,scale,opacity] after:absolute after:inset-0 after:bg-black/5 after:opacity-0 after:transition-opacity after:duration-100 after:ease-out after:pointer-events-none data-ending-style:top-[calc(50%+0.25rem+1.25rem*var(--nested-dialogs,0))] data-ending-style:scale-[0.96] data-nested-dialog-open:after:opacity-100 data-starting-style:top-[calc(50%+0.25rem+1.25rem*var(--nested-dialogs,0))] data-starting-style:scale-[0.96] data-nested:backdrop:bg-transparent gap-1">
<DialogTitle id="new-tweet-title">New tweet</DialogTitle>
<form method="dialog" className="flex flex-col gap-4">
<textarea aria-labelledby="new-tweet-title" required placeholder="What’s on your mind?" className="w-full border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 dark:text-white placeholder:text-neutral-500 dark:placeholder:text-neutral-400 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white min-h-32 p-2"></textarea>
<div className="flex justify-end gap-3">
<DialogClose>Cancel</DialogClose>
<button type="submit" 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">Tweet</button>
</div>
</form>
<AlertDialog handle="discard-tweet">
<AlertDialogPopup className="top-[calc(50%+1.25rem*var(--nested-dialogs,0))] scale-[calc(1-0.1*var(--nested-dialogs,0))] transition-[top,scale,opacity] after:absolute after:inset-0 after:bg-black/5 after:opacity-0 after:transition-opacity after:duration-100 after:ease-out after:pointer-events-none data-ending-style:top-[calc(50%+0.25rem+1.25rem*var(--nested-dialogs,0))] data-ending-style:scale-[0.96] data-nested-dialog-open:after:opacity-100 data-starting-style:top-[calc(50%+0.25rem+1.25rem*var(--nested-dialogs,0))] data-starting-style:scale-[0.96] data-nested:backdrop:bg-transparent">
<div className="flex flex-col gap-1">
<AlertDialogTitle>Discard tweet?</AlertDialogTitle>
<AlertDialogDescription>Your tweet will be lost.</AlertDialogDescription>
</div>
<div className="flex justify-end gap-3">
<AlertDialogClose>Go back</AlertDialogClose>
<DialogDiscard>Discard</DialogDiscard>
</div>
</AlertDialogPopup>
</AlertDialog>
</DialogPopup>
</Dialog>
);
}
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.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Dialog, Field};
use function Phem\Ui\{DialogClose, DialogDescription, DialogPopup, DialogTitle, DialogTrigger, FieldControl, FieldLabel, Fieldset};
#[Component]
function DialogFocusDemo(): Node
{
return (
<div className="flex flex-wrap justify-center gap-3">
<Dialog finalFocus="#dialog-final-focus">
<DialogTrigger>Open feedback</DialogTrigger>
<DialogPopup>
<div className="flex flex-col gap-1">
<DialogTitle>Feedback form</DialogTitle>
<DialogDescription>Your feedback means a lot to us.</DialogDescription>
</div>
<Fieldset className="gap-3 border-0 p-0 m-0">
<Field>
<FieldLabel className="font-normal">Full name</FieldLabel>
<FieldControl placeholder="Enter your name" className="w-full" />
</Field>
<Field>
<FieldLabel className="font-normal">Feedback</FieldLabel>
<FieldControl autofocus required placeholder="Enter your feedback" className="w-full" />
</Field>
</Fieldset>
<div className="flex justify-end gap-3">
<DialogClose>Close</DialogClose>
</div>
</DialogPopup>
</Dialog>
<button id="dialog-final-focus" type="button" 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">Final focus</button>
</div>
);
}
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.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Dialog, DialogViewport};
use function Phem\Ui\{DialogClose, DialogDescription, DialogPopup, DialogTitle, DialogTrigger};
const DIALOG_SECTIONS = [
[
'What a dialog is for',
'Use a dialog when you need the user to complete a focused task or read something important without navigating away. It opens on top of the page and returns focus back where it started when closed.',
],
[
'Anatomy at a glance',
'Root, Trigger, Portal, Backdrop, Viewport, Popup, Title, Description, Close. Keep the title short and the first paragraph specific so screen readers announce something meaningful.',
],
[
'Opening and closing',
'Control it using external state via the `open` and `onOpenChange` props, or let it manage state for you internally.',
],
[
'Keyboard and focus behavior',
'Focus moves inside the dialog when it opens. Tab and Shift+Tab loop within, and Esc requests close.',
],
[
'Accessible labeling',
'Set an explicit title and description using the `Dialog.Title` and `Dialog.Description` components.',
],
[
'Backdrop and page scrolling',
'The backdrop visually separates layers while background content is inert. Don’t rely on dimness alone—keep copy clear and buttons obvious so actions are easy to choose.',
],
[
'Portals and stacking',
'Dialogs render in a portal so they sit above the `isolation: isolate` app content and avoid local z-index wars.',
],
[
'Viewport overflow',
'Let long content overflow the bottom edge and reveal as you scroll the page container. Keep generous padding at the top and bottom so the dialog doesn’t feel jammed against the edges.',
],
[
'Nested dialogs and confirmations',
'If closing a dialog needs confirmation, open a child alert dialog rather than mutating the current one. The parent stays visible behind it; only the topmost layer should feel interactive.',
],
[
'Transitions that respect motion settings',
'Use small, fast transitions (opacity plus a few pixels of Y translation or scale). Subtle motion helps people notice what changed without slowing them down.',
],
[
'Controlled vs. uncontrolled',
'Controlled state is best when other parts of the page need to react to open/close. Uncontrolled is fine for local cases where only the dialog matters.',
],
[
'Close affordances',
'Always offer a visible close button in the corner. Don’t rely only on Esc or the backdrop for pointer outside presses. Touch screen readers and accessibility users benefit from a clear, targetable control to click to close the dialog.',
],
[
'Forms inside dialogs',
'Keep forms short; longer flows usually deserve a full page. Validate inline, keep button text specific (“Create project”), and disable destructive actions until the input is valid.',
],
[
'Content guidelines',
'Lead with the outcome (“Rename project?”) and follow with one or two short, concrete sentences. Avoid long prose; link out for details instead.',
],
[
'SSR and hydration notes',
'Because dialogs render in a portal, make sure your portal container exists on the client.',
],
[
'Mobile ergonomics',
'Use larger touch targets and keep the close button reachable with the thumb. Avoid full-screen modals unless the task truly needs a whole screen.',
],
[
'Theming and density',
'Match spacing and corner radius to your system. Use a slightly denser layout than pages so the dialog feels purpose-built, not like a mini web page.',
],
[
'Internationalization',
'Plan for longer text. Buttons can grow to two lines; titles should wrap gracefully. Keep destructive terms consistent across locales.',
],
[
'Performance',
'The content of the dialog is in the first render of the page. If it is slow to render, put it in a `<Suspense>`: the page sends a fallback first and the content after, so the page does not wait for it. With `lazy`, the browser loads the content when the fallback comes into view.',
],
[
'When a popover is better',
'If the content is a small hint or a few quick actions anchored to a control, use a popover or menu instead of a dialog. Dialogs interrupt on purpose—use that sparingly.',
],
[
'Follow-up and cleanup',
'After a successful action, close the dialog and show confirmation in context (toast, inline message, or updated UI) so people can see the result of what they just did.',
],
];
#[Component]
function DialogOutsideScrollDemo(): Node
{
$sections = DIALOG_SECTIONS;
return (
<Dialog>
<DialogTrigger>Open dialog</DialogTrigger>
<DialogViewport className="overflow-y-auto overscroll-contain backdrop:duration-[600ms] backdrop:ease-[cubic-bezier(0.45,1.005,0,1.005)] data-ending-style:backdrop:duration-[350ms] data-ending-style:backdrop:ease-[cubic-bezier(0.375,0.015,0.545,0.455)]">
<div className="flex min-h-full items-center overflow-clip">
<DialogPopup tabindex="-1" autofocus className="relative mx-auto my-16 flex w-[min(40rem,calc(100vw-2rem))] flex-col gap-4 p-4 transition-[translate] duration-[700ms] ease-[cubic-bezier(0.45,1.005,0,1.005)] data-starting-style:translate-y-[100dvh] data-ending-style:translate-y-[max(100dvh,100%)] data-ending-style:duration-[350ms] data-ending-style:ease-[cubic-bezier(0.375,0.015,0.545,0.455)] motion-reduce:transition-none">
<div className="relative flex flex-col gap-1 pr-8">
<DialogTitle>Dialog</DialogTitle>
<DialogDescription>This layout keeps an outer container scrollable while the dialog can extend past the bottom edge.</DialogDescription>
<DialogClose aria-label="Close" className="absolute -top-1 -right-1 inline-flex w-control border-none bg-transparent p-0 hover:bg-neutral-100 dark:hover:bg-neutral-800 active:bg-neutral-200 dark:active:bg-neutral-700">
<DialogXIcon />
</DialogClose>
</div>
<DialogSections sections={{ sections }} />
<p className="text-sm text-neutral-600 dark:text-neutral-400">Related docs: <a className="text-neutral-950 dark:text-white underline underline-offset-[0.16em] decoration-[1px] hover:no-underline focus-visible:outline-2 focus-visible:outline-neutral-950 dark:focus-visible:outline-white focus-visible:outline-offset-2" href="/components/scroll-area">Scroll Area</a>, <a className="text-neutral-950 dark:text-white underline underline-offset-[0.16em] decoration-[1px] hover:no-underline focus-visible:outline-2 focus-visible:outline-neutral-950 dark:focus-visible:outline-white focus-visible:outline-offset-2" href="/components/drawer">Drawer</a>, <a className="text-neutral-950 dark:text-white underline underline-offset-[0.16em] decoration-[1px] hover:no-underline focus-visible:outline-2 focus-visible:outline-neutral-950 dark:focus-visible:outline-white focus-visible:outline-offset-2" href="/components/popover">Popover</a>.</p>
</DialogPopup>
</div>
</DialogViewport>
</Dialog>
);
}
#[Component]
function DialogXIcon(): Node
{
return (
<svg aria-hidden="true" width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-linecap="square" stroke-linejoin="round" className="block">
<path d="m2.5 2.5 11 11m-11 0 11-11" />
</svg>
);
}
#[Component]
function DialogSections(
array $sections,
string $className = 'flex flex-col gap-4',
string $itemClass = 'flex flex-col gap-1',
): Node
{
return (
<div className={{ className }}>
{% for section in sections %}
<section key={{ loop.index }} className={{ itemClass }}>
<h3 className="text-sm font-bold">{{ section[0] }}</h3>
<p className="text-sm text-neutral-700 dark:text-neutral-300">{{ section[1] }}</p>
</section>
{% endfor %}
</div>
);
}
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.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Dialog, DialogViewport};
use function Phem\Ui\{DialogClose, DialogDescription, DialogPopup, DialogTitle, DialogTrigger};
const DIALOG_SECTIONS = [
[
'What a dialog is for',
'Use a dialog when you need the user to complete a focused task or read something important without navigating away. It opens on top of the page and returns focus back where it started when closed.',
],
[
'Anatomy at a glance',
'Root, Trigger, Portal, Backdrop, Viewport, Popup, Title, Description, Close. Keep the title short and the first paragraph specific so screen readers announce something meaningful.',
],
[
'Opening and closing',
'Control it using external state via the `open` and `onOpenChange` props, or let it manage state for you internally.',
],
[
'Keyboard and focus behavior',
'Focus moves inside the dialog when it opens. Tab and Shift+Tab loop within, and Esc requests close.',
],
[
'Accessible labeling',
'Set an explicit title and description using the `Dialog.Title` and `Dialog.Description` components.',
],
[
'Backdrop and page scrolling',
'The backdrop visually separates layers while background content is inert. Don’t rely on dimness alone—keep copy clear and buttons obvious so actions are easy to choose.',
],
[
'Portals and stacking',
'Dialogs render in a portal so they sit above the `isolation: isolate` app content and avoid local z-index wars.',
],
[
'Viewport overflow',
'Let long content overflow the bottom edge and reveal as you scroll the page container. Keep generous padding at the top and bottom so the dialog doesn’t feel jammed against the edges.',
],
[
'Nested dialogs and confirmations',
'If closing a dialog needs confirmation, open a child alert dialog rather than mutating the current one. The parent stays visible behind it; only the topmost layer should feel interactive.',
],
[
'Transitions that respect motion settings',
'Use small, fast transitions (opacity plus a few pixels of Y translation or scale). Subtle motion helps people notice what changed without slowing them down.',
],
[
'Controlled vs. uncontrolled',
'Controlled state is best when other parts of the page need to react to open/close. Uncontrolled is fine for local cases where only the dialog matters.',
],
[
'Close affordances',
'Always offer a visible close button in the corner. Don’t rely only on Esc or the backdrop for pointer outside presses. Touch screen readers and accessibility users benefit from a clear, targetable control to click to close the dialog.',
],
[
'Forms inside dialogs',
'Keep forms short; longer flows usually deserve a full page. Validate inline, keep button text specific (“Create project”), and disable destructive actions until the input is valid.',
],
[
'Content guidelines',
'Lead with the outcome (“Rename project?”) and follow with one or two short, concrete sentences. Avoid long prose; link out for details instead.',
],
[
'SSR and hydration notes',
'Because dialogs render in a portal, make sure your portal container exists on the client.',
],
[
'Mobile ergonomics',
'Use larger touch targets and keep the close button reachable with the thumb. Avoid full-screen modals unless the task truly needs a whole screen.',
],
[
'Theming and density',
'Match spacing and corner radius to your system. Use a slightly denser layout than pages so the dialog feels purpose-built, not like a mini web page.',
],
[
'Internationalization',
'Plan for longer text. Buttons can grow to two lines; titles should wrap gracefully. Keep destructive terms consistent across locales.',
],
[
'Performance',
'The content of the dialog is in the first render of the page. If it is slow to render, put it in a `<Suspense>`: the page sends a fallback first and the content after, so the page does not wait for it. With `lazy`, the browser loads the content when the fallback comes into view.',
],
[
'When a popover is better',
'If the content is a small hint or a few quick actions anchored to a control, use a popover or menu instead of a dialog. Dialogs interrupt on purpose—use that sparingly.',
],
[
'Follow-up and cleanup',
'After a successful action, close the dialog and show confirmation in context (toast, inline message, or updated UI) so people can see the result of what they just did.',
],
];
#[Component]
function DialogInsideScrollDemo(): Node
{
$sections = DIALOG_SECTIONS;
return (
<Dialog>
<DialogTrigger>Open dialog</DialogTrigger>
<DialogViewport className="open:flex items-center justify-center py-6 [@media(min-height:600px)]:pb-12 [@media(min-height:600px)]:pt-8">
<DialogPopup className="relative flex w-[min(40rem,calc(100vw-2rem))] max-h-full max-w-full min-h-0 flex-col transition-[scale,opacity] duration-100 ease-out data-starting-style:scale-[0.98] data-starting-style:opacity-0 data-ending-style:scale-[0.98] data-ending-style:opacity-0">
<div className="flex flex-col gap-1 p-4 border-b border-neutral-950 dark:border-white">
<DialogTitle>Dialog</DialogTitle>
<DialogDescription>This layout keeps the popup fully on screen while allowing its content to scroll.</DialogDescription>
</div>
<DialogSections sections={{ sections }} className="flex-auto overflow-y-auto overscroll-contain focus-visible:outline-2 focus-visible:outline-offset-0 focus-visible:outline-neutral-950 dark:focus-visible:outline-white" itemClass="flex flex-col gap-1 p-4" />
<div className="flex justify-end gap-3 p-4 border-t border-neutral-950 dark:border-white">
<DialogClose autofocus>Close</DialogClose>
</div>
</DialogPopup>
</DialogViewport>
</Dialog>
);
}
#[Component]
function DialogSections(
array $sections,
string $className = 'flex flex-col gap-4',
string $itemClass = 'flex flex-col gap-1',
): Node
{
return (
<div className={{ className }}>
{% for section in sections %}
<section key={{ loop.index }} className={{ itemClass }}>
<h3 className="text-sm font-bold">{{ section[0] }}</h3>
<p className="text-sm text-neutral-700 dark:text-neutral-300">{{ section[1] }}</p>
</section>
{% endfor %}
</div>
);
}
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.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Dialog, DialogViewport};
use function Phem\Ui\{DialogClose, DialogPopup, DialogTrigger};
#[Component]
function DialogUncontainedDemo(): Node
{
return (
<Dialog>
<DialogTrigger>Open dialog</DialogTrigger>
<DialogViewport className="open:grid place-items-center px-4 py-12 xl:py-6">
<DialogPopup className="group/popup relative flex h-full w-full max-w-[70rem] xl:max-w-none justify-center border-0 bg-transparent shadow-none pointer-events-none transition-opacity duration-150 data-starting-style:opacity-0 data-ending-style:opacity-0">
<DialogClose aria-label="Close" className="absolute right-0 -top-10 w-control p-0 shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:shadow-none xl:top-0 pointer-events-auto">
<DialogXIcon />
</DialogClose>
<div className="pointer-events-auto h-full w-full max-w-[70rem] bg-white dark:bg-neutral-950 p-4 text-neutral-950 dark:text-white border border-neutral-950 dark:border-white shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:shadow-none transition-[scale] duration-500 ease-[cubic-bezier(0.22,1,0.36,1)] group-data-starting-style/popup:scale-105"></div>
</DialogPopup>
</DialogViewport>
</Dialog>
);
}
#[Component]
function DialogXIcon(): Node
{
return (
<svg aria-hidden="true" width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-linecap="square" stroke-linejoin="round" className="block">
<path d="m2.5 2.5 11 11m-11 0 11-11" />
</svg>
);
}
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.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Dialog;
use function Phem\Ui\{DialogClose, DialogDescription, DialogPopup, DialogTitle, DialogTrigger};
#[Component]
function DialogDetachedDemo(): Node
{
return (
<div>
<DialogTrigger handle="detached-dialog">View notifications</DialogTrigger>
<Dialog handle="detached-dialog">
<DialogPopup>
<div className="flex flex-col gap-1">
<DialogTitle>Notifications</DialogTitle>
<DialogDescription>You are all caught up. Good job!</DialogDescription>
</div>
<div className="flex justify-end gap-3">
<DialogClose>Close</DialogClose>
</div>
</DialogPopup>
</Dialog>
</div>
);
}
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.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Dialog;
use function Phem\Ui\{DialogClose, DialogDescription, DialogPayload, DialogPopup, DialogTitle, DialogTrigger};
#[Component]
function DialogMultipleDemo(): Node
{
return (
<div className="flex gap-2">
<DialogTrigger handle="multiple-dialog" payload="Trigger 1">Trigger 1</DialogTrigger>
<DialogTrigger handle="multiple-dialog" payload="Trigger 2">Trigger 2</DialogTrigger>
<Dialog handle="multiple-dialog">
<DialogPopup>
<div className="flex flex-col gap-1">
<DialogTitle>Dialog</DialogTitle>
<DialogDescription>This has been opened by <DialogPayload /></DialogDescription>
</div>
<div className="flex justify-end gap-3">
<DialogClose>Close</DialogClose>
</div>
</DialogPopup>
</Dialog>
</div>
);
}
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.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Dialog;
use function Phem\Ui\{DialogClose, DialogPayload, DialogPopup, DialogTitle, DialogTrigger};
#[Component]
function DialogControlledDemo(): Node
{
return (
<Dialog>
<div className="flex flex-wrap justify-center gap-2">
<DialogTrigger id="dialog-trigger-1" payload="Dialog 1">Open 1</DialogTrigger>
<DialogTrigger id="dialog-trigger-2" payload="Dialog 2">Open 2</DialogTrigger>
<DialogTrigger id="dialog-trigger-3" payload="Dialog 3">Open 3</DialogTrigger>
<button type="button" data-action="phem-dialog#open" data-phem-dialog-trigger-id-param="dialog-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>
<DialogPopup>
<DialogTitle>
<DialogPayload />
</DialogTitle>
<div className="flex justify-end gap-3">
<DialogClose>Close</DialogClose>
</div>
</DialogPopup>
</Dialog>
);
}
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.
| Prop | Type | Default |
|---|---|---|
handle | ?string | null |
defaultOpen | bool | false |
triggerId | ?string | null |
dismissible | bool | true |
disablePointerDismissal | bool | false |
modal | bool | true |
confirmClose | ?string | null |
finalFocus | ?string | null |
children | ?Html | null |
...attrs | array | [] |
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.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
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.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...attrs | array | [] |
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.
| Prop | Type | Default |
|---|---|---|
handle | ?string | null |
payload | ?string | null |
disabled | bool | false |
children | ?Html | null |
...rest | mixed | — |
DialogTitle#
The heading of the dialog. It names the dialog (aria-labelledby).
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
DialogDescription#
The text that describes the dialog (aria-describedby).
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
DialogClose#
A button that closes the dialog.
| Prop | Type | Default |
|---|---|---|
disabled | bool | false |
children | ?Html | null |
...rest | mixed | — |
DialogPayload#
The text of the payload of the trigger that opened the dialog, or its children.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
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.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
The source of the examples: examples/ui/Dialog.phem.