File Drop Zone

Picks files by drag and drop or with the keyboard.

Built on ui-sdk’s File Input, so accept, maxSize, maxFiles and multiple validate dropped and chosen files alike. Chosen files are listed below with their sizes and a remove button.

It’s a form input like any other: the files submit with the form under its name, required holds the form back until there’s one, a form reset clears them and a disabled <fieldset> disables it.

Preview
basic.tsx
import { FileDropZone } from "~/components/ui/file-drop-zone"

export default function Example() {
  return (
    <FileDropZone
      className="max-w-md"
      name="photos"
      multiple
      accept="image/*"
      title="Drop photos for this edition"
      hint="JPEG, PNG or TIFF, up to 50 MB each"
    />
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesButtonprimitiveTextprimitive
Used byNothing yet.
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20lucide-react@1.73.0

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/file-drop-zone.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20 lucide-react@1.73.0

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/file-drop-zone.tsx
import {
  FileInput,
  FileInputDropzone,
  FileInputItem,
  FileInputItemRemove,
  FileInputList,
  FileInputTrigger,
  formatFileSize,
} from "@ui-sdk/react/file-input"
import { Upload, X } from "lucide-react"
import type * as React from "react"

import { buttonVariants } from "~/components/ui/button"
import { Text, textVariants } from "~/components/ui/text"
import { cn, cnState } from "~/lib/utils"

/*
 * Picks files by drag and drop or with the keyboard, and lists them with their
 * sizes. Validation is ui-sdk's: `accept`, `maxSize`, `maxFiles` and `multiple`
 * on the FileInput apply to dropped and chosen files alike.
 */

type FileDropZoneProps = React.ComponentProps<typeof FileInput> & {
  /** What to drop, in the zone's first line. */
  title?: React.ReactNode
  /** Limits worth knowing before choosing, like file types and size. */
  hint?: React.ReactNode
  /** The browse button's label. */
  browseLabel?: React.ReactNode
}

/** A drop zone with a browse button, and the chosen files below it. */
function FileDropZone({
  title = "Drop files here",
  hint,
  browseLabel = "Browse files",
  className,
  ...props
}: FileDropZoneProps) {
  return (
    <FileInput className={cnState("grid w-full gap-md", className)} {...props}>
      <FileInputDropzone
        className={cn(
          "flex flex-col items-center justify-center gap-md rounded-xl border border-dashed border-border bg-input/20 px-xl py-3xl text-center transition-colors",
          "data-drag-reject:border-destructive data-drag-reject:bg-destructive/5 data-dragging:border-ring data-dragging:bg-muted",
          "data-disabled:opacity-50",
        )}
      >
        <Upload className="size-5 text-muted-foreground" />
        <div className="grid gap-2xs">
          <Text size="sm" weight="medium">
            {title}
          </Text>
          {hint !== undefined && (
            <Text size="xs" tone="muted">
              {hint}
            </Text>
          )}
        </div>
        <FileInputTrigger className={buttonVariants({ variant: "outline", size: "sm" })}>
          {browseLabel}
        </FileInputTrigger>
      </FileInputDropzone>
      <FileInputList aria-label="Chosen files" className="grid gap-2xs empty:hidden">
        <FileInputItem
          className={cn(
            textVariants({ size: "sm" }),
            "flex items-center gap-md rounded-lg bg-muted/60 px-md py-sm",
          )}
        >
          {({ file }) => (
            <>
              <span className="min-w-0 flex-1 truncate">{file.name}</span>
              <span className={cn(textVariants({ size: "xs", tone: "muted", tabular: true }))}>
                {formatFileSize(file.size)}
              </span>
              <FileInputItemRemove
                aria-label={`Remove ${file.name}`}
                className={buttonVariants({ variant: "ghost", size: "icon-xs" })}
              >
                <X />
              </FileInputItemRemove>
            </>
          )}
        </FileInputItem>
      </FileInputList>
    </FileInput>
  )
}

export { FileDropZone }
export type { FileDropZoneProps }

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

FileDropZone

A drop zone with a browse button, and the chosen files below it.

browseLabelours

The browse button's label.

Type ReactNode · Default Browse files

hintours

Limits worth knowing before choosing, like file types and size.

Type ReactNode

accept

The files to accept, like the accept attribute: MIME types ("image/png"), wildcards ("image/*") and extensions (".pdf"), in a comma-separated string or an array. Without it, any file.

Type string | readonly string[]

announcement

What screen readers hear after a change. Return null for nothing.

Type (change: FileInputChange) => string | null · Default "2 files accepted. 1 file rejected.", or "photo.png removed."

capture

Opens the camera on phones instead of the files: "user" for the front camera, "environment" for the back one, or true for the default one.

Type boolean | "user" | "environment"

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: FileInputState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: FileInputState) => string)

controller

Adds and removes files from anywhere. Create one with createFileInputController().

Type { getValue: () => readonly File[]; setValue: (files: readonly File[], options?: FileInputChangeOptions) => boolean; addFiles: (files: readonly File[] | FileList, options?: FileInputChangeOptions) => boolean; ... 4 more ...; isDragging: () => boolean; }

debug

Logs every state change with its reason to the console, in development builds.

Type boolean

defaultValue

The files at first, when uncontrolled. A form reset returns to them.

Type File[] · Default []

directory

Picks a whole folder, with every file inside it. Turns on multiple.

Type boolean

disabled

Disables it like a native control: the trigger can't be focused or used, drops and pastes are ignored, and nothing is submitted.

Type boolean

form

The id of a form to belong to, when it isn't inside that form.

Type string

maxFiles

The most files accepted at a time. The first ones that pass the other checks are accepted, and the rest are rejected.

Type number

maxSize

The largest size accepted, in bytes.

Type number

minSize

The smallest size accepted, in bytes.

Type number

multiple

Holds more than one file. New files join the ones it has, up to maxFiles in all. Without it, a new file replaces the one it has.

Type boolean · Default false

name

The name the files submit under. Without one, nothing is submitted.

Type string

onReject

Runs when files fail the checks, after onValueChange, with every reason each failed, even when none were accepted.

Type (rejected: FileRejection[], details: CommitDetails<FileInputValueChangeReason>) => void

onValueChange

Runs when files are added or removed, with what changed in the details. details.cancel() stops it.

Type (value: File[], details: FileInputValueChangeDetails) => void

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<FileInputState>

required

Blocks form submission until it holds a file, with the browser's own message.

Type boolean

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: FileInputState) => CSSProperties)

validate

A check of your own. Return a reason, like "duplicate", to reject the file, or nothing to accept it. accepted holds the files of the same choice, drop or paste accepted before this one.

Type (file: File, batch: { accepted: readonly File[]; }) => string | void | null

value

The files, to control the file input.

Type File[]