# Popover

Content shown on top of the page from a button, with the `popover` attribute.

_Diagram: A popover opens from a button, such as Share beside the page heading, as a small panel on top of the page under the button._

## Live example

### Popover

```html
<button type="button" popovertarget="share-popover">Share</button>
<div id="share-popover" popover data-popover>
  <p>Anyone with the link can view this page.</p>
</div>
```

### Above the button, with a close button

```html
<button type="button" popovertarget="help-popover">Help</button>
<div id="help-popover" popover="manual" data-popover="block-start">
  <p><strong>Need a hand?</strong></p>
  <p>Read the <a href="#above-title">guide</a> or ask in the forum.</p>
  <button type="button" popovertarget="help-popover" popovertargetaction="hide" data-button="small">Close</button>
</div>
```

### Tooltip

```html
<div class="cluster">
  <dui-tooltip>
    <button type="button" aria-describedby="save-tip">Save</button>
    <span id="save-tip" popover="hint" role="tooltip">Shortcut: Ctrl+S</span>
  </dui-tooltip>
  <dui-tooltip>
    <button type="button" aria-label="Copy link" aria-describedby="copy-tip" data-button="quiet">⧉</button>
    <span id="copy-tip" popover="hint" role="tooltip">Copies the page address</span>
  </dui-tooltip>
</div>
```

## Attributes

| Attribute or value | Where | Effect |
| --- | --- | --- |
| `data-popover` | a `[popover]` element | panel look, anchored below the invoker, aligned to its start edge |
| `data-popover="block-start \| inline-start \| inline-end"` |  | place it above or beside the invoker instead |
| `role="tooltip"` | a `[popover]` element | tooltip look, placed above the trigger |
| `popover="auto \| manual \| hint"` |  | native: auto light-dismisses, manual does not |

Children: with a `<nav>` of links, the panel takes the menu's inset, so a `<p>` or heading, an `<hr>` and a `<form>` with a quiet button line up with the links (an account popover).

### Positioning

A popover opened by popovertarget (or by showPopover() with a source) has its invoker as an implicit anchor, so position-area places it next to the button and position-try-fallbacks flips it when it would overflow. Inside @supports only; where anchor positioning is missing the popover sits centered at the bottom of the viewport, readable and clear of the content it belongs to.

### Tooltip

`role="tooltip"` on a `popover="hint"` element, referenced from the trigger with `aria-describedby`. The APG Tooltip pattern is still marked as work in progress, so these rules come from it and from WCAG 1.4.13 Content on Hover or Focus:

- A tooltip supplements the trigger's own accessible name; it is never the only label, because touch and some assistive technology users never see it;
- It appears on keyboard focus as well as on hover;
- Esc hides it without moving focus (dismissible), the pointer can move onto it without it closing (hoverable), and it stays until focus or the pointer leaves (persistent);
- It holds no interactive content (use a popover for that).

HTML cannot show a popover on hover or focus yet: the interestfor attribute is the coming standard, shipped only in Chromium today. Until it is interoperable, the `<dui-tooltip>` web component (`web-components/dui-tooltip.js`) does the showing and hiding. `popover="hint"` does not close other open popovers; where "hint" is unsupported the attribute falls back to "manual" and the component still handles Esc.

### Motion

A `data-popover` panel fades and grows from `--dui-motion-zoom` over `--dui-motion-duration-fast`, entering with `--dui-motion-easing-enter` and leaving with `--dui-motion-easing-exit` (`@starting-style` and transition-behavior: allow-discrete). The zoom runs for that duration times `--dui-motion-movement`, so reduced motion keeps only the fade (WCAG 2.3.3). Tooltips appear at once.

### Elevation

Panels use `--dui-elevation-popover` and `--dui-elevation-popover-surface`; tooltips use the popover shadow on their inverse surface. The role's 1px ring draws the edge. Both keep a transparent border, which forced colors mode paints in place of the dropped shadow.

### Keyboard

| Key | Action |
| --- | --- |
| <kbd>Enter</kbd> / <kbd>Space</kbd> | on a popover button, opens or closes the popover |
| <kbd>Tab</kbd> | moves into the open popover, then on through the page |
| <kbd>Esc</kbd> | closes an auto popover and returns focus to its button |
| <kbd>Tab to a tooltip trigger</kbd> | shows its tooltip / Esc: hides it, focus stays |

