Skip to content
Product block

Drag & Drop Upload

Dropzone with per-file progress and a drag counter that fixes the flicker every naive dragleave handler produces.

219 lineslucide-reactAdded 8 Aug 2026
  • upload
  • dropzone
  • drag and drop
  • files
  • progress

What's included

  • components/file-dropzone.tsx
  • Needs lucide-react

Works with

  • React
  • Next.js
  • Tailwind CSS
  • TypeScript

npx hoverlab add file-dropzone

Or over MCP, from your editor's agent — no account needed.

License

Free to read, copy and install, for personal and non-commercial projects. Shipping it in client work or a paid product needs Pro ($79 once). The source lands in your repo and stops being ours — no attribution, nothing to upgrade.

Was this useful?

Start a page with this section — add more, order them, and leave with the page source.

Preview

  • brand-guidelines.pdf2.3 MB

    Uploaded

  • hero-render.png8.5 MB

  • archive.zip142.0 MB

    Larger than the 20 MB limit

Rendered live in your current theme — this is the same component whose source is below, not a screenshot of it.

Source

components/file-dropzone.tsx
'use client'

/**
 * <FileDropzone> — drag-and-drop upload with per-file progress.
 *
 * The drag counter is the part everyone gets wrong. `dragleave` fires when
 * the pointer crosses into a *child* element, so a naive
 * `onDragLeave={() => setActive(false)}` makes the highlight flicker as you
 * move across the zone's own contents. Counting enter/leave pairs and only
 * clearing at zero is the fix.
 *
 * The zone is also a real <label> wrapping a file input, so clicking and
 * keyboard activation both work without re-implementing either — a div with
 * an onClick that opens a file picker is unreachable by keyboard.
 *
 * Rejected files stay listed with the reason rather than being dropped
 * silently, which is how users end up believing an upload succeeded.
 */

import * as React from 'react'
import { CloudUpload, File as FileIcon, X, CircleCheck, TriangleAlert } from 'lucide-react'

export interface UploadItem {
  id: string
  name: string
  size: number
  /** 0–100. */
  progress: number
  error?: string
}

export interface FileDropzoneProps {
  accept?: string
  maxSizeMb?: number
  initialFiles?: UploadItem[]
  onFiles?: (files: File[]) => void
  className?: string
}

const DEFAULT_FILES: UploadItem[] = [
  { id: '1', name: 'brand-guidelines.pdf', size: 2_411_724, progress: 100 },
  { id: '2', name: 'hero-render.png', size: 8_930_112, progress: 64 },
  { id: '3', name: 'archive.zip', size: 148_930_112, progress: 0, error: 'Larger than the 20 MB limit' },
]

