Components

File Upload

A drag-and-drop file upload component for React and Tailwind CSS: an animated dropzone that also opens the file picker on click or keyboard, type and size validation with animated errors, and a file list with per-file progress bars, retry and cancel. Plug in your own async upload function or use the built-in simulation for demos.

  • Drag & drop
  • Real file input
  • Progress per file
  • Validation
  • Cancel & retry
  • prefers-reduced-motion

    Installation

    $ npx shadcn@latest add https://duskui.com/r/file-upload.json

    Uses the DuskUI Button and Progress; the CLI installs them for you.

    Usage

    avatar-upload.tsx
    import { FileUpload, type FileUploadHandler } from "@/components/dusk/file-upload/file-upload"
    
    // XMLHttpRequest reports upload progress; fetch doesn't.
    const upload: FileUploadHandler = (file, report, signal) =>
      new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest()
        xhr.open("POST", "/api/upload")
        xhr.upload.onprogress = (e) => e.lengthComputable && report((e.loaded / e.total) * 100)
        xhr.onload = () => (xhr.status < 300 ? resolve() : reject(new Error(`Upload failed (${xhr.status})`)))
        xhr.onerror = () => reject(new Error("Network error"))
        signal.addEventListener("abort", () => xhr.abort())
        const body = new FormData()
        body.append("file", file)
        xhr.send(body)
      })
    
    export function AvatarUpload() {
      return <FileUpload accept="image/*" maxSize={5 * 1024 * 1024} maxFiles={3} onUpload={upload} />
    }

    Examples

    Your own upload function

    onUpload(file, report, signal) runs once per accepted file. Call report with 0–100, resolve when done, or throw to mark the file failed with your message; the user can then retry. Removing a file aborts signal. Files over 2 MB fail in this demo.

      Single file

      With multiple={false} a new file replaces the current one. Customize the copy with label and hint (the hint is generated from accept and maxSize otherwise).

      Résumé

        API Reference

        Also accepts any HTML attribute for the wrapper div. id goes to the file input.

        PropTypeDefaultDescription
        acceptstring—Accepted types, like the input's accept: "image/*,.pdf". Also enforced on drop.
        maxSizenumber—Max bytes per file.
        maxFilesnumber—Max files in the list.
        multiplebooleantrueAllow several files. When false, a new file replaces the current one.
        onUpload(file, report, signal) => Promise<void>—Your upload. report(0–100) for progress, throw to fail. Simulated when omitted.
        simulateDurationnumber2000Length of the simulated upload in ms.
        onFilesAdded(files: File[]) => void—Files that passed validation.
        onReject(rejections: FileRejection[]) => void—Files that failed validation, with reason (type | size | count) and message.
        onFileComplete(file: File) => void—A file finished uploading.
        onFileRemove(file: File) => void—The user removed (or cancelled) a file.
        labelReactNode—Main dropzone text.
        hintReactNode—Secondary text. Defaults to the accepted types and limits.
        disabledbooleanfalseDisable picking and dropping.
        namestring—name attribute for the file input.
        dropzoneClassNamestring—Classes for the dropzone.

        Accessibility

        • The dropzone is the <label> of a real, visually hidden <input type="file">. Keyboard users Tab to it and press Space or Enter; the dropzone shows the focus ring, and the hint is linked with aria-describedby.
        • Drag and drop is an enhancement, never the only way to add files.
        • Validation errors appear as role="alert" messages that can be dismissed; added, finished, failed and removed files are announced through a polite live region.
        • Each upload has a labelled progressbar; Cancel, Remove and Retry buttons name the file they act on. After removing a file, focus returns to the input.
        • Status changes swap inside a fixed-height slot, so rows never shift.
        • With prefers-reduced-motion the dropzone icon stays still and list items only fade.