Skip to content

Toast

A stack of transient messages in a live region. A toast closes itself after its timeout, closes from its Dismiss button, and pauses its timer while the pointer or focus is on it.

Decision

When Use
Confirming an action or reporting a problem without moving the page Toast
A result that belongs beside a button Status Text
A message that stays Callout

Usage

import { createToaster } from './configurationpage?name=myplugin_stolon.js';

var toaster = createToaster({ timeout: 5000 });
toaster.success('Saved.');
toaster.error('Could not connect.', 0);

Classes: stolon-toaster, stolon-toaster-bottom-end, stolon-toaster-bottom-start, stolon-toaster-top-end, stolon-toaster-top-start, stolon-toast, stolon-toast-close. The handle is { element, show, info, success, warn, error, clear, destroy }. show({ message, kind, title, timeout }) returns { element, dismiss }, and each kind method takes (message, timeout).

Parts: stolon-toaster-live, stolon-toast-body, stolon-toast-title.

States and Modifiers

  • A toast is info, success, warn, or error. The first three have role="status", and an error has role="alert".
  • The stack holds two stolon-toaster-live regions, a polite one with role="status" and an assertive one with role="alert" for errors.
  • position is bottom-end (default), bottom-start, top-end, or top-start. parent is the element that holds the stack, the document body by default.
  • timeout is 5000 ms by default, and 0 keeps the toast until dismissed. An error toast stays until dismissed unless a timeout is set. max is 5, and the oldest closes first.
  • label ("Notifications") names the stack and dismissLabel ("Dismiss") names the button. onDismiss(spec) runs when a toast closes.
  • While a dialog is open and no parent is given, the stack moves into the dialog on top, so the modal does not hide it from a screen reader.

Gotchas

  • The stack is fixed to a corner, so pass parent to keep it inside a page or a preview.

Reference

Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.

createToaster

function createToaster(options?: ToasterOptions): ToasterHandle

A stack of transient messages in persistent live regions. A toast closes itself after the timeout, closes from its Dismiss button, and pauses its timer while the pointer or focus is on it. Info, success, and warn toasts join a polite region, and an error toast joins an assertive one, so a screen reader announces each as it arrives. An error toast stays until dismissed unless a timeout is set.

Option Type What it does
parent HTMLElement | null element that holds the stack (default the document body, or the open dialog while one is open, so a modal does not hide the toast from a screen reader)
position 'bottom-end' | 'bottom-start' | 'top-end' | 'top-start' | string 'bottom-end', 'bottom-start', 'top-end', or 'top-start' (default 'bottom-end')
timeout number milliseconds before a toast closes, and 0 keeps it until dismissed (default 5000, and 0 for an error toast unless set here or on the toast)
max number most toasts shown at once, where the oldest closes first (default 5)
label string aria-label of the stack (default 'Notifications')
dismissLabel string title and aria-label of the dismiss button (default 'Dismiss')
onDismiss (spec: ToastSpec): void function(toastOptions) called when a toast closes [optional]
Handle member Type
show (spec: ToastSpec | string): Toast
info (message: string, timeout?: number): Toast
success (message: string, timeout?: number): Toast
warn (message: string, timeout?: number): Toast
error (message: string, timeout?: number): Toast
clear (): void
destroy (): void
Type What it is
ToastKind The tone of a toast. An error toast joins the assertive live region, and the rest the polite one.
ToastSpec What show takes for one toast. A plain string is the message.
Toast One shown toast, as show returns it.
Class What it looks like
stolon-toaster Fixed stack of toasts in a corner of the screen, which lets clicks through around the toasts
stolon-toaster-live Persistent live region inside the stack that holds the toasts of one urgency
stolon-toast One message on the stolon-float surface, built as stolon-icon-text with an icon, text, and a dismiss button, bordered in the color of its kind
stolon-toast-close The dismiss button, pinned to the toast's top right corner and inset by its padding

Example

Example Code

<!-- title: Toast -->
<!-- min-height: 300 -->
<div id="mount"></div>
<script type="module">
const base = await window.__baseReady;
const view = document.getElementById('view');
const mount = document.getElementById('mount');
const toaster = base.createToaster({ parent: view, timeout: 4000 });
['info', 'success', 'warn', 'error'].forEach((kind) => {
    const button = document.createElement('button');
    button.type = 'button';
    button.className = 'raised stolon-button stolon-small';
    button.style.marginRight = '8px';
    button.textContent = kind;
    button.addEventListener('click', () => toaster[kind]('This is a ' + kind + ' message.'));
    mount.appendChild(button);
});
</script>