/** * 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) * * ```ts import.meta.vitest * 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) * ) * ``` * * @stability unstable * @since 4.2.0 */ import * as Context from "../../Context.ts" import * as Effect from "../../Effect.ts" import * as Layer from "../../Layer.ts" import * as Queue from "../../Queue.ts" import * as Schema from "../../Schema.ts" import * as Stream from "../../Stream.ts" import type * as Component from "../Component.ts" import * as Html from "../html/Html.ts" import * as Subscription from "../Subscription.ts" import * as View from "../View.ts" import { type ToastData, ToastProvider } from "./Toast.ts" /** * What a `Toaster` posts to a `ToastProvider`: `RequestedAdd`, * `RequestedUpdate` or `RequestedClose`. * * @stability unstable * @category models * @since 4.2.0 */ export type ToastRequest = Component.Shared.Receives /** * Posts toasts to the `ToastProvider` above the app's `ToasterRelay`. * * **Details** * * - `add` shows a toast, or replaces the one with the same `id`. * - `update` changes the fields it names on the toast `id`. * - `close` closes the toast `id`. * - `requests` streams 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. * * @stability unstable * @category services * @since 4.2.0 */ export class Toaster extends Context.Service Effect.Effect readonly update: (changes: ToastData) => Effect.Effect readonly close: (id: string) => Effect.Effect readonly requests: Stream.Stream }>()("effect/ui/kit/Toaster") { /** * A Toaster for the services below the component that provides it. * * @stability unstable * @since 4.2.0 */ static readonly layer: Layer.Layer = Layer.effect( this, Effect.map(Queue.sliding(16), (queue) => { const post = (request: ToastRequest) => Effect.asVoid(Queue.offer(queue, request)) return Toaster.of({ add: (toast) => post({ _tag: "RequestedAdd", toast }), update: (changes) => post({ ...changes, _tag: "RequestedUpdate" }), close: (id) => post({ _tag: "RequestedClose", id }), requests: Stream.fromQueue(queue) }) }) ) } const relayed = [ Subscription.make( "Toaster", Stream.unwrap(Toaster.useSync((toaster) => toaster.requests)) ) ] /** * Passes the `Toaster`'s requests to the `ToastProvider` above it. Render * it once, inside the provider's closure. It draws nothing. * * @stability unstable * @category components * @since 4.2.0 */ export const ToasterRelay = Html.component({ name: "ToasterRelay", model: Schema.Null, message: ToastProvider.receives }).make({ init: () => null, update: ($, request) => $.send(ToastProvider, request), subscriptions: () => relayed, view: () => View.text("") })