File Upload
Takes files from a drop zone, the system picker or a paste, checks them, and hands them to your app to upload.
Usage
import {
FileUpload,
FileUploadDescription,
FileUploadDropzone,
FileUploadIcon,
FileUploadTitle,
FileUploadTrigger,
} from 'nyctoui/file-upload'
<FileUpload multiple maxSize={10_000_000} onValueChange={(files) => upload(files)}>
<FileUploadDropzone>
<FileUploadIcon />
<FileUploadTitle>Drop files here or click to browse</FileUploadTitle>
<FileUploadDescription>Up to 10 MB each</FileUploadDescription>
<FileUploadTrigger>Choose files</FileUploadTrigger>
</FileUploadDropzone>
</FileUpload>FileUpload holds the files and checks them; it doesn’t send them anywhere. The drop zone is its empty state. It shows no file details of its own: draw each file with an Attachment, which shows the name, size, upload progress and errors, so the two never repeat each other.
Good for
- Attaching files to a message or a support ticket.
- Uploading a profile photo or a company logo in settings.
- Importing a CSV of contacts into a CRM.
- Adding receipts to an expense report, with a drop zone.
Examples
With attachments
The demo at the top of the page. The app keeps an upload per file, with its progress and status, and passes the files back as a controlled value, so maxFiles counts the files already there. Each file is drawn as an Attachment inside a FileUploadItem, which makes the list one Tab stop: the arrow keys move between files and Delete removes one. Progress here is simulated, and every third file fails once so the retry shows.
Message composer
The plain drop zone draws nothing until files are dragged over it, so it can wrap a composer. With pasteTarget, files pasted into the text area attach too, and text pastes stay text. Attachments sit above the message as cards and Send waits for them to finish.
Validation
accept, maxFiles and maxSize check every file, however it arrives. Rejected files never join the value; read them from onReject and show them as Attachments with status="error". A function as the drop zone’s children gets its drag state, here to warn while files that won’t be accepted are over it.
Button only
A trigger alone opens the picker. Without multiple, a new file replaces the one held. A function as the root’s children gets the files.
Disabled
A disabled file upload ignores drops and pastes, and its trigger can’t be focused.
Best practices
- Set
acceptto the file types that work, so the system picker shows only those. - Set
maxSizeandmaxFiles, and say the limits inFileUploadDescription. - Show each chosen file as an attachment with its progress and a remove button.
- Handle
onRejectand say why a file was turned away: too large, wrong type, too many.
- Leave out the drop zone when there’s room for it. Dragging files in is what people try first.
- Use it for one short text value. An input is enough.
API reference
FileUpload
Wraps ui-sdk’s FileInput and takes its props, including name, required, validate and directory. Its children can be a function of its state.
| Prop | Type | Default |
|---|---|---|
multipleHolds more than one file. Without it, a new file replaces the one held. | boolean | false |
acceptMIME types, wildcards and extensions to accept, such as image/* and .pdf. | string | string[] | — |
maxFilesThe most files held at once. | number | — |
maxSizeThe largest file accepted, in bytes. | number | — |
valueThe files, to control the upload. | File[] | — |
defaultValueThe files at first, when uncontrolled. | File[] | — |
onValueChangeRuns when files are added or removed. details has the added and removed files. | (value: File[], details) => void | — |
onRejectRuns with each file that failed the checks, and its reasons. | (rejected: FileRejection[]) => void | — |
disabledIgnores drops, pastes and the trigger. | boolean | false |
controllerOpens the picker, adds or removes files from anywhere. Create one with createFileUploadController(). | FileUploadController | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
FileUploadDropzone
Where files are dropped or pasted. Sets data-dragging, data-drag-accept and data-drag-reject; its children can be a function of that state.
| Prop | Type | Default |
|---|---|---|
variantdashed is a drop area of its own; plain shows only while files are dragged over, to wrap other content. | 'dashed' | 'plain' | 'dashed' |
openOnClickOpens the picker on a press anywhere in the zone that isn’t on a control. | boolean | true for dashed |
pasteTargetAnother place to take pasted files from, such as a text area. | 'document' | Element | string | () => Element | — |
targetTakes files dropped anywhere on the page. | 'document' | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
FileUploadIcon
A 32px tile above the title. Decorative.
| Prop | Type | Default |
|---|---|---|
childrenA 16px icon. | ReactNode | upload arrow |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
FileUploadTitle
A <p> saying what to do.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
FileUploadDescription
A <p> saying what’s accepted.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
FileUploadTrigger
A Button that opens the file picker. Keep one inside every drop zone for keyboard users.
| Prop | Type | Default |
|---|---|---|
variantButton treatment. | ButtonVariant | 'secondary' |
sizeButton height. | 'sm' | 'md' | 'lg' | 'sm' |
iconOnlySquare for a single icon. Pass an aria-label. | boolean | false |
controllerConnects a trigger outside the FileUpload. | FileUploadController | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
FileUploadList
The files, one item each, as a single Tab stop. Name it with an aria-label.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
FileUploadItem
One file in the list. Its children can be a function of { file, index }.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
FileUploadItemRemove
An AttachmentRemove that removes its item’s file, named for the file and out of the Tab order.