Toast
FreeImperative toast stack with 4 variants, 6 anchor positions, action callbacks and an auto-dismiss timer.
</>sv-toast-host toastnotificationfeedback
import { SvToastHostComponent } from '@styloviz/toast';Demo & usage
Basic usage
Usage example — open the gallery for the full interactive demo.
An interactive demo for this component lives in the gallery.
Open live demoMore examples
With an action
example.tsTypeScript
9 lines
// An Undo instead of a confirm prompt. `action` takes a label and a // callback; pressing it runs the callback and dismisses the toast. const id = this.toast.info('Invoice INV-2041 archived', { duration: 8000, action: { label: 'Undo', callback: () => this.restore('INV-2041') }, }); // The returned id dismisses it early — e.g. if the user undid it elsewhere. this.toast.dismiss(id);
One toast for an async action
example.tsTypeScript
8 lines
// A spinner and "Saving…" while the work runs — it cannot time out or be // closed until the work settles — then the SAME toast becomes the outcome, // announced again. It returns the work's own promise. await this.toast.promise(this.api.save(rows), { loading: 'Saving…', success: (saved) => `Saved ${saved.length} rows`, error: (e) => `Could not save: ${describe(e)}`, });
A retry loop that fails eleven times
example.tsTypeScript
18 lines
// Deduplication is on by default. Eleven identical failures are one // toast with a x11 badge and a countdown that restarts from the last // one - not eleven toasts that evict every other message in a stack // bounded at five. for (const attempt of attempts) { try { await attempt(); } catch { this.toast.error('Sync failed'); } } // An explicit key deduplicates on the SUBJECT rather than the wording, // so these two replace each other instead of stacking. this.toast.info('Saving...', { dedupeKey: 'record-7', duration: 0 }); await this.save(); this.toast.success('Saved', { dedupeKey: 'record-7' }); // And opt a single call out when two identical sentences are genuinely // two events the user must see separately. this.toast.info('Row added', { dedupeKey: null });
A toast that is about the PAGE, not the action
example.tsTypeScript
13 lines
// The default is scope: 'global', and it is the right default: the // commonest toast in any app is raised BY the action that navigates, // and it has to survive that navigation to be read at all. await this.save(); this.toast.success('Invoice saved'); // survives this.router.navigate(['/invoices']); // A message about the view the user is looking at is stale the moment // they leave it. Say so, and provideSvOverlayRouteClose() drops it on // the next navigation. this.toast.warning('3 filters could not be applied to this view', { scope: 'route', });
When to use
Confirming a completed action transiently without interrupting the flow. Non-blocking feedback: "Saved", "Copied", "Email sent". For persistent status use an Alert Banner; for decisions, a Confirm Dialog. Not sure this is the right outcome surface? Compare toast, banner, result, empty state, notification center and modal by severity, persistence and whether the user can correct it in the “Choosing a feedback pattern” guide under Guides.
Async states
None — this component has nothing to load, so it has no loading, empty or error state to drive.
Inputs
| Name | Type | Default | Description |
|---|---|---|---|
| position | ToastPosition | 'top-right' | Where the stack is anchored on screen. |
| maxVisible | number | 5 | Maximum number of toasts visible simultaneously. |
| portal | boolean | false | Move the region to `<body>` so it stays usable above a modal. `inert` is inherited and cannot be undone by a descendant. A modal makes the page behind it inert, and a host mounted inside the app shell is part of that page: its toasts are still announced (speech goes through the shared announcer) but an "Undo" button on one cannot be pressed until the dialog closes. Portalled, the region is a `<body>` child the stack keeps live at any depth. Off by default because it moves the region's DOM (and its `data-sv-toast-*` hooks) out of the host. |
Customization & theming
Six anchor positions and four variants. Action buttons and auto-dismiss duration. Driven imperatively via ToastService. Deduplication, on by default: a repeat refreshes the toast that is already up, increments a `×n` badge and restarts its countdown, instead of filling a bounded stack with eleven copies of one failure. It keeps its place and its id, so the caller's handle still works and a screen reader is not read the same sentence twice. Key it yourself with `dedupeKey` (or `null` to opt out). Route scope: a toast survives navigation by default, and `{ scope: 'route' }` marks one as belonging to the page it was raised on. `provideSvOverlayRouteClose()` (from `@styloviz/layout`) drops those and only those on `NavigationEnd`; `ToastService.navigated()` is the router-agnostic call behind it, for an app on a different router. A bounded stack: `maxVisible` (validated — anything below 1 is 1, `Infinity` means no cap) shows the newest toasts and a "+N more" line counts the older ones held back; they stay alive and slide back into view as newer ones go. A toast raised while the pointer or focus is on the stack is paused on arrival, and a keyed replacement (`Saving…` then `Saved` on one `dedupeKey`) is announced — only an identical repeat is silent. `[portal]="true"` moves the region into a `<body>` portal. Use it when toasts carry actions and your app opens modals: a modal makes the page behind it `inert`, and `inert` is inherited, so a region inside the app shell can still be heard (speech goes through the shared announcer) but its Undo button cannot be pressed until the dialog closes. Portalled, the region is a `<body>` child the overlay stack keeps live. Off by default because it moves the region out of the host element. `toast.promise(work, { loading, success, error }, opts?)`: one toast for an async action. It shows `loading` with a spinner (`pending: true`, `aria-busy`), persists and is not dismissible until the work settles, then turns into the success or error toast in place — same id, no repeat count — and is announced again. `success` and `error` may be functions of the result. `opts.duration` sets how long the outcome stays (default 4 s, 6 s for an error). Returns the work promise, so you still await the result and handle the rejection. Phones: the stack keeps `max(1rem, env(safe-area-inset-*))` from each edge, so it clears the notch, the rounded corners and the home indicator (add `viewport-fit=cover` to your viewport meta), and a bottom stack is lifted above the virtual keyboard while one is up, so a toast raised by a form Save is not hidden by the keyboard that form raised.
Best practices
Do: Mount exactly one `<sv-toast-host>` near the app root, then call the service. Do: Offer an Undo action for destructive operations instead of a confirm prompt. Don't: Don't put critical, must-read information only in a toast — it disappears. Do: Give a repeated message an explicit `dedupeKey` when the wording changes but the subject does not — `{ dedupeKey: 'record-7' }` makes "Saving…" and "Saved" replace each other instead of stacking. Deduplication is on by default and keyed on variant + title + message + position; `{ dedupeKey: null }` opts a call out. Do: Pass `{ scope: 'route' }` for a message ABOUT the current page, and leave the default `'global'` for the outcome of an action. A confirmation raised by a save that navigates must survive the navigation it caused; a note about a view the user has left is worse than nothing two pages later. Don't: Don't mount two hosts at the same `position` — each renders every toast for its position, so every toast and its announcement appear twice. Dev builds warn once per position when it happens.
Accessibility
One shared live region announces every toast (`SvLiveAnnouncer`) — assertively for `error`, politely for the rest, including `warning`. A toast is not a live region of its own: a region inserted with its text already inside it is what AT is least likely to read, and it would take its region away with it seconds later, mid-sentence. The stack container stays `role="region"` + `aria-label="Notifications"`, so a screen reader user can navigate to a toast and press its action. Auto-dismiss pauses while the pointer OR keyboard focus is over the stack — both, separately — and resumes only when neither holds (WCAG 2.2.1); actions are keyboard reachable before dismiss.
Feedback on Toast
Spotted a gap in this page, or something that didn't work as described? Comments are GitHub Discussions in the public styloviz/community repo, so posting needs a GitHub account.