File Upload

Lets the user pick files, or drop them onto an area, and lists the files chosen.

    Usage

    Code
    import { FileUpload } from '@leverui/components';
    
    Code
    <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:

    Code
    <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:

    Code
    <FileUpload.ItemGroup>
      <FileUpload.Items />
    </FileUpload.ItemGroup>
    

    FileUploadList

    Renders FileUpload.Items inside FileUpload.ItemGroup.

    Code
    <FileUpload.List />
    

    Same as:

    Code
    <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.

            Drag and drop files here
            .png, .jpg up to 5MB

              Conditional Dropzone

              Hide the dropzone when the maximum number of files has been reached by using useFileUploadContext to access the accepted files count.

              Drag and drop files here
              3 more files allowed

                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

                    PropDefaultTypeDescription
                    allowDroptruebooleanAllow drag and drop onto the dropzone.
                    locale'en-US'stringLocale, as a BCP 47 language tag.
                    maxFiles1numberMost files allowed.
                    maxFileSizeInfinitynumberLargest file size, in bytes.
                    preventDocumentDroptruebooleanBlock the drop event on the document.
                    asdivNative tag nameHTML tag to render.
                    asChild—Solid render functionRender your own element with the root's props.
                    unstyled—booleanTurn 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—booleanAllow choosing folders. WebKit browsers only.
                    disabled—booleanDisable the file input.
                    ids—Element ID mapIds for the parts.
                    invalid—booleanMark the file input invalid.
                    minFileSize0numberSmallest file size, in bytes.
                    name—stringname of the hidden file input.
                    onFileAccept—(details: FileAcceptDetails) => voidCalled when a file is accepted.
                    onFileChange—(details: FileChangeDetails) => voidCalled when files change, accepted or rejected.
                    onFileReject—(details: FileRejectDetails) => voidCalled when a file is rejected.
                    readOnly—booleanMake the file input read-only.
                    required—booleanMake the file input required.
                    transformFiles—(files: File[]) => Promise<File[]>Transform the accepted files.
                    translations—IntlTranslationsTranslated messages.
                    validate—(file: File, details: FileValidateDetails) => FileError[] | nullCheck 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.

                    Code
                    .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