Slab
Slab

Overlays

Tooltip

A short description that shows up beside an element on hover and focus.

Element
<slab-tooltip>
Built on
[role=tooltip]

Example

Here's a styled version, <slab-styled-tooltip>. 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
Slump is how far a cone of fresh concrete sags. 4 inches is typical for a slab.
HTML
<slab-styled-tooltip placement="right">
  <button class="styled-button styled-button--icon" slot="anchor" type="button" aria-label="About slump">?</button>
  <span>Slump is how far a cone of fresh concrete sags. 4 inches is typical for a slab.</span>
</slab-styled-tooltip>
Slump is how far a cone of fresh concrete sags. 4 inches is typical for a slab.
HTML
<slab-tooltip placement="right">
  <button class="styled-button styled-button--icon" slot="anchor" type="button" aria-label="About slump">?</button>
  <span>Slump is how far a cone of fresh concrete sags. 4 inches is typical for a slab.</span>
</slab-tooltip>

Usage

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

export function MyTooltip() {
  return (
    <slab-tooltip>
      <button slot="anchor">Save</button>
      <span>Saves a copy to your account</span>
    </slab-tooltip>
  );
}

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

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

@customElement('my-tooltip')
export class MyTooltip extends LitElement {
  render() {
    return html`
      <slab-tooltip>
        <button slot="anchor">Save</button>
        <span>Saves a copy to your account</span>
      </slab-tooltip>
    `;
  }
}

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

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

<template>
  <slab-tooltip>
    <button slot="anchor">Save</button>
    <span>Saves a copy to your account</span>
  </slab-tooltip>
</template>

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

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

<slab-tooltip>
  <button slot="anchor">Save</button>
  <span>Saves a copy to your account</span>
</slab-tooltip>

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

import 'slab-elements/tooltip.js';

export const MyTooltip = <template>
  <slab-tooltip>
    <button slot="anchor">Save</button>
    <span>Saves a copy to your account</span>
  </slab-tooltip>
</template>;

MyTooltip.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/tooltip.js';
</script>

<slab-tooltip>
  <button slot="anchor">Save</button>
  <span>Saves a copy to your account</span>
</slab-tooltip>

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

Keyboard

Key What it does
Tab Focusing the anchor opens the tooltip at once. Leaving it closes the tooltip.
Escape Closes the tooltip.

Behavior

A tooltip with no visual styles: [role=tooltip] content, shown in the top layer beside the element slotted as its anchor.

It opens when the anchor is hovered, after open-delay, and at once when the anchor is focused. It closes when the pointer leaves, after close-delay, when the anchor loses focus, and on Escape. The pointer can cross from the anchor onto the tooltip without it closing, so its content can be read at any zoom, which is what WCAG 1.4.13 asks of content shown on hover.

The anchor is described by the tooltip's content. ARIA references can't cross a shadow root, so the content is slotted rather than rendered: it stays in the anchor's tree, and the anchor's aria-describedby points at it. A Slab anchor, such as a Copy Button, passes that on to the element in its shadow root that has focus.

It's placed beside its anchor with CSS anchor positioning, as Popover is, 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(tooltip). Where anchor positioning isn't supported, the tooltip falls back to the browser's centering.

Style it through its tooltip part and :state(open).

API

<slab-tooltip>

Attributes
Name Type Default Description
close-delay number 200 How many milliseconds it waits after the pointer and focus leave before it closes.
disabled boolean false The tooltip doesn't open and doesn't describe its anchor.
open-delay number 300 How many milliseconds the pointer rests on the anchor before it opens.
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 at once, without waiting for close-delay.
show(): void Opens it at once, without waiting for open-delay. It doesn't open while it's disabled.
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
toggle ToggleEvent When it opens or closes, as a ToggleEvent with oldState and newState that doesn't bubble, as a popover's does.
beforetoggle ToggleEvent Before it opens or closes, as a ToggleEvent that doesn't bubble. Cancel it while it's opening to keep it closed.
Slots
Name Type Description
anchor Element The element the tooltip is for. It should be one that can be focused.
(default) Element The tooltip's content, which describes the anchor
CSS parts
Name Description
tooltip The tooltip
CSS states
Name Description
:state(disabled) While it's disabled.
:state(open) While it's open.

Copy and own it

<slab-styled-tooltip> 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-tooltip> 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.

