Slab

Overlays

Tooltip

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

Element
<slab-tooltip>
Built on
[role=tooltip]
As Markdown
tooltip.md

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

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.

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
disabled boolean false The tooltip doesn't open and doesn't describe its anchor.
open-delay number 300
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
Events
Name Type Description
toggle Event
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

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

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);
  }
`;