toaster

toaster shows short messages, such as "Draft saved", and screen readers announce each new one. The family holds the queue of messages. You write the elements for each message.

import { computed } from '@markless/core';import { toaster } from '@markless/ui';export default function ToasterBasic() @{	const message = 'Draft saved';	const tone = 'success';	const toasts = toaster.state();	const none: toaster.ToastRecord = {		id: 'save',		title: '',		description: '',		tone: 'neutral',		icon: '',		dueAt: 0,		remaining: 0,	};	const front = computed(() => toasts.queue[0] ?? none);	const quiet = computed(() => toasts.queue.length === 0);	<div class="desk">		<button			class="save"			type="button"			onClick={() => {				toasts.queue = toaster.say(toasts.queue, message, {					id: 'save',					tone,					description: 'You can close this tab.',				});			}}		>Save draft</button>		<toaster.root class="stack">			<toaster.item class="toast" toast={front} hidden={quiet}>				<toaster.itemicon class="toast-icon" />				<div class="toast-text">					<toaster.itemtitle class="toast-title" />					<toaster.itemdescription class="toast-note" />				</div>				<toaster.itemclose class="toast-close" aria-label="Dismiss">×</toaster.itemclose>			</toaster.item>		</toaster.root>	</div>;}
import { toaster } from '@markless/ui';

export default function Save() @{
	const toasts = toaster.state();

	<section class="frame">
		<button
			class="save"
			type="button"
			onClick={() => {
				toasts.queue = toaster.say(toasts.queue, 'Draft saved', {
					id: 'save',
					tone: 'success',
					description: 'You can close this tab.',
				});
			}}
		>Save draft</button>
		<toaster.root class="region">
			<div role="presentation">
				@for (const message of toasts.queue; key message.id) {
					<toaster.item class="toast" toast={message}>
						<toaster.itemicon class="icon" />
						<div>
							<toaster.itemtitle class="title" />
							<toaster.itemdescription />
						</div>
						<toaster.itemclose class="close" aria-label="Dismiss">×</toaster.itemclose>
					</toaster.item>
				}
			</div>
		</toaster.root>
	</section>
}

If a person clicks Save draft twice, they still see one message. Both calls use id: 'save', so the second call updates the first message in place. Focus stays on the button. A message never takes focus.

How a message gets on screen

toaster.state() returns the page's one queue. Every component on the page that calls it gets the same queue. So a button far from the region can still raise a message.

To raise a message, set the queue to what toaster.say() returns:

toasts.queue = toaster.say(toasts.queue, 'Upload failed', {
	tone: 'error',
	description: 'The file was too large.',
});

toaster.say(queue, title, options) takes these options: id, tone, description and duration. tone is neutral, success, warning or error. To remove a message from your own code, write toasts.queue = toaster.drop(toasts.queue, id).

toaster.root renders no messages of its own. Your @for decides which ones show. To show at most three, put toaster.shown() in a computed() and loop over that:

import { computed } from '@markless/core';

const shown = computed(() => toaster.shown(toasts.queue, 3));

Do not write the call inside the @for line. That form breaks when the page renders on a server. The other messages wait in the queue, and dismissing one brings the next forward.

When to use it

Use toaster for a short status that does not need an answer, such as "Saved" or "Copied". If the person has to decide something, use a modal instead.

Parts

toaster anatomy
Anatomy perspectiveFlat
Layers
✓Changes savedYou are good to go.×Your next idea is waiting.
toaster.root

The region every message appears in.

  • toaster.root is the live region, a <div> with aria-live="polite". Keep it on the page from the start. A screen reader can miss a region that appears together with its text.
  • toaster.item is one message. It needs toast, one record from the queue. It has role="status", or role="alert" for the error tone, so a screen reader interrupts for errors.
  • toaster.itemtitle shows the message's title. Give it children and they show instead.
  • toaster.itemdescription shows the description line, or its own children.
  • toaster.itemicon shows a mark for the tone: ✓, !, × or i. It is aria-hidden.
  • toaster.itemclose is a button that dismisses the message it sits in. With no children, its name is a hidden word, "Close".

Styling hooks

Attribute On Present when
ui-toaster root Always.
ui-paused root The pointer rests on the region, or focus is inside it.
ui-toast item Always.
ui-tone item Always. Its value is the tone, such as ui-tone="error".
ui-front item This message is first in the queue.

Each toaster.item also gets two CSS custom properties in its style attribute. --index is its place in the queue, from 0. --offset is that place times 100%. Use them to stack the messages. The family owns that style attribute, so style items from your stylesheet, not with a style prop.

A modal does not hide the region. The messages behind an open dialog stay reachable.

Gotchas

API reference

toaster.root

No props of its own.

toaster.item

toastToastRecord
Required. The message this row shows.

toaster.itemclose

No props of its own.

toaster.itemdescription

No props of its own.

toaster.itemicon

No props of its own.

toaster.itemtitle

No props of its own.