Skeleton
Placeholders that shimmer with the library's dither, or ripple as fabric, while things load.
Installation
npx shadcn@latest add https://fabrickit.org/r/skeleton.jsonpnpm dlx shadcn@latest add https://fabrickit.org/r/skeleton.jsonyarn shadcn@latest add https://fabrickit.org/r/skeleton.jsonbunx --bun shadcn@latest add https://fabrickit.org/r/skeleton.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 Skeleton files
The component itself. Create each one at the same path in your project. It's your code from here on.components/fabricui/skeleton.tsx"use client"; import * as React from "react"; import { mount } from "@/lib/fabricui/mount"; import { ClothDrop } from "@/lib/fabricui/cloth-drop"; import { start } from "@/lib/fabricui/skeleton-engine"; import "./skeleton.css"; const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect; const cx = (...c: (string | false | undefined)[]) => c.filter(Boolean).join(" "); const len = (v: number | string | undefined) => (typeof v === "number" ? `${v}px` : v); export type SkeletonProps = Omit<React.HTMLAttributes<HTMLSpanElement>, "children"> & { /** "line" (text, 10px tall), "circle" (avatars) or "block" (anything else: set height). */ shape?: "line" | "circle" | "block"; /** Its width (px, or any CSS length like "64%"). 100% by default; a circle's height follows it. */components/fabricui/skeleton.css/* * Skeleton — placeholders while something loads. * * The library's dither does the shimmer: a band of 1-bit Bayer dots glints * across the grey shapes (skeleton.js), in page space, so one band passes * over every shape on the page together. When the content arrives it fades * in over the placeholders, which fade out. * * <div class="loadable" data-loading> * <div class="loadable__skeleton" aria-hidden="true"> * <span class="skeleton skeleton--circle" style="--w:40px"></span> * <span class="skeleton skeleton--line" style="--w:60%"></span> * </div> * <div class="loadable__content">…the real thing…</div> * </div> *lib/fabricui/skeleton-engine.js/* * skeleton-engine.js — the dither glint over every .skeleton on the page. From skeleton.js. * * Each skeleton gets a canvas. A soft band moves diagonally across the page; * where it passes, 2px dots light up through a 4×4 Bayer dither (the same * ordered dither as the cloth), so the shimmer is made of the library's dots * rather than a smooth gradient. The band crosses the whole .loadable (or the * lone skeleton) in one continuous sweep over all its shapes, and its clock * starts when the skeleton appears, so the first pass comes right away. * Only skeletons that are showing are drawn, at ~30fps; with reduced motion * nothing moves. A .loadable[data-fabric] shimmers as cloth instead. Any * element with [data-skeleton-area] (a table, say) sweeps as one, too. * * Changes from skeleton.js, for React: the loop runs while at least one * skeleton is mounted (start() returns its stop), instead of from page load. */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/skeleton. The Usage example below shows it in a page.
Code
"use client";
import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { ClothDrop } from "@/lib/fabricui/cloth-drop";
import { start } from "@/lib/fabricui/skeleton-engine";
import "./skeleton.css";
const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
const cx = (...c: (string | false | undefined)[]) => c.filter(Boolean).join(" ");
const len = (v: number | string | undefined) => (typeof v === "number" ? `${v}px` : v);
export type SkeletonProps = Omit<React.HTMLAttributes<HTMLSpanElement>, "children"> & {
/** "line" (text, 10px tall), "circle" (avatars) or "block" (anything else: set height). */
shape?: "line" | "circle" | "block";
/** Its width (px, or any CSS length like "64%"). 100% by default; a circle's height follows it. *//*
* Skeleton — placeholders while something loads.
*
* The library's dither does the shimmer: a band of 1-bit Bayer dots glints
* across the grey shapes (skeleton.js), in page space, so one band passes
* over every shape on the page together. When the content arrives it fades
* in over the placeholders, which fade out.
*
* <div class="loadable" data-loading>
* <div class="loadable__skeleton" aria-hidden="true">
* <span class="skeleton skeleton--circle" style="--w:40px"></span>
* <span class="skeleton skeleton--line" style="--w:60%"></span>
* </div>
* <div class="loadable__content">…the real thing…</div>
* </div>
*/*
* skeleton-engine.js — the dither glint over every .skeleton on the page. From skeleton.js.
*
* Each skeleton gets a canvas. A soft band moves diagonally across the page;
* where it passes, 2px dots light up through a 4×4 Bayer dither (the same
* ordered dither as the cloth), so the shimmer is made of the library's dots
* rather than a smooth gradient. The band crosses the whole .loadable (or the
* lone skeleton) in one continuous sweep over all its shapes, and its clock
* starts when the skeleton appears, so the first pass comes right away.
* Only skeletons that are showing are drawn, at ~30fps; with reduced motion
* nothing moves. A .loadable[data-fabric] shimmers as cloth instead. Any
* element with [data-skeleton-area] (a table, say) sweeps as one, too.
*
* Changes from skeleton.js, for React: the loop runs while at least one
* skeleton is mounted (start() returns its stop), instead of from page load.
*/Usage
import { Loadable, Skeleton } from "@/components/fabricui/skeleton";
export function Profile({ user }: { user?: { name: string; role: string } }) {
return (
<Loadable
loading={!user}
skeleton={
<div style={{ display: "flex", alignItems: "center", gap: 14 }}>
<Skeleton shape="circle" width={56} />
<div style={{ flex: 1, display: "grid", gap: 14 }}>
<Skeleton width={110} />
<Skeleton width={180} />
</div>
</div>
}
>
<h3>{user?.name}</h3>
<p>{user?.role}</p>
</Loadable>
);
}Props
| Prop | Type | Default |
|---|---|---|
| loading Loadable: while true the placeholders show; when it turns false the content fades in over them as they fade out. | boolean | — |
| skeleton Loadable: the placeholders, Skeleton shapes laid out like the content. One glint sweeps across all of them together. | ReactNode | — |
| fabric Loadable: the whole card ripples as cloth while it loads, and settles flat before the content comes in. | boolean | false |
| paused Loadable, fabric: stop its motion (while it's off screen, say); the placeholders stay. | boolean | false |
| skeletonClassName / contentClassName Loadable: classes for the two layers, to lay them out the same. | string | — |
| shape Skeleton: a line of text (10px tall), an avatar, or anything else (set height). | "line" | "circle" | "block" | "line" |
| width / height Skeleton: px, or any CSS length ("64%"). Width is 100% by default; a circle's height follows its width. | number | string | — |