# Popover

A small panel that opens beside its trigger, for detail or a quick task that does not need a dialog.

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/popover shows the Dex app's own React component, which only works inside the Dex codebase.

## HTML

A `dex-popover` inside a `dex-popover-anchor` opens 4px under its trigger, lined up with its start. Show and hide it with the `hidden` attribute, and keep the trigger's `aria-expanded` in step:

```html
<div class="dex-popover-anchor">
  <button type="button" class="dex-button dex-button--outline" aria-expanded="false" aria-controls="salary-note">Why this range?</button>
  <div class="dex-popover" id="salary-note" role="dialog" aria-label="Why this range" hidden>
    <p class="dex-small">Synthesia pays £110,000 to £130,000 for this role, from what they told Dex.</p>
  </div>
</div>

<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))
    }
    trigger.addEventListener('click', () => setOpen(popover.hidden))
    document.addEventListener('keydown', (event) => {
      if (event.key === 'Escape' && !popover.hidden) {
        setOpen(false)
        trigger.focus()
      }
    })
    document.addEventListener('click', (event) => {
      if (!anchor.contains(event.target)) setOpen(false)
    })
  })
</script>
```

- **Lined up with the trigger's end**: add `dex-popover--end`.
- **Above the trigger**: layout CSS of your own on the popover, `style="top: auto; bottom: calc(100% + 0.25rem)"`.
- **With a form**: fields (`dex-form-row`, `dex-field`) and a button inside it. Saving closes it.
- **A list or a calendar inside**: `dex-popover--list` (a combobox) and `dex-popover--calendar` (a date field). See https://brand.meetdex.ai/components/combobox.md and https://brand.meetdex.ai/components/date-field.md.
- **Bottom sheet on a phone**: in the app, a popover with a long list, a calendar or a field becomes a sheet from the bottom of the screen below 640px. `dex.css` has no sheet. In a prototype, add a media query of your own that fixes the popover to the bottom (`position: fixed; inset: auto 0 0; width: auto; max-width: none; border-radius: var(--dex-radius-lg) var(--dex-radius-lg) 0 0`).

## Rules

- A popover holds a little content or a small task beside the thing it's about, and leaves the page behind it usable. For something that must be answered first, use a dialog.
- No border: the `lg` shadow does the work. 8px radius.
- Name it: `aria-label` or `aria-labelledby` on the popover.
- It closes on Escape (focus goes back to the trigger) and on a press outside it.
- Near the edge of the screen it should stay in view: open it on the other side, or line it up with the trigger's end.
