Popover
A small task, hanging from its bar.
Share “Q3 report”
Anyone with the link can view.
acme.studio/f/q3-report
Mira Chen (you)Owner
Jon ParkCan edit
Allow comments
Installation
Terminal
npx shadcn@latest add https://fabrickit.org/r/popover.jsonTerminal
pnpm dlx shadcn@latest add https://fabrickit.org/r/popover.jsonTerminal
yarn shadcn@latest add https://fabrickit.org/r/popover.jsonTerminal
bunx --bun shadcn@latest add https://fabrickit.org/r/popover.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: Add what it builds on
It uses Button. Add it first, the same way (skip any you already have).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 Popover files
The component itself. Create each one at the same path in your project. It's your code from here on.components/fabricui/popover.tsx"use client"; import * as React from "react"; import { mount } from "@/lib/fabricui/mount"; import { Popover as PopoverEngine } from "@/lib/fabricui/popover-engine"; import { Button } from "@/components/fabricui/button"; import "./popover.css"; const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect; export type PopoverProps = { /** What the bar shows, left of the trigger: a file's icon and name, say. */ bar: React.ReactNode; /** The trigger button's text. */ trigger: React.ReactNode; /** The panel's name, for screen readers ("Share Q3 report"). */components/fabricui/popover.css/* * popover.css — Popover (popover.js). Moved out of popover.html. */ /* * Popover. A little task in a panel: here, sharing a file. The Share * button sits in the file's bar, and the panel hangs from behind that bar * like the Select's (cloth-drop.js, overlay, tucked 15px behind it): it * drops as cloth and collapses plainly. Non-modal: Escape, a click outside * or tabbing away closes it. */ .pop:where(.fui-popover, .fui-popover *) { position: relative; width: 320px; height: 52px; } /* keeps the bar's height; the panel overlays below */ /* ---------- File bar ---------- */ .pop__bar:where(.fui-popover, .fui-popover *) { position: relative;lib/fabricui/popover-engine.js/* * popover-engine.js — Popover (popover.css). From popover.js; the share demo's * Copy button moved out to the demo, so the panel can hold anything. */ import { ClothDrop } from "./cloth-drop"; /** * Popover: non-modal. The trigger toggles it. Opened from the keyboard, * focus moves to its first control; Escape closes and returns focus to the * trigger; a click outside or tabbing out closes it. */ export class Popover { /** * @param {HTMLElement} root the .pop element * @param {{ onOpenChange?: (open: boolean) => void, onHidden?: () => void }} [options] */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/popover. The Usage example below shows it in a page.
Code
components/fabricui/popover.tsx
"use client";
import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { Popover as PopoverEngine } from "@/lib/fabricui/popover-engine";
import { Button } from "@/components/fabricui/button";
import "./popover.css";
const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
export type PopoverProps = {
/** What the bar shows, left of the trigger: a file's icon and name, say. */
bar: React.ReactNode;
/** The trigger button's text. */
trigger: React.ReactNode;
/** The panel's name, for screen readers ("Share Q3 report"). */components/fabricui/popover.css
/*
* popover.css — Popover (popover.js). Moved out of popover.html.
*/
/*
* Popover. A little task in a panel: here, sharing a file. The Share
* button sits in the file's bar, and the panel hangs from behind that bar
* like the Select's (cloth-drop.js, overlay, tucked 15px behind it): it
* drops as cloth and collapses plainly. Non-modal: Escape, a click outside
* or tabbing away closes it.
*/
.pop:where(.fui-popover, .fui-popover *) { position: relative; width: 320px; height: 52px; } /* keeps the bar's height; the panel overlays below */
/* ---------- File bar ---------- */
.pop__bar:where(.fui-popover, .fui-popover *) {
position: relative;lib/fabricui/popover-engine.js
/*
* popover-engine.js — Popover (popover.css). From popover.js; the share demo's
* Copy button moved out to the demo, so the panel can hold anything.
*/
import { ClothDrop } from "./cloth-drop";
/**
* Popover: non-modal. The trigger toggles it. Opened from the keyboard,
* focus moves to its first control; Escape closes and returns focus to the
* trigger; a click outside or tabbing out closes it.
*/
export class Popover {
/**
* @param {HTMLElement} root the .pop element
* @param {{ onOpenChange?: (open: boolean) => void, onHidden?: () => void }} [options]
*/Usage
Usage
import { Popover } from "@/components/fabricui/popover";
import { Button } from "@/components/fabricui/button";
export function ShareFile() {
return (
<Popover
trigger="Share"
label="Share Q3 report"
bar={<div className="file__text"><div className="file__name">Q3 report.pdf</div><div className="file__meta">Edited 2h ago</div></div>}
>
<h2 className="pop__title">Share “Q3 report”</h2>
<p className="pop__desc">Anyone with the link can view.</p>
<div className="link">
<span className="link__url">acme.studio/f/q3-report</span>
<Button variant="soft" size="sm">Copy</Button>
</div>
</Popover>
);
}Props
| Prop | Type | Default |
|---|---|---|
| bar What the bar shows, left of the trigger. The classes file__icon, file__text, file__name and file__meta style a file's icon and name. | ReactNode | — |
| trigger The trigger button's text. | ReactNode | — |
| label The panel's name, for screen readers. | string | — |
| children The panel. pop__title, pop__desc, link, sep, people/person and setting style the parts of a share panel. | ReactNode | — |
| open Open or closed, if you control it. Leave out and the trigger toggles it. | boolean | — |
| onOpenChange Called when it opens or closes. | (open: boolean) => void | — |
| onHidden Called once it has finished closing: reset what's in it here, out of sight. | () => void | — |