SearchCtrlK

  • ↑↓navigate
  • ↵open
  • escclose

Can’t find it? Contact us

Toaster

Headless queue of toast notifications a Toast.Region island subscribes to, re-rendering on change.

tsx
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

typescript
new Toaster<Data = unknown>(init?: Toaster.Init)

Parameters

ParameterTypeDefaultRequiredDescription
initToaster.Init—NoConstruction options; see Toaster.Init.

Properties

PropertyTypeDefaultRequiredDescription
toastsreadonly Toaster.Toast<Data>[]—YesEvery queued toast, in the order it was added.
sizenumber—YesNumber of toasts currently queued.

Methods

MethodDescription
get(id: string): Toaster.Toast<Data> | undefinedLooks up one queued toast by id.
add(data: Data, options?: Toaster.AddOptions): stringQueues 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): booleanPatches 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): stringShows 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): booleanRemoves one queued toast by id and clears its timer.
dismissAll(): voidEmpties the queue and clears every timer, dispatching "change" when it held at least one toast.
pause(id?: string): voidPauses 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): voidResumes 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(): voidEmpties the queue and clears every pending timer silently, for an island to call as it unmounts so each countdown ends with it.

Examples

tsx
whileLive(handle, (model: Toaster, signal) => model.addEventListener("change", () => handle.update(), { signal }))(toaster);

The events, constants and types its module publishes, each imported from @sdxc/ui/app too.

Interface

Toaster.Toast

One queued toast.

MemberTypeDefaultRequiredDescription
idreadonly string—YesStable id used to target this toast with Toaster.dismiss, Toaster.update, Toaster.pause, and Toaster.resume.
datareadonly Data—YesConsumer-supplied payload the island renders — copy, variant, action, and any other data the toast needs.
durationreadonly number | null—YesMilliseconds until this toast auto-dismisses, or null when it only leaves the queue through Toaster.dismiss.
createdAtreadonly number—YesDate.now() timestamp this toast was queued at.
pausedreadonly boolean—Yestrue while this toast's auto-dismiss timer is paused.
Interface

Toaster.AddOptions

Options accepted by Toaster.add.

MemberTypeDefaultRequiredDescription
idstring—NoId to queue the toast under. Defaults to a generated id; reusing an id already queued replaces that toast.
durationnumber | null—NoMilliseconds 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.

MemberTypeDefaultRequiredDescription
durationnumber | null—NoReplacement 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.

MemberTypeDefaultRequiredDescription
loadingData—YesShown at once and held, with no timer, until the promise settles.
successData | ((value: Value) => Data)—YesReplaces PromiseStates.loading in place once the promise resolves.
errorData | ((reason: unknown) => Data)—YesReplaces PromiseStates.loading in place once the promise rejects.
Interface

Toaster.Init

Construction options accepted by Toaster.

MemberTypeDefaultRequiredDescription
defaultDurationnumber—NoAuto-dismiss delay, in milliseconds, used when Toaster.AddOptions.duration is omitted. Defaults to 5000.
Interface

Toaster.Events

Events dispatched by Toaster.

MemberTypeDefaultRequiredDescription
changeEvent—YesDispatched after a toast is added, updated, dismissed (by timeout or by id), paused, resumed, or the queue is cleared.
toastEvent—YesDispatched after a new toast is added, ahead of "change", so a listener can react to the arrival alone.
@sdxc/ui

Can’t find it? Contact us