Chart
Area, bar and line. Hover a chart, or focus it and use the arrow keys.
Installation
npx shadcn@latest add https://fabrickit.org/r/chart.jsonpnpm dlx shadcn@latest add https://fabrickit.org/r/chart.jsonyarn shadcn@latest add https://fabrickit.org/r/chart.jsonbunx --bun shadcn@latest add https://fabrickit.org/r/chart.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,lib/fabricui/goo-slide.js/* * goo-slide.js — the library's sliding highlight (Tabs, Radio cards). * * The highlight moves like a drop of liquid: its two edges are separate * springs. The edge on the side it's heading to is quick and the other is * lazier, so on the way the pill stretches toward its target, then the back * catches up and it settles to exactly the new option's size. While it's * stretched it squashes a touch the other way, like jelly. No blur, no * filters: just two springs, worked out every frame. * * const slide = new GooSlide(el, { axis: "x" }); // "x": side to side, "y": up and down * slide.to(optionEl); // animate there * slide.to(optionEl, true); // jump there (load, resize) * * The element is positioned with left/top 0 inside the options' container * (the option's offsetLeft/Top are used); the script sets its transform andStep 3: Copy the Chart files
The component itself. Create each one at the same path in your project. It's your code from here on.components/fabricui/chart.tsx"use client"; import * as React from "react"; import { mount } from "@/lib/fabricui/mount"; import { LineChart as LineChartEngine, BarChart as BarChartEngine } from "@/lib/fabricui/chart-engine"; import { GooSlide } from "@/lib/fabricui/goo-slide"; import "./chart.css"; const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect; const cx = (...c: (string | false | undefined)[]) => c.filter(Boolean).join(" "); /** One line of a LineChart. Colours go on the marks only (line, dots, keys), never on text. */ export type ChartSeries = { name: string; values: number[];components/fabricui/chart.css/* * Charts — line / area and bar (chart.js), and the card they sit in. * * <section class="ch-card"> * <header class="ch-head"> title, figure, delta, legend or range </header> * <div class="ch-plot" id="…"></div> chart.js fills it * * A plain card; values and dates in small pills; series colours only on * marks (lines, bars, dots, keys), never on text. Pills and the range control * follow the roundness rule (40% of the height). */ .ch-card:where(.fui-chart, .fui-chart *) { position: relative; display: flex; flex-direction: column; padding: 20px 20px 16px;lib/fabricui/chart-engine.jsimport { Dither } from "./dither"; /* * chart-engine.js — line / area and bar charts (see chart.css). * * const c = new LineChart(el, { * labels: [Date, …], // one per point * series: [{ name, values, color, gradient: [c1, c2], area: true }], * format: v => "$" + v, // values * axis: v => "$" + v, // y pills (defaults to format) * ticks: "days" | "months" | "all", // which x labels get a pill * readout: "callouts" | "tooltip", // a popup riding above the point, or one tooltip * glow: true, // a dithered aura behind the lines * }); * c.set({ labels, series }); // new data: the lines glide to it *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/chart. The Usage example below shows it in a page.
Code
"use client";
import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { LineChart as LineChartEngine, BarChart as BarChartEngine } from "@/lib/fabricui/chart-engine";
import { GooSlide } from "@/lib/fabricui/goo-slide";
import "./chart.css";
const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
const cx = (...c: (string | false | undefined)[]) => c.filter(Boolean).join(" ");
/** One line of a LineChart. Colours go on the marks only (line, dots, keys), never on text. */
export type ChartSeries = {
name: string;
values: number[];/*
* Charts — line / area and bar (chart.js), and the card they sit in.
*
* <section class="ch-card">
* <header class="ch-head"> title, figure, delta, legend or range </header>
* <div class="ch-plot" id="…"></div> chart.js fills it
*
* A plain card; values and dates in small pills; series colours only on
* marks (lines, bars, dots, keys), never on text. Pills and the range control
* follow the roundness rule (40% of the height).
*/
.ch-card:where(.fui-chart, .fui-chart *) {
position: relative;
display: flex;
flex-direction: column;
padding: 20px 20px 16px;import { Dither } from "./dither";
/*
* chart-engine.js — line / area and bar charts (see chart.css).
*
* const c = new LineChart(el, {
* labels: [Date, …], // one per point
* series: [{ name, values, color, gradient: [c1, c2], area: true }],
* format: v => "$" + v, // values
* axis: v => "$" + v, // y pills (defaults to format)
* ticks: "days" | "months" | "all", // which x labels get a pill
* readout: "callouts" | "tooltip", // a popup riding above the point, or one tooltip
* glow: true, // a dithered aura behind the lines
* });
* c.set({ labels, series }); // new data: the lines glide to it
*Usage
"use client";
import * as React from "react";
import { ChartCard, ChartHeader, ChartTitle, ChartFigure, ChartDelta, LineChart } from "@/components/fabricui/chart";
export function Revenue({ days, values }: { days: Date[]; values: number[] }) {
// the same array until the data changes (a new one makes the lines glide again)
const series = React.useMemo(() => [{ name: "Revenue", values, color: "#338ced", area: true }], [values]);
return (
<ChartCard aria-labelledby="rev">
<ChartHeader>
<div>
<ChartTitle id="rev">Revenue</ChartTitle>
<ChartFigure hero>$137,261</ChartFigure>
<ChartDelta value="5.1%">vs previous 30 days</ChartDelta>
</div>
</ChartHeader>
<LineChart
labels={days}
series={series}
format={v => "$" + v.toLocaleString("en-US")}
title="Revenue by day"
/>
</ChartCard>
);
}Props
| Prop | Type | Default |
|---|---|---|
| LineChart labels One per point. Dates read as "Nov 30" (or "Nov"). | (Date | string)[] | — |
| LineChart series The lines. gradient runs left to right along the line; area adds a dithered fill under it; width is in px (2). | { name, values, color, gradient?, area?, width? }[] | — |
| LineChart ticks Which points get a date pill: five spread out, each month's first, or every one. | "days" | "months" | "all" | "days" |
| LineChart readout One tooltip while hovering, or a popup riding above each line's point, always shown. | "tooltip" | "callouts" | "tooltip" |
| LineChart glow A dithered aura behind the lines, round the crosshair. | boolean | false |
| BarChart Bars, one per label. name is what a value is, in the tooltip ("Signups"). | { labels, values, color?, name?, format?, axis?, title? } | — |
| format / axis How a value reads in readouts and the hidden data table / on the y-axis pills (axis defaults to format). | (v: number) => string | — |
| title What the chart shows, for screen readers. | string | "Chart" |
| ChartRange A small segmented control (7D / 30D / 90D) whose pill slides to the chosen option. | { options, value?, defaultValue?, onValueChange? } | — |
| ChartCard, ChartHeader, ChartTitle, ChartFigure, ChartDelta, ChartLegend The card round a chart: its name, the big figure (hero for the large one), a change with an arrow, and a legend of { name, color }. | components | — |