Short notifications with an optional action like Undo.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Button } from "./ui/uicontext/components/Button.ts"import { showToast, Toaster, ToastProvider } from "./ui/uicontext/components/Toast.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ row: { display: "flex", flexWrap: "wrap", justifyContent: "center", gap: 8 },})const Message = UiSchema.TaggedEnum({ ClickedSave: {}, ClickedSync: {}, ClickedExport: {} })// Inside the Toaster, so its update can send to the provider.const Actions = Html.component({ name: "ToastActions", model: Schema.Struct({}), message: Message,}).make({ init: () => ({}), update: { ClickedSave: ($) => $.send(ToastProvider, showToast({ id: "saved", title: "Changes saved", variant: "success" })), ClickedSync: ($) => $.send(ToastProvider, showToast({ id: "sync-failed", title: "Couldn't sync your calendar", description: "Reconnect it from Settings → Connections.", variant: "danger", })), // a timeout of 0 keeps it until it's closed ClickedExport: ($) => $.send(ToastProvider, showToast({ id: "export", title: "Export ready", description: "It stays here until you close it.", timeout: 0 })), }, view: (h) => h.div.attrs(sx(styles.row)).children( Button(h).on("click", Message.ClickedSave()).children("Save changes"), Button(h).on("click", Message.ClickedSync()).children("Sync calendar"), Button(h, { emphasis: "ghost" }).on("click", Message.ClickedExport()).children("Export workspace"), ),})export const ToastDemo = Html.component({ name: "ToastDemo", model: Schema.Struct({}), message: Schema.Never,}).make({ init: () => ({}), update: ($) => $.model, view: (h) => Toaster(h, { children: [Actions(h, {})] }),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/toastUsage
import { Button } from "./ui/uicontext/components/Button.ts"import { showToast, Toaster, ToastProvider } from "./ui/uicontext/components/Toast.ts"const Message = UiSchema.TaggedEnum({ ClickedSave: {} })// Any component inside the Toaster can send a toast to the provider from its update.const SaveButton = Html.component({ name: "SaveButton", model: Schema.Struct({}), message: Message }).make({ init: () => ({}), update: { ClickedSave: ($) => $.send(ToastProvider, showToast({ id: "saved", title: "Changes saved", variant: "success" })), }, view: (h) => Button(h).on("click", Message.ClickedSave()).children("Save"),})// Put one Toaster around your app's content.export const App = <M>(h: Html.H<M>) => Toaster(h, { children: [SaveButton(h, {})] })Examples
With an Undo action
action adds a button to the toasts it chooses. It closes the toast and sends your message with the toast's id.
Undo restores the issue.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Button } from "./ui/uicontext/components/Button.ts"import { showToast, Toaster, ToastProvider } from "./ui/uicontext/components/Toast.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ stack: { display: "grid", justifyItems: "center", gap: 12 }, status: { margin: 0, fontSize: font.sm, color: color.muted },})const ArchiveMessage = UiSchema.TaggedEnum({ ClickedArchive: {} })// Inside the Toaster: archiving shows a toast whose id names what was archived.const ArchiveButton = Html.component({ name: "ArchiveButton", model: Schema.Struct({ count: Schema.Number }), message: ArchiveMessage,}).make({ init: () => ({ count: 0 }), update: { ClickedArchive: ($) => $.evolve({ count: (n) => n + 1 }).send(ToastProvider, showToast({ id: `archived:${$.model.count + 1}`, title: "Issue archived", description: "ENG-142 moved to the archive.", })), }, view: (h) => Button(h).on("click", ArchiveMessage.ClickedArchive()).children("Archive issue"),})const Message = UiSchema.TaggedEnum({ ClickedUndo: { id: Schema.String } })export const UndoToast = Html.component({ name: "UndoToast", model: Schema.Struct({ restored: Schema.Number }), message: Message,}).make({ init: () => ({ restored: 0 }), update: { ClickedUndo: ($) => $.evolve({ restored: (n) => n + 1 }) }, view: (h, { restored }) => h.div.attrs(sx(styles.stack)).children( Toaster(h, { // the action closes the toast and tells the app which one it was action: (toast) => (toast.id.startsWith("archived:") ? { label: "Undo", message: Message.ClickedUndo({ id: toast.id }) } : undefined), children: [ArchiveButton(h, {})], }), h.p.attrs({ role: "status", ...sx(styles.status) }).children( restored === 0 ? "Undo restores the issue." : `Restored ${restored} ${restored === 1 ? "issue" : "issues"}.`, ), ),})API reference
ToasterView function
Options
| Name | Type | Default | Description |
|---|---|---|---|
childrenrequired | View.Children | — | The app's body: components inside it can send to the provider |
action | (toast: ToastItem) => ToastAction<Message> | none | The action button a toast offers, from its id or variant; none by default |
onDismissed | (id: string) => Message | — | A toast closed by the person or by its timer |
timeout | number | 5000 | Milliseconds before a toast closes on its own, 5000 by default |
limit | number | 3 | How many show at once, 3 by default; older ones wait their turn |
label | string | "Notifications" | The stack's landmark name, "Notifications" by default |
style | Style | — | Styles for the element around the children: a flex column, when the body fills a frame |
ToastPreviewView function
Options
| Name | Type | Description |
|---|---|---|
toastsrequired | ToastPreviewItem[] | The toasts to draw, oldest first |
Accessibility
- Toasts are read out politely, without interrupting, and never take focus.
- F6 moves focus to the stack and back. The timer pauses while focus or the pointer is on it, so there's time to reach an action like Undo.
- Each toast has a Dismiss button, and the stack stays usable above an open dialog.