Back to Reui

File Upload

content/docs/components/base/file-upload.mdx

2.0.05.8 KB
Original Source
<ComponentPreview styleName="base-nova" name="c-file-upload-1" />

Installation

<CodeTabs> <TabsList> <TabsTrigger value="cli">CLI</TabsTrigger> <TabsTrigger value="manual">Manual</TabsTrigger> </TabsList> <TabsContent value="cli">
bash
npx shadcn@latest add @reui/use-file-upload
</TabsContent> <TabsContent value="manual"> <Steps>

<Step>Copy and paste the following code into your project.</Step>

<ComponentSource styleName="base-nova" name="use-file-upload" title="hooks/use-file-upload.ts" />

</Steps> </TabsContent> </CodeTabs>

Usage

tsx
import { useFileUpload } from "@/hooks/use-file-upload"
tsx
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

<ComponentPreview styleName="base-nova" name="c-file-upload-2" />

Compact Upload

<ComponentPreview styleName="base-nova" name="c-file-upload-3" />

<ComponentPreview styleName="base-nova" name="c-file-upload-4" className="[&_.preview]:h-auto [&_.preview>*]:w-full" />

Progress Upload

<ComponentPreview styleName="base-nova" name="c-file-upload-5" className="[&_.preview]:h-auto [&_.preview>*]:w-full" />

Table Upload

<ComponentPreview styleName="base-nova" name="c-file-upload-6" className="[&_.preview]:h-auto [&_.preview>*]:w-full" />

Image Upload

<ComponentPreview styleName="base-nova" name="c-file-upload-7" className="[&_.preview]:h-auto [&_.preview>*]:w-full" />

Sortable Upload

<ComponentPreview styleName="base-nova" name="c-file-upload-8" className="[&_.preview]:h-auto [&_.preview>*]:w-full" />

API Reference

useFileUpload

A custom hook for managing file upload state and interactions.

tsx
const [state, actions] = useFileUpload(options)

Options

PropTypeDefaultDescription
maxFilesnumberInfinityMaximum number of files allowed (only when multiple is true).
maxSizenumberInfinityMaximum size for each file in bytes.
acceptstring"*"Accepted file types (e.g., "image/*", ".pdf,.docx").
multiplebooleanfalseWhether to allow multiple file selection.
initialFilesFileMetadata[][]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

PropertyTypeDescription
filesFileWithPreview[]The list of currently selected/uploaded files.
isDraggingbooleanWhether a file is currently being dragged over the target area.
errorsstring[]Any current validation error messages.

Actions

MethodTypeDescription
addFiles(files: FileList | File[]) => voidManually add files to the state.
removeFile(id: string) => voidRemove a file by its unique ID.
clearFiles() => voidRemove all files from the state.
clearErrors() => voidClear all current error messages.
openFileDialog() => voidProgrammatically open the browser's file selection dialog.
getInputProps(props?) => InputPropsReturns props for a hidden <input type="file" /> element.
handleDragEnter(e) => voidEvent handler for the onDragEnter event.
handleDragLeave(e) => voidEvent handler for the onDragLeave event.
handleDragOver(e) => voidEvent handler for the onDragOver event.
handleDrop(e) => voidEvent handler for the onDrop event.

Types

FileMetadata

tsx
type FileMetadata = {
  name: string
  size: number
  type: string
  url: string
  id: string
}

FileWithPreview

tsx
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).

tsx
function formatBytes(bytes: number, decimals?: number): string