# Popover

> Content that pops up beside the element that opened it, in the top layer, and gets dismissed the way a native popover does.

`<slab-popover>` · Overlays · built on `[popover]`

## Install

```sh
pnpm add slab-elements lit
```

## Usage

```html
<script type="module">
  import 'slab-elements/popover.js';
</script>

<slab-popover placement="bottom">
  <button slot="anchor">Details</button>
  Anything that isn't a menu, a tooltip or a dialog.
</slab-popover>
```

## Example

Here's a styled version, `<slab-styled-popover>`. It's not in the package — it's an example of what you can build with the headless element. Copy it and make it yours at the bottom of this page.

```html
<slab-styled-popover>
  <button class="styled-button" slot="anchor" type="button">Who's on site?</button>
  Ana and Chen are setting forms. Ben brings the pump at 6:30.
</slab-styled-popover>
```

Swap `slab-styled-` for `slab-` in that markup and you've got the same example as it ships: the headless elements with no CSS at all. That's your starting point.

## Keyboard

| Key          | What it does                                                 |
| ------------ | ------------------------------------------------------------ |
| Enter, Space | On the anchor, opens or closes the popover, as a click does. |
| Escape       | Closes the popover and returns focus to the anchor.          |

## Behavior

A popover with no visual styles, around a native `[popover="auto"]`, so it opens in the top layer and is dismissed the way one is: by Escape, by a click outside it, and by another popover opening. None of that is reimplemented here.

Its anchor, the element that opens it, is whatever is slotted into `anchor`. The obvious wiring is `popovertarget` or `commandfor`, which would also give the anchor its `aria-expanded` for free. Neither can reach across a shadow root, and the anchor is outside this one while the popover is inside. So a click on the anchor calls `showPopover({ source })` or `hidePopover()` instead. `source` gives the platform part of what `popovertarget` would: it makes the anchor the popover's implicit one for positioning, and focus goes back to it on close. The rest is done here, and is the reason this component is more than a slot: `aria-expanded` on the anchor, focus going back to it from slotted content (which the platform doesn't count as inside the popover), and a second click on the anchor closing the popover rather than light dismiss closing it and the click opening it again.

`open` reflects whether the popover is open, however it got there or went away, and setting it opens or closes the popover. "beforetoggle" and "toggle" are the popover's own, dispatched again from the host because neither leaves the shadow root. Cancelling "beforetoggle" cancels the opening, as it does on a native popover.

Positioning is the one place it carries CSS beyond the bare minimum. Left to the browser, a popover is centered in the viewport, not by its anchor, which isn't a look so much as not working. So it's put beside its anchor with CSS anchor positioning and flipped when it would leave the screen. `placement` picks the side and nothing else: the gap, the alignment, and every other visual choice are left to `::part(popover)`. Where anchor positioning isn't supported, the popover falls back to the browser's centering.

## API

### `<slab-popover>`

#### Attributes

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `open` | `boolean` | `false` |  |
| `placement` | `'bottom' \| 'left' \| 'right' \| 'top'` | `'bottom'` | The side of its anchor it opens on, when there's room. It flips when there isn't. |
| `version` | `string` |  |  |

#### Methods

| Signature | Description |
| --- | --- |
| `hide(): void` |  |
| `show(): void` |  |
| `toggle(): void` |  |

#### Events

| Name | Type | Description |
| --- | --- | --- |
| `beforetoggle` | `ToggleEvent` |  |
| `toggle` | `ToggleEvent` |  |

#### Slots

| Name | Type | Description |
| --- | --- | --- |
| `anchor` | `Element` |  |
| `(default)` | `Element \| string` |  |

#### CSS parts

| Name | Description |
| --- | --- |
| `popover` | The popover's content |

## Copy and own it

`<slab-styled-popover>` isn't in the package. It's an example — copy it into your project and change whatever you want. Under the hood, it's just the headless element, subclassed, with `styles` set. That's it. Keyboard, focus, ARIA, forms, and events all come from the headless element, and the styles only reach in through `::part()` and `:state()`, same as yours would.

