# File upload

A place to drop or choose files, and a row for each file that shows how its upload is going.

```tsx
import { FileUpload, FileItem, ImageUpload } from '@halo-ds/react';
```

## When to use it
Use the drop zone when uploading is a main task, like adding documents or images. People can drag files onto it or choose them with the Browse button. Each file then appears as a File item that shows progress, success or a problem, with a way to cancel, retry or remove it. In tight spaces like a form, use just a Button with File items below.

Use Image upload for one picture in a circle, like a profile photo or a company logo.

## Do
- Say which types and sizes are accepted before people choose.
- Keep the Browse button, even where dragging works.
- Show each file as soon as it is chosen.
- Let people cancel, retry and remove files.

## Don't
- Rely on dragging alone.
- Start uploading a file you already know is too large.
- Clear the whole list because one file failed.
- Show upload errors only in a toast; keep them on the file.

## Drop zone or button?
A drop zone when uploading is the main task. A button when space is tight.
- **Drop zone**: For pages and modals where uploading is the main task. Fills the width of its container.
- **Button**: For forms and tight spaces. A Secondary Button opens the file picker; chosen files show as File items below.

## Props
### FileUpload props
| Prop | Type | What it does |
| --- | --- | --- |
| `onFiles` (required) | `(files: File[]) => void` | Called with the files that pass `accept` and `maxSize`. |
| `onReject` | `((rejected: { file: File; reason: "type" \| "size"; }[]) => void) \| undefined` | Called with files that were turned away and why. |
| `accept` | `string \| undefined` | File types, like "image/*,.pdf". |
| `multiple` | `boolean \| undefined` |  |
| `maxSize` | `number \| undefined` | Largest file size in bytes. |
| `disabled` | `boolean \| undefined` |  |
| `error` | `ReactNode` | Shows a red border and this message. Figma: State=Error. |
| `title` | `ReactNode` | Figma: Title. |
| `description` | `ReactNode` | Say which files are allowed and how big. Figma: Description. |
| `icon` | `ReactNode` | Figma: Change icon. |
| `buttonLabel` | `string \| undefined` |  |
| `classNames` | `Partial<Record<FileUploadPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<FileUploadPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

### FileItem props
| Prop | Type | What it does |
| --- | --- | --- |
| `name` (required) | `string` |  |
| `size` | `number \| undefined` | Size in bytes. |
| `status` | `"error" \| "uploading" \| "complete" \| undefined` | Figma: State. |
| `progress` | `number \| undefined` | Upload progress from 0 to 100. |
| `error` | `ReactNode` | Message for the error state. |
| `icon` | `ReactNode` | Figma: Change L icon. Defaults to a file icon. |
| `onCancel` | `(() => void) \| undefined` |  |
| `onRemove` | `(() => void) \| undefined` |  |
| `onRetry` | `(() => void) \| undefined` |  |
| `classNames` | `Partial<Record<FileItemPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<FileItemPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

### ImageUpload props
| Prop | Type | What it does |
| --- | --- | --- |
| `src` | `string \| undefined` | The current picture. Leave it out to show the empty circle. Figma: State. |
| `name` | `string \| undefined` | Who or what the picture shows, for screen readers. |
| `onFile` (required) | `(file: File) => void` | Called with the chosen file if it passes `accept` and `maxSize`. |
| `onRemove` | `(() => void) \| undefined` | Called when the picture is removed. Leave it out to hide the remove button. |
| `onReject` | `((file: File, reason: "type" \| "size") => void) \| undefined` | Called with a file that was turned away and why. |
| `kind` | `"person" \| "business" \| undefined` | The icon in the empty circle: a person, or a building for a company. Figma: Kind. |
| `layout` | `"default" \| "compact" \| undefined` | `default` has a title and hint. `compact` is a smaller picture and the buttons. Figma: Layout. |
| `title` | `ReactNode` |  |
| `hint` | `ReactNode` | Say which files are allowed and how big. |
| `accept` | `string \| undefined` | File types, like "image/png,image/jpeg". |
| `maxSize` | `number \| undefined` | Largest file size in bytes. |
| `disabled` | `boolean \| undefined` |  |
| `uploadLabel` | `string \| undefined` |  |
| `changeLabel` | `string \| undefined` |  |
| `removeLabel` | `string \| undefined` |  |
| `classNames` | `Partial<Record<ImageUploadPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<ImageUploadPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

## Examples
### Overview
```tsx
import { FileItem, FileUpload } from '@halo-ds/react';

export default function Example() {
  return (
    <div style={{ display: 'grid', gap: 12, width: 400 }}>
      <FileUpload description="PNG, JPG or PDF, up to 10 MB" accept="image/png,image/jpeg,.pdf" onFiles={() => {}} />
      <FileItem name="Brand guidelines.pdf" size={2_400_000} status="uploading" progress={60} onCancel={() => {}} />
      <FileItem name="Cover.png" size={840_000} status="complete" onRemove={() => {}} />
    </div>
  );
}
```

### States
An error that says what went wrong, and disabled.
```tsx
import { FileUpload } from '@halo-ds/react';

export default function Demo() {
  return (
    <div className="demo-stack" style={{ width: 400 }}>
      <FileUpload description="PDF up to 10 MB" error="That file is over 10 MB." onFiles={() => {}} />
      <FileUpload description="PDF up to 10 MB" disabled onFiles={() => {}} />
    </div>
  );
}
```

### File items
Uploading with progress, done, and failed with a retry.
```tsx
import { FileItem } from '@halo-ds/react';

export default function Demo() {
  return (
    <div className="demo-stack" style={{ width: 400 }}>
      <FileItem name="Quarterly report.pdf" size={2_400_000} status="uploading" progress={45} onCancel={() => {}} />
      <FileItem name="Cover image.png" size={840_000} status="complete" onRemove={() => {}} />
      <FileItem name="Intro video.mov" size={48_000_000} status="error" onRetry={() => {}} onRemove={() => {}} />
    </div>
  );
}
```

### Image upload
One picture in a circle, like a profile photo or a company logo. Compact leaves out the title.
```tsx
import { useState } from 'react';
import { ImageUpload } from '@halo-ds/react';

export default function Demo() {
  const [src, setSrc] = useState<string>();
  return (
    <div className="demo-stack" style={{ width: 400 }}>
      <ImageUpload src={src} name="Your photo" onFile={(file) => setSrc(URL.createObjectURL(file))} onRemove={() => setSrc(undefined)} />
      <ImageUpload kind="business" layout="compact" onFile={() => {}} />
    </div>
  );
}
```

More: https://halodesignsystem.dev/components/file-upload
