Data table
Sort, search, select and page through records.
Installation
npx shadcn@latest add https://fabrickit.org/r/data-table.jsonpnpm dlx shadcn@latest add https://fabrickit.org/r/data-table.jsonyarn shadcn@latest add https://fabrickit.org/r/data-table.jsonbunx --bun shadcn@latest add https://fabrickit.org/r/data-table.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/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 andlib/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. */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";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 4: Copy the Data table files
The component itself. Create each one at the same path in your project. It's your code from here on.components/fabricui/data-table.tsx"use client"; import * as React from "react"; import { mount } from "@/lib/fabricui/mount"; import { DataTable as Engine } from "@/lib/fabricui/data-table-engine"; import { start } from "@/lib/fabricui/skeleton-engine"; import { weave } from "@/lib/fabricui/avatar-engine"; import "./button.css"; import "./checkbox.css"; import "./skeleton.css"; import "./badge.css"; import "./avatar.css"; import "./data-table.css"; const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;components/fabricui/data-table.css/* * Data table — rows of records to sort, search, select and page through. * Shared by every page with a table; table.js drives it (class DataTable). * * <div class="dt"> the frame * <div class="dt__bar">…</div> a toolbar (title, search, actions): the page's own * <div class="dt__body"></div> DataTable renders the table and its footer here * </div> * * Effects, where they fit: * - dither: while a page or sort is loading, the rows are skeletons and one * glint sweeps across the whole table (skeleton.css/js); * - goo: the current page's highlight in the pager slides between numbers * like a drop (goo-slide.js); * - fabric: the bar of actions for selected rows pops in as cloth, and a * row's menu drops from its button (cloth-drop.js, context-menu.js).lib/fabricui/data-table-engine.js/* * data-table-engine.js — DataTable: sorting, searching, selecting and paging a * list of records into a .dt__body (see data-table.css). From table.js. Uses * checkbox.css for row selection, skeleton-engine.js for the loading glint, * goo-slide.js for the pager. Added for React: destroy(). * * const table = new DataTable(el, { * rows, // your records * rowId: r => r.id, * columns: [ * { key: "name", label: "Name", width: "34%", sort: r => r.name, html: r => `…`, skeleton: "person" }, * { key: "amount", label: "Amount", align: "end", sort: r => r.cents, html: r => `…` }, * { key: "more", width: "48px", html: r => `…` }, // no label, no sort * ], * pageSize: 6, * selectable: true, // a checkbox column, select-all for the pageStep 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/data-table. The Usage example below shows it in a page.
Code
"use client";
import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { DataTable as Engine } from "@/lib/fabricui/data-table-engine";
import { start } from "@/lib/fabricui/skeleton-engine";
import { weave } from "@/lib/fabricui/avatar-engine";
import "./button.css";
import "./checkbox.css";
import "./skeleton.css";
import "./badge.css";
import "./avatar.css";
import "./data-table.css";
const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
/*
* Data table — rows of records to sort, search, select and page through.
* Shared by every page with a table; table.js drives it (class DataTable).
*
* <div class="dt"> the frame
* <div class="dt__bar">…</div> a toolbar (title, search, actions): the page's own
* <div class="dt__body"></div> DataTable renders the table and its footer here
* </div>
*
* Effects, where they fit:
* - dither: while a page or sort is loading, the rows are skeletons and one
* glint sweeps across the whole table (skeleton.css/js);
* - goo: the current page's highlight in the pager slides between numbers
* like a drop (goo-slide.js);
* - fabric: the bar of actions for selected rows pops in as cloth, and a
* row's menu drops from its button (cloth-drop.js, context-menu.js)./*
* data-table-engine.js — DataTable: sorting, searching, selecting and paging a
* list of records into a .dt__body (see data-table.css). From table.js. Uses
* checkbox.css for row selection, skeleton-engine.js for the loading glint,
* goo-slide.js for the pager. Added for React: destroy().
*
* const table = new DataTable(el, {
* rows, // your records
* rowId: r => r.id,
* columns: [
* { key: "name", label: "Name", width: "34%", sort: r => r.name, html: r => `…`, skeleton: "person" },
* { key: "amount", label: "Amount", align: "end", sort: r => r.cents, html: r => `…` },
* { key: "more", width: "48px", html: r => `…` }, // no label, no sort
* ],
* pageSize: 6,
* selectable: true, // a checkbox column, select-all for the pageUsage
import { DataTable, type DataTableColumn } from "@/components/fabricui/data-table";
type Invoice = { id: number; customer: string; cents: number };
const esc = (s: string) => s.replace(/[&<>"]/g, c => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]!);
const money = (c: number) => (c / 100).toLocaleString("en-US", { style: "currency", currency: "USD" });
const columns: DataTableColumn<Invoice>[] = [
{ key: "id", label: "Invoice", width: "24%", sort: r => r.id, html: r => `INV-${r.id}` },
{ key: "customer", label: "Customer", sort: r => r.customer, html: r => esc(r.customer) },
{ key: "amount", label: "Amount", align: "end", sort: r => r.cents, html: r => money(r.cents) },
];
export function Invoices({ rows }: { rows: Invoice[] }) {
return <DataTable rows={rows} columns={columns} pageSize={6} loadMs={380} toolbar={<h2 className="dt__title">Invoices</h2>} />;
}Props
| Prop | Type | Default |
|---|---|---|
| rows Your records. New rows draw again, keeping the selection of rows still there. | R[] | — |
| columns html(row) is the cell as HTML (escape what users typed). sort(row) makes the header sort: ascending, descending, none. skeleton is the cell's placeholder while loading: "person", "badge" or "none". A new columns array takes effect on the next draw (cells, sorting, skeletons); the header (which columns, labels, widths) is set when the table mounts. | { key, label?, width?, align?, sort?, html, skeleton? }[] | — |
| rowId Each row's id. A new one takes effect on the next draw. | (row) => id | row => row.id |
| pageSize Rows per page. A change draws again right away. | number | 8 |
| selectable A checkbox column with select-all for the page; clicking a row ticks it. Read once, when the table mounts. | boolean | false |
| search Does a row match? Used by ref.search(query). A new one takes effect on the next draw. | (row, query) => boolean | — |
| loadMs A short load on page and sort changes: skeletons with the dither glint. Read once, when the table mounts. | number | 0 |
| empty What shows when nothing matches, as HTML. A new one takes effect on the next draw. | (query) => { icon?, title, sub?, action? } | — |
| summary The footer's left side, as HTML ("1–6 of 24" by default). A new one takes effect on the next draw. | (view, from, to) => string | — |
| onSelect Called with the selected rows' ids. | (ids) => void | — |
| toolbar The bar above the table: a title (className="dt__title"), a search, buttons. | ReactNode | — |
| children Goes after the table: a bar of actions for selected rows (className="dt-bulk"), say. | ReactNode | — |
| ref Search, select, show a load or draw again from code. | { search(q), select(ids), load(ms), render(), selected } | — |