Combobox
Type to find an option, or several.
Installation
npx shadcn@latest add https://fabrickit.org/r/combobox.jsonpnpm dlx shadcn@latest add https://fabrickit.org/r/combobox.jsonyarn shadcn@latest add https://fabrickit.org/r/combobox.jsonbunx --bun shadcn@latest add https://fabrickit.org/r/combobox.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/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 Combobox files
The component itself. Create each one at the same path in your project. It's your code from here on.components/fabricui/combobox.tsx"use client"; import * as React from "react"; import { mount } from "@/lib/fabricui/mount"; import { Combobox as Engine } from "@/lib/fabricui/combobox-engine"; import { weave } from "@/lib/fabricui/avatar-engine"; import "./badge.css"; import "./checkbox.css"; import "./avatar.css"; import "./combobox.css"; const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect; export type ComboboxOption = { value: string; label: string;components/fabricui/combobox.css/* * Combobox — a field you type into to find an option. Shared by every page * with one; combobox.js drives it (class Combobox). Built like the Select: * the field is the trigger, and the list hangs from behind it, dropping open * as cloth (cloth-drop.js) and collapsing plainly. While you type, the list * filters in place (the cloth steps aside first, so it never shows a stale * list) and the matched letters are lit. * * Single: pick one (the field shows it). Multiple: picks become chips in * the field; Backspace in an empty field takes the last one off. */ /* the field floats over the page with its list; the box keeps the field's height in the flow */ .cb:where(.fui-combobox, .fui-combobox *) { position: relative; width: 100%; height: var(--cb-h, 38px); } .cb__panel:where(.fui-combobox, .fui-combobox *) { position: absolute; inset: 0 0 auto 0; z-index: 10; } /* ---------- The field ---------- */lib/fabricui/combobox-engine.js/* * combobox-engine.js — a field you type into to find an option (see combobox.css). * From combobox.js. Uses cloth-drop.js; badge.css for chips; checkbox.css for multiple. * Added for React: setValue() (a value set from code), setOptions() and destroy(). * * const cb = new Combobox(el, { * options: [{ value, label, meta?, lead?: "<html>", group?, dot?: "#hex" }], * value: "ada", // or ["bug", "docs"] with multiple * multiple: false, * placeholder: "Search people", * label: "Assignee", // for screen readers * create: { label: q => `Invite “${q}”`, make: q => ({ value, label }) }, // optional * empty: q => "No one matches", * onChange: value => …, * onRender: listEl => …, // after the list is drawn (avatars) * });Step 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/combobox. The Usage example below shows it in a page.
Code
"use client";
import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { Combobox as Engine } from "@/lib/fabricui/combobox-engine";
import { weave } from "@/lib/fabricui/avatar-engine";
import "./badge.css";
import "./checkbox.css";
import "./avatar.css";
import "./combobox.css";
const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
export type ComboboxOption = {
value: string;
label: string;/*
* Combobox — a field you type into to find an option. Shared by every page
* with one; combobox.js drives it (class Combobox). Built like the Select:
* the field is the trigger, and the list hangs from behind it, dropping open
* as cloth (cloth-drop.js) and collapsing plainly. While you type, the list
* filters in place (the cloth steps aside first, so it never shows a stale
* list) and the matched letters are lit.
*
* Single: pick one (the field shows it). Multiple: picks become chips in
* the field; Backspace in an empty field takes the last one off.
*/
/* the field floats over the page with its list; the box keeps the field's height in the flow */
.cb:where(.fui-combobox, .fui-combobox *) { position: relative; width: 100%; height: var(--cb-h, 38px); }
.cb__panel:where(.fui-combobox, .fui-combobox *) { position: absolute; inset: 0 0 auto 0; z-index: 10; }
/* ---------- The field ---------- *//*
* combobox-engine.js — a field you type into to find an option (see combobox.css).
* From combobox.js. Uses cloth-drop.js; badge.css for chips; checkbox.css for multiple.
* Added for React: setValue() (a value set from code), setOptions() and destroy().
*
* const cb = new Combobox(el, {
* options: [{ value, label, meta?, lead?: "<html>", group?, dot?: "#hex" }],
* value: "ada", // or ["bug", "docs"] with multiple
* multiple: false,
* placeholder: "Search people",
* label: "Assignee", // for screen readers
* create: { label: q => `Invite “${q}”`, make: q => ({ value, label }) }, // optional
* empty: q => "No one matches",
* onChange: value => …,
* onRender: listEl => …, // after the list is drawn (avatars)
* });Usage
"use client";
import * as React from "react";
import { Combobox, avatarLead } from "@/components/fabricui/combobox";
export function Assignee() {
const [who, setWho] = React.useState("ada");
return (
<Combobox
label="Assign to"
placeholder="Search people"
value={who}
onValueChange={setWho}
options={[
{ value: "mira", label: "Mira Chen", meta: "Design", lead: avatarLead("Mira Chen") },
{ value: "ada", label: "Ada Lee", meta: "Engineering", lead: avatarLead("Ada Lee") },
]}
/>
);
}
export function Labels() {
const [labels, setLabels] = React.useState(["feature"]);
return (
<Combobox
multiple
label="Labels"
placeholder="Add labels"
value={labels}
onValueChange={setLabels}
options={[
{ value: "bug", label: "Bug", dot: "var(--fabric-danger)" },
{ value: "feature", label: "Feature", dot: "var(--fabric-success)" },
]}
create={{ label: q => `Create ${q}`, make: q => ({ value: q.toLowerCase(), label: q }) }}
/>
);
}Props
| Prop | Type | Default |
|---|---|---|
| options The choices. meta is dim text on the right; group puts it under a heading (while nothing's typed); lead is HTML before the label (avatarLead(name) makes an avatar); dot is a coloured dot (multiple). | { value, label, meta?, group?, lead?, dot? }[] | — |
| label What screen readers hear for the field. | string | — |
| placeholder The empty field's hint. | string | "Search" |
| multiple Pick several: they become chips in the field, and the list stays open. | boolean | false |
| value The choice (an array with multiple), if you control it. | string | string[] | — |
| defaultValue The starting choice when you don't. | string | string[] | — |
| onValueChange Called when the choice changes. | (value) => void | — |
| create Offer to make one from what was typed: label(q) is the row's text, make(q) returns the new option. | { label(q), make(q) } | — |
| empty What the list says when nothing matches. | (q) => string | — |
| ref Open or close it from code; restore puts the field back to the choice. | { open(), close({ restore }) } | — |