Tooltip.jsx
import 'slab-elements/tooltip.js';
import './tooltip.css';

export function Tooltip(props) {
  return <slab-tooltip {...props} />;
}
tooltip.css
/* A closed tooltip isn't displayed, so this runs each time it opens. */
slab-tooltip::part(tooltip) {
  animation: slab-tooltip-fade 120ms ease-out;
  background-color: var(--ink);
  border: 0;
  box-shadow: 3px 3px 0 var(--ember);
  color: var(--paper);
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  line-height: 1.4;
  margin: 0.625rem;
  max-inline-size: 18rem;
  padding: 0.5rem 0.75rem;
}

@keyframes slab-tooltip-fade {
  from {
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  slab-tooltip::part(tooltip) {
    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.

tooltip.ts
import { customElement } from 'lit/decorators.js';
import Tooltip from 'slab-elements/tooltip.js';
import styles from './tooltip.styles.js';
import tokens from './tokens.styles.js';

declare global {
  interface HTMLElementTagNameMap {
    'slab-styled-tooltip': StyledTooltip;
  }
}

/**
 * Tooltip, styled: a small block of paper-on-ink monospace with an ember
 * shadow.
 */
@customElement('slab-styled-tooltip')
export default class StyledTooltip extends Tooltip {
  static override styles = [tokens, styles];
}
tooltip.styles.ts
import { css } from 'lit';

export default css`
  /* A closed tooltip isn't displayed, so this runs each time it opens. */
  [part~='tooltip'] {
    animation: fade 120ms ease-out;
    background-color: var(--ink);
    border: 0;
    box-shadow: 3px 3px 0 var(--ember);
    color: var(--paper);
    font-family: var(--font-mono);
    font-size: 0.8125rem;
    line-height: 1.4;
    margin: 0.625rem;
    max-inline-size: 18rem;
    padding: 0.5rem 0.75rem;
  }

  @keyframes fade {
    from {
      opacity: 0;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    [part~='tooltip'] {
      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-tooltip> 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.

Tooltip.vue
<script setup>
import 'slab-elements/tooltip.js';
import './tooltip.css';
</script>

<template>
  <slab-tooltip><slot /></slab-tooltip>
</template>
tooltip.css
/* A closed tooltip isn't displayed, so this runs each time it opens. */
slab-tooltip::part(tooltip) {
  animation: slab-tooltip-fade 120ms ease-out;
  background-color: var(--ink);
  border: 0;
  box-shadow: 3px 3px 0 var(--ember);
  color: var(--paper);
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  line-height: 1.4;
  margin: 0.625rem;
  max-inline-size: 18rem;
  padding: 0.5rem 0.75rem;
}

@keyframes slab-tooltip-fade {
  from {
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  slab-tooltip::part(tooltip) {
    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-tooltip> on the page. Import tokens.css, the colors and fonts every styled example shares, once, wherever your global styles go.

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

tooltip.js
import 'slab-elements/tooltip.js';
import './tooltip.css';
tooltip.css
/* A closed tooltip isn't displayed, so this runs each time it opens. */
slab-tooltip::part(tooltip) {
  animation: slab-tooltip-fade 120ms ease-out;
  background-color: var(--ink);
  border: 0;
  box-shadow: 3px 3px 0 var(--ember);
  color: var(--paper);
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  line-height: 1.4;
  margin: 0.625rem;
  max-inline-size: 18rem;
  padding: 0.5rem 0.75rem;
}

@keyframes slab-tooltip-fade {
  from {
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  slab-tooltip::part(tooltip) {
    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-tooltip> 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.

Tooltip.gjs
import 'slab-elements/tooltip.js';
import './tooltip.css';

export const Tooltip = <template>
  <slab-tooltip ...attributes>{{yield}}</slab-tooltip>
</template>;
tooltip.css
/* A closed tooltip isn't displayed, so this runs each time it opens. */
slab-tooltip::part(tooltip) {
  animation: slab-tooltip-fade 120ms ease-out;
  background-color: var(--ink);
  border: 0;
  box-shadow: 3px 3px 0 var(--ember);
  color: var(--paper);
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  line-height: 1.4;
  margin: 0.625rem;
  max-inline-size: 18rem;
  padding: 0.5rem 0.75rem;
}

@keyframes slab-tooltip-fade {
  from {
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  slab-tooltip::part(tooltip) {
    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;
  }
}