组件

Dropzone

A patch of paper, stitched round its edge, that files can be dropped on or picked into. It checks type, size and count and tells you why a file was refused; it hands you File objects and shows them in a ruled list with thumbnails and progress lines. It uploads nothing: that part is yours.

安装

terminal
npx shadcn@latest add @wobko/dropzone

第一次使用?先把 registry 加进 components.json。 如何安装

示例

Drop or browse

Drag files over it (Stubby comes out with a sheet of paper), drop them, or click / Tab and press Enter to browse. Pick something that is not an image, or bigger than 2 MB, or a fifth file, and the reason appears in red.

Attach files, with progress

No upload is built in: you drive each row from your own request. Here a timer stands in for the network: rows fill their highlighter line, finish with a green tick, and one file is made to fail so you can see the red rule and its wavy line.

One file: a profile picture

multiple={false}: one file, a new one replaces the old. A narrow zone whose own content follows the state through useDropzone (here: the chosen picture as a taped polaroid).

Required, in a form

The zone is a plain form field: name puts the files in FormData, required stops the submit until one is chosen (the browser points at the input), and invalid draws the red stitches. Submit empty, then with a file.

API 参考

Dropzone

The state and the layout (a flex column). Without children it is a `DropzoneArea` (icon, title, description), `DropzoneRejections` and `DropzoneList`. The accepted files are `File` objects; uncontrolled (`defaultFiles`) or controlled (`files` + `onFilesChange`, called with the whole list).

属性类型默认值说明
acceptstring | string[]-What the picker offers and what is accepted: `image/*`, `image/png`, `.pdf`, comma separated or an array. Extensions are checked case-insensitively.
multiplebooleantrue`false`: one file, a new one replaces the old, extra files in one drop are refused.
maxFiles / maxSize / minSizenumber-How many files in all, and the size limits in bytes.
validator(file: File) => FileError | null-Your own check per file; return `{ code, message }` to refuse it.
messagesPartial<Record<code, string | ((file) => string)>>-Your words for the reasons (`file-invalid-type`, `file-too-large`, `file-too-small`, `too-many-files`), for translation.
onDrop / onReject(accepted, rejected) => void-Called for every drop or pick with what was taken and what was not (each rejection has its `file` and `errors[{ code, message }]`).
id / name / required-On the file input (`id` is for a Label). The accepted files are put in it (DataTransfer) so a plain <form> submits them, dropped ones included; a form reset empties the list with the input (or goes back to `defaultFiles`).
disabled / invalidboolean-Pale dashed stitches, nothing happens / red stitches on a pink wash (also `aria-invalid` on the input).

DropzoneArea

The patch you drop on: a `<label>` (a click anywhere opens the picker) round the real, visually hidden file input (Tab reaches it, Enter and Space open the picker). Drawn as a paper-shade patch with a cut edge and an even running stitch a few pixels in (stitches of uneven length, even in calm); the stitches turn blue on a yellow wash while a file is over it, and the drawing wiggles. `inputProps` reaches the input.

属性类型默认值说明
inputPropsInputHTMLAttributes-`aria-label`, `capture`, `webkitdirectory`...
seedstring | number-Pins the hand: the same seed always sews the same patch.

DropzoneItem

One file: a polaroid thumbnail for images (a little crooked) or a file icon, name, size, an optional highlighter progress line and status, and a remove button. The ruled pencil line under the row is the drawing; an error makes it red and wavy.

属性类型默认值说明
fileFile-The file to show.
progress / statusnumber 0-100 / "idle" | "uploading" | "done" | "error"-Yours to drive from your upload. Uploading with a progress shows a `progressbar`.
errorstring-Shown in red when `status` is "error".
thumbnailbooleantruePreview image files.
onRemove / removeLabel-Replace the default removal; name the x.

无障碍

  • The drop target is a `<label>` round a real `<input type="file">`: the input is a tab stop (visually hidden, never `display: none`), Enter and Space open the picker natively, the label's text is its name (override with `inputProps['aria-label']`), and a screen reader user never needs drag and drop.
  • Keyboard focus shows the blue ring round the whole patch (the input is inside the labeled host). Drag state is only visual: the drop works for pointer users, the picker for everyone.
  • Refusals are announced: `DropzoneRejections` is a `role="alert"` with one line per file and reason. The list is a named `<ul>`; each remove button is named "Remove <file>" and hands the focus to the zone when its row goes; a progress line is a `progressbar` with `aria-valuenow`.
  • The state is never color alone: over is a thicker blue stitch on yellow with Stubby carrying a parcel; invalid is red stitches, `aria-invalid` and the alert; disabled is pale stitches and a disabled input.
  • Reduced motion stops the wiggle and the icon's lift. Windows high contrast draws a real dashed border (solid while a file is over).
  • Touch: the whole patch is the target (at least 160 px tall by default, 128 compact, 96 calm); rows are 40 px with a 28 px remove target.

源码

CLI 会复制进你项目的文件。

正在加载源码