cnippet/ui
  1. ui
  2. forms
  3. file upload

forms

File Upload

A drag-and-drop dropzone and file picker with type, size and count limits, image previews and per-file progress. Copy-paste ready.

Installation

Usage

Anatomy

Good to know

  • FileUpload only collects files. Uploading them is up to you: read the list from onValueChange, or submit the hidden input with a form by passing name.
  • Files that fail accept, maxSize or maxFiles are never added. Each one calls onFileReject(file, reason) with "type", "size" or "count", so you can show your own message.
  • Without multiple, a new file replaces the current one.
  • accept uses the native format: MIME types (image/png), wildcards (image/*) and extensions (.pdf). It's checked for dropped files too, since the browser only filters the picker.
  • useFileUpload() gives any child the files, addFiles, removeFile, clearFiles and openFilePicker helpers, for custom layouts like an avatar or a gallery.
  • Don't put a FileUploadTrigger inside a FileUploadDropzone. Both open the picker, so a click would open it twice.

API Reference

FileUpload

FileUploadDropzone

A focusable drop target. Gets data-dragging while files are over it and data-disabled when disabled. Renders an upload icon and "Drop files here or browse" when it has no children.

FileUploadTrigger

A Button (variant="outline" by default) that opens the file picker. Takes all Button props.

FileUploadList

FileUploadItem

Renders a preview, name, size and remove button unless you pass your own children.

Helpers

isFileAccepted(file, accept) and formatFileSize(bytes) are exported too.

Examples

Limits and errors

Images and PDFs only, up to four files of 2 MB each. A rejected file shows why in an alert.

Avatar

A single image with a round preview, built with FileUploadTrigger and useFileUpload.

Message attachments

A paperclip trigger in a composer, with attached files shown as removable chips.

A grid of image previews with an add tile that disappears at the limit.