# Combobox

Picks from a list that can be searched, for more options than a select can hold.

This is the HTML to build it with `dex.css`, for work outside the Dex codebase. Read https://brand.meetdex.ai/design.md first: it says how to set up a page and how to use the stylesheet. The page at https://brand.meetdex.ai/components/combobox shows the Dex app's own React component, which only works inside the Dex codebase.

## HTML

A button that looks like a field opens a popover under it, with a search and the options. The options are `dex-menu-item`s; the chosen one takes `aria-selected="true"` and its tick.

```html
<label class="dex-field-label" id="city-label" for="city">City</label>
<div class="dex-popover-anchor">
  <button type="button" id="city" class="dex-field dex-field-button" aria-labelledby="city-label" aria-haspopup="listbox" aria-expanded="false">
    <svg aria-hidden="true"><!-- Lucide map-pin --></svg>
    <span>London</span><span class="dex-menu-detail">England</span>
  </button>
  <div class="dex-popover dex-popover--list" hidden>
    <input class="dex-search" placeholder="Search cities" aria-label="Search cities" />
    <div class="dex-listbox" role="listbox" aria-labelledby="city-label">
      <div class="dex-menu-item" role="option" aria-selected="true">London<span class="dex-menu-detail">England</span></div>
      <div class="dex-menu-item" role="option">Manchester<span class="dex-menu-detail">England</span></div>
      <div class="dex-menu-item" role="option">Edinburgh<span class="dex-menu-detail">Scotland</span></div>
    </div>
    <p class="dex-listbox-empty" hidden>No matching city or town</p>
  </div>
</div>
```

- **No value yet**: the placeholder goes in the button as `<span class="dex-muted">Choose a city</span>`.
- **Icon and detail**: an optional Lucide icon leads the field. A `dex-menu-detail` is muted at the end of a row (a city's region) and shows in the field too once chosen.
- **In a field**: wrap it in a `dex-form-row` with a `dex-field-label` and a `dex-field-help` or `dex-field-error` under it.
- **Invalid**: `aria-invalid="true"` on the button turns its edge red.
- **Disabled**: the `disabled` attribute.
- **Read-only**: `aria-readonly="true"` on the button, which takes a muted fill and doesn't open. Add helper text saying where the value came from.
- **Several values**: open the same popover from a chip or button instead of a field, and let rows toggle `aria-selected` without closing. At a limit, set `aria-disabled="true"` on the unticked rows; ticked ones can still be unticked.

Opening and closing, for every popover on the page (combobox, date field, popover):

```html
<script>
  document.querySelectorAll('.dex-popover-anchor').forEach((anchor) => {
    const trigger = anchor.querySelector('[aria-expanded]')
    const popover = anchor.querySelector('.dex-popover')
    const setOpen = (open) => {
      popover.hidden = !open
      trigger.setAttribute('aria-expanded', String(open))
      if (open) popover.querySelector('.dex-search')?.focus()
    }
    trigger.addEventListener('click', () => {
      if (trigger.getAttribute('aria-readonly') !== 'true') setOpen(popover.hidden)
    })
    document.addEventListener('click', (event) => {
      if (!anchor.contains(event.target)) setOpen(false)
    })
    anchor.addEventListener('keydown', (event) => {
      if (event.key === 'Escape' && !popover.hidden) {
        setOpen(false)
        trigger.focus()
      }
    })
  })
</script>
```

Filter the rows as the search changes (set `hidden` on the ones that don't match, and take it off `dex-listbox-empty` when none do), and on a choice move `aria-selected`, copy the label into the button and close.

## States

- The field rests at the 25% edge, steps to 50% on hover and 75% on focus, and keeps the 75% edge while its list is open.
- A row takes a muted fill under the pointer or keyboard. Only the tick marks the chosen one.

## Rules

- Use a combobox when there are more options than a select can hold, such as cities or programming languages. For a short, fixed list, use a select.
- The list opens the width of the field, 4px below it, filters as people type and closes on a choice.
- Say what wasn't found: "No matching city or town", not "No results".
- On a phone the Dex app opens the list in a bottom sheet. `dex.css` has no bottom sheet; in a mock, keep the popover.
