Dropzone
Drop files to upload. The drop area takes them like stretched fabric.
Installation
npx shadcn@latest add https://fabrickit.org/r/dropzone.jsonpnpm dlx shadcn@latest add https://fabrickit.org/r/dropzone.jsonyarn shadcn@latest add https://fabrickit.org/r/dropzone.jsonbunx --bun shadcn@latest add https://fabrickit.org/r/dropzone.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 Dropzone files
The component itself. Create each one at the same path in your project. It's your code from here on.components/fabricui/dropzone.tsx"use client"; import * as React from "react"; import { mount } from "@/lib/fabricui/mount"; import { Dropzone as ZoneEngine, UploadList } from "@/lib/fabricui/dropzone-engine"; import "./button.css"; import "./progress.css"; import "./dropzone.css"; const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect; const cx = (...c: (string | false | undefined)[]) => c.filter(Boolean).join(" "); /** A file, or anything with a name and a size in bytes (a pretend one, for a demo). */ export type DropFile = File | { name: string; size: number; [key: string]: unknown }; /** Sends one file. Call onProgress with 0–1 as it goes; reject to fail (the row offers Retry); stop when signal aborts. */components/fabricui/dropzone.css/* * Dropzone — drop files in (or click / press Enter to browse), then watch * them upload. Shared by every page that takes files; dropzone.js drives it, * and each file's bar is the library's Progress (progress.css/js). * * One frame, in two tones: the drop area is the lighter top (panel, the * calendar's colour), sitting on the darker body (surface) that holds the * files. The top is the lighter one so the fabric's dithered shading shows. * * Effects: * - fabric, for the drag and drop: while files are dragged over, the drop * area turns to cloth held at its edges and sags under the pointer; the * drop lands with a dip, a bounce and a ring running out from where it * hit, then the cloth lies flat again (cloth-drop.js, cushion mode). * Rows arrive with a small spring and leave by closing up. *lib/fabricui/dropzone-engine.js/* * dropzone-engine.js — the drop zone and its list of uploads (see dropzone.css). * From dropzone.js. Uses progress-engine.js for the bars, and cloth-drop.js for the fabric. * * Changes from dropzone.js, for React: the cloth's wrapper is used if the * markup already has it; the window's drop guards belong to each zone (so a * zone can come and go); sample() returns a function that undoes it; the * file icons live here; the rows' buttons and bars carry Fabric Kit's classes; * destroy() stops the uploads still running. * * const zone = new Dropzone(frame, { * multiple: true, accept: ".pdf", * onFiles: files => list.add(files), * handoff: 0, // ms after the drop to hand the files over (0: at once) * }); * const list = new UploadList(frame.querySelector(".up-list"), {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/dropzone. The Usage example below shows it in a page.
Code
"use client";
import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { Dropzone as ZoneEngine, UploadList } from "@/lib/fabricui/dropzone-engine";
import "./button.css";
import "./progress.css";
import "./dropzone.css";
const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
const cx = (...c: (string | false | undefined)[]) => c.filter(Boolean).join(" ");
/** A file, or anything with a name and a size in bytes (a pretend one, for a demo). */
export type DropFile = File | { name: string; size: number; [key: string]: unknown };
/** Sends one file. Call onProgress with 0–1 as it goes; reject to fail (the row offers Retry); stop when signal aborts. *//*
* Dropzone — drop files in (or click / press Enter to browse), then watch
* them upload. Shared by every page that takes files; dropzone.js drives it,
* and each file's bar is the library's Progress (progress.css/js).
*
* One frame, in two tones: the drop area is the lighter top (panel, the
* calendar's colour), sitting on the darker body (surface) that holds the
* files. The top is the lighter one so the fabric's dithered shading shows.
*
* Effects:
* - fabric, for the drag and drop: while files are dragged over, the drop
* area turns to cloth held at its edges and sags under the pointer; the
* drop lands with a dip, a bounce and a ring running out from where it
* hit, then the cloth lies flat again (cloth-drop.js, cushion mode).
* Rows arrive with a small spring and leave by closing up.
*/*
* dropzone-engine.js — the drop zone and its list of uploads (see dropzone.css).
* From dropzone.js. Uses progress-engine.js for the bars, and cloth-drop.js for the fabric.
*
* Changes from dropzone.js, for React: the cloth's wrapper is used if the
* markup already has it; the window's drop guards belong to each zone (so a
* zone can come and go); sample() returns a function that undoes it; the
* file icons live here; the rows' buttons and bars carry Fabric Kit's classes;
* destroy() stops the uploads still running.
*
* const zone = new Dropzone(frame, {
* multiple: true, accept: ".pdf",
* onFiles: files => list.add(files),
* handoff: 0, // ms after the drop to hand the files over (0: at once)
* });
* const list = new UploadList(frame.querySelector(".up-list"), {Usage
import { Dropzone, type UploadFn } from "@/components/fabricui/dropzone";
// send one file, reporting 0–1 as it goes
const upload: UploadFn = (file, { onProgress, signal }) =>
new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open("POST", "/api/upload");
xhr.upload.onprogress = e => onProgress(e.loaded / e.total);
xhr.onload = () => (xhr.status < 300 ? resolve(xhr.response) : reject(new Error(xhr.statusText)));
xhr.onerror = reject;
signal.addEventListener("abort", () => xhr.abort());
const body = new FormData();
body.append("file", file as File);
xhr.send(body);
});
export function Attachments() {
return (
<Dropzone
label="Upload files: drop them here, or press Enter to browse"
title={<>Drop files here or <u>browse</u></>}
hint="Images, PDFs and ZIPs, up to 20 MB each"
upload={upload}
maxSize={20 * 2 ** 20}
/>
);
}Props
| Prop | Type | Default |
|---|---|---|
| title The drop area's text. Wrap the word to click in <u>. | ReactNode | — |
| hint The dim line under it. | ReactNode | — |
| icon The drop area's icon, an <svg className="icon">. | ReactNode | — |
| label What screen readers hear for the drop area. | string | — |
| upload Sends one file. With it, the zone keeps a list with a bar for each file; reject to fail it (its row offers Retry). | (file, { onProgress, signal }) => Promise | — |
| onFiles Called with the files as they arrive, before any upload. | (files) => void | — |
| maxSize The largest file, in bytes. Bigger ones are listed with the reason, not sent. | number | — |
| accept Extensions it takes, like [".pdf"]. | string[] | — |
| single One file at a time: a new one replaces it. | boolean | false |
| compact One line, for a field in a form. | boolean | false |
| onCountChange Called when the number of files in the list changes. | (count: number) => void | — |
| handoff ms after a drop before the files are handed over, so the dip lands first. | number | 0 |
| children Goes between the drop area and the list: a header with a count and Clear, say. | ReactNode | — |
| ref Drop files from code, clear the list, or make an element a file that can be dragged in. | { drop(files), clear(), sample(el, make) } | — |