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 timerConfig
Section titled “Config”| Option | Type | Default |
|---|---|---|
defaultDuration | number (ms) | 3000 |
maxToasts | number | 5 |
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.
Actions
Section titled “Actions”| Member | Signature | Notes |
|---|---|---|
present(message, type?, duration?) | (string, ToastType?, number?) => number | Returns 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() | () => void | Clears every pending timer too. |
No queries bucket; read toasts.state.toasts directly.
Prop-getters & cleanup
Section titled “Prop-getters & cleanup”| Getter | Returns |
|---|---|
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).
Live example
Section titled “Live example”Next steps
Section titled “Next steps”Alert — a single-item, non-queued relative for inline (not floating) status messages.