sdxc

Type to search, or start from one of these:

[ @sdxc/ui/behaviors ]

Toaster

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

import { Toaster } from "@sdxc/ui/behaviors";

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

ParameterTypeDescription
init?Toaster.InitConstruction options; see Toaster.Init.

Properties

PropertyTypeDescription
toastsreadonly Toaster.Toast<Data>[]Every queued toast, in the order it was added.
sizenumberNumber 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.
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. 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

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

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

Interface

Toaster.Toast

One queued toast.

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

Toaster.AddOptions

Options accepted by Toaster.add.

MemberTypeDescription
id?stringId to queue the toast under. Defaults to a generated id; reusing an id already queued replaces that toast.
duration?number | nullMilliseconds 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.

MemberTypeDescription
duration?number | nullReplacement duration. When provided, restarts the toast's auto-dismiss timer from full, preserving whether the toast is currently paused.
Interface

Toaster.Init

Construction options accepted by Toaster.

MemberTypeDescription
defaultDuration?numberAuto-dismiss delay, in milliseconds, used when Toaster.AddOptions.duration is omitted. Defaults to 5000.
Interface

Toaster.Events

Events dispatched by Toaster.

MemberTypeDescription
changeEventDispatched after a toast is added, updated, dismissed (by timeout or by id), paused, resumed, or the queue is cleared.
toastEventDispatched after a new toast is added, ahead of "change", so a listener can react to the arrival alone.

Written by Sergio Xalambrí. Follow @sergiodxa for new packages, or sponsor the work.