Upload
File picker button, dropzone, progress, and toasts that render one useUpload hook.
Upload files with a button, a dropzone, or a paste, and show their progress.
Upload holds one useUpload hook, and the parts
inside it render that hook.
For a form value, use Upload Field. For a profile picture, use Avatar Upload.
Drag and drop files here
You can upload 4 files. Each up to 75.0 MB.
Installation
import {
Upload,
UploadDropzone,
UploadStatus,
UploadToasts,
UploadTrigger,
} from "@dimah-s3/ui";Complete Installation first: the package or registry
setup, the stylesheet, and one <Toaster /> in your layout.
Usage
"use client";
import { useUpload } from "@dimah-s3/react";
import { Upload, UploadDropzone, UploadToasts } from "@dimah-s3/ui";
export function Attachments() {
const upload = useUpload({ route: "attachments", maxFiles: 5, append: true });
return (
<>
<Upload upload={upload}>
<UploadDropzone />
</Upload>
<UploadToasts upload={upload} />
</>
);
}Anatomy
<Upload upload={upload}>
<UploadTrigger />
<UploadDropzone>
<UploadDropzoneTrigger />
<UploadStatus />
</UploadDropzone>
<UploadFileList />
</Upload>
<UploadToasts upload={upload} />| Part | Description |
|---|---|
Upload | Root. Holds the hook and the hidden file input. Renders no element |
UploadTrigger | A Button that opens the file picker |
UploadDropzone | A drop area. By default it holds a dropzone trigger and the status |
UploadDropzoneTrigger | The clickable part of a dropzone, with an icon, a title, and the limits |
UploadStatus | Progress of the queue: one card for one file, a summary and rows for several |
UploadFileList | Every file in the queue, as rows or a grid. Render each file yourself |
UploadToasts | Progress, results, and rejected files as toasts. Mount once per hook |
Place the parts anywhere inside Upload. Several parts can share one hook,
such as a toolbar button and a dropzone.
Examples
Button
A trigger with the status under it.
Button styles
UploadTrigger is a Button: pass variant, size, and children to
restyle it, and tooltip to add a tooltip. At an icon size it shows the icon
alone and keeps its accessible name.
Image grid
Give UploadDropzone children to arrange it yourself. layout="grid" shows
the files as tiles.
Add photos to the album
Drop up to 8 images, or click to choose them.
Compact dropzone
A one-line dropzone for a form, with the status outside it. useUploadHints
returns the route's limits as text.
Custom file rows
Pass a function to UploadFileList to render each file from upload.files,
with its phase, progress, and error.
Paste
usePasteFiles uploads files pasted anywhere on the page. Pastes into text
fields are left alone.
Copy an image or a screenshot, then press Ctrl + V anywhere on this page.
Disabled
Pass disabled to useUpload. Every part follows it: triggers and the
dropzone disable, and drops and pastes do nothing.
Drag and drop files here
You can upload 2 files. Each up to 75.0 MB.
Dropzone
Without children, UploadDropzone renders this:
<UploadDropzone>
<UploadDropzoneTrigger />
<UploadStatus />
</UploadDropzone>Pass children to change the layout, such as moving UploadStatus outside.
UploadDropzoneTrigger is a button: put links and other buttons next to it,
not inside.
Style the drag states with data attributes:
| Attribute | Set when |
|---|---|
data-drag-active | Files are dragged over the dropzone |
data-drag-reject | The dragged files break accept or maxFiles |
data-disabled | The hook is disabled, or the root is full |
Status
- One file shows as its own card, with cancel, retry, or dismiss.
- Several files show a summary, with the overall progress, the count, and the time left, then a row for each file. The summary cancels every upload, retries the failed ones, and dismisses the list once it settles.
- Pause appears while a multipart upload is in flight (a file of at least 50 MiB on a route with multipart), since it resumes from its uploaded parts. A paused file shows "Paused" and its own resume action. Once every file in flight is paused, the summary offers resume for all of them.
Call upload.reset() to clear the status from your own code.
Toasts
Parts never toast. Mount UploadToasts once per hook. It shows:
- A progress toast with a cancel action while files upload, and a resume action while they are paused.
- The result once the queue settles. A failure has a retry action.
- The reason for each call that rejects files, such as a file that is too large.
Per-call options
call on UploadTrigger or UploadDropzone applies to the files they take
in. Its uploadOptions merge over the hook's, and maxFiles lowers the
hook's limit for that call.
<UploadTrigger
call={{ uploadOptions: { clientMetadata: { albumId } } }}
tooltip="Add photos"
/>Inside an ObjectList with the same hook,
files go to the open folder, and your call merges over that.
Accessibility
UploadTriggeris named "Upload file" or "Upload files". At an icon size the label is visually hidden and stays its accessible name.UploadDropzoneTriggeris a button: click, Enter, or Space opens the picker, and a paste while it has focus uploads. The route's limits are its description.- A polite live region announces when dragged files can or cannot be dropped.
API Reference
Upload
| Prop | Type | Default | Description |
|---|---|---|---|
upload | UseUploadReturn | required | The useUpload return the parts read |
isFull | boolean | false | Renders the triggers and retry actions as aria-disabled. The hook's limit decides |
hideUploaded | boolean | false | Leaves finished files out of UploadStatus and UploadFileList |
children | ReactNode | required | The parts |
UploadTrigger
Also accepts every Button prop.
| Prop | Type | Default | Description |
|---|---|---|---|
call | UploadCallOptions | — | Options for the files picked through it |
tooltip | ReactNode | — | Tooltip on hover and focus |
disabled | boolean | upload.disabled | Disables this trigger only. The hook can still disable it |
children | ReactNode | Icon and "Upload file(s)" | Replaces the icon and label |
UploadDropzone
Also accepts every div prop.
| Prop | Type | Default | Description |
|---|---|---|---|
call | UploadCallOptions | — | Options for dropped files, and files picked or pasted on its trigger |
children | ReactNode | Trigger and UploadStatus | The dropzone content |
UploadDropzoneTrigger
Accepts every div prop. children replace the default icon, title, and
limits.
UploadStatus
Also accepts every div prop except children.
| Prop | Type | Default | Description |
|---|---|---|---|
layout | "list" | "grid" | "list" | Rows, or tiles with image previews |
UploadFileList
Also accepts every ul prop except children.
| Prop | Type | Default | Description |
|---|---|---|---|
layout | "list" | "grid" | "list" | Rows, or tiles with image previews |
children | (file: UploadFileState) => ReactNode | UploadFileItem | Renders one file |
UploadFileItem
The default card of one file, with its progress, error, and cancel or retry.
Also accepts the FileAttachment props it
does not set itself, such as actions and description.
| Prop | Type | Default | Description |
|---|---|---|---|
file | UploadFileState | required | One entry of upload.files |
UploadToasts
| Prop | Type | Default | Description |
|---|---|---|---|
upload | UseUploadReturn | required | The hook return to report |
usePasteFiles
usePasteFiles(upload, options?) uploads files pasted anywhere on the page,
except into form fields, editable text, or a dropzone that already took the
paste.
| Option | Type | Default | Description |
|---|---|---|---|
call | UploadCallOptions | — | Options for the pasted files |
enabled | boolean | true | Listens for pastes. Off while the hook is disabled |
useUploadHints
useUploadHints(upload.policy) returns the route's limits as localized text,
the same lines the default dropzone shows.
| Property | Type | Description |
|---|---|---|
limits | string | null | Count and size, such as "You can upload 3 files." |
accept | string | null | Accepted types, such as "Accepted PNG, JPEG." |
useUploadContext
Returns the root's values inside Upload, for parts of your own. See
Custom UI.
| Property | Type | Description |
|---|---|---|
upload | UseUploadReturn | The root's hook |
isFull | boolean | The root takes no more files |
hideUploaded | boolean | Status parts leave out the uploaded files |
Frequently asked questions
Call event.preventDefault() in the trigger's onClick:
<UploadTrigger
onClick={(event) => {
if (!signedIn) event.preventDefault();
}}
/>Pass disabled to that UploadTrigger. The hook stays enabled for the other
parts. While the hook itself is disabled, the picker stays closed whatever the
prop says.
UploadTrigger is the button, and UploadStatus is the status it rendered
under it. UploadDropzone now goes inside Upload and reads its hook from
there. See Upgrade to 2.0.