# Dialog

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.

- Entry point: `menhir/dialog`
- Size: 3.5 kB, minified and gzipped, Solid left out

> To add Dialog to a project, install the packages below with the project's package manager, then copy the example that matches how the project is styled, Tailwind CSS or plain CSS, and adapt its markup and class names to the project.

## Install

```sh
pnpm add menhir solid-js @solidjs/web
```

## Anatomy

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

## Example

### Tailwind CSS

`DialogExample.tsx`:

```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>
  );
}
```

### CSS

`DialogExample.tsx`:

```tsx
import { Dialog } from "menhir/dialog";

import "./dialog.css";

export default function DialogExample() {
  return (
    <Dialog.Root>
      <Dialog.Trigger class="dialog-trigger">Edit profile</Dialog.Trigger>
      <Dialog.Popup class="dialog-popup">
        <Dialog.Title class="dialog-title">Edit profile</Dialog.Title>
        <Dialog.Description class="dialog-description">
          Your name is shown on your posts.
        </Dialog.Description>
        <label class="dialog-field">
          Name
          <input name="name" value="Ada Lovelace" />
        </label>
        <div class="dialog-actions">
          <Dialog.Close class="dialog-close">Done</Dialog.Close>
        </div>
      </Dialog.Popup>
    </Dialog.Root>
  );
}
```

`dialog.css`:

```css
/* The least CSS that makes the Dialog look like one: a card over a dimmed
   page. The <dialog> is centred in the top layer by the browser, and
   ::backdrop covers the page behind it. It fades in on data-open and out
   on data-closed: the Popup stays shown until the exit animation has
   ended. Colours are Tailwind's stone palette; light-dark() picks the
   second one in dark mode (color-scheme: dark). */
.dialog-trigger,
.dialog-close {
  padding: 0.375rem 0.75rem;
  border: 1px solid
    light-dark(
      oklch(86.9% 0.005 56.366) /* stone-300 */,
      oklch(44.4% 0.011 73.639) /* stone-600 */
    );
  border-radius: 0.375rem;
  background: transparent;
  color: inherit;
  font:
    0.875rem system-ui,
    sans-serif;
  cursor: pointer;
}

.dialog-popup {
  width: min(24rem, calc(100% - 2rem));
  padding: 1.5rem;
  border: 0;
  border-radius: 0.75rem;
  background: light-dark(white, oklch(21.6% 0.006 56.043) /* stone-900 */);
  color: inherit;
  font:
    0.875rem system-ui,
    sans-serif;
}

.dialog-popup::backdrop {
  background: oklch(0% 0 0 / 40%);
  backdrop-filter: blur(4px);
}

.dialog-popup[data-open],
.dialog-popup[data-open]::backdrop {
  animation: dialog-fade 150ms ease-out;
}

.dialog-popup[data-closed],
.dialog-popup[data-closed]::backdrop {
  animation: dialog-fade 150ms ease-in reverse;
}

@keyframes dialog-fade {
  from {
    opacity: 0;
  }
}

.dialog-title {
  margin: 0;
  font-size: 1rem;
}

.dialog-description {
  margin: 0.5rem 0 1rem;
  color: light-dark(
    oklch(44.4% 0.011 73.639) /* stone-600 */,
    oklch(70.9% 0.01 56.259) /* stone-400 */
  );
}

.dialog-field {
  display: grid;
  gap: 0.25rem;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 1.5rem;
}
```

## API reference

### Root

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

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` | `boolean` | — | 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. |
| `defaultOpen` | `boolean` | `false` | Whether 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>.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `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.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `initialFocus` | `HTMLElement \| ((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. |
| `returnFocus` | `HTMLElement \| ((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.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `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.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `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>.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `render` | `(props) => JSX.Element` | — | Renders the close button with your own button component. Read once. |

## Keyboard

| Keys | Action |
| --- | --- |
| `Space` | On the Trigger, opens the dialog. |
| `Enter` | On the Trigger, opens the dialog. |
| `Tab` | Moves focus to the next element in the dialog, from the last back to the first. |
| `Shift + Tab` | Moves focus to the previous element in the dialog, from the first to the last. |
| `Escape` | Closes the dialog, and the topmost one of nested dialogs. |
