File Upload
Drag and drop or browse for files, with validation, previews and a hook for custom UIs.
Drop a file here or click to browse
Any file up to 5 MB
Installation
pnpm dlx nachui add file-uploadAnatomy
The root owns the hidden file input and the state: the accepted files, the validation errors and whether something is being dragged over. The dropzone opens the picker on click, Enter or Space and accepts drops.
List renders one Item per file with a preview, the name, the size and a remove button, and takes a render function when you want your own row. Image files get an object URL preview that is revoked when the file leaves the list.Composition
Use the following composition to build a
FileUpload:Variants
Multiple with limits
multiple keeps adding to the list. maxFiles, maxSize and accept reject what does not fit and the reasons show up in Errors. A batch that would go over maxFiles is refused as a whole.Drop up to 3 files
PDF, Word or images, 2 MB each
Avatar
A single image with the preview inside the dropzone itself.
useFileUploadContext reads the state from any child, so the preview is a few lines.PNG or JPG up to 1 MB
Compact
Trigger is a button that opens the picker, for forms where a dropzone takes too much room. The list is laid out as chips through className.Images or PDF
Progress
Pass a render function to
List and compose the item parts around your own upload logic. This demo simulates the upload with a timer and a Progress bar.Drop files to start uploading
Progress is simulated in this demo
Image grid
The same parts arranged as a grid of thumbnails, with the remove button revealed on hover.
Drop images here
Up to 6 images
Hook
useFileUpload is the engine behind the component and is exported on its own for interfaces that do not fit the parts above. It takes the same options as the root and returns the state plus the handlers to wire into any element.| Return | Type | Description |
|---|---|---|
files | FileWithPreview[] | Accepted files with an id and an optional preview |
errors | string[] | Messages from the last validation |
isDragging | boolean | A drag is over the dropzone |
addFiles | (files: FileList | File[]) => void | Validate and add files programmatically |
removeFile | (id: string) => void | Remove one file |
clearFiles | () => void | Remove every file |
clearErrors | () => void | Reset the error list |
openFileDialog | () => void | Open the native picker |
getInputProps | () => InputProps | Props for the hidden file input |
getDropzoneProps | () => DragHandlers | Drag enter, leave, over and drop handlers |
formatBytes(bytes, decimals?) turns a byte count into 1.5 MB.API Reference
FileUpload
| Prop | Type | Default | Description |
|---|---|---|---|
accept | string | - | Accepted types, as the native accept attribute |
multiple | boolean | false | Keep adding files instead of replacing |
maxFiles | number | Infinity | Maximum number of files when multiple |
maxSize | number | Infinity | Maximum size per file, in bytes |
initialFiles | FileMetadata[] | [] | Files already uploaded, with id, name, size, type and url |
disabled | boolean | false | Disable the dropzone, trigger and actions |
name | string | - | Name of the hidden input for form submissions |
onFilesChange | (files: FileWithPreview[]) => void | - | Called with the full list after every change |
onFilesAdded | (files: FileWithPreview[]) => void | - | Called with the files accepted in a batch |
onError | (errors: string[]) => void | - | Called when a batch produces validation errors |
className | string | - | Additional CSS classes |
FileUpload.List
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | (file: FileWithPreview) => ReactNode | - | Custom rows. With no children each file renders an Item |
className | string | - | Additional CSS classes |
FileUpload.Item
| Prop | Type | Default | Description |
|---|---|---|---|
file | FileWithPreview | - | The file to render |
children | ReactNode | - | Custom parts. Without children renders preview, info and remove |
className | string | - | Additional CSS classes |
FileUpload.Dropzone, FileUpload.Trigger, FileUpload.ItemPreview, FileUpload.ItemInfo, FileUpload.ItemRemove, FileUpload.Errors, FileUpload.Clear
Parts wired to the shared state. Each accepts
className and the standard attributes of the element it renders. ItemRemove ships with an English aria-label you can override.Found something to improve?
Notice a bug, typo, or missing detail on this page? Help us make the documentation better by opening a GitHub issue.