GooseUI

Components

Dropzone

New

A file upload area built on the useFileDrop hook. Files come in by drag and drop, from the file picker or from the clipboard, and all three go through the same checks for type, size and count. Images get previews that are released when a file is removed. The area is a <label> around a native file input: clicking, the keyboard and form submission work even before JavaScript loads, while drop, paste, checks and previews need it.

Browser Support

:has()

Highlights the focused file input area

Your browser:

Preview

Drop images, click the area, or focus it and press Ctrl+V with an image in the clipboard. No image at hand? Use the sample button.

Installation

npx shadcn@latest add @gooseui/dropzone
pnpm dlx shadcn@latest add @gooseui/dropzone
yarn dlx shadcn@latest add @gooseui/dropzone
bunx --bun shadcn@latest add @gooseui/dropzone

The command also installs the useFileDrop hook.

Usage

import { Dropzone } from "@/components/ui/dropzone"

export default function Example() {
  const [files, setFiles] = useState<File[]>([])

  return (
    <Dropzone
      accept="image/*"
      maxSize={2 * 1024 * 1024}
      maxFiles={4}
      value={files}
      onValueChange={setFiles}
    />
  )
}

Examples

Image slots

variant="slot" is a square for one image. Clicking a filled slot replaces the image, the cross removes it.

Front side

Back side

<Dropzone
  variant="slot"
  accept="image/png,image/jpeg,image/webp"
  maxSize={5 * 1024 * 1024}
  aria-label="Front side"
  onValueChange={([file]) => setFront(file ?? null)}
/>

One document

maxFiles={1} keeps the list to one file. Try dropping an image here to see the type error.

<Dropzone
  accept=".pdf,.doc,.docx,application/pdf"
  maxSize={10 * 1024 * 1024}
  maxFiles={1}
  labels={{ hint: "PDF or Word, up to 10 MB" }}
/>

Your own texts

Every text, including error messages, comes from labels. Messages are functions, so they can include the file name and the limit.

<Dropzone
  accept="image/*"
  maxSize={2 * 1024 * 1024}
  labels={{
    title: "Drop photos or click to choose",
    dragging: "Release to add",
    remove: "Delete",
    typeError: (file) => `${file.name} is not a photo`,
    sizeError: (file, max) => `${file.name} is over ${max / 1024 / 1024} MB`,
    countError: (max) => `Up to ${max} photos`,
  }}
  onReject={(rejections) => console.log(rejections)}
/>

Props

PropTypeDefaultDescription
variant"zone" | "slot""zone"Large area with a list, or a square slot for one image
valueFile[]-Controlled list of files
defaultValueFile[][]Initial files for uncontrolled use
onValueChange(files: File[]) => void-Called with the new list after adding or removing
onReject(rejections: { file, reason }[]) => void-Called with rejected files and reasons: type, size or count
acceptstring-Allowed types, same format as the accept attribute
maxSizenumber-Max size of one file, bytes
maxFilesnumber-Max number of files. The slot always holds one
disabledbooleanfalseTurns off clicks, drops and paste
namestring-Name of the file input, so the files go with the form
labelsPartial<DropzoneLabels>-Texts and error messages
aria-labelstring-Accessible name of the area
classNamestring-Classes for the wrapper

Features

  • Three sources: drag and drop, the file picker and paste from the clipboard
  • Validation: type, size and count checks with readable messages; duplicates are skipped
  • Previews: object URLs for images are revoked on removal and on unmount
  • Animation: file, preview and feedback transitions respect reduced motion preferences
  • Keyboard: the native file input inside the area takes focus, Enter or Space opens the picker, remove buttons are real buttons