Skip to Content
dimah-s3v2.8.1

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} />
PartDescription
UploadRoot. Holds the hook and the hidden file input. Renders no element
UploadTriggerA Button that opens the file picker
UploadDropzoneA drop area. By default it holds a dropzone trigger and the status
UploadDropzoneTriggerThe clickable part of a dropzone, with an icon, a title, and the limits
UploadStatusProgress of the queue: one card for one file, a summary and rows for several
UploadFileListEvery file in the queue, as rows or a grid. Render each file yourself
UploadToastsProgress, 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.

Drop your contract, or browsePDF only. Up to 20.0 MB.

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:

AttributeSet when
data-drag-activeFiles are dragged over the dropzone
data-drag-rejectThe dragged files break accept or maxFiles
data-disabledThe 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

  • UploadTrigger is named "Upload file" or "Upload files". At an icon size the label is visually hidden and stays its accessible name.
  • UploadDropzoneTrigger is 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

PropTypeDefaultDescription
uploadUseUploadReturnrequiredThe useUpload return the parts read
isFullbooleanfalseRenders the triggers and retry actions as aria-disabled. The hook's limit decides
hideUploadedbooleanfalseLeaves finished files out of UploadStatus and UploadFileList
childrenReactNoderequiredThe parts

UploadTrigger

Also accepts every Button prop.

PropTypeDefaultDescription
callUploadCallOptions—Options for the files picked through it
tooltipReactNode—Tooltip on hover and focus
disabledbooleanupload.disabledDisables this trigger only. The hook can still disable it
childrenReactNodeIcon and "Upload file(s)"Replaces the icon and label

UploadDropzone

Also accepts every div prop.

PropTypeDefaultDescription
callUploadCallOptions—Options for dropped files, and files picked or pasted on its trigger
childrenReactNodeTrigger and UploadStatusThe dropzone content

UploadDropzoneTrigger

Accepts every div prop. children replace the default icon, title, and limits.

UploadStatus

Also accepts every div prop except children.

PropTypeDefaultDescription
layout"list" | "grid""list"Rows, or tiles with image previews

UploadFileList

Also accepts every ul prop except children.

PropTypeDefaultDescription
layout"list" | "grid""list"Rows, or tiles with image previews
children(file: UploadFileState) => ReactNodeUploadFileItemRenders 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.

PropTypeDefaultDescription
fileUploadFileStaterequiredOne entry of upload.files

UploadToasts

PropTypeDefaultDescription
uploadUseUploadReturnrequiredThe 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.

OptionTypeDefaultDescription
callUploadCallOptions—Options for the pasted files
enabledbooleantrueListens 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.

PropertyTypeDescription
limitsstring | nullCount and size, such as "You can upload 3 files."
acceptstring | nullAccepted types, such as "Accepted PNG, JPEG."

useUploadContext

Returns the root's values inside Upload, for parts of your own. See Custom UI.

PropertyTypeDescription
uploadUseUploadReturnThe root's hook
isFullbooleanThe root takes no more files
hideUploadedbooleanStatus parts leave out the uploaded files

Frequently asked questions

On this page