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>;}
.desk { display: grid; gap: 1.4em; justify-items: center; width: min(24rem, 100%); font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step--1); line-height: 1.5; color: var(--ink);}.save { padding: 0.6em 1.1em; border: 1px solid var(--ink); border-radius: 6px; background: var(--paper); color: var(--ink); font: inherit; font-family: 'Shantell Sans', system-ui, sans-serif; cursor: pointer;}.save:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.save:active { background: color-mix(in srgb, var(--purple) 26%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.save:focus-visible,.toast-close:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px;}.stack { display: grid; align-content: start; width: 100%; min-height: 5em;}.toast { --wash: var(--purple); display: flex; gap: 0.75em; align-items: flex-start; padding: 0.85em 1em; border: 1px solid var(--ink); border-radius: 6px; background: color-mix(in srgb, var(--wash) 30%, var(--paper)); animation: arrive 140ms ease-out;}.toast[hidden] { display: none;}@keyframes arrive { from { opacity: 0; translate: 0 0.4em; }}.toast[ui-tone='success'] { --wash: var(--switch-on);}.toast[ui-tone='warning'] { --wash: var(--yellow);}.toast[ui-tone='error'] { --wash: var(--pink);}.toast-icon { display: grid; flex: none; place-items: center; width: 1.5em; height: 1.5em; border: 1px solid var(--ink); border-radius: 999px; background: var(--paper); line-height: 1;}.toast-text { flex: 1;}.toast-note { opacity: 0.8;}.toast-close { flex: none; width: 1.6em; height: 1.6em; padding: 0; border: 1px solid transparent; border-radius: 6px; background: transparent; color: var(--ink); font: inherit; line-height: 1; cursor: pointer;}.toast-close:hover { background: color-mix(in srgb, var(--wash) 55%, var(--paper));}@media (prefers-reduced-motion: reduce) { .toast { animation: none; }}
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
The region every message appears in.
toaster.rootis the live region, a<div>witharia-live="polite". Keep it on the page from the start. A screen reader can miss a region that appears together with its text.toaster.itemis one message. It needstoast, one record from the queue. It hasrole="status", orrole="alert"for theerrortone, so a screen reader interrupts for errors.toaster.itemtitleshows the message's title. Give it children and they show instead.toaster.itemdescriptionshows the description line, or its own children.toaster.itemiconshows a mark for the tone:✓,!,×ori. It isaria-hidden.toaster.itemcloseis 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
toastToastRecordRequired.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.