# Popover

A popup placed against its trigger that leaves the page usable, such as a few settings, a short form or details about an item: the WAI-ARIA dialog pattern, read for a non-modal dialog. The Popup is a popover="auto" element, so the browser puts it in the top layer, above every z-index, and closes it on Escape or a press outside; there is no portal. Menhir places it against the Trigger with Floating UI, flipping it to the other side or shifting it along when it would leave the viewport, and keeps it shown until its exit animation has ended. Render the Popup right after the Trigger: Tab then moves from the Trigger into the Popup and on to what follows, as WCAG's focus order asks. The Popup gets data-side and data-align, with the side and alignment it was placed on, and the CSS variables --anchor-width, --anchor-height and --transform-origin (the point nearest the trigger, for an animation that grows from it). For a popup that keeps focus inside until it is closed, use Dialog. A popover open by default is rendered closed on the server and opens once the page has hydrated.

- Entry point: `menhir/popover`
- Size: 9.6 kB, minified and gzipped, Solid left out

> To add Popover to a project, install the packages below with the project's package manager, then copy the example that matches how the project is styled, Tailwind CSS or plain CSS, and adapt its markup and class names to the project.

## Install

```sh
pnpm add menhir solid-js @solidjs/web
```

## Anatomy

```tsx
<Popover.Root>
  <Popover.Trigger />
  <Popover.Popup>
    <Popover.Arrow />
    <Popover.Title />
    <Popover.Description />
    <Popover.Close />
  </Popover.Popup>
</Popover.Root>
```

## Example

### Tailwind CSS

`PopoverExample.tsx`:

```tsx
import { Popover } from "menhir/popover";

export default function PopoverExample() {
  return (
    <Popover.Root>
      <Popover.Trigger class="cursor-pointer rounded-md border border-stone-300 px-3 py-1.5 text-sm focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 dark:border-stone-600">
        Notifications
      </Popover.Trigger>
      <Popover.Popup
        sideOffset={8}
        class="w-64 origin-(--transform-origin) overflow-visible rounded-lg border border-stone-200 bg-white p-4 text-sm text-inherit shadow-lg transition-[opacity,scale] duration-150 data-closed:scale-95 data-closed:opacity-0 dark:border-stone-700 dark:bg-stone-900 starting:data-open:scale-95 starting:data-open:opacity-0"
      >
        <Popover.Arrow class="absolute size-2.5 rotate-45 border-stone-200 bg-white data-[side=bottom]:-top-1.5 data-[side=bottom]:border-t data-[side=bottom]:border-l data-[side=left]:-right-1.5 data-[side=left]:border-t data-[side=left]:border-r data-[side=right]:-left-1.5 data-[side=right]:border-b data-[side=right]:border-l data-[side=top]:-bottom-1.5 data-[side=top]:border-r data-[side=top]:border-b dark:border-stone-700 dark:bg-stone-900" />
        <Popover.Title class="m-0 text-sm font-semibold">
          Notifications
        </Popover.Title>
        <Popover.Description class="mt-1 mb-3 text-stone-600 dark:text-stone-400">
          You are all caught up.
        </Popover.Description>
        <Popover.Close class="cursor-pointer rounded-md border border-stone-300 px-3 py-1.5 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 dark:border-stone-600">
          Close
        </Popover.Close>
      </Popover.Popup>
    </Popover.Root>
  );
}
```

### CSS

`PopoverExample.tsx`:

```tsx
import { Popover } from "menhir/popover";

import "./popover.css";

export default function PopoverExample() {
  return (
    <Popover.Root>
      <Popover.Trigger class="popover-trigger">Notifications</Popover.Trigger>
      <Popover.Popup class="popover-popup" sideOffset={8}>
        <Popover.Arrow class="popover-arrow" />
        <Popover.Title class="popover-title">Notifications</Popover.Title>
        <Popover.Description class="popover-description">
          You are all caught up.
        </Popover.Description>
        <Popover.Close class="popover-close">Close</Popover.Close>
      </Popover.Popup>
    </Popover.Root>
  );
}
```

`popover.css`:

```css
/* The least CSS that makes the Popover look like one: a card below its
   trigger, with an arrow. Menhir places the popup and writes data-side; the
   arrow sits on the edge that faces the trigger. The browser's own popover
   styles scroll its overflow, which would clip the arrow. It fades in on
   data-open and out on data-closed, growing from the trigger with
   --transform-origin. Colours are Tailwind's stone palette; light-dark()
   picks the second one in dark mode (color-scheme: dark). */
.popover-trigger,
.popover-close {
  padding: 0.375rem 0.75rem;
  border: 1px solid
    light-dark(
      oklch(86.9% 0.005 56.366) /* stone-300 */,
      oklch(44.4% 0.011 73.639) /* stone-600 */
    );
  border-radius: 0.375rem;
  background: transparent;
  color: inherit;
  font:
    0.875rem system-ui,
    sans-serif;
  cursor: pointer;
}

.popover-popup {
  --border: light-dark(
    oklch(92.3% 0.003 48.717) /* stone-200 */,
    oklch(37.4% 0.01 67.558) /* stone-700 */
  );
  --background: light-dark(white, oklch(21.6% 0.006 56.043) /* stone-900 */);
  width: 16rem;
  padding: 1rem;
  overflow: visible;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  background: var(--background);
  color: inherit;
  font:
    0.875rem system-ui,
    sans-serif;
  box-shadow: 0 4px 12px oklch(0% 0 0 / 10%);
  transform-origin: var(--transform-origin);
}

.popover-popup[data-open] {
  animation: popover-in 150ms ease-out;
}

.popover-popup[data-closed] {
  animation: popover-in 150ms ease-in reverse;
}

@keyframes popover-in {
  from {
    opacity: 0;
    scale: 0.95;
  }
}

/* A square turned 45 degrees, half of it outside the popup's edge, with
   the popup's border on the two sides that show. */
.popover-arrow {
  position: absolute;
  width: 10px;
  height: 10px;
  rotate: 45deg;
  background: var(--background);
}

.popover-arrow[data-side="bottom"] {
  top: -6px;
  border-top: 1px solid var(--border);
  border-left: 1px solid var(--border);
}

.popover-arrow[data-side="top"] {
  bottom: -6px;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}

.popover-arrow[data-side="right"] {
  left: -6px;
  border-bottom: 1px solid var(--border);
  border-left: 1px solid var(--border);
}

.popover-arrow[data-side="left"] {
  right: -6px;
  border-top: 1px solid var(--border);
  border-right: 1px solid var(--border);
}

.popover-title {
  margin: 0;
  font-size: 0.875rem;
}

.popover-description {
  margin: 0.25rem 0 0.75rem;
  color: light-dark(
    oklch(44.4% 0.011 73.639) /* stone-600 */,
    oklch(70.9% 0.01 56.259) /* stone-400 */
  );
}
```

## API reference

### Root

Holds the open state and renders no element. Every part has data-open or data-closed.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` | `boolean` | — | Whether the popover is open, when controlled. Pair it with onOpenChange. A controlled popover with no Trigger needs an anchor on the Popup. |
| `defaultOpen` | `boolean` | `false` | Whether the popover is open at first, when uncontrolled. |
| `onOpenChange` | `(open: boolean, details: { reason, event }) => void` | — | Called with the new value when the user opens or closes the popover, and why: "trigger-press", "close-press", "escape-key", "outside-press" or "close-request" (such as another popover opening). A controlled popover changes only if open does, so keeping open true refuses the close. |

### Trigger

<button type="button" aria-haspopup="dialog"> that opens and closes the popover, with aria-expanded and with popovertarget and aria-controls naming the Popup. Name it with its content. Its render function must render a <button>.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `render` | `(props) => JSX.Element` | — | Renders the trigger with your own button component. Read once. |

### Popup

<div popover="auto" role="dialog">, placed against the Trigger. Named by the Title in aria-labelledby and described by the Description in aria-describedby, while they are rendered; without a Title, give it an aria-label. Its id is generated, for the Trigger to name. The browser's own popover styles scroll its overflow: set overflow: visible to show an Arrow outside its edge.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `side` | `"top" \| "right" \| "bottom" \| "left"` | `"bottom"` | The side of the Trigger it is placed against. It moves to the other side when it does not fit, and data-side says where it went. |
| `align` | `"start" \| "center" \| "end"` | `"center"` | Where along that side it sits. start and end follow the writing direction. |
| `sideOffset` | `number` | `0` | The gap between the Trigger and the Popup, in pixels. |
| `alignOffset` | `number` | `0` | The shift along the Trigger's side, in pixels. |
| `collisionPadding` | `number` | `5` | The space it keeps from the viewport's edges, in pixels. |
| `anchor` | `Element \| VirtualElement \| (() => Element \| VirtualElement \| undefined)` | — | What it is placed against instead of the Trigger: an element, a virtual element with a getBoundingClientRect(), such as a point, or a function returning one, for an element you hold in a ref. |
| `initialFocus` | `HTMLElement \| ((focused) => HTMLElement \| null \| undefined)` | — | Where focus goes once it has opened, instead of staying on the Trigger, or going to the element inside with autofocus. A function gets the browser's choice; use one for an element you hold in a ref. |
| `returnFocus` | `HTMLElement \| ((focused) => HTMLElement \| null \| undefined)` | — | Where focus goes once it has closed with focus inside, instead of the Trigger. A function gets the element the browser focused, or null. |
| `render` | `(props) => JSX.Element` | — | Renders the popup with your own component. Read once. |

### Arrow

aria-hidden <div> that points at the center of the Trigger: Menhir sets its left or top. Position it absolutely on the Popup's edge by its data-side, which it shares with the Popup. An app that renders no Arrow does not bundle the code that places it.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `render` | `(props) => JSX.Element` | — | Renders the arrow with your own component. Read once. |

### Title

<h2> that names the Popup. Takes your id, else a generated one.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `render` | `(props) => JSX.Element` | — | Renders the title with your own component. Read once. |

### Description

<p> that describes the Popup. Takes your id, else a generated one.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `render` | `(props) => JSX.Element` | — | Renders the description with your own component. Read once. |

### Close

<button type="button"> that closes the popover, such as Done, or an icon with an aria-label. Its render function must render a <button>.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `render` | `(props) => JSX.Element` | — | Renders the close button with your own button component. Read once. |

## Keyboard

| Keys | Action |
| --- | --- |
| `Space` | On the Trigger, opens or closes the popover. |
| `Enter` | On the Trigger, opens or closes the popover. |
| `Tab` | From the Trigger, moves focus into the popover; from its last element, on to what follows the Trigger. The popover stays open. |
| `Shift + Tab` | Moves focus back, through the popover to the Trigger. |
| `Escape` | Closes the popover, the topmost one of nested popovers, and returns focus to its Trigger when it was inside. |