function formatSize(bytes: number): string {
  if (bytes < 1024) return `${bytes} B`
  if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(0)} KB`
  return `${(bytes / (1024 * 1024)).toFixed(1)} MB`
}

export function FileDropzone({
  accept = 'image/*,application/pdf',
  maxSizeMb = 20,
  initialFiles = DEFAULT_FILES,
  onFiles,
  className = '',
}: FileDropzoneProps) {
  const [files, setFiles] = React.useState(initialFiles)
  const [active, setActive] = React.useState(false)

  // Enter/leave fire per element crossed, not per zone — count the pairs.
  const depth = React.useRef(0)

  function handleDragEnter(event: React.DragEvent) {
    event.preventDefault()
    depth.current += 1
    setActive(true)
  }

  function handleDragLeave(event: React.DragEvent) {
    event.preventDefault()
    depth.current -= 1
    if (depth.current <= 0) {
      depth.current = 0
      setActive(false)
    }
  }

  function handleDrop(event: React.DragEvent) {
    event.preventDefault()
    depth.current = 0
    setActive(false)
    addFiles(Array.from(event.dataTransfer.files))
  }

  function addFiles(incoming: File[]) {
    if (incoming.length === 0) return
    onFiles?.(incoming)

    setFiles((prev) => [
      ...prev,
      ...incoming.map((file, i) => ({
        id: `${Date.now()}-${i}`,
        name: file.name,
        size: file.size,
        progress: 0,
        error:
          file.size > maxSizeMb * 1024 * 1024
            ? `Larger than the ${maxSizeMb} MB limit`
            : undefined,
      })),
    ])
  }

  return (
    <div className={`w-full ${className}`}>
      {/* A label wrapping the input — click and keyboard both work natively. */}
      <label
        onDragEnter={handleDragEnter}
        onDragLeave={handleDragLeave}
        onDragOver={(e) => e.preventDefault()}
        onDrop={handleDrop}
        className={`flex cursor-pointer flex-col items-center justify-center rounded-2xl border-2 border-dashed px-6 py-12 text-center transition-colors focus-within:ring-2 focus-within:ring-primary ${
          active
            ? 'border-primary bg-primary/5'
            : 'border-border/60 bg-card/40 hover:border-primary/40 hover:bg-muted/30'
        }`}
      >
        <input
          type="file"
          multiple
          accept={accept}
          onChange={(e) => addFiles(Array.from(e.target.files ?? []))}
          className="sr-only"
        />

        <span
          aria-hidden
          className={`inline-flex h-12 w-12 items-center justify-center rounded-2xl transition-colors ${
            active ? 'bg-primary/15 text-primary' : 'bg-muted text-muted-foreground'
          }`}
        >
          <CloudUpload className="h-6 w-6" />
        </span>

        <span className="mt-4 block text-sm font-semibold">
          {active ? 'Drop to upload' : 'Drag files here, or click to browse'}
        </span>
        <span className="mt-1 block text-xs text-muted-foreground">
          Images and PDFs up to {maxSizeMb} MB each
        </span>
      </label>

      {files.length > 0 ? (
        <ul className="mt-4 space-y-2">
          {files.map((file) => {
            const done = !file.error && file.progress >= 100

            return (
              <li
                key={file.id}
                className="flex items-center gap-3 rounded-xl border border-border/60 bg-card/60 px-4 py-3"
              >
                <span
                  aria-hidden
                  className={`inline-flex h-9 w-9 shrink-0 items-center justify-center rounded-lg ${
                    file.error
                      ? 'bg-destructive/10 text-destructive'
                      : done
                        ? 'bg-emerald-500/15 text-emerald-500'
                        : 'bg-muted text-muted-foreground'
                  }`}
                >
                  {file.error ? (
                    <TriangleAlert className="h-4 w-4" />
                  ) : done ? (
                    <CircleCheck className="h-4 w-4" />
                  ) : (
                    <FileIcon className="h-4 w-4" />
                  )}
                </span>

                <div className="min-w-0 flex-1">
                  <p className="flex items-baseline justify-between gap-3">
                    <span className="truncate text-sm font-medium">{file.name}</span>
                    <span className="shrink-0 text-xs tabular-nums text-muted-foreground">
                      {formatSize(file.size)}
                    </span>
                  </p>

                  {file.error ? (
                    <p className="mt-0.5 text-xs text-destructive">{file.error}</p>
                  ) : done ? (
                    <p className="mt-0.5 text-xs text-muted-foreground">Uploaded</p>
                  ) : (
                    <div
                      role="progressbar"
                      aria-valuenow={file.progress}
                      aria-valuemin={0}
                      aria-valuemax={100}
                      aria-label={`Uploading ${file.name}`}
                      className="mt-1.5 h-1 overflow-hidden rounded-full bg-muted"
                    >
                      <span
                        className="block h-full rounded-full bg-primary transition-all"
                        style={{ width: `${file.progress}%` }}
                      />
                    </div>
                  )}
                </div>

                <button
                  type="button"
                  onClick={() => setFiles((prev) => prev.filter((f) => f.id !== file.id))}
                  aria-label={`Remove ${file.name}`}
                  className="shrink-0 rounded-lg p-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
                >
                  <X aria-hidden className="h-4 w-4" />
                </button>
              </li>
            )
          })}
        </ul>
      ) : null}
    </div>
  )
}

Before you paste

  • Styling is Tailwind utility classes on semantic tokens (bg-card, text-muted-foreground) — it inherits your theme instead of overriding it.
  • Install: npm i lucide-react
  • Every prop has a default, so it renders standalone before you wire it up.

Where it goes

Drop it at components/file-dropzone.tsx and import it where you need the section:

import { FileDropzone } from '@/components/file-dropzone'

Customize

2 of this block’s props are simple enough to drive from here. Change them and the block below re-renders — it is the same component whose source is above, not a mock of it. Everything else it accepts is in the table underneath.

Props

Read out of the component’s own type and signature, so this cannot drift from the source below. Every prop has a default — the component renders standalone before you pass it anything.

PropTypeDefault
acceptstring'image/*,application/pdf'
maxSizeMbnumber20
initialFilesUploadItem[]DEFAULT_FILES
onFiles(files: File[]) => void—
classNamestring''

Not using React?

The same block rendered once to markup, wrapped as a file your framework compiles. Tailwind classes are framework-agnostic, so the design transfers intact — the behaviour does not.

This block is interactive. The markup below is its initial state with the event handlers stripped — you will need to re-wire the behaviour in your framework.

file-dropzone.html
<!--
  Drag & Drop Upload — markup from the Hoverlab catalog.

  This is the block rendered once to HTML and wrapped as a component
  file. It is not a port of the React source: the Tailwind classes carry
  the design, which is the part that took the work, and they are the same
  in every framework.

  This block is interactive in React and the handlers are NOT here.
  Buttons, toggles and menus render in their initial state and do
  nothing until you wire them up.
-->
<div class="w-full ">
  <label class="flex cursor-pointer flex-col items-center justify-center rounded-2xl border-2 border-dashed px-6 py-12 text-center transition-colors focus-within:ring-2 focus-within:ring-primary border-border/60 bg-card/40 hover:border-primary/40 hover:bg-muted/30">
    <input type="file" multiple="" accept="image/*,application/pdf" class="sr-only" />
    <span aria-hidden="true" class="inline-flex h-12 w-12 items-center justify-center rounded-2xl transition-colors bg-muted text-muted-foreground">
      <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-cloud-upload h-6 w-6" aria-hidden="true">
        <path d="M12 13v8"></path>
        <path d="M4 14.899A7 7 0 1 1 15.71 8h1.79a4.5 4.5 0 0 1 2.5 8.242"></path>
        <path d="m8 17 4-4 4 4"></path>
      </svg>
    </span>
    <span class="mt-4 block text-sm font-semibold">Drag files here, or click to browse</span>
    <span class="mt-1 block text-xs text-muted-foreground">Images and PDFs up to 20 MB each</span>
  </label>
  <ul class="mt-4 space-y-2">
    <li class="flex items-center gap-3 rounded-xl border border-border/60 bg-card/60 px-4 py-3">
      <span aria-hidden="true" class="inline-flex h-9 w-9 shrink-0 items-center justify-center rounded-lg bg-emerald-500/15 text-emerald-500">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-circle-check h-4 w-4" aria-hidden="true">
          <circle cx="12" cy="12" r="10"></circle>
          <path d="m9 12 2 2 4-4"></path>
        </svg>
      </span>
      <div class="min-w-0 flex-1">
        <p class="flex items-baseline justify-between gap-3">
          <span class="truncate text-sm font-medium">brand-guidelines.pdf</span>
          <span class="shrink-0 text-xs tabular-nums text-muted-foreground">2.3 MB</span>
        </p>
        <p class="mt-0.5 text-xs text-muted-foreground">Uploaded</p>
      </div>
      <button type="button" aria-label="Remove brand-guidelines.pdf" class="shrink-0 rounded-lg p-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-x h-4 w-4" aria-hidden="true">
          <path d="M18 6 6 18"></path>
          <path d="m6 6 12 12"></path>
        </svg>
      </button>
    </li>
    <li class="flex items-center gap-3 rounded-xl border border-border/60 bg-card/60 px-4 py-3">
      <span aria-hidden="true" class="inline-flex h-9 w-9 shrink-0 items-center justify-center rounded-lg bg-muted text-muted-foreground">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-file h-4 w-4" aria-hidden="true">
          <path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"></path>
          <path d="M14 2v4a2 2 0 0 0 2 2h4"></path>
        </svg>
      </span>
      <div class="min-w-0 flex-1">
        <p class="flex items-baseline justify-between gap-3">
          <span class="truncate text-sm font-medium">hero-render.png</span>
          <span class="shrink-0 text-xs tabular-nums text-muted-foreground">8.5 MB</span>
        </p>
        <div role="progressbar" aria-valuenow="64" aria-valuemin="0" aria-valuemax="100" aria-label="Uploading hero-render.png" class="mt-1.5 h-1 overflow-hidden rounded-full bg-muted">
          <span class="block h-full rounded-full bg-primary transition-all" style="width:64%"></span>
        </div>
      </div>
      <button type="button" aria-label="Remove hero-render.png" class="shrink-0 rounded-lg p-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-x h-4 w-4" aria-hidden="true">
          <path d="M18 6 6 18"></path>
          <path d="m6 6 12 12"></path>
        </svg>
      </button>
    </li>
    <li class="flex items-center gap-3 rounded-xl border border-border/60 bg-card/60 px-4 py-3">
      <span aria-hidden="true" class="inline-flex h-9 w-9 shrink-0 items-center justify-center rounded-lg bg-destructive/10 text-destructive">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-triangle-alert h-4 w-4" aria-hidden="true">
          <path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3"></path>
          <path d="M12 9v4"></path>
          <path d="M12 17h.01"></path>
        </svg>
      </span>
      <div class="min-w-0 flex-1">
        <p class="flex items-baseline justify-between gap-3">
          <span class="truncate text-sm font-medium">archive.zip</span>
          <span class="shrink-0 text-xs tabular-nums text-muted-foreground">142.0 MB</span>
        </p>
        <p class="mt-0.5 text-xs text-destructive">Larger than the 20 MB limit</p>
      </div>
      <button type="button" aria-label="Remove archive.zip" class="shrink-0 rounded-lg p-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-x h-4 w-4" aria-hidden="true">
          <path d="M18 6 6 18"></path>
          <path d="m6 6 12 12"></path>
        </svg>
      </button>
    </li>
  </ul>
</div>
  • This is rendered HTML, not a translation of the React source. The Tailwind classes carry the design and work in any framework.
  • It is one frame: the component in its initial state, with no props applied beyond the defaults.
  • This block is interactive in React — toggles, menus or form state. None of that survives here; the markup is the closed/default state and the handlers are gone. Re-wire them in your own framework.
  • Requires Tailwind, and the design tokens the classes reference (bg-card, text-muted-foreground, and so on). The template ZIPs ship a globals.css that defines them.

What each framework gets across the whole catalog — effects convert properly; this rung is markup.

For AI

The component, its props, the design tokens it expects and the command that installs it — as one prompt. Paste it into Claude, Cursor, v0 or ChatGPT and what they build around it will match the rest of the catalog instead of inventing its own system.

See the prompt

Used in these pages

Want the whole screen instead of this one section? Open a page and copy it entire.

6 more blocks in File Upload

All of them free to read, copy and install — no account, no locked tiles, no watermarked preview. The whole catalog is open, and so are the API and the CLI.

Browse File Upload

Shipping one commercially

Copying the code is free. Putting it in client work or a paid product is what Pro is for — the licence, not the access.

  • A commercial licence for everything in the catalog
  • Unlimited bundle exports, in Vue, Svelte and Tailwind
  • One payment — no subscription, nothing to renew
Pro — $79 once

More File Upload blocks

View category
Open the full page for this block

Upload Progress List

The list that follows the dropzone: complete, uploading, queued and failed rows with cancel and retry — because hiding the failure is the classic upload bug.

File Upload171 lines1 dep
Open the full page for this block

Resumable Large Upload

One big file on a connection that will drop: a bar allowed to go backwards when a chunk is retried, a time estimate that is a range or nothing, and a promise about closing the tab made before it is needed.

File Upload237 lines1 dep
Open the full page for this block

CSV Column Mapper

The screen between "file uploaded" and "data imported": source columns mapped to schema fields, with a sample value under each and required fields blocking the button.

File Upload280 lines1 dep
Open the full page for this block

Avatar Crop & Upload

Framing a profile photo with keyboard-operable zoom and position, showing what gets trimmed rather than hiding it behind the mask.

File Upload268 lines1 dep
Open the full page for this block

Upload Requirements Form

An upload form that states the constraints before the file is chosen, rather than rejecting it after a two-minute transfer.

File Upload184 linesNo deps
Open the full page for this block

Import From URL Form

The import path for a file that is already on the internet, which does not need to travel through the browser at all.

File Upload178 linesNo deps