Toast

Short notifications with an optional action like Undo.

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/toast

Usage

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.

API reference

ToasterView function

Options

Toaster options
NameTypeDefaultDescription
childrenrequiredView.Children—The app's body: components inside it can send to the provider
action(toast: ToastItem) => ToastAction<Message>noneThe 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
timeoutnumber5000Milliseconds before a toast closes on its own, 5000 by default
limitnumber3How many show at once, 3 by default; older ones wait their turn
labelstring"Notifications"The stack's landmark name, "Notifications" by default
styleStyle—Styles for the element around the children: a flex column, when the body fills a frame

ToastPreviewView function

Options

ToastPreview options
NameTypeDescription
toastsrequiredToastPreviewItem[]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.

Source