Avatar
People, small: initials or a weave made from their name.
Installation
npx shadcn@latest add https://fabrickit.org/r/avatar.jsonpnpm dlx shadcn@latest add https://fabrickit.org/r/avatar.jsonyarn shadcn@latest add https://fabrickit.org/r/avatar.jsonbunx --bun shadcn@latest add https://fabrickit.org/r/avatar.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: 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 3: Copy the Avatar files
The component itself. Create each one at the same path in your project. It's your code from here on.components/fabricui/avatar.tsx"use client"; import * as React from "react"; import { mount } from "@/lib/fabricui/mount"; import { weave as drawWeave, speaker } from "@/lib/fabricui/avatar-engine"; import "./avatar.css"; const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect; const cx = (...c: (string | false | undefined)[]) => c.filter(Boolean).join(" "); /** "Mira Chen" → "MC" */ const initialsOf = (name = "") => { const w = name.trim().split(/\s+/).filter(Boolean); return ((w[0]?.[0] ?? "") + (w.length > 1 ? w[w.length - 1][0] : "")).toUpperCase(); };components/fabricui/avatar.css/* * Avatar — a person, small. Shared by every page that shows people. * * <span class="avatar avatar--md" data-name="Mira Chen">MC</span> initials * <span class="avatar avatar--md" data-name="Mira Chen" data-weave></span> weave (avatar.js draws it) * * Sizes: --xs 24, --sm 32, --md 40 (default), --lg 56. * A weave avatar is a 1-bit dither pattern made from the person's name (the * same ordered dither as the cloth): unique to them, the same every time. * * Status: add <span class="avatar__status" data-status="online|away|busy|offline"> * inside. Group: wrap in .avatar-stack (it fans open on hover and focus). * Speaking: wrap in .avatar-speaker; avatar.js turns the speaker to cloth. */ .avatar:where(.fui-avatar, .fui-avatar *) { --s: 40px;lib/fabricui/avatar-engine.js/* * avatar-engine.js — draws weave avatars and turns speakers to cloth (see avatar.css). From avatar.js. * * Weave: every [data-weave] avatar gets a 1-bit dither pattern made from its * data-name: a soft field (a few blobs placed by a hash of the name) run * through the same 4×4 Bayer dither the cloth uses. Same name, same weave. * * The weave is drawn once and kept as an image, so the cloth (which draws * the page's own markup) can carry it too. * * Speaker: speaker(wrapper) returns { speak(on) }. While someone * speaks, their avatar turns to cloth and soft ripples run out from its * middle with a voice-like rhythm (cloth-drop.js speak()); when they stop * it lies flat again. */ import { Dither } from "./dither";Step 4: 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 5: Use it
Import it from @/components/fabricui/avatar. The Usage example below shows it in a page.
Code
"use client";
import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { weave as drawWeave, speaker } from "@/lib/fabricui/avatar-engine";
import "./avatar.css";
const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
const cx = (...c: (string | false | undefined)[]) => c.filter(Boolean).join(" ");
/** "Mira Chen" → "MC" */
const initialsOf = (name = "") => {
const w = name.trim().split(/\s+/).filter(Boolean);
return ((w[0]?.[0] ?? "") + (w.length > 1 ? w[w.length - 1][0] : "")).toUpperCase();
};
/*
* Avatar — a person, small. Shared by every page that shows people.
*
* <span class="avatar avatar--md" data-name="Mira Chen">MC</span> initials
* <span class="avatar avatar--md" data-name="Mira Chen" data-weave></span> weave (avatar.js draws it)
*
* Sizes: --xs 24, --sm 32, --md 40 (default), --lg 56.
* A weave avatar is a 1-bit dither pattern made from the person's name (the
* same ordered dither as the cloth): unique to them, the same every time.
*
* Status: add <span class="avatar__status" data-status="online|away|busy|offline">
* inside. Group: wrap in .avatar-stack (it fans open on hover and focus).
* Speaking: wrap in .avatar-speaker; avatar.js turns the speaker to cloth.
*/
.avatar:where(.fui-avatar, .fui-avatar *) {
--s: 40px;/*
* avatar-engine.js — draws weave avatars and turns speakers to cloth (see avatar.css). From avatar.js.
*
* Weave: every [data-weave] avatar gets a 1-bit dither pattern made from its
* data-name: a soft field (a few blobs placed by a hash of the name) run
* through the same 4×4 Bayer dither the cloth uses. Same name, same weave.
*
* The weave is drawn once and kept as an image, so the cloth (which draws
* the page's own markup) can carry it too.
*
* Speaker: speaker(wrapper) returns { speak(on) }. While someone
* speaks, their avatar turns to cloth and soft ripples run out from its
* middle with a voice-like rhythm (cloth-drop.js speak()); when they stop
* it lies flat again.
*/
import { Dither } from "./dither";Usage
import { Avatar, AvatarStack, AvatarSpeaker } from "@/components/fabricui/avatar";
export function Member() {
return <Avatar name="Mira Chen" weave status="online" />;
}
export function People() {
return (
<AvatarStack aria-label="People in this file">
<Avatar as="button" name="Mira Chen" weave />
<Avatar as="button" name="Jon Park" weave />
<Avatar as="button" className="avatar--more" aria-label="2 more">+2</Avatar>
</AvatarStack>
);
}
export function OnCall({ talking }: { talking: boolean }) {
return (
<AvatarSpeaker speaking={talking}>
<Avatar size="lg" name="Ada Lee" weave />
</AvatarSpeaker>
);
}Props
| Prop | Type | Default |
|---|---|---|
| name The person's name: their initials, the weave's seed, and the label screen readers hear. | string | — |
| size 24, 32, 40 or 56px. | "xs" | "sm" | "md" | "lg" | "md" |
| weave A 1-bit dither pattern made from the name instead of the initials: unique to them, the same every time. | boolean | false |
| status A dot in the corner. It pops when it changes. | "online" | "away" | "busy" | "offline" | — |
| as button, to make it clickable and reachable with Tab. | "span" | "button" | "span" |
| decorative No label of its own, when what it sits in already names the person. | boolean | false |
| children Shown instead of the initials. With className="avatar--more" it's the "+3" at the end of a group. | ReactNode | — |
| AvatarStack Overlapping avatars that fan open on hover and focus. | component | — |
| AvatarSpeaker Wraps one Avatar. While speaking, it turns to cloth and ripples out from the middle like a voice. | component | — |