Toaster
Headless queue of toast notifications a Toast.Region island subscribes to, re-rendering on change.
import { Toaster } from "@sdxc/ui/app";Usage
Owns a queue of toast notifications and each one's auto-dismiss timer. An
island subscribes to "change" and calls Toaster.pause and
Toaster.resume so a toast under the cursor stays readable.
Signature
new Toaster<Data = unknown>(init?: Toaster.Init)Parameters
| Parameter | Type | Default | Required | Description |
|---|---|---|---|---|
init | Toaster.Init | — | No | Construction options; see Toaster.Init. |
Properties
| Property | Type | Default | Required | Description |
|---|---|---|---|---|
toasts | readonly Toaster.Toast<Data>[] | — | Yes | Every queued toast, in the order it was added. |
size | number | — | Yes | Number of toasts currently queued. |
Methods
| Method | Description |
|---|---|
get(id: string): Toaster.Toast<Data> | undefined | Looks up one queued toast by id. |
add(data: Data, options?: Toaster.AddOptions): string | Queues a toast and starts its auto-dismiss timer. Reusing an id already queued replaces that toast in place, clearing its previous timer. Dispatches "toast" and then "change". |
update(id: string, data: Data, options?: Toaster.UpdateOptions): boolean | Patches a queued toast's data in place. Passing duration also restarts its timer from full, preserving whether the toast is currently paused. |
promise<Value>(promise: Promise<Value>, states: Toaster.PromiseStates<Data, Value>, options?: Toaster.AddOptions): string | Shows one toast for a promise's whole life: loading with no timer, then success or error patched onto the same toast, whose timer starts from options.duration (or the default) once it settles. |
dismiss(id: string): boolean | Removes one queued toast by id and clears its timer. |
dismissAll(): void | Empties the queue and clears every timer, dispatching "change" when it held at least one toast. |
pause(id?: string): void | Pauses the auto-dismiss timer for one toast, or every toast when id is omitted, recording how much time was left on each; a queue-wide pause also holds toasts added before the matching resume. Dispatches "change" only when at least one running timer paused. |
resume(id?: string): void | Resumes the auto-dismiss timer for one toast, or every toast when id is omitted, continuing from the time left when it paused. Dispatches "change" only when at least one paused timer resumed. |
dispose(): void | Empties the queue and clears every pending timer silently, for an island to call as it unmounts so each countdown ends with it. |
Examples
whileLive(handle, (model: Toaster, signal) => model.addEventListener("change", () => handle.update(), { signal }))(toaster);Used with it
The events, constants and types its module publishes, each imported from @sdxc/ui/app too.
Interface
Toaster.Toast
One queued toast.
| Member | Type | Default | Required | Description |
|---|---|---|---|---|
id | readonly string | — | Yes | Stable id used to target this toast with Toaster.dismiss, Toaster.update, Toaster.pause, and Toaster.resume. |
data | readonly Data | — | Yes | Consumer-supplied payload the island renders — copy, variant, action, and any other data the toast needs. |
duration | readonly number | null | — | Yes | Milliseconds until this toast auto-dismisses, or null when it only leaves the queue through Toaster.dismiss. |
createdAt | readonly number | — | Yes | Date.now() timestamp this toast was queued at. |
paused | readonly boolean | — | Yes | true while this toast's auto-dismiss timer is paused. |
Interface
Toaster.AddOptions
Options accepted by Toaster.add.
| Member | Type | Default | Required | Description |
|---|---|---|---|---|
id | string | — | No | Id to queue the toast under. Defaults to a generated id; reusing an id already queued replaces that toast. |
duration | number | null | — | No | Milliseconds until auto-dismiss, or null for a toast that only leaves the queue through Toaster.dismiss. Defaults to the constructor's Toaster.Init.defaultDuration. |
Interface
Toaster.UpdateOptions
Options accepted by Toaster.update.
| Member | Type | Default | Required | Description |
|---|---|---|---|---|
duration | number | null | — | No | Replacement duration. When provided, restarts the toast's auto-dismiss timer from full, preserving whether the toast is currently paused. |
Interface
Toaster.PromiseStates
The three payloads Toaster.promise shows over one promise's life.
success and error may derive their payload from the settled value.
| Member | Type | Default | Required | Description |
|---|---|---|---|---|
loading | Data | — | Yes | Shown at once and held, with no timer, until the promise settles. |
success | Data | ((value: Value) => Data) | — | Yes | Replaces PromiseStates.loading in place once the promise resolves. |
error | Data | ((reason: unknown) => Data) | — | Yes | Replaces PromiseStates.loading in place once the promise rejects. |
Interface
Toaster.Init
Construction options accepted by Toaster.
| Member | Type | Default | Required | Description |
|---|---|---|---|---|
defaultDuration | number | — | No | Auto-dismiss delay, in milliseconds, used when Toaster.AddOptions.duration is omitted. Defaults to 5000. |
Interface
Toaster.Events
Events dispatched by Toaster.
| Member | Type | Default | Required | Description |
|---|---|---|---|---|
change | Event | — | Yes | Dispatched after a toast is added, updated, dismissed (by timeout or by id), paused, resumed, or the queue is cleared. |
toast | Event | — | Yes | Dispatched after a new toast is added, ahead of "change", so a listener can react to the arrival alone. |