Toaster unstable
Toasts from code outside the component tree, such as an Effect service that reports a lost connection or a finished background sync.
A service posts with the Toaster's add, update and close. A
ToasterRelay, rendered once inside a ToastProvider's closure, passes
each request to that provider; outside one, it doesn't compile. Provide
Toaster.layer on the component that owns its lifetime, usually the root.
An app that doesn't import this module doesn't bundle it.
Example A service that toasts on its own
import { Component } from "effect/ui"import { Html } from "effect/ui/html"import { Toaster, ToasterRelay, ToastProvider } from "effect/ui/kit"import { Effect, Layer, Schema } from "effect"
const App = Html.component({ name: "App", model: Schema.Null, message: Schema.Never}).make({ init: () => null, update: ($) => $.model, view: (h) => ToastProvider(h, { view: (toastProvider) => h.div( ToasterRelay(h, {}), toastProvider.viewport(h.div).children( toastProvider.toasts.map((toast) => toastProvider.root({ element: h.div, toast }).children(toast.title) ) ) ) })})
// Posts before the relay listens; the request waits in the Toaster.const Offline = Layer.effectDiscard(Effect.gen(function*() { const toaster = yield* Toaster yield* toaster.add({ id: "offline", title: "Working offline", type: "error" })}))
// The service and the relay share the root's Toaster.export const Root = Component.provide( App, Layer.provideMerge(Offline, Toaster.layer))Components
ToasterRelay
unstablePasses the Toaster's requests to the ToastProvider above it. Render
it once, inside the provider's closure. It draws nothing.
Signature
declare const ToasterRelay: Component<"ToasterRelay", {}, null, { readonly _tag: "RequestedClose"; readonly id: string;} | { readonly _tag: "RequestedAdd"; readonly toast: { readonly description?: string; readonly id: string; readonly priority?: "low" | "high"; readonly timeout?: number; readonly title?: string; readonly type?: string; };} | { readonly _tag: "RequestedUpdate"; readonly description?: string; readonly id: string; readonly priority?: "low" | "high"; readonly timeout?: number; readonly title?: string; readonly type?: string;}, never, Shared<"ToastProvider"> | Toaster, Elements, QueryEntries<{}, {}>, never>Models
What a Toaster posts to a ToastProvider: RequestedAdd,
RequestedUpdate or RequestedClose.
Signature
type ToastRequest = Component.Shared.Receives<typeof ToastProvider>Services
Posts toasts to the ToastProvider above the app's ToasterRelay.
Details
addshows a toast, or replaces the one with the sameid.updatechanges the fields it names on the toastid.closecloses the toastid.requestsstreams the posted requests, in order. Each goes to one reader, so render one relay per Toaster.
Requests posted while no relay listens, such as before mount or on the server, wait in the Toaster. It keeps the newest 16, in order, and drops the oldest.
Signature
declare class Toaster extends Shape<"effect/ui/kit/Toaster", { readonly add: (toast: { readonly description?: string; readonly id: string; readonly priority?: "low" | "high"; readonly timeout?: number; readonly title?: string; readonly type?: string; }) => Effect<void>; readonly close: (id: string) => Effect<void>; readonly requests: Stream<{ readonly _tag: "RequestedClose"; readonly id: string; } | { readonly _tag: "RequestedAdd"; readonly toast: { readonly description?: string; readonly id: string; readonly priority?: "low" | "high"; readonly timeout?: number; readonly title?: string; readonly type?: string; }; } | { readonly _tag: "RequestedUpdate"; readonly description?: string; readonly id: string; readonly priority?: "low" | "high"; readonly timeout?: number; readonly title?: string; readonly type?: string; }>; readonly update: (changes: { readonly description?: string; readonly id: string; readonly priority?: "low" | "high"; readonly timeout?: number; readonly title?: string; readonly type?: string; }) => Effect<void>;}, this> { constructor(_: never); static readonly layer: Layer<Toaster>;}