Toast

Notes that drop in from the top, and ones that follow an agent's work.

    Installation

    Terminal
    npx shadcn@latest add https://fabrickit.org/r/toast.json

    This copies the component into components/fabricui and its engine into lib/fabricui. It's your code from then on. First time? Add the theme once: see Installation.

    Code

    components/fabricui/toast.tsx
    "use client";
    
    import * as React from "react";
    import { mount } from "@/lib/fabricui/mount";
    import { Toaster as ToasterEngine, agent as agentToast } from "@/lib/fabricui/toast-engine";
    import "./button.css";
    import "./toast.css";
    
    const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
    
    export type ToastKind = "success" | "error" | "info";
    
    export type ToastOptions = {
      title: string;
      description?: string;
      /** The icon (and an error is announced as an alert). */
    components/fabricui/toast.css
    /*
     * toast.css — Toast (toast.js). Moved out of toast.html.
     */
    /*
     * Toast. Cards drop in as cloth from the top of the stack (cloth-drop.js,
     * in-flow, no trigger: tuck 0), newest on top, pushing the others down as
     * they fall. Dismissed toasts collapse plainly and the stack closes up.
     */
    
    /* ---------- Stack ---------- */
    .toasts:where(.fui-toast, .fui-toast *) {
      position: absolute;
      top: 76px;   /* below the page title */
      left: 50%;
      width: 360px;
      margin: 0 0 0 -180px;
    lib/fabricui/toast-engine.js
    /*
     * toast-engine.js — Toast (toast.css). Moved out of toast.html so it can be used anywhere.
     * (The page's icons, escape helper, segDist and agent() live here too, as the
     * Toaster and Weave use them.)
     */
    import { ClothDrop } from "./cloth-drop";
    import { Dither } from "./dither";
    
    const ICONS = {
      success: `<path d="M22 12C22 6.47715 17.5228 2 12 2C6.47715 2 2 6.47715 2 12C2 17.5228 6.47715 22 12 22C17.5228 22 22 17.5228 22 12Z" /><path d="M8 12.5L10.5 15L16 9" />`,
      error: `<circle cx="12" cy="12" r="10" /><path d="M12 8V12" /><path d="M12.125 15.75H12M12.25 15.75C12.25 15.8881 12.1381 16 12 16C11.8619 16 11.75 15.8881 11.75 15.75C11.75 15.6119 11.8619 15.5 12 15.5C12.1381 15.5 12.25 15.6119 12.25 15.75Z" />`,
      info: `<circle cx="12" cy="12" r="10" /><path d="M12 16V12" /><path d="M12.125 8.25H12M12.25 8.25C12.25 8.11193 12.1381 8 12 8C11.8619 8 11.75 8.11193 11.75 8.25C11.75 8.38807 11.8619 8.5 12 8.5C12.1381 8.5 12.25 8.38807 12.25 8.25Z" />`,
    };
    const CLOSE = `<svg class="icon icon--sm" viewBox="0 0 24 24" aria-hidden="true"><path d="M18.9991 19L5 5.00098" /><path d="M19 5L5.00092 18.999" /></svg>`;
    const esc = s => String(s).replace(/[&<>"]/g, c => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c]);
    

    Usage

    Usage
    import { Toaster, toast } from "@/components/fabricui/toast";
    
    // once, inside a positioned element (the stack sits 76px from its top, centred)
    <Toaster />
    
    toast.success("Changes saved", { description: "Your profile is up to date." });
    toast({ title: "File deleted", action: { label: "Undo", onClick: restore } });
    
    // one toast that follows a task, changing in place
    const t = toast.agent({ title: "Writing tests", description: "Reading 12 files", progress: 0.1 });
    t?.step({ progress: 0.6, description: "Writing 6 tests" });
    t?.step({ state: "done", title: "6 tests added", actions: [{ label: "Review", onClick: h => { openReview(); h.dismiss(); } }], expire: 4000 });

    Props

    PropTypeDefault
    Toaster max

    At most this many show at once; the oldest makes room.

    number3
    Toaster duration

    ms before a toast goes by itself (paused while the pointer is over the stack).

    number4000
    toast(options)

    Shows a toast and returns a handle. kind is "success", "error" (announced as an alert) or "info"; action is a small button (Undo) in place of the close button; duration 0 keeps it until dismissed.

    { title, description?, kind?, action?, duration? }—
    toast.success / .error / .info

    The same, with the kind set.

    (title, options?) => handle—
    toast.dismiss

    Takes a toast away.

    (handle) => void—
    toast.agent

    A toast that follows one task: its weave shimmers and fills with progress while working, breathes while waiting, and settles into a tick or a cross. Move it on with .step({ title, description, state, progress, actions, expire }). It stays until acted on unless expire is set; an action without onClick dismisses it.

    ({ title, description?, state?, progress? }) => AgentToast—