File Upload

@allem-ui/file-upload

Drag-and-drop file upload with image previews, progress tracking, and validation.

Installation

npm install @allem-ui/file-upload

Add the @source directive to your CSS so Tailwind generates the component classes:

@source "@allem-ui/file-upload";

Drop Zone

Drag files onto the zone or click to browse. Supports type, size, and count validation.

Click to upload or drag and drop

image/*, .pdf

Custom Trigger

Use FileUploadTrigger to wrap any element as an upload button.

With Validation

Restrict to 2 files, max 1 MB each, only .png and .jpg. Files that exceed limits show an error state.

Click to upload or drag and drop

.png, .jpg

FileUpload Props

PropTypeDefaultDescription
onFilesSelected*(files: File[]) => void—Called with selected files
acceptstring—Accepted file types (e.g. "image/*,.pdf")
multiplebooleantrueAllow multiple files
maxFilesnumber—Maximum number of files — shows count indicator when set
currentCountnumber0Current number of files — used with maxFiles for the count display
maxSizenumber—Maximum file size in bytes — shows size hint in the drop zone
disabledbooleanfalseDisable the drop zone
classNamestring—Additional CSS classes
childrenReactNode—Custom drop zone content

FileUploadItem Props

Renders a single file row with thumbnail preview (for images), file icon with type-based coloring, progress bar, and status indicator. Used internally by FileUploadList — import it directly for custom list layouts.

PropTypeDefaultDescription
file*UploadFile—The file object to display
onRemove(id: string) => void—Callback when remove button is clicked
classNamestring—Additional CSS classes

FileUploadList Props

PropTypeDefaultDescription
files*UploadFile[]—Array of files to display
onRemove(id: string) => void—Callback when a file is removed
classNamestring—Additional CSS classes

FileUploadTrigger Props

PropTypeDefaultDescription
onFilesSelected*(files: File[]) => void—Called with selected files
acceptstring—Accepted file types
multiplebooleantrueAllow multiple files
disabledbooleanfalseDisable the trigger
children*ReactNode—Custom trigger element

useFileUpload

Hook that manages file state, validation, and image previews.

Options

PropTypeDefaultDescription
maxFilesnumber—Maximum number of files
maxSizenumber—Maximum file size in bytes
acceptstring[]—Accepted types (e.g. ["image/*", ".pdf"])

Return Value

PropTypeDefaultDescription
filesUploadFile[]—Current array of managed files
addFiles(files: File[]) => RejectedFile[]—Add files with validation — returns rejected files
removeFile(id: string) => void—Remove a file by ID
updateProgress(id: string, progress: number) => void—Update upload progress (0–100)
setStatus(id: string, status: Status, error?: string) => void—Set file status ("idle" | "uploading" | "success" | "error")
clearFiles() => void—Remove all files
rejectedFilesRejectedFile[]—Files rejected from the last addFiles() call

Built-in Behavior

FeatureDescription
Keyboard accessibleEnter/Space triggers file selection on both FileUpload and FileUploadTrigger.
Image previewsObject URLs are automatically generated for image files and revoked on removal.
Type-based file iconsFileUploadItem shows color-coded icons — emerald for images, red for PDFs, purple for videos.
Smart validationSupports wildcard MIME matching (image/*), exact MIME, and extension matching (.pdf).

UploadFile Type

PropTypeDefaultDescription
idstring—Unique file identifier
fileFile—Native File object
namestring—File name
sizenumber—File size in bytes
typestring—MIME type
previewstring—Preview URL for images
progressnumber—Upload progress (0–100)
status"idle" | "uploading" | "success" | "error"—Current upload status
errorstring—Error message when status is error

RejectedFile Type

Returned by addFiles() for files that failed validation. Use this to show feedback (e.g. toast notifications).

PropTypeDefaultDescription
fileFile—The native File object that was rejected
reason"type" | "size" | "limit"—Why the file was rejected — wrong type, too large, or file count exceeded