File Dropzone
file-dropzoneA drop target that is also a real file picker, so click, keyboard and drag all work. Checks type and size on drop as well as in the picker, and reports each rejected file by name.
"use client";
import { FileText, X } from "lucide-react";
import { useEffect, useState } from "react";
import { FileDropzone, formatBytes } from "@/components/forme/file-dropzone/file-dropzone";
interface Upload {
id: string;
name: string;
size: number;
progress: number;
}
export function FileDropzoneDemo() {
const [uploads, setUploads] = useState<Upload[]>([
{ id: "seed", name: "brand-guidelines.pdf", size: 2_480_000, progress: 100 },
]);
const [errors, setErrors] = useState<string[]>([]);
// A pretend upload, so the list has something to show. Yours would report
// progress from `XMLHttpRequest.upload.onprogress` or a presigned-URL client.
useEffect(() => {
if (uploads.every((upload) => upload.progress >= 100)) return;
const timer = setInterval(() => {
setUploads((current) =>
current.map((upload) =>
upload.progress >= 100
? upload
: { ...upload, progress: Math.min(100, upload.progress + 6 + Math.random() * 14) },
),
);
}, 180);
return () => clearInterval(timer);
}, [uploads]);
return (
<div className="flex w-full max-w-md flex-col gap-3">
<FileDropzone
hint="PNG, JPG or PDF, up to 10 MB each"
accept="image/png,image/jpeg,.pdf"
maxSize={10 * 1024 * 1024}
onFiles={(files) => {
setErrors([]);
setUploads((current) => [
...files.map((file) => ({
id: `${file.name}-${file.lastModified}-${Math.random()}`,
name: file.name,
size: file.size,
progress: 0,
})),
...current,
]);
}}
onReject={setErrors}
/>
{errors.length ? (
<ul role="alert" className="text-caption-1-regular text-destructive">
{errors.map((error) => (
<li key={error}>{error}</li>
))}
</ul>
) : null}
<ul className="flex flex-col gap-2">
{uploads.map((upload) => {
const done = upload.progress >= 100;
return (
<li
key={upload.id}
className="flex items-center gap-3 rounded-2xl border border-border bg-card p-3"
>
<span className="grid size-8 shrink-0 place-items-center rounded-lg bg-muted text-muted-foreground">
<FileText aria-hidden className="size-4" />
</span>
<div className="min-w-0 flex-1">
<div className="flex items-baseline justify-between gap-3">
<p className="truncate text-body-2-medium text-foreground">{upload.name}</p>
<p className="shrink-0 text-caption-1-regular tabular-nums text-muted-foreground">
{done ? formatBytes(upload.size) : `${Math.round(upload.progress)}%`}
</p>
</div>
<div className="mt-1.5 h-1 overflow-hidden rounded-full bg-muted">
<div
className={`h-full rounded-full transition-[width] duration-200 ${done ? "bg-success" : "bg-primary"}`}
style={{ width: `${upload.progress}%` }}
/>
</div>
</div>
<button
type="button"
aria-label={`Remove ${upload.name}`}
onClick={() =>
setUploads((current) => current.filter((entry) => entry.id !== upload.id))
}
className="grid size-6 shrink-0 cursor-pointer place-items-center rounded-sm text-muted-foreground hover:bg-accent hover:text-foreground"
>
<X aria-hidden className="size-3.5" />
</button>
</li>
);
})}
</ul>
</div>
);
}Install
Adds the component, its registry dependencies, and the token stylesheet you import once.
Usage
import { FileDropzone } from "@/components/forme/file-dropzone/file-dropzone";
<FileDropzone
accept="image/*"
maxSize={10 * 1024 * 1024}
hint="PNG or JPG up to 10 MB"
onFiles={upload}
onReject={(messages) => messages.forEach((message) => toast.error(message))}
/>When to use
Reach for it
- Uploading files by dropping them or picking them.
- Any other drop target, like an avatar or a banner, built on the exported
useFileDrop.
Use something else
- A single small file in a dense form, where a plain file input fits better.
Examples
7 more ways to put it to work. Install one by name and it lands beside the component, or copy it from the Code tab into your app.
Avatar
A round profile photo that is itself the drop target, with an instant preview, a remove button and the size limit under it.
"use client";
import { Camera, X } from "lucide-react";
import { useEffect, useState } from "react";
import { cn } from "@/components/forme/lib/cn";
import { formatBytes, useFileDrop } from "@/components/forme/file-dropzone/file-dropzone";
const MAX_SIZE = 2 * 1024 * 1024;
export function AvatarExample() {
const [file, setFile] = useState<File | null>(null);
const [preview, setPreview] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
// An object URL holds the image in memory until it is revoked, so each one
// is released when it is replaced, removed, or the field goes.
const [urls] = useState(() => new Set<string>());
useEffect(() => () => urls.forEach((url) => URL.revokeObjectURL(url)), [urls]);
function show(next: File | null) {
if (preview) {
URL.revokeObjectURL(preview);
urls.delete(preview);
}
const url = next ? URL.createObjectURL(next) : null;
if (url) urls.add(url);
setFile(next);
setPreview(url);
}
const { dragging, inputId, dropProps, inputProps } = useFileDrop({
accept: "image/png,image/jpeg,image/webp",
multiple: false,
maxSize: MAX_SIZE,
onFiles: ([next]) => {
setError(null);
show(next ?? null);
},
onReject: ([message]) => setError(message ?? null),
});
function clear() {
show(null);
setError(null);
}
return (
<div className="flex items-center gap-5">
<div className="relative">
{/* The avatar itself is the target: click it, tab to it, or drop on it. */}
{/* oxlint-disable-next-line jsx-a11y/no-noninteractive-element-interactions */}
<label
htmlFor={inputId}
{...dropProps}
className={cn(
"group relative grid size-20 cursor-pointer place-items-center overflow-hidden rounded-full border border-dashed border-control-border-hover bg-muted text-muted-foreground",
"hover:border-control-border-active hover:text-foreground",
"has-[:focus-visible]:outline-2 has-[:focus-visible]:outline-offset-2 has-[:focus-visible]:outline-ring",
"data-[dragging]:border-primary data-[dragging]:bg-brand-subtle data-[dragging]:text-brand",
preview && "border-solid border-border",
)}
>
<input {...inputProps} aria-describedby={`${inputId}-hint`} />
{preview ? (
// A local object URL, so a plain img: there is nothing for an image optimiser to fetch.
// eslint-disable-next-line @next/next/no-img-element
<img src={preview} alt="" className="size-full animate-enter object-cover" />
) : (
<Camera
aria-hidden
className="size-6 transition-[scale] duration-standard ease-standard group-data-[dragging]:scale-110"
/>
)}
<span className="sr-only">{preview ? "Change photo" : "Upload a photo"}</span>
</label>
{preview ? (
<button
type="button"
onClick={clear}
aria-label="Remove photo"
className="absolute -top-0.5 -right-0.5 grid size-6 animate-enter cursor-pointer place-items-center rounded-full border border-border bg-card text-muted-foreground shadow-sm outline-none hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring"
>
<X aria-hidden className="size-3.5" />
</button>
) : null}
</div>
<div className="flex min-w-0 flex-col gap-1">
<p className="text-body-medium text-foreground">
{dragging ? "Drop to use this photo" : file ? file.name : "Profile photo"}
</p>
<p id={`${inputId}-hint`} className="text-caption-1-regular text-muted-foreground">
{file ? formatBytes(file.size) : `PNG, JPG or WebP, up to ${formatBytes(MAX_SIZE)}`}
</p>
{error ? (
<p role="alert" className="text-caption-1-regular text-destructive-strong">
{error}
</p>
) : null}
</div>
</div>
);
}Compact
One strip for a form: an Add files button, the picks as thumbnails beside it and a count against the limit.
"use client";
import { FileText, Plus, X } from "lucide-react";
import { useEffect, useState } from "react";
import { cn } from "@/components/forme/lib/cn";
import { Button } from "@/components/forme/button/button";
import { useFileDrop } from "@/components/forme/file-dropzone/file-dropzone";
const MAX_FILES = 5;
interface Pick {
id: string;
file: File;
url: string | null;
}
export function CompactExample() {
const [picks, setPicks] = useState<Pick[]>([]);
const [error, setError] = useState<string | null>(null);
const left = MAX_FILES - picks.length;
// Release every preview URL when the field goes away.
const [urls] = useState(() => new Set<string>());
useEffect(() => () => urls.forEach((url) => URL.revokeObjectURL(url)), [urls]);
const { dragging, dropProps, inputProps, open } = useFileDrop({
accept: "image/*,.pdf",
maxFiles: left,
maxSize: 5 * 1024 * 1024,
disabled: left === 0,
onFiles: (files) => {
setError(null);
setPicks((current) => [
...current,
...files.map((file) => {
const url = file.type.startsWith("image/") ? URL.createObjectURL(file) : null;
if (url) urls.add(url);
return { id: crypto.randomUUID(), file, url };
}),
]);
},
onReject: (messages) => setError(messages[0] ?? null),
});
function remove(pick: Pick) {
if (pick.url) {
URL.revokeObjectURL(pick.url);
urls.delete(pick.url);
}
setPicks((current) => current.filter((entry) => entry.id !== pick.id));
}
return (
<div className="flex w-full max-w-lg flex-col gap-2">
{/* One strip for the button, the picks and the count: a form field, not a page section. */}
<div
{...dropProps}
className={cn(
"flex min-h-16 flex-wrap items-center gap-2 rounded-xl border border-dashed border-control-border-hover bg-card p-2",
"data-[dragging]:border-primary data-[dragging]:bg-brand-subtle",
)}
>
<input {...inputProps} aria-label="Attach files" />
<Button variant="secondary" size="small" leadingIcon={Plus} onClick={open} disabled={!left}>
Add files
</Button>
{picks.map((pick) => (
<span
key={pick.id}
className="group/pick relative size-12 shrink-0 animate-enter overflow-hidden rounded-md border border-border bg-muted"
>
{pick.url ? (
// eslint-disable-next-line @next/next/no-img-element
<img src={pick.url} alt={pick.file.name} className="size-full object-cover" />
) : (
<span className="grid size-full place-items-center text-muted-foreground">
<FileText aria-hidden className="size-5" />
<span className="sr-only">{pick.file.name}</span>
</span>
)}
<button
type="button"
onClick={() => remove(pick)}
aria-label={`Remove ${pick.file.name}`}
// Revealed on hover for a tidy strip, always there for touch and the keyboard.
className="absolute top-0.5 right-0.5 grid size-5 cursor-pointer place-items-center rounded-sm bg-card text-foreground shadow-sm outline-none focus-visible:ring-2 focus-visible:ring-ring pointer-fine:opacity-0 pointer-fine:group-hover/pick:opacity-100 pointer-fine:focus-visible:opacity-100"
>
<X aria-hidden className="size-3" />
</button>
</span>
))}
<span className="ml-auto px-1 text-caption-1-medium text-muted-foreground tnum">
{dragging ? "Drop to attach" : `${picks.length} / ${MAX_FILES}`}
</span>
</div>
{error ? (
<p role="alert" className="text-caption-1-regular text-destructive-strong">
{error}
</p>
) : (
<p className="text-caption-1-regular text-muted-foreground">
Images or PDFs, up to 5 MB each. Drop them anywhere on the strip.
</p>
)}
</div>
);
}Gallery
A drop zone over an image grid, seeded with saved photos, with a running total and Clear all.
"use client";
import { ImagePlus, X } from "lucide-react";
import { useEffect, useState } from "react";
import { Button } from "@/components/forme/button/button";
import { FileDropzone, formatBytes } from "@/components/forme/file-dropzone/file-dropzone";
interface Photo {
id: string;
name: string;
size: number;
src: string;
/** Object URLs are ours to release; saved photos are not. */
local: boolean;
}
// Photos already saved, as an edit form would load them.
const saved: Photo[] = [
{ id: "coast", name: "coast.webp", size: 182_000, src: "/images/coast.webp", local: false },
{ id: "dunes", name: "dunes.webp", size: 164_000, src: "/images/dunes.webp", local: false },
{ id: "forest", name: "forest.webp", size: 201_000, src: "/images/forest.webp", local: false },
];
export function GalleryExample() {
const [photos, setPhotos] = useState<Photo[]>(saved);
const [errors, setErrors] = useState<string[]>([]);
const [urls] = useState(() => new Set<string>());
useEffect(() => () => urls.forEach((url) => URL.revokeObjectURL(url)), [urls]);
const total = photos.reduce((sum, photo) => sum + photo.size, 0);
function remove(ids: Set<string>) {
for (const photo of photos) {
if (ids.has(photo.id) && photo.local) {
URL.revokeObjectURL(photo.src);
urls.delete(photo.src);
}
}
setPhotos((current) => current.filter((photo) => !ids.has(photo.id)));
}
return (
<div className="flex w-full max-w-xl flex-col gap-4">
<FileDropzone
label="Drop photos here or browse"
hint="JPG, PNG or WebP, up to 8 MB each"
accept="image/*"
maxSize={8 * 1024 * 1024}
maxFiles={12 - photos.length}
disabled={photos.length >= 12}
className="py-8"
onFiles={(files) => {
setErrors([]);
setPhotos((current) => [
...current,
...files.map((file) => {
const src = URL.createObjectURL(file);
urls.add(src);
return {
id: crypto.randomUUID(),
name: file.name,
size: file.size,
src,
local: true,
};
}),
]);
}}
onReject={setErrors}
/>
{errors.length ? (
<ul role="alert" className="text-caption-1-regular text-destructive-strong">
{errors.map((error) => (
<li key={error}>{error}</li>
))}
</ul>
) : null}
<div className="flex items-center justify-between gap-3">
<p className="text-body-2-medium text-foreground">
{photos.length} {photos.length === 1 ? "photo" : "photos"}
<span className="text-muted-foreground tnum"> · {formatBytes(total)}</span>
</p>
<Button
variant="ghost"
size="small"
disabled={!photos.length}
onClick={() => remove(new Set(photos.map((photo) => photo.id)))}
>
Clear all
</Button>
</div>
{photos.length ? (
<ul className="grid grid-cols-2 gap-2 sm:grid-cols-3">
{photos.map((photo, index) => (
<li
key={photo.id}
className="group/photo relative aspect-square animate-enter overflow-hidden rounded-xl border border-border bg-muted"
style={{ animationDelay: `${Math.min(index, 8) * 40}ms` }}
>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={photo.src}
alt={photo.name}
className="size-full object-cover transition-[scale] duration-enter ease-standard group-hover/photo:scale-105"
/>
{/* Media chrome over a photo: fixed black and white, not theme tokens. */}
<span className="pointer-events-none absolute inset-x-0 bottom-0 bg-gradient-to-t from-black/60 to-transparent px-2.5 pt-6 pb-2 text-caption-1-medium text-white">
<span className="block truncate">{photo.name}</span>
</span>
<button
type="button"
onClick={() => remove(new Set([photo.id]))}
aria-label={`Remove ${photo.name}`}
className="absolute top-2 right-2 grid size-7 cursor-pointer place-items-center rounded-md bg-black/55 text-white outline-none backdrop-blur-sm hover:bg-black/75 focus-visible:ring-2 focus-visible:ring-white pointer-fine:opacity-0 pointer-fine:group-hover/photo:opacity-100 pointer-fine:focus-visible:opacity-100"
>
<X aria-hidden className="size-3.5" />
</button>
</li>
))}
</ul>
) : (
<div className="grid place-items-center gap-2 rounded-xl border border-border bg-muted py-10 text-center">
<ImagePlus aria-hidden className="size-5 text-muted-foreground" />
<p className="text-body-2-regular text-muted-foreground">No photos yet.</p>
</div>
)}
</div>
);
}Upload queue
Each file as a card with its own progress bar, a retry when one fails, and counts of what is done, running and failed.
"use client";
import { CircleAlert, CircleCheck, FileText, RotateCw, X } from "lucide-react";
import { useEffect, useState } from "react";
import { cn } from "@/components/forme/lib/cn";
import { FileDropzone, formatBytes } from "@/components/forme/file-dropzone/file-dropzone";
type Status = "uploading" | "done" | "failed";
interface Upload {
id: string;
name: string;
size: number;
progress: number;
status: Status;
}
export function ProgressExample() {
const [uploads, setUploads] = useState<Upload[]>([
{ id: "brief", name: "launch-brief.pdf", size: 1_240_000, progress: 100, status: "done" },
{ id: "deck", name: "q3-review.key", size: 18_600_000, progress: 46, status: "failed" },
]);
// A pretend transfer so the queue has something to show, failing now and
// then so retry has something to do. Yours would read progress from
// `XMLHttpRequest.upload.onprogress` or a presigned-URL client.
const active = uploads.some((upload) => upload.status === "uploading");
useEffect(() => {
if (!active) return;
const timer = setInterval(() => {
setUploads((current) =>
current.map((upload) => {
if (upload.status !== "uploading") return upload;
if (Math.random() < 0.02) return { ...upload, status: "failed" };
const progress = Math.min(100, upload.progress + 4 + Math.random() * 12);
return { ...upload, progress, status: progress >= 100 ? "done" : "uploading" };
}),
);
}, 200);
return () => clearInterval(timer);
}, [active]);
const count = (status: Status) => uploads.filter((upload) => upload.status === status).length;
const update = (id: string, change: Partial<Upload>) =>
setUploads((current) =>
current.map((upload) => (upload.id === id ? { ...upload, ...change } : upload)),
);
return (
<div className="flex w-full max-w-lg flex-col gap-4">
<FileDropzone
hint="Any file, up to 50 MB each"
maxSize={50 * 1024 * 1024}
className="py-8"
onFiles={(files) =>
setUploads((current) => [
...files.map((file) => ({
id: crypto.randomUUID(),
name: file.name,
size: file.size,
progress: 0,
status: "uploading" as const,
})),
...current,
])
}
/>
<p
aria-live="polite"
className="flex flex-wrap gap-x-4 gap-y-1 text-caption-1-medium text-muted-foreground tnum"
>
<span>
<span className="text-foreground">{count("done")}</span> done
</span>
<span>
<span className="text-foreground">{count("uploading")}</span> uploading
</span>
{count("failed") ? (
<span className="text-destructive-strong">{count("failed")} failed</span>
) : null}
</p>
<ul className="flex flex-col gap-2">
{uploads.map((upload) => (
<li
key={upload.id}
className={cn(
"flex animate-enter items-center gap-3 rounded-xl border bg-card p-3 lit edge",
upload.status === "failed" ? "border-destructive/40" : "border-border",
)}
>
<span
className={cn(
"grid size-9 shrink-0 place-items-center rounded-md",
upload.status === "done" && "bg-success-subtle text-success-strong",
upload.status === "failed" && "bg-destructive-subtle text-destructive-strong",
upload.status === "uploading" && "bg-muted text-muted-foreground",
)}
>
{upload.status === "done" ? (
<CircleCheck key="done" aria-hidden className="size-4 animate-icon-swap" />
) : upload.status === "failed" ? (
<CircleAlert key="failed" aria-hidden className="size-4 animate-icon-swap" />
) : (
<FileText key="file" aria-hidden className="size-4" />
)}
</span>
<div className="flex min-w-0 flex-1 flex-col gap-1.5">
<div className="flex items-baseline justify-between gap-3">
<p className="truncate text-body-2-medium text-foreground">{upload.name}</p>
<p className="shrink-0 text-caption-1-medium text-muted-foreground tnum">
{upload.status === "done"
? formatBytes(upload.size)
: upload.status === "failed"
? "Upload failed"
: `${Math.round(upload.progress)}%`}
</p>
</div>
<div
role="progressbar"
aria-label={`${upload.name} upload`}
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={Math.round(upload.progress)}
className="h-1 overflow-hidden rounded-full bg-muted well"
>
<div
className={cn(
"h-full rounded-full transition-[width] duration-data ease-standard",
upload.status === "done" && "bg-success",
upload.status === "failed" && "bg-destructive",
upload.status === "uploading" && "bg-meter",
)}
style={{ width: `${upload.progress}%` }}
/>
</div>
</div>
{upload.status === "failed" ? (
<button
type="button"
onClick={() => update(upload.id, { status: "uploading", progress: 0 })}
aria-label={`Retry ${upload.name}`}
className="grid size-7 shrink-0 cursor-pointer place-items-center rounded-md text-muted-foreground outline-none hover:bg-ghost-hover hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring"
>
<RotateCw aria-hidden className="size-3.5" />
</button>
) : null}
<button
type="button"
onClick={() =>
setUploads((current) => current.filter((entry) => entry.id !== upload.id))
}
aria-label={
upload.status === "uploading" ? `Cancel ${upload.name}` : `Remove ${upload.name}`
}
className="grid size-7 shrink-0 cursor-pointer place-items-center rounded-md text-muted-foreground outline-none hover:bg-ghost-hover hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring"
>
<X aria-hidden className="size-3.5" />
</button>
</li>
))}
</ul>
</div>
);
}Table
Documents in a table you drop straight onto, with a progress ring while each sends and download, retry and delete per row.
"use client";
import {
Download,
FileArchive,
FileImage,
FileSpreadsheet,
FileText,
RotateCw,
Trash2,
Upload,
} from "lucide-react";
import { useEffect, useState } from "react";
import { cn } from "@/components/forme/lib/cn";
import { Badge } from "@/components/forme/badge/badge";
import { Button } from "@/components/forme/button/button";
import { formatBytes, useFileDrop } from "@/components/forme/file-dropzone/file-dropzone";
type Status = "uploading" | "done" | "failed";
interface Row {
id: string;
name: string;
size: number;
progress: number;
status: Status;
/** Set for a picked file, so the download button hands back what was chosen. */
url?: string;
}
function iconFor(name: string) {
const ext = name.split(".").pop()?.toLowerCase() ?? "";
if (["png", "jpg", "jpeg", "webp", "gif", "svg"].includes(ext)) return FileImage;
if (["csv", "xls", "xlsx", "numbers"].includes(ext)) return FileSpreadsheet;
if (["zip", "gz", "rar", "7z"].includes(ext)) return FileArchive;
return FileText;
}
/** A percent ring: the stroke draws round as the transfer runs. */
function Ring({ value }: { value: number }) {
return (
<svg viewBox="0 0 20 20" aria-hidden className="size-4 -rotate-90">
<circle cx="10" cy="10" r="8" fill="none" strokeWidth="2.5" className="stroke-muted" />
<circle
cx="10"
cy="10"
r="8"
fill="none"
strokeWidth="2.5"
strokeLinecap="round"
pathLength={100}
strokeDasharray="100"
strokeDashoffset={100 - value}
className="stroke-primary transition-[stroke-dashoffset] duration-data ease-standard"
/>
</svg>
);
}
export function TableExample() {
const [rows, setRows] = useState<Row[]>([
{ id: "a", name: "contract-signed.pdf", size: 412_000, progress: 100, status: "done" },
{ id: "b", name: "revenue-2026.xlsx", size: 1_980_000, progress: 100, status: "done" },
{ id: "c", name: "brand-assets.zip", size: 48_300_000, progress: 62, status: "failed" },
]);
const [urls] = useState(() => new Set<string>());
useEffect(() => () => urls.forEach((url) => URL.revokeObjectURL(url)), [urls]);
// A pretend transfer; replace it with your upload's progress events.
const active = rows.some((row) => row.status === "uploading");
useEffect(() => {
if (!active) return;
const timer = setInterval(() => {
setRows((current) =>
current.map((row) => {
if (row.status !== "uploading") return row;
const progress = Math.min(100, row.progress + 5 + Math.random() * 15);
return { ...row, progress, status: progress >= 100 ? "done" : "uploading" };
}),
);
}, 220);
return () => clearInterval(timer);
}, [active]);
const { dragging, dropProps, inputProps, open } = useFileDrop({
maxSize: 100 * 1024 * 1024,
onFiles: (files) =>
setRows((current) => [
...current,
...files.map((file) => {
const url = URL.createObjectURL(file);
urls.add(url);
return {
id: crypto.randomUUID(),
name: file.name,
size: file.size,
progress: 0,
status: "uploading" as const,
url,
};
}),
]),
});
function remove(row: Row) {
if (row.url) {
URL.revokeObjectURL(row.url);
urls.delete(row.url);
}
setRows((current) => current.filter((entry) => entry.id !== row.id));
}
const action =
"grid size-7 cursor-pointer place-items-center rounded-md text-muted-foreground outline-none hover:bg-ghost-hover hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-40";
return (
// The whole table takes a drop, so there is no separate zone to aim for.
<div
{...dropProps}
className={cn(
"w-full max-w-2xl overflow-hidden rounded-xl border border-border bg-card lit edge",
"data-[dragging]:border-primary data-[dragging]:bg-brand-subtle",
)}
>
<input {...inputProps} aria-label="Upload files" />
<div className="flex items-center justify-between gap-3 border-b border-border px-4 py-3">
<div>
<p className="text-body-medium text-foreground">Documents</p>
<p className="text-caption-1-regular text-muted-foreground">
{dragging ? "Drop to add to this folder" : "Drop files on the table, or upload them."}
</p>
</div>
<Button size="small" leadingIcon={Upload} onClick={open}>
Upload
</Button>
</div>
<table className="w-full text-left">
<thead>
<tr className="border-b border-border text-caption-1-medium text-muted-foreground">
<th className="px-4 py-2">Name</th>
<th className="hidden px-4 py-2 sm:table-cell">Size</th>
<th className="px-4 py-2">Status</th>
<th className="w-0 px-4 py-2">
<span className="sr-only">Actions</span>
</th>
</tr>
</thead>
<tbody>
{rows.map((row) => {
const Icon = iconFor(row.name);
return (
<tr
key={row.id}
className="animate-enter border-b border-border last:border-0 hover:bg-accent"
>
<td className="max-w-0 px-4 py-2.5">
<span className="flex items-center gap-2.5">
<Icon aria-hidden className="size-4 shrink-0 text-muted-foreground" />
<span className="truncate text-body-2-medium text-foreground">{row.name}</span>
</span>
</td>
<td className="hidden px-4 py-2.5 text-caption-1-medium whitespace-nowrap text-muted-foreground tnum sm:table-cell">
{formatBytes(row.size)}
</td>
<td className="px-4 py-2.5 whitespace-nowrap">
{row.status === "uploading" ? (
<span className="inline-flex items-center gap-2 text-caption-1-medium text-muted-foreground tnum">
<Ring value={row.progress} />
{Math.round(row.progress)}%
</span>
) : row.status === "done" ? (
<Badge variant="success" size="sm">
Uploaded
</Badge>
) : (
<Badge variant="destructive" size="sm">
Failed
</Badge>
)}
</td>
<td className="px-3 py-2.5">
<span className="flex items-center justify-end gap-0.5">
{row.status === "failed" ? (
<button
type="button"
aria-label={`Retry ${row.name}`}
onClick={() =>
setRows((current) =>
current.map((entry) =>
entry.id === row.id
? { ...entry, status: "uploading", progress: 0 }
: entry,
),
)
}
className={action}
>
<RotateCw aria-hidden className="size-3.5" />
</button>
) : (
<a
href={row.url}
download={row.name}
aria-label={`Download ${row.name}`}
aria-disabled={!row.url || row.status !== "done"}
onClick={(event) => {
if (!row.url || row.status !== "done") event.preventDefault();
}}
className={cn(
action,
(!row.url || row.status !== "done") && "cursor-not-allowed opacity-40",
)}
>
<Download aria-hidden className="size-3.5" />
</a>
)}
<button
type="button"
aria-label={`Delete ${row.name}`}
onClick={() => remove(row)}
className={action}
>
<Trash2 aria-hidden className="size-3.5" />
</button>
</span>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
);
}Sortable
Listing photos you put in order by dragging a handle or with the arrow keys; the first one is the cover.
"use client";
import { X } from "lucide-react";
import { useEffect, useState } from "react";
import { SortableList } from "@/components/forme/sortable-list/sortable-list";
import { FileDropzone, formatBytes } from "@/components/forme/file-dropzone/file-dropzone";
interface Shot {
id: string;
name: string;
size: number;
src: string;
local: boolean;
}
const saved: Shot[] = [
{ id: "city", name: "city.webp", size: 214_000, src: "/images/city.webp", local: false },
{ id: "lake", name: "lake.webp", size: 176_000, src: "/images/lake.webp", local: false },
{ id: "glacier", name: "glacier.webp", size: 190_000, src: "/images/glacier.webp", local: false },
];
export function SortableExample() {
const [shots, setShots] = useState<Shot[]>(saved);
const [urls] = useState(() => new Set<string>());
useEffect(() => () => urls.forEach((url) => URL.revokeObjectURL(url)), [urls]);
function remove(shot: Shot) {
if (shot.local) {
URL.revokeObjectURL(shot.src);
urls.delete(shot.src);
}
setShots((current) => current.filter((entry) => entry.id !== shot.id));
}
return (
<div className="flex w-full max-w-md flex-col gap-3">
<FileDropzone
label="Add listing photos"
hint="The first photo is the cover. Drag the handles to reorder."
accept="image/*"
className="py-7"
onFiles={(files) =>
setShots((current) => [
...current,
...files.map((file) => {
const src = URL.createObjectURL(file);
urls.add(src);
return {
id: crypto.randomUUID(),
name: file.name,
size: file.size,
src,
local: true,
};
}),
])
}
/>
<SortableList
label="Listing photos"
items={shots}
onReorder={setShots}
getId={(shot) => shot.id}
getLabel={(shot) => shot.name}
renderItem={(shot) => {
const cover = shots[0]?.id === shot.id;
return (
<div className="flex items-center gap-3">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={shot.src}
alt=""
className="size-11 shrink-0 rounded-md border border-border object-cover"
/>
<div className="flex min-w-0 flex-1 flex-col">
<span className="flex items-center gap-2">
<span className="truncate text-body-2-medium text-foreground">{shot.name}</span>
{cover ? (
<span className="shrink-0 animate-enter rounded-sm bg-brand-subtle px-1.5 text-caption-2-medium text-brand-strong">
Cover
</span>
) : null}
</span>
<span className="text-caption-1-regular text-muted-foreground tnum">
{formatBytes(shot.size)}
</span>
</div>
<button
type="button"
onClick={() => remove(shot)}
aria-label={`Remove ${shot.name}`}
className="grid size-7 shrink-0 cursor-pointer place-items-center rounded-md text-muted-foreground outline-none hover:bg-ghost-hover hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring"
>
<X aria-hidden className="size-3.5" />
</button>
</div>
);
}}
/>
</div>
);
}Cover image
A wide banner with Change and Remove over the photo, a percent ring while the new one uploads, and the image guidelines below.
"use client";
import { ImageUp, Trash2 } from "lucide-react";
import { useEffect, useState } from "react";
import { cn } from "@/components/forme/lib/cn";
import { useFileDrop } from "@/components/forme/file-dropzone/file-dropzone";
export function CoverExample() {
const [cover, setCover] = useState<string | null>("/images/aurora.webp");
const [progress, setProgress] = useState<number | null>(null);
const [error, setError] = useState<string | null>(null);
const [urls] = useState(() => new Set<string>());
useEffect(() => () => urls.forEach((url) => URL.revokeObjectURL(url)), [urls]);
// A pretend upload that counts to 100; replace it with your request's progress.
const uploading = progress !== null;
useEffect(() => {
if (!uploading) return;
const timer = setInterval(() => {
setProgress((value) => {
if (value === null) return null;
const next = value + 6 + Math.random() * 14;
return next >= 100 ? null : next;
});
}, 160);
return () => clearInterval(timer);
}, [uploading]);
function replace(next: string | null) {
if (cover && urls.has(cover)) {
URL.revokeObjectURL(cover);
urls.delete(cover);
}
if (next) urls.add(next);
setCover(next);
}
const { dragging, inputId, dropProps, inputProps, open } = useFileDrop({
accept: "image/png,image/jpeg,image/webp",
multiple: false,
maxSize: 10 * 1024 * 1024,
onFiles: ([file]) => {
if (!file) return;
setError(null);
replace(URL.createObjectURL(file));
setProgress(0);
},
onReject: ([message]) => setError(message ?? null),
});
return (
<div className="flex w-full max-w-2xl flex-col gap-3">
<div
{...dropProps}
className={cn(
"group/cover relative aspect-[21/9] overflow-hidden rounded-xl border bg-muted",
cover ? "border-border" : "border-dashed border-control-border-hover",
"data-[dragging]:border-primary data-[dragging]:ring-4 data-[dragging]:ring-brand-subtle",
)}
>
<input {...inputProps} aria-label="Cover image" aria-describedby={`${inputId}-hint`} />
{cover ? (
// Keyed by source, so a new cover fades in over the old one's place.
// eslint-disable-next-line @next/next/no-img-element
<img
key={cover}
src={cover}
alt="Cover"
className={cn(
"size-full animate-enter object-cover transition-[filter,scale] duration-enter ease-standard",
(uploading || dragging) && "scale-[1.02] blur-[2px]",
)}
/>
) : (
<button
type="button"
onClick={open}
className="flex size-full cursor-pointer flex-col items-center justify-center gap-2 text-center outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset"
>
<ImageUp aria-hidden className="size-6 text-muted-foreground" />
<span className="text-body-medium text-foreground">Add a cover image</span>
<span className="text-caption-1-regular text-muted-foreground">or drop one here</span>
</button>
)}
{/* Media chrome over the photo: fixed black and white, not theme tokens. */}
{cover && !uploading ? (
<div className="absolute inset-0 flex items-end justify-end gap-2 bg-gradient-to-t from-black/45 via-transparent p-3 pointer-fine:opacity-0 pointer-fine:group-hover/cover:opacity-100 pointer-fine:has-[:focus-visible]:opacity-100">
<button
type="button"
onClick={open}
className="inline-flex h-8 cursor-pointer items-center gap-1.5 rounded-md bg-black/55 px-2.5 text-body-2-medium text-white outline-none backdrop-blur-sm hover:bg-black/75 focus-visible:ring-2 focus-visible:ring-white"
>
<ImageUp aria-hidden className="size-4" />
Change cover
</button>
<button
type="button"
onClick={() => replace(null)}
aria-label="Remove cover"
className="grid size-8 cursor-pointer place-items-center rounded-md bg-black/55 text-white outline-none backdrop-blur-sm hover:bg-black/75 focus-visible:ring-2 focus-visible:ring-white"
>
<Trash2 aria-hidden className="size-4" />
</button>
</div>
) : null}
{uploading || dragging ? (
<div className="absolute inset-0 grid animate-fade-in place-items-center bg-black/35 text-white">
{uploading ? (
<div
role="progressbar"
aria-label="Uploading cover"
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={Math.round(progress)}
className="relative grid size-16 place-items-center"
>
<svg viewBox="0 0 40 40" aria-hidden className="absolute inset-0 -rotate-90">
<circle
cx="20"
cy="20"
r="17"
fill="none"
strokeWidth="3"
className="stroke-white/25"
/>
<circle
cx="20"
cy="20"
r="17"
fill="none"
strokeWidth="3"
strokeLinecap="round"
pathLength={100}
strokeDasharray="100"
strokeDashoffset={100 - progress}
className="stroke-white transition-[stroke-dashoffset] duration-standard ease-standard"
/>
</svg>
<span className="text-caption-1-semibold tnum">{Math.round(progress)}%</span>
</div>
) : (
<span className="text-body-medium">Drop to replace the cover</span>
)}
</div>
) : null}
</div>
{error ? (
<p role="alert" className="text-caption-1-regular text-destructive-strong">
{error}
</p>
) : (
<p id={`${inputId}-hint`} className="text-caption-1-regular text-muted-foreground">
2100 × 900 or larger, PNG, JPG or WebP up to 10 MB. Keep faces and text away from the
bottom edge, where the profile overlaps.
</p>
)}
</div>
);
}API reference
Read from the source, so it is the API of the file you install. A prop marked * is required.
FileDropzone
A drop target that is also a button.
| Prop | Type | Default | Description |
|---|---|---|---|
label | ReactNode | "Drop files here or browse" | |
hint | ReactNode | — | Shown under the label, e.g. "PNG, JPG or PDF up to 10 MB". |
className | string | — | |
accept | string | — | Passed to the file input, and checked again on drop, where the browser does not. |
multiple | boolean | — | |
maxSize | number | — | Bytes. Files over this are rejected with a message rather than silently dropped. |
maxFiles | number | — | How many files may be taken at once. Extra files are rejected by name. |
disabled | boolean | — | |
onFiles | (files: File[]) => void | — | Receives only the files that passed every check. |
onReject | (messages: string[]) => void | — | Receives a message for each rejected file. |
Accessibility
- The zone is a
<label>for a hidden file input, so clicking, Enter, Space and the native picker all work. - Rejected files come back to
onRejectas messages to show, never silently dropped.
Responsive behaviour
- On a phone, tapping opens the system file picker; dragging is only an extra path.
Related
ProAvatar Upload
A profile-photo editor: pick or drop an image, place it in the circle with drag, pinch, scroll or the keyboard, zoom from 1× to 4×, then upload the 512 px crop with progress, cancel, failure and retry.
inputs

Calendar
A month grid for a single date or a range, with no date library. Full WAI-ARIA keyboard support, min, max and disabled days, a range that previews under the pointer, and a fixed six-week height so nothing below it jumps.
inputs

Input
A text field with a real label, optional description, an error that is announced and wired up with aria-describedby, and room for an icon on either side. Sizes and the invalid state come from the same tokens as every other Forme surface.
inputs






