Toast
A toast queue with a provider, a hook and a stack.
Demo
Usage
import { Slant, Toast, ToastProvider, Toaster, useToast } from '@tuttn/slant'
// Once, above the app and inside its <Slant> root, whose live regions
// announce each toast.
<Slant theme="sage">
<ToastProvider lifetime={4000} max={3}>
<App />
<Toaster position="fixed" placement="bottom-end" />
</ToastProvider>
</Slant>
// Anywhere below.
const { push, dismiss, pause, resume } = useToast()
const id = push({ tone: 'success', title: 'Checkpoint saved', body: 'TASK-24 is ready to hand off.' })
push({ tone: 'danger', title: 'Upload failed', lifetime: 0 }) // stays until dismissed
dismiss(id)
// An action: pressing it calls onAction and dismisses the toast. The toast
// stays twice the provider's lifetime unless it sets its own, and is announced
// as "Message deleted. Undo" (altText, when given, replaces the label there).
push({ title: 'Message deleted', action: { label: 'Undo', onAction: restore } })
// A standalone Toast takes the same action; pressing it calls onAction, then onDismiss.
<Toast title="Message deleted" action={{ label: 'Undo', onAction: restore }} onDismiss={hide} />
// The Toaster holds the timers while it is hovered or focused; hold them
// yourself while something else needs the reader, one resume() per pause().
pause()
resume()Props
ToastProvider
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | — | The app, a <Toaster> among it. |
lifetime | number | 4000 | Milliseconds before a toast dismisses itself; 0 keeps toasts until dismissed. |
max | number | 3 | Older toasts drop off the queue beyond this many. |
Toaster
| Prop | Type | Default | Description |
|---|---|---|---|
position | 'inline' | 'fixed' | 'inline' | `inline` stacks the toasts in place; `fixed` pins them to a corner of the viewport, above everything but tooltips. |
placement | 'top-start' | 'top-end' | 'bottom-start' | 'bottom-end' | 'bottom-end' | The corner a fixed Toaster sits in. `start` and `end` follow the text direction: `bottom-end` is the bottom right of a left-to-right page. Ignored inline. |
label | string | messages.notifications ('Notifications') | The region's accessible name. |
empty | ReactNode | — | Shown while the queue is empty. |
Toast
| Prop | Type | Default | Description |
|---|---|---|---|
tone | Tone | 'neutral' | The pixel's colour. |
title | ReactNode | — | The headline. |
body | ReactNode | — | A line or two under the title. |
onDismiss | () => void | — | Called by the dismiss button, which shows only when this is given. |
dismissLabel | string | messages.dismiss ('Dismiss') | The dismiss button's accessible name. A Toaster names it after the toast. |
lifetime | number | — | Milliseconds the timer bar takes to run out; omitted or 0 hides the bar. |
paused | boolean | — | Freezes the timer bar where it is. A Toaster sets it while its provider's timers are paused. |
action | { label: string; onAction: () => void; altText?: string } | — | One thing to do about the news, such as Undo: a small button after the body, named by `label`. Pressing it calls `onAction`, then `onDismiss`. `altText` is what a provider's announcement says for it; a toast you show yourself is not announced. |
useToast() | { toasts, push, dismiss, clear, pause, resume, paused } | — | The queue, newest first. push returns the new toast’s id; dismiss and clear remove toasts; pause holds every timer and resume lets it go. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, other attributes and the ref go on the Toaster’s region and on each Toast’s element. |
Keyboard
| Tab | Moves through each toast’s action and dismiss button, newest toast first. While focus is inside the Toaster, every timer holds. |
| Enter / Space | On the action: runs it and dismisses the toast. On the dismiss button: dismisses the toast. |
Accessibility
- Each pushed toast is announced once through the root’s live regions: its title, its body, then its action’s
altTextorlabel. Danger toasts are assertive, the rest polite. - The action is a plain button named by its
label; the dismiss button is named after the toast (messages.dismissNamed). - Timers hold while the pointer is over the Toaster or focus is inside it (WCAG 2.2.1), and a toast with an action waits twice as long. Still, never make the action the only way to do something.
The queue
The queue is the provider’s own state: push to it and dismiss from it, and the Toaster renders whatever it holds. A toast leaves after its lifetime, after max newer ones push it off the end, or when its dismiss button (named after the toast, from messages.dismissNamed) is pressed.
Timers
While the pointer is over the Toaster or focus is inside it, every timer holds, and the timer bars with them; once both have gone, each toast runs on with the time it had left, so nobody loses a toast they are reading or reaching for. pause() and resume() hold the timers from your own code, one resume() for each pause().
Announcements
Each pushed toast is announced once through the persistent live regions of the nearest <Slant> root: a danger toast assertively, every other tone politely. Toasts carry no live role themselves, which would read them twice. A standalone Toast you show yourself is not announced; give it role="status" or role="alert" if it should be.
Actions
A toast can carry one action, { label, onAction, altText? }: a small button after the body, before the dismiss button. Pressing it (a click, or Enter or Space once Tab reaches it) calls onAction and dismisses the toast. A toast with an action stays twice the provider’s lifetime unless it sets its own, and like the rest of the toast its timer holds while the pointer is on it or focus is in it.
The announcement ends with the action, so a screen reader hears “Message deleted. Undo”. Pass altText when the label alone wouldn’t make sense read after the toast, or to name another way to do it; the button itself stays named by label. A toast can leave before the reader reaches it, and a touch or screen reader user may never get there, so the action must never be the only way to do the thing: keep Undo in the app (a menu item, ⌘Z) as well.
Placement
A fixed Toaster sits at --sl-z-toast, in the corner placement names. The corner is logical: bottom-end is the bottom right of a left-to-right page and the bottom left of a right-to-left one.