FileUpload

Drag-and-drop or click to choose files, with type, size and count checks and a file list with progress.

Status: beta. Category: Forms.

import { FileUpload } from '@glyphui/ui'

Exports: FileUpload, formatBytes, matchesAccept, fileUploadStyles.

Notes

  • The dropzone is a button: Enter or Space opens the file picker.
  • Rejected files are reported in a live region and through onReject with a reason: too-large, wrong-type or too-many.

FileUpload props

PropTypeDefaultDescription
acceptstringAllowed types, as for <input type="file" accept>.
aria-describedbystring
aria-invalidboolean
classNamestring
defaultFilesFileItem[]
disabledboolean
filesFileItem[]
hintReactNodeSecondary dropzone text. Defaults to a summary of accept and maxSize.
idstring
labelReactNodeMain dropzone text.
maxFilesnumberMaximum number of files in the list.
maxSizenumberMaximum size per file, in bytes.
multiplebooleanAllow several files. Default true.
onFilesChange((files: FileItem[]) => void)
onReject((rejections: FileRejection[]) => void)Called with files that were not added and why.

Example: Images up to 5 MB

import { FileUpload } from '@glyphui/ui'

export default function Example() {
  return (
    <FileUpload
      className="w-full max-w-md"
      accept="image/png,image/jpeg,.webp"
      maxSize={5 * 1024 * 1024}
      maxFiles={4}
    />
  )
}

Example: Uploading with progress

FileUpload never uploads; set progress and error on items from your own upload code.

import { type FileItem, FileUpload } from '@glyphui/ui'
import { useState } from 'react'

/** Simulates an upload: progress ticks to 100, and files over 2 MB fail. */
function fakeUpload(item: FileItem, update: (id: string, patch: Partial<FileItem>) => void) {
  let progress = 0
  const timer = setInterval(() => {
    progress += 12
    if (item.file.size > 2 * 1024 * 1024 && progress > 50) {
      clearInterval(timer)
      update(item.id, {
        progress: undefined,
        error: 'Upload failed: the server rejected files over 2 MB.',
      })
      return
    }
    update(item.id, { progress: Math.min(progress, 100) })
    if (progress >= 100) clearInterval(timer)
  }, 250)
}

export default function Example() {
  const [files, setFiles] = useState<FileItem[]>([])
  const update = (id: string, patch: Partial<FileItem>) =>
    setFiles((current) => current.map((f) => (f.id === id ? { ...f, ...patch } : f)))

  return (
    <FileUpload
      className="w-full max-w-md"
      files={files}
      onFilesChange={(next) => {
        const added = next.filter((f) => !files.some((old) => old.id === f.id))
        setFiles(next.map((f) => (added.includes(f) ? { ...f, progress: 0 } : f)))
        for (const item of added) fakeUpload(item, update)
      }}
    />
  )
}

Live page: https://glyphui.xyz/components/file-upload