"use client"
import Image from "next/image"Installation
pnpm dlx shadcn@latest add @reui/file-upload
Usage
import { useFileUpload } from "@/hooks/use-file-upload"const [{ files }, { openFileDialog, getInputProps }] = useFileUpload({
accept: "image/*",
multiple: false,
})
return (
<div>
<Button onClick={openFileDialog}>Upload Image</Button>
<input {...getInputProps()} className="sr-only" />
{files.map((file) => (
<div key={file.id}>{file.file.name}</div>
))}
</div>
)Examples
Avatar Upload
"use client"
import {Compact Upload
"use client"
import {Gallery Upload
"use client"
import { useState } from "react"Progress Upload
"use client"
import { useEffect, useState } from "react"Table Upload
"use client"
import { useEffect, useState } from "react"Image Upload
"use client"
import { useCallback, useState } from "react"Sortable Upload
"use client"
import { useCallback, useEffect, useState } from "react"API Reference
useFileUpload
A custom hook for managing file upload state and interactions.
const [state, actions] = useFileUpload(options)Options
| Prop | Type | Default | Description |
|---|---|---|---|
maxFiles | number | Infinity | Maximum number of files allowed (only when multiple is true). |
maxSize | number | Infinity | Maximum size for each file in bytes. |
accept | string | "*" | Accepted file types (e.g., "image/*", ".pdf,.docx"). |
multiple | boolean | false | Whether to allow multiple file selection. |
initialFiles | FileMetadata[] | [] | Initial set of files to populate the state. |
onFilesChange | (files: FileWithPreview[]) => void | - | Callback fired whenever the files list changes. |
onFilesAdded | (files: FileWithPreview[]) => void | - | Callback fired when new valid files are added. |
onError | (errors: string[]) => void | - | Callback fired when validation errors occur. |
State
| Property | Type | Description |
|---|---|---|
files | FileWithPreview[] | The list of currently selected/uploaded files. |
isDragging | boolean | Whether a file is currently being dragged over the target area. |
errors | string[] | Any current validation error messages. |
Actions
| Method | Type | Description |
|---|---|---|
addFiles | (files: FileList | File[]) => void | Manually add files to the state. |
removeFile | (id: string) => void | Remove a file by its unique ID. |
clearFiles | () => void | Remove all files from the state. |
clearErrors | () => void | Clear all current error messages. |
openFileDialog | () => void | Programmatically open the browser's file selection dialog. |
getInputProps | (props?) => InputProps | Returns props for a hidden <input type="file" /> element. |
handleDragEnter | (e) => void | Event handler for the onDragEnter event. |
handleDragLeave | (e) => void | Event handler for the onDragLeave event. |
handleDragOver | (e) => void | Event handler for the onDragOver event. |
handleDrop | (e) => void | Event handler for the onDrop event. |
Types
FileMetadata
type FileMetadata = {
name: string
size: number
type: string
url: string
id: string
}FileWithPreview
type FileWithPreview = {
file: File | FileMetadata
id: string
preview?: string
}formatBytes
A utility function to format a byte count into a human-readable string (e.g., 1.5 MB).
function formatBytes(bytes: number, decimals?: number): string