Menhir

Dialog

View as Markdown

A window over the page that the user deals with before going back to it, such as a form or a confirmation: the WAI-ARIA modal dialog pattern. The Popup is a native <dialog> opened with showModal(), so the browser puts it in the top layer, above every z-index, makes the rest of the page inert and closes it on Escape; there is no portal. Menhir adds what the browser leaves out: Tab and Shift+Tab stay inside, a press on the backdrop closes it, the page behind stops scrolling, and it stays shown until its exit animation has ended. Style the backdrop with ::backdrop, such as a blur with backdrop-filter, or Tailwind's backdrop: variant (backdrop:backdrop-blur-xs). A dialog open by default is rendered closed on the server and opens once the page has hydrated, since server HTML cannot open a modal dialog.

Size
3.5 kB

Example

Styling

Edit profile

Your name is shown on your posts.

DialogExample.tsx
import { Dialog } from "menhir/dialog";

export default function DialogExample() {
  return (
    <Dialog.Root>
      <Dialog.Trigger class="cursor-pointer rounded-md border border-stone-300 px-3 py-1.5 text-sm focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 dark:border-stone-600">
        Edit profile
      </Dialog.Trigger>
      <Dialog.Popup class="m-auto w-[min(24rem,calc(100%-2rem))] rounded-xl bg-white p-6 text-sm text-inherit transition-opacity duration-150 backdrop:bg-black/40 backdrop:backdrop-blur-xs backdrop:transition-opacity backdrop:duration-150 data-closed:opacity-0 data-closed:backdrop:opacity-0 dark:bg-stone-900 starting:open:opacity-0 starting:open:backdrop:opacity-0">
        <Dialog.Title class="m-0 text-base font-semibold">
          Edit profile
        </Dialog.Title>
        <Dialog.Description class="mt-2 mb-4 text-stone-600 dark:text-stone-400">
          Your name is shown on your posts.
        </Dialog.Description>
        <label class="grid gap-1">
          Name
          <input
            name="name"
            value="Ada Lovelace"
            class="rounded-md border border-stone-300 px-2 py-1 dark:border-stone-600"
          />
        </label>
        <div class="mt-6 flex justify-end">
          <Dialog.Close class="cursor-pointer rounded-md border border-stone-300 px-3 py-1.5 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 dark:border-stone-600">
            Done
          </Dialog.Close>
        </div>
      </Dialog.Popup>
    </Dialog.Root>
  );
}

Anatomy

<Dialog.Root>
  <Dialog.Trigger />
  <Dialog.Popup>
    <Dialog.Title />
    <Dialog.Description />
    <Dialog.Close />
  </Dialog.Popup>
</Dialog.Root>

API reference

Root

Holds the open state and renders no element. Every part has data-open or data-closed.

PropTypeDefaultDescription
openboolean—Whether the dialog is open, when controlled. Pair it with onOpenChange. A controlled dialog needs no Trigger: open it from any button of your own, and focus returns to that button.
defaultOpenbooleanfalseWhether the dialog is open at first, when uncontrolled.
onOpenChange(open: boolean, details: { reason, event }) => void—Called with the new value when the user opens or closes the dialog, and why: "trigger-press", "close-press", "escape-key", "outside-press" (the backdrop) or "close-request" (such as the back gesture on Android or a <form method="dialog">). A controlled dialog changes only if open does, so keeping open true refuses the close.

Trigger

<button type="button" aria-haspopup="dialog"> that opens the dialog. Name it with its content. Its render function must render a <button>.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the trigger with your own button component. Read once.

Popup

The <dialog> itself, opened with showModal(). Named by the Title in aria-labelledby and described by the Description in aria-describedby, while they are rendered; without a Title, give it an aria-label. Its render function must render a <dialog>. While it plays an exit animation it has data-closed and is inert.

PropTypeDefaultDescription
initialFocusHTMLElement | ((focused) => HTMLElement | null | undefined)—Where focus goes once it has opened, instead of the browser's choice: the element with autofocus, else the first focusable one. A function gets the browser's choice; use one for an element you hold in a ref.
returnFocusHTMLElement | ((focused) => HTMLElement | null | undefined)—Where focus goes once it has closed, instead of the browser's choice: the element focused before it opened. A function gets that element, or null if it is gone, as in (focused) => focused ?? fallback.
render(props) => JSX.Element—Renders the popup with your own component, which spreads its props onto a <dialog>. Read once.

Title

<h2> that names the Popup. Takes your id, else a generated one.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the title with your own component. Read once.

Description

<p> that describes the Popup. Takes your id, else a generated one.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the description with your own component. Read once.

Close

<button type="button"> that closes the dialog, such as Cancel, or an icon with an aria-label. Its render function must render a <button>.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the close button with your own button component. Read once.

Keyboard

KeysAction
SpaceOn the Trigger, opens the dialog.
EnterOn the Trigger, opens the dialog.
TabMoves focus to the next element in the dialog, from the last back to the first.
Shift + TabMoves focus to the previous element in the dialog, from the first to the last.
EscapeCloses the dialog, and the topmost one of nested dialogs.