Dropzone

Drop files to upload. The drop area takes them like stretched fabric.

Drop files here or browseImages, PDFs and ZIPs, up to 20 MB each
Drag a sample file in

Send for signature

Attach the contract and we'll send it to Acme Studio.

Drop a PDF or browseOne file, up to 10 MB
Drag a sample file in

Installation

Terminal
npx shadcn@latest add https://fabrickit.org/r/dropzone.json

This 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.

Code

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"), {

Usage

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

PropTypeDefault
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.

booleanfalse
compact

One line, for a field in a form.

booleanfalse
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.

number0
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) }—