Signature
A pad to sign on, with ink that thins as you speed up. Pick an eraser and clear it as dust, a cloth wipe, a dither, a melt or a rewind.
Installation
npx shadcn@latest add https://fabrickit.org/r/signature.jsonpnpm dlx shadcn@latest add https://fabrickit.org/r/signature.jsonyarn shadcn@latest add https://fabrickit.org/r/signature.jsonbunx --bun shadcn@latest add https://fabrickit.org/r/signature.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 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/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,lib/fabricui/cloth-sheet.js/* * cloth-sheet.js — a small sheet of sheer cloth, simulated and drawn on a * canvas. Shared by Hold to delete (the list row's cloth) and the Hold * button (its red fabric fill). * * const cloth = new ClothSheet(canvas, { tint, opacity }); * cloth.resize(W, H); // canvas size, css px * const S = cloth.build(box, offX); // a sheet laid over box {x,y,w,h}, shifted offX * cloth.step(dt, pins); // advance; pins: [[particle, x, y, z, weight], …] * cloth.draw({ alpha, clip, round }); // clip: a rounded box {x,y,w,h,r} it stays inside, or null; * // round: the sheet's own corner radius * cloth.clear(); * * The sheet is position-based cloth (stretch, shear and bend constraints) * lying on the surface under gravity, gripping it a little. It's drawn as * the library's sheer cloth: a fine knit you can see through (denser seenStep 4: Copy the Signature files
The component itself. Create each one at the same path in your project. It's your code from here on.components/fabricui/signature.tsx"use client"; import * as React from "react"; import { mount } from "@/lib/fabricui/mount"; import { SignatureEngine } from "@/lib/fabricui/signature-engine"; import { Button } from "@/components/fabricui/button"; import { Avatar } from "@/components/fabricui/avatar"; import { Select } from "@/components/fabricui/select"; import "./signature.css"; const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect; export type SignatureEffect = "dust" | "wipe" | "dither" | "melt" | "rewind"; /** A stroke: the points the pen passed, in px from the pad's top left (w, the line's width there). */ export type SignatureStroke = { x: number; y: number; w?: number }[];components/fabricui/signature.css/* * Signature — a pad to sign on (lib/fabricui/signature-engine.js draws the * ink and erases it), and a footer: who's signing, the eraser to use (Select), * Erase and the submit button (Button). Colours are the theme's: the ink is * the text colour, so it's light on dark and near-black on light. */ .sig:where(.fui-signature, .fui-signature *) { position: relative; width: 560px; max-width: 100%; background: var(--fabric-card); border-radius: 22px; box-shadow: var(--fabric-edge); color: var(--fabric-text); }lib/fabricui/signature-engine.js/* * signature-engine.js — Signature (signature.css). A pad to sign on, and five * ways to erase what's on it. * * const s = new SignatureEngine(root, { onChange }); // root holds .sig__pad * s.erase("dust"); // a promise, settled once it's gone * s.write(strokes); // draws strokes in, as if being signed * s.isEmpty; s.strokes; s.toDataURL(); * * The ink: each stroke is the points the pointer passed, drawn as a smooth * curve through their midpoints. It thins as the pen speeds up and thickens * as it slows (a pen's pressure is used where there is one), like ink from a * nib. It's drawn in the pad's text colour, and again when the theme changes. * * The erasers (all but rewind and wipe take the ink's own pixels and move them): * dust every pixel of ink lifts off as a mote of dust, a wave crossingStep 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/signature. The Usage example below shows it in a page.
Code
"use client";
import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { SignatureEngine } from "@/lib/fabricui/signature-engine";
import { Button } from "@/components/fabricui/button";
import { Avatar } from "@/components/fabricui/avatar";
import { Select } from "@/components/fabricui/select";
import "./signature.css";
const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
export type SignatureEffect = "dust" | "wipe" | "dither" | "melt" | "rewind";
/** A stroke: the points the pen passed, in px from the pad's top left (w, the line's width there). */
export type SignatureStroke = { x: number; y: number; w?: number }[];
/*
* Signature — a pad to sign on (lib/fabricui/signature-engine.js draws the
* ink and erases it), and a footer: who's signing, the eraser to use (Select),
* Erase and the submit button (Button). Colours are the theme's: the ink is
* the text colour, so it's light on dark and near-black on light.
*/
.sig:where(.fui-signature, .fui-signature *) {
position: relative;
width: 560px;
max-width: 100%;
background: var(--fabric-card);
border-radius: 22px;
box-shadow: var(--fabric-edge);
color: var(--fabric-text);
}
/*
* signature-engine.js — Signature (signature.css). A pad to sign on, and five
* ways to erase what's on it.
*
* const s = new SignatureEngine(root, { onChange }); // root holds .sig__pad
* s.erase("dust"); // a promise, settled once it's gone
* s.write(strokes); // draws strokes in, as if being signed
* s.isEmpty; s.strokes; s.toDataURL();
*
* The ink: each stroke is the points the pointer passed, drawn as a smooth
* curve through their midpoints. It thins as the pen speeds up and thickens
* as it slows (a pen's pressure is used where there is one), like ink from a
* nib. It's drawn in the pad's text colour, and again when the theme changes.
*
* The erasers (all but rewind and wipe take the ink's own pixels and move them):
* dust every pixel of ink lifts off as a mote of dust, a wave crossingUsage
"use client";
import * as React from "react";
import { Signature, type SignatureHandle } from "@/components/fabricui/signature";
export function SignContract({ onSigned }: { onSigned: (png: string) => void }) {
const sig = React.useRef<SignatureHandle>(null);
return (
<Signature
ref={sig}
name="Mira Chen"
caption="Signing for Acme Studio"
onSubmit={png => onSigned(png)}
/>
);
}
// from code: sig.current?.erase("dust"), and "wipe", "dither", "melt" or "rewind"Props
| Prop | Type | Default |
|---|---|---|
| label The pad's label, top left. | string | "Sign here" |
| name Who's signing: shown in the footer with their avatar (a weave). | string | — |
| caption A line under the name. | string | — |
| submitLabel The submit button. It's off until there's a signature. | string | "Continue" |
| effects The erasers the picker offers, in order; Erase clears it with the one picked. Dust lifts off in fine motes; wipe draws the library's cloth across and gathers it off; dither dissolves it in ordered dots; melt runs it down like goo; rewind unwrites it, last stroke first. | ("dust" | "wipe" | "dither" | "melt" | "rewind")[] | all five |
| defaultEffect The eraser picked at first. | "dust" | "wipe" | "dither" | "melt" | "rewind" | the first of effects |
| defaultStrokes Strokes it starts with, drawn in as it appears (points in px from the pad's top left; w is the line's width). | { x, y, w? }[][] | — |
| onChange Called as it gains a signature (true) or is erased (false). | (signed: boolean) => void | — |
| onSubmit Called with the signature as a PNG data URL (transparent around the ink) when the submit button is pressed. | (png: string) => void | — |
| ref Erase it or draw strokes in from code (each returns a promise that settles when it's done), or take the image. | { erase(effect?), write(strokes), toDataURL(type?), isEmpty } | — |