# Dialog and drawer

A modal `<dialog>`, either centered or as a drawer along one edge of the viewport.

_Diagram: A modal dialog sits centred over the dimmed page; a drawer takes the same place along one edge of the window._

## Live example

### Modal dialog

```html
<button type="button" commandfor="confirm-dialog" command="show-modal">Delete project…</button>
<dialog id="confirm-dialog" data-dialog aria-labelledby="confirm-dialog-title">
  <header>
    <h2 id="confirm-dialog-title">Delete project?</h2>
  </header>
  <p>This deletes the project and its 12 files for everyone. You cannot undo it.</p>
  <form method="dialog">
    <footer>
      <button type="submit" value="cancel" data-button="secondary" autofocus>Cancel</button>
      <button type="submit" value="delete" data-button="danger">Delete project</button>
    </footer>
  </form>
</dialog>
```

### Drawer (end)

```html
<button type="button" commandfor="filters-drawer" command="show-modal">Filters</button>
<dialog id="filters-drawer" data-dialog="drawer end" closedby="any" aria-labelledby="filters-drawer-title">
  <header>
    <h2 id="filters-drawer-title">Filters</h2>
    <button type="button" commandfor="filters-drawer" command="close" data-button="quiet small">Close</button>
  </header>
  <form method="dialog" class="stack">
    <fieldset>
      <legend>Status</legend>
      <label><input type="checkbox" name="show-open" checked> Open</label>
      <label><input type="checkbox" name="show-closed"> Closed</label>
    </fieldset>
    <div data-field>
      <label for="filters-owner">Owner</label>
      <input id="filters-owner" name="owner" type="text" autocomplete="off">
    </div>
    <footer>
      <button type="submit" value="apply">Apply filters</button>
    </footer>
  </form>
</dialog>
```

### Drawer (start)

```html
<button type="button" commandfor="menu-drawer" command="show-modal">Menu</button>
<dialog id="menu-drawer" data-dialog="drawer start" closedby="any" aria-labelledby="menu-drawer-title">
  <header>
    <h2 id="menu-drawer-title">Menu</h2>
    <button type="button" commandfor="menu-drawer" command="close" data-button="quiet small">Close</button>
  </header>
  <nav aria-label="Drawer" data-nav>
    <ul>
      <li><a href="#menu-drawer-title" aria-current="page">Overview</a></li>
      <li><a href="#drawer-start-title">Projects</a></li>
      <li><a href="#drawer-start-title">Settings</a></li>
    </ul>
  </nav>
</dialog>
```

### Non-modal dialog

```html
<button type="button" data-show-dialog="note-dialog">Show a note</button>
<dialog id="note-dialog" data-dialog closedby="closerequest" aria-labelledby="note-dialog-title">
  <header>
    <h2 id="note-dialog-title">Autosave is on</h2>
    <button type="button" commandfor="note-dialog" command="close" data-button="quiet small">Close</button>
  </header>
  <p>Changes are saved as you type.</p>
</dialog>
```

## Attributes

| Attribute or value | Where | Effect |
| --- | --- | --- |
| `data-dialog` | `<dialog>` | header, body and footer layout and the open transition |
| `data-dialog="drawer [start \| end]"` |  | a drawer at the inline-start or inline-end edge (end is the default) |
| `closedby="any \| closerequest \| none"` |  | native: how the dialog can be dismissed |

Children: an optional `<header>` (heading and close button) and `<footer>` (actions, end-aligned); everything else is the body.

### Motion

Dialogs fade and lift and drawers slide and fade, with `@starting-style` and transition-behavior: allow-discrete, entering with `--dui-motion-easing-enter` and leaving with `--dui-motion-easing-exit`. Durations are `--dui-motion-duration-normal` (dialog) and `--dui-motion-duration-slow` (drawer) to open and one step shorter to close, so `--dui-motion-scale` retunes them. Under prefers-reduced-motion or `data-motion="reduced"` the movement is dropped and the fade stays (WCAG 2.3.3); `data-motion="none"` turns it off. transition-behavior and `@starting-style` are Baseline in every supported engine, so there is no fallback declaration.

### Elevation

Dialogs and drawers use `--dui-elevation-dialog` and its surface (the highest level). The role's 1px ring draws the edge; the border is transparent, and forced colors mode, which drops box-shadow, paints it in a system color.

### Keyboard

| Key | Action |
| --- | --- |
| <kbd>Enter</kbd> / <kbd>Space</kbd> | on an opener, opens the dialog and moves focus into it |
| <kbd>Tab</kbd> | moves to the next focusable element inside the dialog / Shift+Tab: previous |
| <kbd>Esc</kbd> | closes the dialog and returns focus to the opener |

## Accessibility

- `showModal()` or `command="show-modal"` makes the page inert, moves focus in and returns it on close.
- Name each dialog with `aria-labelledby`. Put `autofocus` on the least destructive action.
- <kbd>Esc</kbd> and the close buttons always work ([WCAG 2.1.2](https://www.w3.org/WAI/WCAG22/Understanding/no-keyboard-trap.html)). [`closedby="any"`](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog#closedby) adds light dismiss.

## Specifications

- [HTML `<dialog>` element](https://html.spec.whatwg.org/multipage/interactive-elements.html#the-dialog-element)
- [APG Dialog (Modal) pattern](https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/)
- [MDN `<dialog>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog)
- [MDN Invoker Commands API](https://developer.mozilla.org/en-US/docs/Web/API/Invoker_Commands_API)
- [MDN `@starting-style`](https://developer.mozilla.org/en-US/docs/Web/CSS/@starting-style)
- [MDN `transition-behavior`](https://developer.mozilla.org/en-US/docs/Web/CSS/transition-behavior)
- [WCAG Animation from interactions](https://www.w3.org/WAI/WCAG22/Understanding/animation-from-interactions.html)

## See also

- [Organisms](https://domestic-ui.odonckers.dev/organisms/index.md)
- Source: [`css/components/molecules/dialog.css`](https://github.com/odonckers/domestic-ui/blob/main/css/components/molecules/dialog.css), [`examples/molecules/dialog.html`](https://github.com/odonckers/domestic-ui/blob/main/examples/molecules/dialog.html)

## How other systems do it

For comparison only; these are not the standard this system follows.

- [Apple Human Interface Guidelines: Modality](https://developer.apple.com/design/human-interface-guidelines/modality)
- [Material Design 3: Dialogs](https://m3.material.io/components/dialogs/overview)
- [Vercel Geist: Modal](https://vercel.com/geist/modal)
