File Upload
A component that is used to upload multiple files.
Anatomy
<FileUpload.Root>
<FileUpload.Label />
<FileUpload.Dropzone>
<FileUpload.Trigger />
</FileUpload.Dropzone>
<FileUpload.ItemGroup>
<FileUpload.Item>
<FileUpload.ItemPreview>
<FileUpload.ItemPreviewImage />
</FileUpload.ItemPreview>
<FileUpload.ItemName />
<FileUpload.ItemSizeText />
<FileUpload.ItemDeleteTrigger />
</FileUpload.Item>
</FileUpload.ItemGroup>
<FileUpload.ClearTrigger />
<FileUpload.HiddenInput />
</FileUpload.Root>
Examples
Initial Files
Use defaultAcceptedFiles to set the initial files.
Clear Trigger
Use ClearTrigger to remove all files at once.
Dropzone
Use Dropzone for drag-and-drop. Style it with data-dragging.
Directory Upload
Set directory to upload whole folders. Read file paths from file.webkitRelativePath.
For directories with many files, raise or remove
maxFilesto avoid rejections.
Accepted File Types
Use accept to restrict file types by MIME type (image/png) or extension (.pdf).
Error Handling
Set constraints with maxFiles, maxFileSize, minFileSize, and accept. Rejected files include error codes like
TOO_MANY_FILES, FILE_INVALID_TYPE, FILE_TOO_LARGE, or FILE_EXISTS.
File Transformations
Use transformFiles to process files before they're added, for example to compress, convert, or resize images.
Field
Use Field to add helper text and error handling.
Root Provider
Use RootProvider with the useFileUpload hook to access the state and methods from outside the component.
Pasting Files
Use setClipboardFiles to accept images pasted from the clipboard.
Media Capture
Use capture to open the device camera: "environment" for the back camera, "user" for the front.
Rejected Files
Read rejectedFiles from the context to show validation errors.
Guides
File Previews
Set type on ItemPreview to render a preview per file format.
type="image/*": image thumbnails viaItemPreviewImagetype="video/*": video filestype="application/pdf": PDF filestype=".*": fallback for any file
<FileUpload.ItemPreview type="image/*">
<FileUpload.ItemPreviewImage />
</FileUpload.ItemPreview>
<FileUpload.ItemPreview type="video/*">
<VideoIcon />
</FileUpload.ItemPreview>
<FileUpload.ItemPreview type="application/pdf">
<PdfIcon />
</FileUpload.ItemPreview>
<FileUpload.ItemPreview type=".*">
<FileIcon />
</FileUpload.ItemPreview>
Disable Dropzone
Set allowDrop={false} to disable drag-and-drop.
<FileUpload.Root allowDrop={false}>{/* ... */}</FileUpload.Root>
Prevent Document Drop
Dropping files outside the dropzone doesn't navigate away by default. Set preventDocumentDrop={false} to turn this
off.
<FileUpload.Root preventDocumentDrop={false}>{/* ... */}</FileUpload.Root>
Prevent Double Open
Set disableClick on Dropzone when it contains a Trigger, so the file picker doesn't open twice.
<FileUpload.Dropzone disableClick>
<FileUpload.Trigger>Choose Files</FileUpload.Trigger>
Drag files here
</FileUpload.Dropzone>
API Reference
Props
Root
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
accept | Record<string, string[]> | FileMimeType | FileMimeType[]The accept file types | |
acceptedFiles | File[]The controlled accepted files | |
allowDrop | true | booleanWhether to allow drag and drop in the dropzone element |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
capture | 'user' | 'environment'The default camera to use when capturing media | |
defaultAcceptedFiles | File[]The default accepted files when rendered. Use when you don't need to control the accepted files of the input. | |
directory | booleanWhether to accept directories, only works in webkit browsers | |
disabled | booleanWhether the file input is disabled | |
ids | ElementIdsThe ids of the elements. Useful for composition. | |
invalid | booleanWhether the file input is invalid | |
locale | 'en-US' | stringThe current locale. Based on the BCP 47 definition. |
maxFiles | 1 | numberThe maximum number of files |
maxFileSize | Infinity | numberThe maximum file size in bytes |
minFileSize | 0 | numberThe minimum file size in bytes |
name | stringThe name of the underlying file input | |
onFileAccept | (details: FileAcceptDetails) => voidFunction called when the file is accepted | |
onFileChange | (details: FileChangeDetails) => voidFunction called when the value changes, whether accepted or rejected | |
onFileReject | (details: FileRejectDetails) => voidFunction called when the file is rejected | |
preventDocumentDrop | true | booleanWhether to prevent the drop event on the document |
readOnly | booleanWhether the file input is read-only | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<FileUploadRootState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. | |
required | booleanWhether the file input is required | |
transformFiles | (files: File[]) => Promise<File[]>Function to transform the accepted files to apply transformations | |
translations | IntlTranslationsThe localized messages to use. | |
validate | (file: File, details: FileValidateDetails) => FileError[] | nullFunction to validate a file |
| Attribute | Description |
|---|---|
[data-file-upload-root] | <uid> |
[data-disabled] | Present when disabled |
[data-readonly] | Present when read-only |
[data-dragging] | Present when in the dragging state |
ClearTrigger
Defines the <button> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-file-upload-clear-trigger] | <uid> |
[data-disabled] | Present when disabled |
[data-readonly] | Present when read-only |
Dropzone
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
disableClick | booleanWhether to disable the click event on the dropzone | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<FileUploadDropzoneState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-file-upload-dropzone] | <uid> |
[data-invalid] | Present when invalid |
[data-disabled] | Present when disabled |
[data-readonly] | Present when read-only |
[data-dragging] | Present when in the dragging state |
HiddenInput
Defines the <input> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
ItemDeleteTrigger
Defines the <button> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-file-upload-item-delete-trigger] | <uid> |
[data-disabled] | Present when disabled |
[data-readonly] | Present when read-only |
[data-type] | The type of the item |
ItemGroup
Defines the <ul> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<FileUploadItemGroupState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. | |
type | ItemType |
| Attribute | Description |
|---|---|
[data-file-upload-item-group] | <uid> |
[data-disabled] | Present when disabled |
[data-type] | The type of the item |
ItemName
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-file-upload-item-name] | <uid> |
[data-disabled] | Present when disabled |
[data-type] | The type of the item |
ItemPreviewImage
Defines the <img> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-file-upload-item-preview-image] | <uid> |
[data-disabled] | Present when disabled |
[data-type] | The type of the item |
ItemPreview
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. | |
type | '.*' | stringThe file type to match against. Matches all file types by default. |
| Attribute | Description |
|---|---|
[data-file-upload-item-preview] | <uid> |
[data-disabled] | Present when disabled |
[data-type] | The type of the item |
Item
Defines the <li> element.
| Attribute | Default | Type |
|---|---|---|
file | File | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<FileUploadItemState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-file-upload-item] | <uid> |
[data-disabled] | Present when disabled |
[data-type] | The type of the item |
ItemSizeText
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-file-upload-item-size-text] | <uid> |
[data-disabled] | Present when disabled |
[data-type] | The type of the item |
Label
Defines the <label> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-file-upload-label] | <uid> |
[data-disabled] | Present when disabled |
[data-invalid] | Present when invalid |
[data-required] | Present when required |
RootProvider
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
value | UseFileUploadReturn | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | type ONLY_FOR_FORMAT =
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<FileUploadRootProviderState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
Trigger
Defines the <button> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<FileUploadTriggerState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-file-upload-trigger] | <uid> |
[data-disabled] | Present when disabled |
[data-readonly] | Present when read-only |
[data-invalid] | Present when invalid |
Context
API
| Property | Type |
|---|---|
dragging | booleanWhether the user is dragging something over the root element |
focused | booleanWhether the user is focused on the dropzone element |
disabled | booleanWhether the file input is disabled |
readOnly | booleanWhether the file input is in read-only mode |
transforming | booleanWhether files are currently being transformed via `transformFiles` |
maxFilesReached | booleanWhether the maximum number of files has been reached |
remainingFiles | numberThe number of files that can still be added |
openFilePicker | VoidFunctionFunction to open the file dialog |
deleteFile | (file: File, type?: ItemType | undefined) => voidFunction to delete the file from the list |
acceptedFiles | File[]The accepted files that have been dropped or selected |
rejectedFiles | FileRejection[]The files that have been rejected |
setFiles | (files: File[]) => voidSets the accepted files |
clearFiles | VoidFunctionClears the accepted files |
clearRejectedFiles | VoidFunctionClears the rejected files |
getFileSize | (file: File) => stringReturns the formatted file size (e.g. 1.2MB) |
createFileUrl | (file: File, cb: (url: string) => void) => VoidFunctionReturns the preview url of a file. Returns a function to revoke the url. |
setClipboardFiles | (dt: DataTransfer | null) => booleanSets the clipboard files Returns `true` if the clipboard data contains files, `false` otherwise. |
getRootState | () => RootStateReturns the state of the root |
getDropzoneState | () => DropzoneStateReturns the state of the dropzone |
getTriggerState | () => TriggerStateReturns the state of the trigger |
getItemGroupState | (props: ItemGroupProps) => ItemGroupStateReturns the state of the item group |
getItemState | (props: ItemProps) => ItemStateReturns the state of the item |