# File upload

Pick files with a button or by dropping them, validate size and type, and show each file with its progress or error.

- Family: form
- Import: `import { FileTrigger, DropZone, FileUploadList, FileRow } from "@senvenli/react/form";`
- Page: https://design.senvenli.com/docs/components/file-upload
- JSON: https://design.senvenli.com/docs/components/file-upload.json

## Example: Drop zone with a list

States the accepted types and the size limit up front; rejected files say why.

```tsx
import { DropZone, FileUploadList, FileRow, type RejectedFile } from "@senvenli/react/form";
import { Stack } from "@senvenli/react/layout";
import { useState } from "react";

export default function Example() {
  const [files, setFiles] = useState<File[]>([]);
  const [rejected, setRejected] = useState<RejectedFile[]>([]);
  return (
    <Stack space="space-150">
      <DropZone
        label="Drag your invoices here"
        typesLabel="PDF"
        acceptedFileTypes={["application/pdf"]}
        maxSize={10_000_000}
        allowsMultiple
        onSelect={(accepted) => setFiles((current) => [...current, ...accepted])}
        onReject={setRejected}
      />
      {files.length + rejected.length > 0 && (
        <FileUploadList aria-label="Invoices">
          {files.map((file, index) => (
            <FileRow key={`${index}-${file.name}`} file={file} onRemove={() => setFiles(files.filter((item) => item !== file))} />
          ))}
          {rejected.map(({ file, reason }, index) => (
            <FileRow key={`rejected-${index}-${file.name}`} file={file} error={reason} onRemove={() => setRejected(rejected.filter((item) => item.file !== file))} />
          ))}
        </FileUploadList>
      )}
    </Stack>
  );
}
```

## Examples

### From a button

FileTrigger opens the system picker from any Button.

```tsx
import { Button } from "@senvenli/react/action";
import { FileUploadList, FileRow, FileTrigger } from "@senvenli/react/form";
import { Stack } from "@senvenli/react/layout";
import { useState } from "react";

export default function Example() {
  const [file, setFile] = useState<File>();
  return (
    <Stack space="space-150" align="start">
      <FileTrigger acceptedFileTypes={["image/*"]} maxSize={5_000_000} onSelect={([picked]) => setFile(picked)}>
        <Button iconBefore="upload">Upload a logo</Button>
      </FileTrigger>
      {file && (
        <FileUploadList aria-label="Logo">
          <FileRow file={file} onRemove={() => setFile(undefined)} />
        </FileUploadList>
      )}
    </Stack>
  );
}
```

### Row states

Uploaded, uploading with progress, and failed.

```tsx
import { FileUploadList, FileRow } from "@senvenli/react/form";
import { useState } from "react";

// Stored documents have no File object: the row takes a name and a size in bytes.
const DOCUMENTS = [
  { name: "invoice-1042.pdf", size: 248_000 },
  { name: "packing-list.pdf", size: 3_600_000, progress: 0.4 },
  { name: "contract-scan.pdf", size: 18_400_000, error: "The file is larger than 10 MB." },
];

export default function Example() {
  const [documents, setDocuments] = useState(DOCUMENTS);
  const remove = (name: string) => setDocuments(documents.filter((item) => item.name !== name));
  return (
    <FileUploadList aria-label="Order documents">
      {documents.map((item) => (
        <FileRow key={item.name} {...item} onCancel={() => remove(item.name)} onRemove={() => remove(item.name)} />
      ))}
    </FileUploadList>
  );
}
```

## Guidelines

### When to use it

- People add documents or photos to a form or record.

### Use something else when

- [image-cropper](https://design.senvenli.com/docs/components/image-cropper.md): One picture must be framed to a fixed shape before it is saved.
- [input](https://design.senvenli.com/docs/components/input.md): A link or path to the file is typed instead of picked.

## Accessibility

Keyboard:

- Enter, Space: Opens the file picker from the trigger or drop zone.

Built in:

- Rejected files explain why (size, type, count).
- Upload progress is exposed as a progress bar.

You provide:

- A label that says what to add.

## API reference

### FileTrigger

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| acceptedFileTypes | `readonly string[]` |  | no | MIME types or extensions: "image/jpeg", "image/*", ".heic", "application/pdf". |
| maxSize | `number` |  | no | Bytes. |
| onSelect | `(files: File[]) => void` |  | no | Files that passed the rules. |
| onReject | `(rejected: RejectedFile[]) => void` |  | no | Files that did not, each with its reason. |

Behavior props from React Aria:

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| children | `ReactNode & ReactElement<unknown, string \| JSXElementConstructor<any>>` |  | yes | The children of the component. The control that opens the picker: a DS Button or Link. |
| allowsMultiple | `boolean` |  | no | Whether multiple files can be selected. |
| defaultCamera | `"user" \| "environment"` |  | no | Specifies the use of a media capture mechanism to capture the media on the spot. |
| acceptDirectory | `boolean` |  | no | Enables the selection of directories instead of individual files. |

Also takes 70 standard DOM and ARIA attributes (id, aria-label, event handlers…).

### DropZone

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| acceptedFileTypes | `readonly string[]` |  | no | MIME types or extensions: "image/jpeg", "image/*", ".heic", "application/pdf". |
| maxSize | `number` |  | no | Bytes. |
| label | `string` |  | yes | What to drop, as a call to action: "Drag your photos here". |
| typesLabel | `string` |  | no | The accepted types as people read them ("JPG, PNG, HEIC"); the size limit is added. |
| allowsMultiple | `boolean` |  | no |  |
| onSelect | `(files: File[]) => void` |  | no |  |
| onReject | `(rejected: RejectedFile[]) => void` |  | no |  |
| isDisabled | `boolean` |  | no |  |

### FileUploadList

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| children | `React.ReactNode` |  | yes |  |
| aria-label | `string` |  | no |  |

### FileRow

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| file | `File` |  | no | The picked file; photos get a thumbnail at once, before upload ends. |
| name | `string` |  | no | Shown name and size when there is no File object (a stored document). |
| size | `number` |  | no |  |
| progress | `number` |  | no | 0–1 while uploading; omit when done. |
| error | `string` |  | no |  |
| onCancel | `() => void` |  | no |  |
| onRemove | `() => void` |  | no |  |
| onReplace | `() => void` |  | no |  |

### Tokens it reads

`--ds-background-brand-bold`, `--ds-background-disabled`, `--ds-background-neutral`, `--ds-background-selected`, `--ds-border`, `--ds-border-bold`, `--ds-border-focused`, `--ds-border-selected`, `--ds-link`, `--ds-radius-full`, `--ds-radius-large`, `--ds-radius-medium`, `--ds-shadow-raised`, `--ds-space-050`, `--ds-space-100`, `--ds-space-150`, `--ds-space-400`, `--ds-surface-raised`, `--ds-surface-sunken`, `--ds-text`, `--ds-text-danger`, `--ds-text-disabled`, `--ds-text-subtlest`

## Related

- [button](https://design.senvenli.com/docs/components/button.md)
