Skip to content

Headless Core — Toast

Powers Toast internally. Unlike the other factories, this one manages a list of items (the toast queue) rather than a single open/closed boolean.

import { createToastManager } from '@andersseen/headless-components/toast';
const toasts = createToastManager({
defaultDuration: 3000,
position: 'bottom-right',
onToastsChange: list => console.log('Toasts:', list),
});
const id = toasts.actions.present('Hello!', 'success');
toasts.actions.dismiss(id);
toasts.destroy(); // clears every pending auto-dismiss timer
OptionTypeDefault
defaultDurationnumber (ms)3000
maxToastsnumber5
position'top-right' | 'top-left' | 'bottom-right' | 'bottom-left' | 'top-center' | 'bottom-center''bottom-right'
onToastsChange(toasts: ToastItem[]) => void

ToastItem is { id: number, message: string, type: ToastType, duration: number }, where ToastType is 'default' \| 'success' \| 'error' \| 'info' \| 'warning'.

toasts: ReadonlyArray<ToastItem>, position.

MemberSignatureNotes
present(message, type?, duration?)(string, ToastType?, number?) => numberReturns the new toast’s id. Over maxToasts, the oldest toasts are dropped (and their timers cleared) to make room.
dismiss(id)(id: number) => void
dismissAll()() => voidClears every pending timer too.

No queries bucket; read toasts.state.toasts directly.

GetterReturns
getContainerProps()role: 'region', aria-label: 'Notifications', aria-live: 'polite', aria-atomic: false, data-position
getToastProps(toast)role: 'alert', aria-live: 'assertive', aria-atomic: true, data-type
getDismissProps()aria-label: 'Dismiss notification', type: 'button'

The container uses aria-live="polite" (announcements queue politely) while each individual toast is role="alert" with aria-live="assertive" — that combination is deliberate: the region doesn’t interrupt, but each toast inside it does, matching how screen readers actually handle nested live regions. destroy() clears every pending auto-dismiss timer; call it when the manager itself is torn down (not per-toast — dismiss(id) already clears that one timer).

Show toast Dismiss all

Alert — a single-item, non-queued relative for inline (not floating) status messages.