Slab
Slab

Overlays

Toast

A polite heads-up that something just happened. It lives on the page up front, so screen readers actually hear it.

Element
<slab-toast>
Built on
[role=status]

Example

Here's a styled version, <slab-styled-toast>. 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
Pour saved.
HTML
<button class="styled-button styled-button--primary" type="button">Save pour</button>

<slab-styled-toast id="toast-example">Pour saved.</slab-styled-toast>
Pour saved.
HTML
<button class="styled-button styled-button--primary" type="button">Save pour</button>

<slab-toast id="toast-example-as-it-ships">Pour saved.</slab-toast>

Usage

Install
pnpm add slab-elements lit
ReactLitVueSvelteEmberHTML
import 'slab-elements/toast.js';
import { useRef } from 'react';

export function MyToast() {
  const toast = useRef(null);

  return (
    <>
      <button onClick={() => toast.current.show()}>Save</button>
      <slab-toast duration={5000} ref={toast}>Changes saved</slab-toast>
    </>
  );
}

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

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

@customElement('my-toast')
export class MyToast extends LitElement {
  @query('slab-toast')
  private toast!: HTMLElementTagNameMap['slab-toast'];

  render() {
    return html`
      <button @click=${() => this.toast.show()}>Save</button>
      <slab-toast duration="5000">Changes saved</slab-toast>
    `;
  }
}

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

<script setup>
import 'slab-elements/toast.js';
import { useTemplateRef } from 'vue';

const toast = useTemplateRef('toast');
</script>

<template>
  <button @click="toast.show()">Save</button>
  <slab-toast ref="toast" :duration="5000">Changes saved</slab-toast>
</template>

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

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

  let toast;
</script>

<button onclick={() => toast.show()}>Save</button>
<slab-toast bind:this={toast} duration={5000}>Changes saved</slab-toast>

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

import 'slab-elements/toast.js';
import Component from '@glimmer/component';
import { on } from '@ember/modifier';
import { modifier } from 'ember-modifier';

