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

```ts
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

| Prop | Type | Default | Description |
|---|---|---|---|
| `accept` | `string` |  | Allowed types, as for `<input type="file" accept>`. |
| `aria-describedby` | `string` |  |  |
| `aria-invalid` | `boolean` |  |  |
| `className` | `string` |  |  |
| `defaultFiles` | `FileItem[]` |  |  |
| `disabled` | `boolean` |  |  |
| `files` | `FileItem[]` |  |  |
| `hint` | `ReactNode` |  | Secondary dropzone text. Defaults to a summary of `accept` and `maxSize`. |
| `id` | `string` |  |  |
| `label` | `ReactNode` |  | Main dropzone text. |
| `maxFiles` | `number` |  | Maximum number of files in the list. |
| `maxSize` | `number` |  | Maximum size per file, in bytes. |
| `multiple` | `boolean` |  | Allow 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

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

```tsx
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