## Accessibility

- A popover does not trap focus. Decisions belong in a modal `<dialog>`. For actions, use the [menu button](https://domestic-ui.odonckers.dev/patterns/menu-button.md).
- A tooltip supplements the trigger's name, shows on focus and hover, and closes on <kbd>Esc</kbd> ([WCAG 1.4.13](https://www.w3.org/WAI/WCAG22/Understanding/content-on-hover-or-focus.html); [APG Tooltip](https://www.w3.org/WAI/ARIA/apg/patterns/tooltip/) is a draft).

## Specifications

- [HTML `popover` attribute](https://html.spec.whatwg.org/multipage/popover.html#the-popover-attribute)
- [MDN Popover API](https://developer.mozilla.org/en-US/docs/Web/API/Popover_API)
- [WHATWG popover](https://html.spec.whatwg.org/multipage/popover.html)
- [MDN `popover`](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/popover)
- [MDN CSS anchor positioning](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_anchor_positioning)
- [MDN `position-area`](https://developer.mozilla.org/en-US/docs/Web/CSS/position-area)
- [MDN `position-try-fallbacks`](https://developer.mozilla.org/en-US/docs/Web/CSS/position-try-fallbacks)
- [W3C css anchor position 1](https://drafts.csswg.org/css-anchor-position-1/)
- [Open UI interest invokers.explainer](https://open-ui.org/components/interest-invokers.explainer/)

## See also

- [Molecules](https://domestic-ui.odonckers.dev/molecules/index.md)
- Source: [`css/components/molecules/popover.css`](https://github.com/odonckers/domestic-ui/blob/main/css/components/molecules/popover.css), [`examples/molecules/popover.html`](https://github.com/odonckers/domestic-ui/blob/main/examples/molecules/popover.html)
- Demos: Dashboard view ([Plain HTML](https://github.com/odonckers/domestic-ui/blob/main/demos/vanilla/index.html), [React](https://github.com/odonckers/domestic-ui/blob/main/demos/react/src/DashboardPage.tsx), [Angular](https://github.com/odonckers/domestic-ui/tree/main/demos/angular/src/app/dashboard), [Astro](https://github.com/odonckers/domestic-ui/blob/main/demos/astro/src/pages/index.astro), [Vue](https://github.com/odonckers/domestic-ui/blob/main/demos/vue/src/DashboardPage.vue), [Svelte](https://github.com/odonckers/domestic-ui/blob/main/demos/svelte/src/DashboardPage.svelte)); Account settings view ([Plain HTML](https://github.com/odonckers/domestic-ui/blob/main/demos/vanilla/settings.html), [React](https://github.com/odonckers/domestic-ui/blob/main/demos/react/src/SettingsPage.tsx), [Angular](https://github.com/odonckers/domestic-ui/tree/main/demos/angular/src/app/settings), [Astro](https://github.com/odonckers/domestic-ui/blob/main/demos/astro/src/pages/settings.astro), [Vue](https://github.com/odonckers/domestic-ui/blob/main/demos/vue/src/SettingsPage.vue), [Svelte](https://github.com/odonckers/domestic-ui/blob/main/demos/svelte/src/SettingsPage.svelte)); Orders view ([Plain HTML](https://github.com/odonckers/domestic-ui/blob/main/demos/vanilla/orders.html), [React](https://github.com/odonckers/domestic-ui/blob/main/demos/react/src/OrdersPage.tsx), [Angular](https://github.com/odonckers/domestic-ui/tree/main/demos/angular/src/app/orders), [Astro](https://github.com/odonckers/domestic-ui/blob/main/demos/astro/src/pages/orders.astro), [Vue](https://github.com/odonckers/domestic-ui/blob/main/demos/vue/src/OrdersPage.vue), [Svelte](https://github.com/odonckers/domestic-ui/blob/main/demos/svelte/src/OrdersPage.svelte))

## How other systems do it

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

- [Apple Human Interface Guidelines: Popovers](https://developer.apple.com/design/human-interface-guidelines/popovers)
- Material Design 3: No equivalent
- Vercel Geist: No equivalent
