Slab
Slab

Overlays

Popover

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

Element
<slab-popover>
Built on
[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. Switch to "As it ships" to see the same markup with the headless element and no CSS at all. That's your starting point.

Styled example As it ships
Ana and Chen are setting forms. Ben brings the pump at 6:30.
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>
Ana and Chen are setting forms. Ben brings the pump at 6:30.
HTML
<slab-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-popover>

Usage

Install
pnpm add slab-elements lit
ReactLitVueSvelteEmberHTML
import 'slab-elements/popover.js';

export function MyPopover() {
  return (
    <slab-popover placement="bottom">
      <button slot="anchor">Details</button>
      Anything that isn't a menu, a tooltip or a dialog.
    </slab-popover>
  );
}

MyPopover.jsx React 19 or later, which sets props on custom elements as properties.

import 'slab-elements/popover.js';
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';

@customElement('my-popover')
export class MyPopover extends LitElement {
  render() {
    return html`
      <slab-popover placement="bottom">
        <button slot="anchor">Details</button>
        Anything that isn't a menu, a tooltip or a dialog.
      </slab-popover>
    `;
  }
}

my-popover.ts Slab is built on Lit, so its elements are at home in Lit's templates.

<script setup>
import 'slab-elements/popover.js';
</script>

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

MyPopover.vue Let Vue's compiler know slab- tags are custom elements with isCustomElement.

<script>
  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>

MyPopover.svelte Svelte binds properties and events to custom elements like any other element.

import 'slab-elements/popover.js';

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

MyPopover.gjs Ember sets attributes, and a dynamic value like {{this.max}} sets the property when the element has one. {{on}} listens for any event by its name.

<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>

index.html Any bundler (or an import map) takes care of the imports.

Give it a label when it holds something you work in, such as a form or a few controls, rather than a few words about its anchor. It’s then a non-modal dialog named by the label, and the anchor tells screenreaders it opens one with aria-haspopup="dialog".

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. A Slab anchor, such as a Copy Button, passes aria-expanded on to its own button.

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.

Give it a label when what it holds is a dialog, such as a form or a set of controls, rather than a few words about its anchor. The popover is then a non-modal role="dialog" named by the label, the way react-aria's is, and its anchor gets aria-haspopup="dialog". Without one, it's a plain container, read as content that follows its anchor.

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
label string Makes the popover a dialog named by it. Leave it unset when the popover only holds a few words about its anchor.
open boolean false Whether it's open. Set it to open or close it.
placement 'bottom' | 'left' | 'right' | 'top' 'bottom' The side of its anchor it opens on, when there's room. It flips when there isn't.
Methods
Signature Description
hide(): void Closes it.
show(): void Opens it.
toggle(force?: boolean): boolean Opens it when it's closed, and closes it when it's open, as a native popover's togglePopover() does. true only opens it and false only closes it. Returns whether it's open.
Events
Name Type Description
beforetoggle ToggleEvent Before it opens or closes, as a ToggleEvent that doesn't bubble. Cancel it while it's opening to keep it closed.
toggle ToggleEvent When it opens or closes, as a ToggleEvent with oldState and newState that doesn't bubble, as a popover's does.
Slots
Name Type Description
anchor Element What opens it, such as a button
(default) Element | string
CSS parts
Name Description
popover The popover's content
CSS states
Name Description
:state(open) While it's open.

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. 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.

ReactLitVueSvelteEmber

The same styles, written from outside the element, so there's nothing to subclass. The CSS styles every <slab-popover> on the page. Import tokens.css, the colors and fonts every styled example shares, once, wherever your global styles go.

Each component registers its headless element and imports its CSS. Use it like the element: its props are the element's attributes and properties.

Popover.jsx
import 'slab-elements/popover.js';
import './popover.css';

export function Popover(props) {
  return <slab-popover {...props} />;
}
popover.css
slab-popover::part(popover) {
  animation: slab-popover-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 slab-popover-drop {
  from {
    opacity: 0;
    translate: 0 -0.375rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  slab-popover::part(popover) {
    animation: none;
  }
}
tokens.css
:root {
  /* 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);
}

/*
  Forced colors, such as Windows' contrast themes, replace every color that
  isn't a system color, so ember fills and paper marks all become the same
  background, and a checked Checkbox looks like an unchecked one. The palette
  becomes the user's system colors instead, which are kept. Ember is the
  highlight, the color of what's selected, and ember-deep, being for text and
  for paper text on it, is the text color.
*/
@media (forced-colors: active) {
  :host {
    --ash: GrayText;
    --chalk: Canvas;
    --ember: Highlight;
    --ember-deep: CanvasText;
    --ink: CanvasText;
    --paper: Canvas;
  }
}

Under the hood, it's just the headless element, subclassed, with styles set. That's it. 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.

popover.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
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
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);
  }

  /*
    Forced colors, such as Windows' contrast themes, replace every color that
    isn't a system color, so ember fills and paper marks all become the same
    background, and a checked Checkbox looks like an unchecked one. The palette
    becomes the user's system colors instead, which are kept. Ember is the
    highlight, the color of what's selected, and ember-deep, being for text and
    for paper text on it, is the text color.
  */
  @media (forced-colors: active) {
    :host {
      --ash: GrayText;
      --chalk: Canvas;
      --ember: Highlight;
      --ember-deep: CanvasText;
      --ink: CanvasText;
      --paper: Canvas;
    }
  }
`;

The same styles, written from outside the element, so there's nothing to subclass. The CSS styles every <slab-popover> on the page. Import tokens.css, the colors and fonts every styled example shares, once, wherever your global styles go.

Each component registers its headless element and imports its CSS. Use it like the element: its props are the element's attributes and properties.

Popover.vue
<script setup>
import 'slab-elements/popover.js';
import './popover.css';
</script>

<template>
  <slab-popover><slot /></slab-popover>
</template>
popover.css
slab-popover::part(popover) {
  animation: slab-popover-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 slab-popover-drop {
  from {
    opacity: 0;
    translate: 0 -0.375rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  slab-popover::part(popover) {
    animation: none;
  }
}
tokens.css
:root {
  /* 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);
}

/*
  Forced colors, such as Windows' contrast themes, replace every color that
  isn't a system color, so ember fills and paper marks all become the same
  background, and a checked Checkbox looks like an unchecked one. The palette
  becomes the user's system colors instead, which are kept. Ember is the
  highlight, the color of what's selected, and ember-deep, being for text and
  for paper text on it, is the text color.
*/
@media (forced-colors: active) {
  :host {
    --ash: GrayText;
    --chalk: Canvas;
    --ember: Highlight;
    --ember-deep: CanvasText;
    --ink: CanvasText;
    --paper: Canvas;
  }
}

The same styles, written from outside the element, so there's nothing to subclass. The CSS styles every <slab-popover> on the page. Import tokens.css, the colors and fonts every styled example shares, once, wherever your global styles go.

Import popover.js once, such as in your root layout, and use <slab-popover> as is. A Svelte component around it would take slot="…" content as its own slot and drop it.

popover.js
import 'slab-elements/popover.js';
import './popover.css';
popover.css
slab-popover::part(popover) {
  animation: slab-popover-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 slab-popover-drop {
  from {
    opacity: 0;
    translate: 0 -0.375rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  slab-popover::part(popover) {
    animation: none;
  }
}
tokens.css
:root {
  /* 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);
}

/*
  Forced colors, such as Windows' contrast themes, replace every color that
  isn't a system color, so ember fills and paper marks all become the same
  background, and a checked Checkbox looks like an unchecked one. The palette
  becomes the user's system colors instead, which are kept. Ember is the
  highlight, the color of what's selected, and ember-deep, being for text and
  for paper text on it, is the text color.
*/
@media (forced-colors: active) {
  :host {
    --ash: GrayText;
    --chalk: Canvas;
    --ember: Highlight;
    --ember-deep: CanvasText;
    --ink: CanvasText;
    --paper: Canvas;
  }
}

The same styles, written from outside the element, so there's nothing to subclass. The CSS styles every <slab-popover> on the page. Import tokens.css, the colors and fonts every styled example shares, once, wherever your global styles go.

Each component registers its headless element and imports its CSS. Use it like the element: its attributes and its content go to the element.

Popover.gjs
import 'slab-elements/popover.js';
import './popover.css';

export const Popover = <template>
  <slab-popover ...attributes>{{yield}}</slab-popover>
</template>;
popover.css
slab-popover::part(popover) {
  animation: slab-popover-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 slab-popover-drop {
  from {
    opacity: 0;
    translate: 0 -0.375rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  slab-popover::part(popover) {
    animation: none;
  }
}
tokens.css
:root {
  /* 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);
}

/*
  Forced colors, such as Windows' contrast themes, replace every color that
  isn't a system color, so ember fills and paper marks all become the same
  background, and a checked Checkbox looks like an unchecked one. The palette
  becomes the user's system colors instead, which are kept. Ember is the
  highlight, the color of what's selected, and ember-deep, being for text and
  for paper text on it, is the text color.
*/
@media (forced-colors: active) {
  :host {
    --ash: GrayText;
    --chalk: Canvas;
    --ember: Highlight;
    --ember-deep: CanvasText;
    --ink: CanvasText;
    --paper: Canvas;
  }
}