Feedback
Toast
A global notification system. Mount Toaster once, then call toast.success(), toast.error() and friends from anywhere.
Mount Toaster once in the root layout:
<!-- +layout.svelte -->
<script>
import { Toaster } from "@codeatlasdev/ui";
let { children } = $props();
</script>
{@render children()}
<Toaster />
Then call toast from anywhere:
<script>
import { toast } from "@codeatlasdev/ui";
</script>
<button onclick={() => toast.success("Changes saved.")}>Save</button>
toast is a global store, so the calls need no component context. The Toaster is a polite live region (role="region", aria-live="polite") and each toast is a role="status" (success, info, default) or role="alert" (error, warning) with a dismiss button. Toasts stack in the bottom right corner, up to 380px wide, in a fixed container layered with --ca-z-snackbar (they are not in the browser top layer, unlike dialogs and popovers). toast.show accepts variant, duration, id, description and action in its options; the shortcut methods take the duration or the same options object (without variant) as a second argument.
Calling toast with the same variant and message again does not stack a copy: the existing toast restarts its timer and shows a ×N count, so toast.error("x") ten times in a row leaves one toast. At most three toasts show at once and the oldest is dropped past that. Pass id to replace one specific toast in place (new text, variant and timer, count back to 1), for example an “Uploading…” toast that becomes “Uploaded”. The demo below mounts its own Toaster because every demo is an independent island.
Variants and duration
Five variants, plus persistent (duration 0) and custom durations. The default auto-dismiss is 4 seconds.
Variants
Duration
Reference #103 · Pick in the gallery
Code
Add a secondary line with description and one inline button with action: { label, onClick }. Clicking the action runs onClick and dismisses the toast, and an actionable toast stays 8 seconds unless you pass duration. The description is part of the same live-region announcement as the message. The auto-dismiss timer pauses while the toast is hovered or holds focus (WCAG 2.2.1) and restarts with the time that was left; Escape dismisses the toast that has focus. Toasts with an action never merge as repeats, since each carries its own handler.
Description and action
A description under the title, a single inline action, and both. The action is a real button reachable with Tab.
Reference #492 · Pick in the gallery
Code
Dedupe and replace by id
Ten identical errors collapse into one toast with a count; the upload toast is replaced in place by its id.
Reference #490 · Pick in the gallery
Code
API
| Prop | Type | Default | Description |
|---|---|---|---|
toast.show(message, opts?) | (message: string, opts?: ToastOptions) => string | – | Shows a toast and returns its id. Variant defaults to 'default'. An identical message and variant bumps the existing toast's count instead of adding one. |
toast.success(message, durationOrOpts?) | (message: string, duration?: number | Omit<ToastOptions, 'variant'>) => string | – | Success toast. |
toast.error(message, durationOrOpts?) | (message: string, duration?: number | Omit<ToastOptions, 'variant'>) => string | – | Error toast. |
toast.warning(message, durationOrOpts?) | (message: string, duration?: number | Omit<ToastOptions, 'variant'>) => string | – | Warning toast. |
toast.info(message, durationOrOpts?) | (message: string, duration?: number | Omit<ToastOptions, 'variant'>) => string | – | Info toast. |
description | string | – | In `ToastOptions`: secondary, smaller line under the message (`--ca-text-secondary`). |
action | { label: string; onClick: () => void } | – | In `ToastOptions`: one inline button. Runs `onClick` and dismisses the toast. Sets the default duration to 8000 ms. |
toast.pause(id) / toast.resume(id) | (id: string) => void | – | Hold and restart the auto-dismiss timer with the time left. The Toaster calls them on hover and focus; call them yourself only for a custom renderer. |
id | string | – | In `ToastOptions`: replaces the toast with this id in place (text, variant, timer, count 1), or creates it with this id. Returned by every call. |
ToastItem.count | number | – | How many identical calls the toast stands for; the Toaster shows `×N` when above 1. |
toast.remove(id) | (id: string) => void | – | Dismisses a toast by id. |
toast.items | ToastItem[] | – | Reactive list of the visible toasts. |
duration | number | 4000 | Auto-dismiss delay in ms. 0 keeps the toast until dismissed. |
<Toaster inline /> | boolean | – | Stacks inside the nearest positioned ancestor (position: absolute) instead of the viewport, so it takes the tokens of a data-preset island. Default false. |
ToastVariant | 'default' | 'success' | 'error' | 'warning' | 'info' | – | Accepted variants. |