# Search dialog

A command palette: a modal `<dialog>` holding a combobox of pages and sections, opened from a Search button or with Cmd-K or Ctrl-K.

_Diagram: A search dialog opens from the Search button in the app bar, over the dimmed page, with a field and its list of results._

## Live example

### Search dialog

```html
<button type="button" commandfor="site-search" command="show-modal" aria-haspopup="dialog" aria-keyshortcuts="Control+K Meta+K" hidden>Search</button>
<dialog id="site-search" data-dialog closedby="any" aria-labelledby="site-search-title">
  <header>
    <h2 id="site-search-title">Search</h2>
    <button type="button" commandfor="site-search" command="close" data-button="quiet">Close</button>
  </header>
  <dui-combobox>
    <label for="site-search-input">Search the docs</label>
    <input type="text" id="site-search-input" list="site-search-options" autocomplete="off" spellcheck="false" autofocus>
    <datalist id="site-search-options">
      <option value="Button" label="Atoms"></option>
      <option value="Field" label="Atoms"></option>
      <option value="Dialog and drawer" label="Organisms"></option>
      <option value="Tabs" label="Molecules"></option>
      <option value="Button › Attributes" label="Atoms"></option>
      <option value="Dialog and drawer › Modal dialog" label="Organisms"></option>
      <option value="Dialog and drawer › Drawer (end)" label="Organisms"></option>
      <option value="Tabs › Keyboard" label="Molecules"></option>
    </datalist>
  </dui-combobox>
</dialog>
<p role="status" id="site-search-result"></p>
```

## Attributes

### Keyboard

| Key | Action |
| --- | --- |
| <kbd>Cmd-K</kbd> / <kbd>Ctrl-K</kbd> | opens the dialog with focus in the field, except from a text field outside it |
| <kbd>Typing</kbd> | filters the results |
| <kbd>Down Arrow</kbd> / <kbd>Up Arrow</kbd> | moves to the next or previous result |
| <kbd>Enter</kbd> | goes to the active result and closes the dialog |
| <kbd>Escape</kbd> | closes the list, then clears the field, then closes the dialog; focus returns to where it was |
| <kbd>Tab</kbd> | moves between the field and the close button |

## Accessibility

- The browser does the modal work: `showModal()` or `command="show-modal"` makes the page inert, moves focus to the field (`autofocus`) and returns it to the opener or wherever it was on close ([APG Dialog (Modal)](https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/)).
- The dialog is named by its heading. The field has a visible label ([WCAG 3.3.2](https://www.w3.org/WAI/WCAG22/Understanding/labels-or-instructions.html)).
- Results are combobox options: focus stays in the field, arrow keys move the active result, and the count is announced after typing pauses ([WCAG 4.1.3](https://www.w3.org/WAI/WCAG22/Understanding/status-messages.html)).
- The shortcut needs a modifier, so it never fires while typing ([WCAG 2.1.4](https://www.w3.org/WAI/WCAG22/Understanding/character-key-shortcuts.html)). `aria-keyshortcuts` on the opener exposes it.
- A result on the same page moves focus to its heading ([WCAG 2.4.3](https://www.w3.org/WAI/WCAG22/Understanding/focus-order.html)).
- The opener stays hidden until the script that fills the results runs, so nothing broken shows without JavaScript.
- Why it is built this way, and the choices that are ours: the [search dialog explainer](https://domestic-ui.odonckers.dev/explainers/search-dialog.md).

## Specifications

- [APG Dialog (Modal) pattern](https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/)
- [APG Combobox pattern](https://www.w3.org/WAI/ARIA/apg/patterns/combobox/)
- [MDN `<dialog>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog)
- [MDN `aria-keyshortcuts`](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-keyshortcuts)

## See also

- [Molecules](https://domestic-ui.odonckers.dev/molecules/index.md)
- Source: [`examples/molecules/search-dialog.html`](https://github.com/odonckers/domestic-ui/blob/main/examples/molecules/search-dialog.html)

## How other systems do it

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

- [Apple Human Interface Guidelines: Searching](https://developer.apple.com/design/human-interface-guidelines/searching)
- [Material Design 3: Search](https://m3.material.io/components/search/overview)
- [Vercel Geist: Command Menu](https://vercel.com/geist/command-menu)