To use it, copy these files, swap the tag in `@customElement()` for one of your own, and import it. `tokens.styles.ts` holds the colors and fonts every styled example shares, so you only need it once.

### TypeScript

These files are TypeScript, and `@customElement()` is one of Lit's decorators (TypeScript's experimental flavor). You'll need two options in your tsconfig, per [Lit's docs](https://lit.dev/docs/components/decorators/#decorators-typescript): `experimentalDecorators` to compile them, and `useDefineForClassFields` turned off so class fields don't stomp on the accessors Lit sets up.

```json
{
  "compilerOptions": {
    "experimentalDecorators": true,
    "useDefineForClassFields": false
  }
}
```

Using plain JavaScript? Drop the types and register the class with `customElements.define()` instead of the decorator.

### popover.ts

```ts
import { customElement } from 'lit/decorators.js';
import Popover from 'slab-elements/popover.js';
import styles from './popover.styles.js';
import tokens from './tokens.styles.js';

declare global {
  interface HTMLElementTagNameMap {
    'slab-styled-popover': StyledPopover;
  }
}

/**
 * Popover, styled: a paper panel with an ink border and a hard shadow, as
 * Menu's is.
 */
@customElement('slab-styled-popover')
export default class StyledPopover extends Popover {
  static override styles = [tokens, styles];
}
```

### popover.styles.ts

```ts
import { css } from 'lit';

/* A closed popover isn't displayed, so the entrance runs each time it opens. */
export default css`
  [part~='popover'] {
    animation: drop 150ms ease-out;
    background-color: var(--paper);
    border: var(--border);
    box-shadow: var(--shadow);
    color: var(--ink);
    font-family: var(--font-body);
    line-height: 1.5;
    margin: 0.5rem;
    max-inline-size: 20rem;
    padding: 1rem 1.25rem;
  }

  @keyframes drop {
    from {
      opacity: 0;
      translate: 0 -0.375rem;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    [part~='popover'] {
      animation: none;
    }
  }
`;
```

### tokens.styles.ts

```ts
import { css } from 'lit';

/*
  The styled components' palette and type, taken from the logo: ink for the
  courses, ember for the base, paper behind them.

  Each is a short alias for a "--slab-styled-*" property, with the default as
  its fallback. Retheming is "--slab-styled-ember" and the rest on ":root",
  which every shadow root inherits. Were the defaults declared under those
  names here instead, each host's own declaration would shadow the page's.

  Each default is "light-dark()", so the components follow the page's
  "color-scheme": "dark" for dark, "light" for light, and "light dark" to
  follow the operating system. Ink and paper trade places in the dark, so
  ink is always the text and paper always what's behind it. Contrast is given
  as light, then dark.
*/
export default css`
  :host {
    /* Secondary text. 6.8:1 and 6.9:1 on paper. */
    --ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
    --border: 2px solid var(--ink);

    /* Behind disabled and read-only controls, and hovered rows. */
    --chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));

    /*
      The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
      enough for borders, fills and large text, but not always for small text,
      which takes "--ember-deep".
    */
    --ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));

    /*
      Orange for small text on paper, and for paper text on it. 5.7:1 and
      7.4:1.
    */
    --ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
    --font-body: var(
      --slab-styled-font-body,
      'IBM Plex Sans',
      system-ui,
      sans-serif
    );
    --font-display: var(
      --slab-styled-font-display,
      'Zilla Slab',
      rockwell,
      'Roboto Slab',
      serif
    );
    --font-mono: var(
      --slab-styled-font-mono,
      'IBM Plex Mono',
      ui-monospace,
      monospace
    );
    --ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
    --paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));

    /* The hard, offset shadow a raised surface casts. */
    --shadow: 4px 4px 0 var(--ink);
  }
`;
```
