File dropzone
Drag files in or browse: the zone wakes up when a file hovers over it, then each file uploads with its own progress bar, size and remove button.
"use client";
import {
useEffect,
useRef,
useState,
type CSSProperties,
type DragEvent,
} from "react";
import "./file-dropzone.css";
type Item = {
id: number;
name: string;
size: number;
type: string;
progress: number;
error?: string;
};
const MAX_MB = 10;
const fmt = (b: number) =>
b > 1e6
? `${(b / 1e6).toFixed(1)} MB`
: `${Math.max(1, Math.round(b / 1e3))} KB`;
const kind = (t: string, n: string) =>
t.startsWith("image/")
? "IMG"
: t === "application/pdf"
? "PDF"
: (n.split(".").pop() ?? "FILE").slice(0, 4).toUpperCase();
/**
* FileDropzone
* - Accepts drops and a hidden <input type="file">. The upload is simulated with a timer:
* replace `fakeUpload` with your own request (fetch + XMLHttpRequest progress, or a presigned URL).
* - Files over MAX_MB show an error instead of uploading.
*/
export function FileDropzone({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const [items, setItems] = useState<Item[]>([
{
id: 1,
name: "brand-guidelines.pdf",
size: 4_200_000,
type: "application/pdf",
progress: 100,
},
{
id: 2,
name: "hero-shot.jpg",
size: 2_800_000,
type: "image/jpeg",
progress: 64,
},
]);
const [over, setOver] = useState(false);
const input = useRef<HTMLInputElement>(null);
const nextId = useRef(3);
const depth = useRef(0); // dragenter/leave fire for children too; count them
// Fake upload loop: every 120ms push unfinished files forward.
useEffect(() => {
const t = setInterval(() => {
setItems((list) =>
list.some((i) => i.progress < 100 && !i.error)
? list.map((i) =>
i.progress < 100 && !i.error
? {
...i,
progress: Math.min(100, i.progress + 4 + Math.random() * 9),
}
: i,
)
: list,
);
}, 120);
return () => clearInterval(t);
}, []);
const addFiles = (files: FileList | null) => {
if (!files) return;
const added = Array.from(files).map((f) => ({
id: nextId.current++,
name: f.name,
size: f.size,
type: f.type,
progress: 0,
error:
f.size > MAX_MB * 1e6
? `Too big. The limit is ${MAX_MB} MB.`
: undefined,
}));
setItems((list) => [...added, ...list]);
};
const drag = (
e: DragEvent<HTMLButtonElement>,
type: "enter" | "leave" | "over" | "drop",
) => {
e.preventDefault();
if (type === "enter") depth.current++;
if (type === "leave") depth.current--;
if (type === "drop") {
depth.current = 0;
addFiles(e.dataTransfer.files);
}
setOver(depth.current > 0);
};
return (
<div className="gu-drop" data-theme={theme}>
<button
type="button"
className="gu-drop__zone"
data-over={over ? "" : undefined}
onClick={() => input.current?.click()}
onDragEnter={(e) => drag(e, "enter")}
onDragLeave={(e) => drag(e, "leave")}
onDragOver={(e) => drag(e, "over")}
onDrop={(e) => drag(e, "drop")}
>
<span className="gu-drop__icon" aria-hidden="true">
<svg viewBox="0 0 32 32">
<path d="M16 21V7m-6 6 6-6 6 6M6 21v3a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-3" />
</svg>
</span>
<strong>{over ? "Drop to upload" : "Drop files here or browse"}</strong>
<small>PNG, JPG, PDF or ZIP · up to {MAX_MB} MB each</small>
</button>
<input
ref={input}
type="file"
multiple
hidden
onChange={(e) => addFiles(e.target.files)}
/>
<ul className="gu-drop__list" aria-label="Files">
{items.map((it) => {
const done = it.progress >= 100;
return (
<li
key={it.id}
className="gu-drop__file"
data-state={it.error ? "error" : done ? "done" : "busy"}
style={{ "--p": `${it.progress}%` } as CSSProperties}
>
<span className="gu-drop__kind">{kind(it.type, it.name)}</span>
<span className="gu-drop__meta">
<b>{it.name}</b>
<small>
{it.error ??
(done
? `${fmt(it.size)} · Uploaded`
: `${fmt((it.size * it.progress) / 100)} of ${fmt(it.size)}`)}
</small>
{!it.error && (
<span
className="gu-drop__bar"
role="progressbar"
aria-label={`Uploading ${it.name}`}
aria-valuenow={Math.round(it.progress)}
aria-valuemin={0}
aria-valuemax={100}
/>
)}
</span>
<button
type="button"
className="gu-drop__x"
aria-label={`${done ? "Remove" : "Cancel"} ${it.name}`}
onClick={() => setItems((l) => l.filter((x) => x.id !== it.id))}
>
{done && !it.error ? (
<svg
viewBox="0 0 16 16"
aria-hidden="true"
className="gu-drop__check"
>
<path d="m3.5 8.5 3 3 6-7" />
</svg>
) : null}
<svg
viewBox="0 0 16 16"
aria-hidden="true"
className="gu-drop__cross"
>
<path d="m4 4 8 8m0-8-8 8" />
</svg>
</button>
</li>
);
})}
</ul>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: File dropzone
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A file upload area: a dashed drop zone that lights up when a file is dragged over it, plus a list of files each with a type badge, size, progress bar and remove/cancel button. Finished files show a green check that turns into × on hover.
## Layout and look
- Zone: full width (max 460px), 2px dashed border, radius 22px, a floating 56px orange icon tile with an upload arrow, bold title and muted limits line.
- Drag-over: orange border, orange-tinted background, scale 1.02, "Drop to upload", icon bobs faster.
- File rows: radius 16px; 44px type badge (PDF, IMG, ZIP), name (ellipsis), "1.8 MB of 2.8 MB" in tabular numbers, a 4px orange progress bar. Errors in orange-red text.
## Behavior and motion
1. Count dragenter/dragleave to avoid flicker from child elements.
2. Dropped or picked files are added to the top; oversize files get an error instead of uploading.
3. Progress is simulated (replace with real upload progress). Bars fill via a CSS variable --p.
4. New rows slide in 8px; check ↔ × swap with a rotate.
## Touch
The zone is a button that opens the native picker.
## Accessibility
- Progress bars have role="progressbar" with values and labels; remove buttons are labelled "Cancel/Remove {file}".
- prefers-reduced-motion: no float or slide.
## Props
theme. MAX_MB at the top; swap fakeUpload for a real request.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### FileDropzone.tsx
```tsx
"use client";
import {
useEffect,
useRef,
useState,
type CSSProperties,
type DragEvent,
} from "react";
import "./file-dropzone.css";
type Item = {
id: number;
name: string;
size: number;
type: string;
progress: number;
error?: string;
};
const MAX_MB = 10;
const fmt = (b: number) =>
b > 1e6
? `${(b / 1e6).toFixed(1)} MB`
: `${Math.max(1, Math.round(b / 1e3))} KB`;
const kind = (t: string, n: string) =>
t.startsWith("image/")
? "IMG"
: t === "application/pdf"
? "PDF"
: (n.split(".").pop() ?? "FILE").slice(0, 4).toUpperCase();
/**
* FileDropzone
* - Accepts drops and a hidden <input type="file">. The upload is simulated with a timer:
* replace `fakeUpload` with your own request (fetch + XMLHttpRequest progress, or a presigned URL).
* - Files over MAX_MB show an error instead of uploading.
*/
export function FileDropzone({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const [items, setItems] = useState<Item[]>([
{
id: 1,
name: "brand-guidelines.pdf",
size: 4_200_000,
type: "application/pdf",
progress: 100,
},
{
id: 2,
name: "hero-shot.jpg",
size: 2_800_000,
type: "image/jpeg",
progress: 64,
},
]);
const [over, setOver] = useState(false);
const input = useRef<HTMLInputElement>(null);
const nextId = useRef(3);
const depth = useRef(0); // dragenter/leave fire for children too; count them
// Fake upload loop: every 120ms push unfinished files forward.
useEffect(() => {
const t = setInterval(() => {
setItems((list) =>
list.some((i) => i.progress < 100 && !i.error)
? list.map((i) =>
i.progress < 100 && !i.error
? {
...i,
progress: Math.min(100, i.progress + 4 + Math.random() * 9),
}
: i,
)
: list,
);
}, 120);
return () => clearInterval(t);
}, []);
const addFiles = (files: FileList | null) => {
if (!files) return;
const added = Array.from(files).map((f) => ({
id: nextId.current++,
name: f.name,
size: f.size,
type: f.type,
progress: 0,
error:
f.size > MAX_MB * 1e6
? `Too big. The limit is ${MAX_MB} MB.`
: undefined,
}));
setItems((list) => [...added, ...list]);
};
const drag = (
e: DragEvent<HTMLButtonElement>,
type: "enter" | "leave" | "over" | "drop",
) => {
e.preventDefault();
if (type === "enter") depth.current++;
if (type === "leave") depth.current--;
if (type === "drop") {
depth.current = 0;
addFiles(e.dataTransfer.files);
}
setOver(depth.current > 0);
};
return (
<div className="gu-drop" data-theme={theme}>
<button
type="button"
className="gu-drop__zone"
data-over={over ? "" : undefined}
onClick={() => input.current?.click()}
onDragEnter={(e) => drag(e, "enter")}
onDragLeave={(e) => drag(e, "leave")}
onDragOver={(e) => drag(e, "over")}
onDrop={(e) => drag(e, "drop")}
>
<span className="gu-drop__icon" aria-hidden="true">
<svg viewBox="0 0 32 32">
<path d="M16 21V7m-6 6 6-6 6 6M6 21v3a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-3" />
</svg>
</span>
<strong>{over ? "Drop to upload" : "Drop files here or browse"}</strong>
<small>PNG, JPG, PDF or ZIP · up to {MAX_MB} MB each</small>
</button>
<input
ref={input}
type="file"
multiple
hidden
onChange={(e) => addFiles(e.target.files)}
/>
<ul className="gu-drop__list" aria-label="Files">
{items.map((it) => {
const done = it.progress >= 100;
return (
<li
key={it.id}
className="gu-drop__file"
data-state={it.error ? "error" : done ? "done" : "busy"}
style={{ "--p": `${it.progress}%` } as CSSProperties}
>
<span className="gu-drop__kind">{kind(it.type, it.name)}</span>
<span className="gu-drop__meta">
<b>{it.name}</b>
<small>
{it.error ??
(done
? `${fmt(it.size)} · Uploaded`
: `${fmt((it.size * it.progress) / 100)} of ${fmt(it.size)}`)}
</small>
{!it.error && (
<span
className="gu-drop__bar"
role="progressbar"
aria-label={`Uploading ${it.name}`}
aria-valuenow={Math.round(it.progress)}
aria-valuemin={0}
aria-valuemax={100}
/>
)}
</span>
<button
type="button"
className="gu-drop__x"
aria-label={`${done ? "Remove" : "Cancel"} ${it.name}`}
onClick={() => setItems((l) => l.filter((x) => x.id !== it.id))}
>
{done && !it.error ? (
<svg
viewBox="0 0 16 16"
aria-hidden="true"
className="gu-drop__check"
>
<path d="m3.5 8.5 3 3 6-7" />
</svg>
) : null}
<svg
viewBox="0 0 16 16"
aria-hidden="true"
className="gu-drop__cross"
>
<path d="m4 4 8 8m0-8-8 8" />
</svg>
</button>
</li>
);
})}
</ul>
</div>
);
}
```
### file-dropzone.css
```css
/* FileDropzone: change these colors to match your brand. */
.gu-drop {
--card: #fbf8f2;
--text: #0e0e10;
--muted: #57534b;
--line: rgb(14 14 16 / 0.2);
--soft: rgb(14 14 16 / 0.07);
--accent: #ff5a1f;
--ok: #16a34a;
--bad: #c2410c;
--focus: #2b35ff;
--ease: cubic-bezier(0.16, 1, 0.3, 1);
display: grid;
gap: 12px;
width: min(100%, 460px);
color: var(--text);
font-family: system-ui, sans-serif;
}
.gu-drop[data-theme="dark"] {
--card: #18181b;
--text: #f3eee3;
--muted: #aea89c;
--line: rgb(243 238 227 / 0.22);
--soft: rgb(243 238 227 / 0.08);
--ok: #4ade80;
--bad: #fb923c;
--focus: #d7ff3a;
}
.gu-drop__zone {
display: grid;
justify-items: center;
gap: 6px;
width: 100%;
padding: 32px 20px;
border: 2px dashed var(--line);
border-radius: 22px;
background: var(--card);
color: inherit;
font: inherit;
cursor: pointer;
transition:
border-color 0.25s,
background-color 0.25s,
transform 0.35s var(--ease);
}
.gu-drop__zone:hover,
.gu-drop__zone[data-over] {
border-color: var(--accent);
}
.gu-drop__zone[data-over] {
background: color-mix(in srgb, var(--accent) 12%, var(--card));
transform: scale(1.02);
}
.gu-drop__zone:focus-visible {
outline: 3px solid var(--focus);
outline-offset: 3px;
}
.gu-drop__zone strong {
font-size: 1.05rem;
font-weight: 800;
}
.gu-drop__zone small {
color: var(--muted);
}
.gu-drop__icon {
display: grid;
place-items: center;
width: 56px;
height: 56px;
margin-bottom: 6px;
border-radius: 18px;
background: var(--accent);
color: #0e0e10;
animation: gu-drop-float 3s ease-in-out infinite;
}
.gu-drop__zone[data-over] .gu-drop__icon {
animation-duration: 0.6s;
}
@keyframes gu-drop-float {
50% {
transform: translateY(-5px);
}
}
.gu-drop__icon svg {
width: 28px;
fill: none;
stroke: currentColor;
stroke-width: 2.4;
stroke-linecap: round;
stroke-linejoin: round;
}
.gu-drop__list {
display: grid;
gap: 8px;
margin: 0;
padding: 0;
list-style: none;
}
.gu-drop__file {
display: grid;
grid-template-columns: 44px minmax(0, 1fr) 36px;
align-items: center;
gap: 12px;
padding: 10px 10px 10px 12px;
border: 1px solid var(--soft);
border-radius: 16px;
background: var(--card);
animation: gu-drop-in 0.4s var(--ease);
}
@keyframes gu-drop-in {
from {
opacity: 0;
transform: translateY(-8px);
}
}
.gu-drop__kind {
display: grid;
place-items: center;
height: 44px;
border-radius: 10px;
background: var(--soft);
font-size: 0.68rem;
font-weight: 900;
letter-spacing: 0.04em;
}
.gu-drop__meta {
display: grid;
gap: 3px;
min-width: 0;
}
.gu-drop__meta b {
overflow: hidden;
font-size: 0.92rem;
text-overflow: ellipsis;
white-space: nowrap;
}
.gu-drop__meta small {
color: var(--muted);
font-size: 0.78rem;
font-variant-numeric: tabular-nums;
}
.gu-drop__file[data-state="error"] small {
color: var(--bad);
font-weight: 600;
}
.gu-drop__file[data-state="error"] {
border-color: color-mix(in srgb, var(--bad) 40%, transparent);
}
.gu-drop__bar {
height: 4px;
margin-top: 3px;
border-radius: 4px;
background:
linear-gradient(var(--accent), var(--accent)) 0 0 / var(--p) 100% no-repeat,
var(--soft);
transition: background-size 0.15s linear;
}
.gu-drop__file[data-state="done"] .gu-drop__bar {
background-color: var(--ok);
background-image: none;
height: 0;
margin: 0;
transition: height 0.3s 0.3s;
}
.gu-drop__x {
position: relative;
display: grid;
place-items: center;
width: 36px;
height: 36px;
border: 0;
border-radius: 50%;
background: none;
color: var(--muted);
cursor: pointer;
}
.gu-drop__x:hover {
background: var(--soft);
color: var(--text);
}
.gu-drop__x:focus-visible {
outline: 2px solid var(--focus);
}
.gu-drop__x svg {
position: absolute;
width: 16px;
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
transition:
opacity 0.2s,
transform 0.25s var(--ease);
}
/* Finished files show a green check that turns into × on hover */
.gu-drop__check {
color: var(--ok);
stroke-width: 2.6 !important;
}
.gu-drop__check + .gu-drop__cross {
opacity: 0;
transform: rotate(-90deg);
}
.gu-drop__x:hover .gu-drop__check,
.gu-drop__x:focus-visible .gu-drop__check {
opacity: 0;
transform: rotate(90deg);
}
.gu-drop__x:hover .gu-drop__cross,
.gu-drop__x:focus-visible .gu-drop__cross {
opacity: 1;
transform: none;
}
@media (prefers-reduced-motion: reduce) {
.gu-drop__icon,
.gu-drop__file {
animation: none;
}
.gu-drop__zone,
.gu-drop__x svg {
transition: none;
}
}
```
You might also want
Next piece: Flip business cardMulti-step form
Components, advanced
Activity rings
Components, intermediate
Before and after slider
Components, beginner