export class MyToast extends Component {
  #toast;
  ref = modifier((element) => (this.#toast = element));
  show = () => this.#toast.show();

  <template>
    <button type="button" {{on "click" this.show}}>Save</button>
    <slab-toast duration="5000" {{this.ref}}>Changes saved</slab-toast>
  </template>
}

MyToast.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/toast.js';
</script>

<slab-toast duration="5000">Changes saved</slab-toast>

<script type="module">
  document.querySelector('slab-toast').show();
</script>

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

Keyboard

Key What it does
F6 Moves focus to the open toast shown most recently, and from inside it, back to where focus was.
Escape Dismisses the toast while focus is in it, and moves focus back to where it was.

Behavior

A toast with no visual styles: a polite [role=status] live region that lives on the page for good and is shown and dismissed rather than added and removed.

Put it on the page up front, not when there's something to say. A live region only announces what's inserted into it after screenreaders have found it, so a region added along with its content is usually read by nobody. The region inside this one is rendered, empty, as soon as the host is connected. Its content goes in the default slot, and the slot is only rendered by show(), which is the insertion screenreaders announce. show() again, with new content or the same, announces it again.

The region is a popover="manual" in the top layer, shown when the host is connected and raised again by each show(), so it sits above a dialog opened since. The top layer doesn't get it out of a modal dialog's inertness, though: everything outside the dialog is inert, and inert content isn't announced or clickable. So a toast for something that happens in a modal dialog belongs inside the dialog. While dismissed, the region stays in the accessibility tree but is hidden visually.

duration dismisses the toast that many milliseconds after it's shown. 0 keeps it until it's dismissed. The countdown pauses while the pointer is over the toast or focus is in it, and starts over when both have left, so no one loses a toast they're reading or about to act on.

A toast with something to act on, such as an Undo button or a link, is never dismissed on a timer, whatever duration says. Someone who needs longer to get to it, or is getting there by keyboard from across the page, would otherwise lose the action along with the toast (WCAG 2.2.1). Give it a way to be dismissed, or dismiss it once it's been acted on.

A toast is in the top layer, nowhere near where focus usually is, so F6 moves focus to the open toast shown most recently, and F6 from inside it moves focus back to where it was. That's how react-aria's toasts are reached. Dismissing a toast with focus in it also moves focus back.

Escape dismisses it while focus is in it.

However it's dismissed, by Escape, its timer or dismiss(), it dispatches "dismiss" first. Cancel that to keep it shown, as Alert's can be.

Style it through its part and through :state(open). Where it sits on the page, and whether several stack, is left to the styles around it.

API

<slab-toast>

Attributes
Name Type Default Description
duration number 5000 Milliseconds until it dismisses itself. 0 keeps it until it's dismissed.
Properties
Name Type Description
open Read-only boolean Whether it's shown.
Methods
Signature Description
dismiss(): void Hides it.
show(): Promise<void> Shows it.
Events
Name Type Description
dismiss Event When it's dismissed, by Escape, its timer or dismiss(). Cancel it to keep it shown.
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
(default) Element | string What's announced
CSS parts
Name Description
region The region it's shown in
CSS states
Name Description
:state(open) While it's shown.

Copy and own it

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

Toast.jsx
import 'slab-elements/toast.js';
import './toast.css';

export function Toast(props) {
  return <slab-toast {...props} />;
}
toast.css
slab-toast:state(open)::part(region) {
  animation: slab-toast-rise 200ms ease-out;
  background-color: var(--ink);
  border: 0;
  border-inline-start: 8px solid var(--ember);
  box-shadow: 4px 4px 0 var(--ember);
  color: var(--paper);
  font-family: var(--font-body);
  inset: auto 1.5rem 1.5rem auto;
  margin: 0;
  max-inline-size: min(24rem, 100% - 3rem);
  padding: 0.875rem 1.25rem;
  position: fixed;
}

@keyframes slab-toast-rise {
  from {
    opacity: 0;
    translate: 0 1rem;
  }
}

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

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

declare global {
  interface HTMLElementTagNameMap {
    'slab-styled-toast': StyledToast;
  }
}

/**
 * Toast, styled: paper on ink in the page's bottom corner, with an ember edge
 * and a hard shadow, rising into place when it's shown.
 */
@customElement('slab-styled-toast')
export default class StyledToast extends Toast {
  static override styles = [tokens, styles];
}
toast.styles.ts
import { css } from 'lit';

/*
  Only while it's open. A dismissed toast's region is hidden visually, and
  padding or a border here would give it size again.
*/
export default css`
  :host(:state(open)) [part~='region'] {
    animation: rise 200ms ease-out;
    background-color: var(--ink);
    border: 0;
    border-inline-start: 8px solid var(--ember);
    box-shadow: 4px 4px 0 var(--ember);
    color: var(--paper);
    font-family: var(--font-body);
    inset: auto 1.5rem 1.5rem auto;
    margin: 0;
    max-inline-size: min(24rem, 100% - 3rem);
    padding: 0.875rem 1.25rem;
    position: fixed;
  }

  @keyframes rise {
    from {
      opacity: 0;
      translate: 0 1rem;
    }
  }

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

Toast.vue
<script setup>
import 'slab-elements/toast.js';
import './toast.css';
</script>

<template>
  <slab-toast><slot /></slab-toast>
</template>
toast.css
slab-toast:state(open)::part(region) {
  animation: slab-toast-rise 200ms ease-out;
  background-color: var(--ink);
  border: 0;
  border-inline-start: 8px solid var(--ember);
  box-shadow: 4px 4px 0 var(--ember);
  color: var(--paper);
  font-family: var(--font-body);
  inset: auto 1.5rem 1.5rem auto;
  margin: 0;
  max-inline-size: min(24rem, 100% - 3rem);
  padding: 0.875rem 1.25rem;
  position: fixed;
}

@keyframes slab-toast-rise {
  from {
    opacity: 0;
    translate: 0 1rem;
  }
}

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

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

toast.js
import 'slab-elements/toast.js';
import './toast.css';
toast.css
slab-toast:state(open)::part(region) {
  animation: slab-toast-rise 200ms ease-out;
  background-color: var(--ink);
  border: 0;
  border-inline-start: 8px solid var(--ember);
  box-shadow: 4px 4px 0 var(--ember);
  color: var(--paper);
  font-family: var(--font-body);
  inset: auto 1.5rem 1.5rem auto;
  margin: 0;
  max-inline-size: min(24rem, 100% - 3rem);
  padding: 0.875rem 1.25rem;
  position: fixed;
}

@keyframes slab-toast-rise {
  from {
    opacity: 0;
    translate: 0 1rem;
  }
}

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

Toast.gjs
import 'slab-elements/toast.js';
import './toast.css';

export const Toast = <template>
  <slab-toast ...attributes>{{yield}}</slab-toast>
</template>;
toast.css
slab-toast:state(open)::part(region) {
  animation: slab-toast-rise 200ms ease-out;
  background-color: var(--ink);
  border: 0;
  border-inline-start: 8px solid var(--ember);
  box-shadow: 4px 4px 0 var(--ember);
  color: var(--paper);
  font-family: var(--font-body);
  inset: auto 1.5rem 1.5rem auto;
  margin: 0;
  max-inline-size: min(24rem, 100% - 3rem);
  padding: 0.875rem 1.25rem;
  position: fixed;
}

@keyframes slab-toast-rise {
  from {
    opacity: 0;
    translate: 0 1rem;
  }
}

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