Gradient
An animated gradient that flows like liquid: glass ribbons with colour-split edges, soft folds of silk, a glow through reeded glass, colour woven into cloth, a beam of light pinched to a point, a glow of heat in thermal colours, or a mesh of drifting colour. The pointer stirs it; the library's dither finishes it.
Installation
npx shadcn@latest add https://fabrickit.org/r/gradient.jsonpnpm dlx shadcn@latest add https://fabrickit.org/r/gradient.jsonyarn shadcn@latest add https://fabrickit.org/r/gradient.jsonbunx --bun shadcn@latest add https://fabrickit.org/r/gradient.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/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 Gradient files
The component itself. Create each one at the same path in your project. It's your code from here on.components/fabricui/gradient.tsx"use client"; import * as React from "react"; import { mount } from "@/lib/fabricui/mount"; import { GradientEngine } from "@/lib/fabricui/gradient-engine"; import "./gradient.css"; const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect; export type GradientVariant = "glass" | "silk" | "fluted" | "weave" | "beam" | "thermal" | "mesh"; /** Ready-made colours for `colors`, deep to light. Blue is the default. */ export const GRADIENT_COLORS = { blue: ["#0b2a5e", "#2f74d6", "#bfe0ff"], violet: ["#2a0b5e", "#7b45e6", "#e2ccff"], rose: ["#4a0626", "#e0457b", "#ffd0e2"],components/fabricui/gradient.css/* * gradient.css — Gradient (gradient-engine.js). The gradient is drawn on the * canvas behind the element's content; size the element and put anything in it. */ .grad:where(.fui-gradient, .fui-gradient *) { position: relative; isolation: isolate; overflow: hidden; background: #010205; /* until the first frame, and without WebGL */ color: #f3f8ff; } .grad__canvas:where(.fui-gradient, .fui-gradient *) { position: absolute; inset: 0; z-index: -1; display: block;lib/fabricui/gradient-engine.js/* * gradient-engine.js — Gradient: an animated gradient that flows like liquid, * drawn in a WebGL shader and finished with the library's ordered dither. * * const g = new GradientEngine(root, { variant: "glass" }); * g.set({ variant: "silk", speed: 0.6, dither: 0.8 }); * g.destroy(); * * Two looks, the same flow under both (a sine field bent through itself a few * times, so it folds and curls without ever repeating): * glass — thin liquid-glass ribbons over a dark, soft glow: a bright rim * where each ribbon turns away, split into colour at its edge, and * the glow behind bent through it. * silk — broad soft folds of satin: lit from above-left, with a sheen * along the crests and black in the valleys. * fluted — soft pools of colour seen through reeded glass: upright flutes,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/gradient. The Usage example below shows it in a page.
Code
"use client";
import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { GradientEngine } from "@/lib/fabricui/gradient-engine";
import "./gradient.css";
const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
export type GradientVariant = "glass" | "silk" | "fluted" | "weave" | "beam" | "thermal" | "mesh";
/** Ready-made colours for `colors`, deep to light. Blue is the default. */
export const GRADIENT_COLORS = {
blue: ["#0b2a5e", "#2f74d6", "#bfe0ff"],
violet: ["#2a0b5e", "#7b45e6", "#e2ccff"],
rose: ["#4a0626", "#e0457b", "#ffd0e2"],/*
* gradient.css — Gradient (gradient-engine.js). The gradient is drawn on the
* canvas behind the element's content; size the element and put anything in it.
*/
.grad:where(.fui-gradient, .fui-gradient *) {
position: relative;
isolation: isolate;
overflow: hidden;
background: #010205; /* until the first frame, and without WebGL */
color: #f3f8ff;
}
.grad__canvas:where(.fui-gradient, .fui-gradient *) {
position: absolute;
inset: 0;
z-index: -1;
display: block;/*
* gradient-engine.js — Gradient: an animated gradient that flows like liquid,
* drawn in a WebGL shader and finished with the library's ordered dither.
*
* const g = new GradientEngine(root, { variant: "glass" });
* g.set({ variant: "silk", speed: 0.6, dither: 0.8 });
* g.destroy();
*
* Two looks, the same flow under both (a sine field bent through itself a few
* times, so it folds and curls without ever repeating):
* glass — thin liquid-glass ribbons over a dark, soft glow: a bright rim
* where each ribbon turns away, split into colour at its edge, and
* the glow behind bent through it.
* silk — broad soft folds of satin: lit from above-left, with a sheen
* along the crests and black in the valleys.
* fluted — soft pools of colour seen through reeded glass: upright flutes,Usage
"use client";
import { Gradient, GRADIENT_COLORS } from "@/components/fabricui/gradient";
export function Hero() {
return (
<Gradient variant="glass" style={{ height: 480, borderRadius: 24 }}>
<h1 style={{ padding: 48 }}>Made of fabric</h1>
</Gradient>
);
}
// soft silk, slower, in a ready-made colour, in coarse dots
export function Banner() {
return <Gradient variant="silk" speed={0.6} dither={1} colors={GRADIENT_COLORS.violet} style={{ height: 240 }} />;
}
// or your own colours, deep to light
export function Card() {
return <Gradient variant="glass" colors={["#3b0a45", "#a43ad6", "#f4c6ff"]} style={{ height: 200, borderRadius: 20 }} />;
}Props
| Prop | Type | Default |
|---|---|---|
| variant glass: liquid-glass ribbons with bright, colour-split rims over a dark glow. silk: soft satin folds with a sheen on the crests. fluted: soft pools of colour seen through reeded glass, each flute squeezing and shearing the glow behind it. weave: the gradient woven into twill cloth, round threads in diagonal ribs, shimmering two-tone as the colour moves. beam: the gradient as light pouring in from the left between two great black curves, pinched to a point that runs on to the right as a thin line; pulses of colour pump along the line and through the point, swelling out to the left, each a slightly different hue, while neighbouring hues swirl and mix through the beam; rays sway and the curved edges catch a tinted light; with prism, white light pumps along the line and splits through the point into colour. In light mode it's drawn on white, fading in from white and deepening to the point; the pointer stirs the rays and nudges the point. thermal: heat filling the whole frame, cold at the top and hot at the base, blended with no edges at all, in a heat map from your deep and mid colours through lavender to orange and red (your colours turned round the hue wheel); slow plumes of heat rise through it, the bands drift and waver gently, its sides run a little cooler, and the pointer warms where it moves. mesh: five pools of colour, your deep, mid and light and your mid turned toward violet and toward teal (or five colours of your own), each drifting on its own slow loop and blending smoothly edge to edge across the frame; the pointer stirs them. | "glass" | "silk" | "fluted" | "weave" | "beam" | "thermal" | "mesh" | "glass" |
| prism Beam only: white light pumps along the thin line and, as through a prism, splits at the point into sky blue, hot pink, orange and cream fanned across the beam, each blended smoothly into the next, its colours sliding as the light streams. | boolean | false |
| speed How fast it flows. | number | 1 |
| dither The library's ordered dither: 0 is smooth (just enough grain to stop banding), 1 is coarse dots. | number | 0.3 |
| interactive The flow swirls round the pointer as it moves and settles when it stops. | boolean | true |
| paused Hold it still. It also pauses off-screen and in a hidden tab, and holds still with reduced motion. | boolean | false |
| colors Your own colours, deep to light (one to three), or a preset: GRADIENT_COLORS.blue, violet, rose, ember, mint or mono. Mesh takes up to five, one for each of its pools, in your order. Changing them blends. Default: the theme's blues, on near-black or white. | string[] | — |
| children Content to sit on it; the gradient is drawn behind. | ReactNode | — |