File Upload
Lets the user pick files, or drop them onto an area, and lists the files chosen.
Usage
import { FileUpload } from '@leverui/components';
<FileUpload.Root>
<FileUpload.HiddenInput />
<FileUpload.Label />
<FileUpload.Dropzone>
<FileUpload.DropzoneContent />
</FileUpload.Dropzone>
<FileUpload.Trigger />
<FileUpload.ItemGroup>
<FileUpload.Item>
<FileUpload.ItemPreview />
<FileUpload.ItemName />
<FileUpload.ItemSizeText />
<FileUpload.ItemDeleteTrigger />
</FileUpload.Item>
</FileUpload.ItemGroup>
</FileUpload.Root>
Shortcuts
FileUploadItems
Renders one item for each uploaded file. Without it:
<FileUpload.ItemGroup>
<FileUpload.Context>
{(context) => (
<For each={context().acceptedFiles}>
{(file) => (
<FileUpload.Item file={file}>
<FileUpload.ItemPreview />
<FileUpload.ItemName />
<FileUpload.ItemSizeText />
<FileUpload.ItemDeleteTrigger />
</FileUpload.Item>
)}
</For>
)}
</FileUpload.Context>
</FileUpload.ItemGroup>
With it, when the items need no changes:
<FileUpload.ItemGroup>
<FileUpload.Items />
</FileUpload.ItemGroup>
FileUploadList
Renders FileUpload.Items inside FileUpload.ItemGroup.
<FileUpload.List />
Same as:
<FileUpload.ItemGroup>
<FileUpload.Items />
</FileUpload.ItemGroup>
Examples
Accepted Files
Define the accepted files for upload using the accept prop.
Multiple Files
Upload multiple files at once by using the maxFiles prop.
Custom Preview
Here's an example of how to show a custom image preview for files.
Directory
Use the directory prop to allow selecting a directory instead of a file.
Media Capture
Use the capture prop to select and upload files from different environments and media types.
Note: This is not fully supported in all browsers.
Dropzone
Drop multiple files inside the dropzone and use the maxFiles prop to set the number of files that can be uploaded at once.
Conditional Dropzone
Hide the dropzone when the maximum number of files has been reached by using useFileUploadContext to access the accepted files count.
Input
Use Input with asChild on FileUpload.Trigger to create a trigger that looks like a text input.
Clearable
Here's an example of a clearable file upload input.
Pasting Files
Here's an example of handling files pasted from the clipboard.
Store
An alternative way to control the file upload is to use the RootProvider component and the useFileUpload store hook.
accepted: rejected: This way you can access the file upload state and methods from outside the file upload.
Guides
Keep FileUpload.HiddenInput in every upload. It opens the file picker and
sends the files when a native form is submitted.
To control the upload, call useFileUpload inside a Solid owner, such as a
component, and pass the accessor to RootProvider. The child function of
Context receives the same accessor. Read context().acceptedFiles in a
tracked place, such as JSX or an effect.
With as or asChild, a callback ref receives a plain Element. To type a
part's props on their own, pass the tag: FileUploadDropzoneProps<'a'> gives
anchor props, including href.
Props
Root
| Prop | Default | Type | Description |
|---|---|---|---|
allowDrop | true | boolean | Allow drag and drop onto the dropzone. |
locale | 'en-US' | string | Locale, as a BCP 47 language tag. |
maxFiles | 1 | number | Most files allowed. |
maxFileSize | Infinity | number | Largest file size, in bytes. |
preventDocumentDrop | true | boolean | Block the drop event on the document. |
as | div | Native tag name | HTML tag to render. |
asChild | — | Solid render function | Render your own element with the root's props. |
unstyled | — | boolean | Turn off the Lever UI styles. |
accept | — | Record<string, string[]> | FileMimeType | FileMimeType[] | Allowed file types. |
acceptedFiles | — | File[] | Accepted files, when controlled. |
capture | — | 'user' | 'environment' | Default camera for a photo or video. |
defaultAcceptedFiles | — | File[] | Accepted files on first render, when uncontrolled. |
directory | — | boolean | Allow choosing folders. WebKit browsers only. |
disabled | — | boolean | Disable the file input. |
ids | — | Element ID map | Ids for the parts. |
invalid | — | boolean | Mark the file input invalid. |
minFileSize | 0 | number | Smallest file size, in bytes. |
name | — | string | name of the hidden file input. |
onFileAccept | — | (details: FileAcceptDetails) => void | Called when a file is accepted. |
onFileChange | — | (details: FileChangeDetails) => void | Called when files change, accepted or rejected. |
onFileReject | — | (details: FileRejectDetails) => void | Called when a file is rejected. |
readOnly | — | boolean | Make the file input read-only. |
required | — | boolean | Make the file input required. |
transformFiles | — | (files: File[]) => Promise<File[]> | Transform the accepted files. |
translations | — | IntlTranslations | Translated messages. |
validate | — | (file: File, details: FileValidateDetails) => FileError[] | null | Check a file and return any errors. |
Customize with CSS
Load the package stylesheet first, then your CSS.
Every part has a .leverui-file-upload__* class, including the hidden input.
.my-upload {
--leverui-colors-bg: #eef4ff;
--leverui-colors-fg-muted: #435276;
}
.my-upload .leverui-file-upload__dropzone {
border-color: #5573a5;
}
For one-off changes, use class and style. Set unstyled on the root or a
single part to turn off its default styles.
Explorer
Click a part name to highlight it in the preview.
Component Anatomy
Hover to highlight, click to select parts