Toast
Notes that drop in from the top, and ones that follow an agent's work.
Installation
npx shadcn@latest add https://fabrickit.org/r/toast.jsonpnpm dlx shadcn@latest add https://fabrickit.org/r/toast.jsonyarn shadcn@latest add https://fabrickit.org/r/toast.jsonbunx --bun shadcn@latest add https://fabrickit.org/r/toast.jsonThis 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.
Step 1: Add the theme (once)
Every colour, radius and curve the components use, as --fabric-* variables. Save it as fabric.css next to your global CSS and import it there. If you've added another Fabric Kit component before, skip this.globals.css@import "./fabric.css";fabric.css/* * fabric.css — Fabric Kit's theme: every colour, radius, ring and curve the * components use, as --fabric-* variables (prefixed, so they never clash * with your own shadcn theme). Installed by `shadcn add @fabrickit/theme` * (it lands in your global CSS) — or import this file once, globally. * Dark by default. For light, put data-theme="light" on <html> (or on any * element: the tokens follow it down the tree; data-theme="dark" turns back). */ :root, [data-theme="dark"] { --fabric-page: #111111; --fabric-surface: #191919; /* triggers, recessed tiles */ --fabric-panel: #202020; /* open panels */ --fabric-raised: #2a2a2a; /* hover, range band */ --fabric-raised-hi: #343434; /* hover inside a band */ --fabric-preview: #252525; /* soft preview fills */ --fabric-text: #e0e0e0;Step 2: Add what it builds on
It uses Button. Add it first, the same way (skip any you already have).Step 3: Copy the shared engines
Plain JavaScript that does the motion, shared between components. Create each file at the same path in your project; if a file is already there from another component, keep it.lib/fabricui/mount.js/* * mount.js — runs a Fabric Kit engine for a React component and cleans up * after it. The engines are plain JavaScript that wire themselves to the page * (listeners on the document, resize observers, a few elements of their own); * mount() records everything an engine attaches and changes while it's being * set up, and dispose() takes it all back, so a component can come and go (or * be set up twice in a row, as React's Strict Mode does) without leaving * anything behind. * * const m = mount(() => new Engine(root, options)); * m.instance; // the engine * m.dispose(); // on unmount: listeners and observers off, the DOM as it was, then instance.destroy() * // (and destroy() on anything it owns that registered itself, like each ClothDrop) */ export function mount(create) { const ac = new AbortController();lib/fabricui/cloth-drop.jsimport { Dither } from "./dither"; /** * cloth-drop.js — the library's open/close motion, shared by every component * that drops open (calendar, accordion, …). * * ClothDrop: the open/close motion. * * Open is a dropped cloth: the panel falls under gravity from behind the * trigger (the calendar rides its bottom edge), the top edge catches, the * fabric stretches a touch and springs back, a wave runs up from the hem and * the drape folds and sway it was thrown into settle. * * The falling cloth is one finely subdivided WebGL mesh, textured with the * browser's own rendering of the calendar (the real HTML + CSS + Geist, drawn * through an SVG foreignObject), so its text is pixel-identical to the page.lib/fabricui/dither.js/* * dither.js — Fabric Kit's dither, in one place. Every dithered thing (cloth * fold shade, shimmer, weaves, chart fills) uses this 4×4 ordered (Bayer) * pattern, so dots line up and read as one texture across components. * * Dither.B4 the 4×4 matrix, 0–15 * Dither.T4 its thresholds, centred: (v + 0.5) / 16 * Dither.at(x, y) the threshold for cell (x, y) * Dither.GLSL the same pattern for shaders: bayer4(cell) → 0–1 */ const B4 = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5]; const T4 = B4.map(v => v / 16 + 1 / 32); export const Dither = { B4, T4,Step 4: Copy the Toast files
The component itself. Create each one at the same path in your project. It's your code from here on.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 => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]);Step 5: Check the import paths
The files import each other through the @/ alias (the shadcn default, there in a new Next.js app). If your project doesn't have it, add it to tsconfig.json, or change the imports to relative paths.tsconfig.json{ "compilerOptions": { "baseUrl": ".", "paths": { "@/*": ["./*"] } } }Step 6: Use it
Import it from @/components/fabricui/toast. The Usage example below shows it in a page.
Code
"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). *//*
* 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;/*
* 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 => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]);
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
| Prop | Type | Default |
|---|---|---|
| Toaster max At most this many show at once; the oldest makes room. | number | 3 |
| Toaster duration ms before a toast goes by itself (paused while the pointer is over the stack). | number | 4000 |
| 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 | — |