# File Upload

Pick files by dropping them on a zone or choosing them from the system dialog.

Pick files by dropping them on a zone or by choosing them from the system dialog. The drop zone is the empty state's block (icon tile, title, hint) inside a dashed frame; every picked file is a tag (a file glyph, the name, a remove button) with its size beside it, and a refused file is a red tag with the reason. Behaviour by Ark UI's file-upload (Zag): limits on count, size and type. Controlled or uncontrolled.

Kind: component · Category: Inputs · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `file-upload` machine, through Ark UI

## Add

```bash
npx fndn add file-upload
```

It writes the folder `file-upload/` into the project's ui folder (`index.ts`, `file-upload.tsx`, `FileUpload.vue`, `FileUpload.svelte`, `file-upload.recipe.ts`), and what it is built from: [Empty State](/components/empty-state.md), [Tag](/components/tag.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { FileUpload } from "@/components/ui/file-upload";
```

```vue
import { FileUpload } from "@/components/ui/file-upload";
```

```svelte
import { FileUpload } from "$lib/components/ui/file-upload";
```

## Parts

In the DOM the root carries `data-slot="file-upload"` and each part `data-slot="file-upload-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `file-upload` |  | Ark's file-upload root: a column (label, drop zone, files), 8px apart. As wide as its container. data-dragging while files are dragged over it, data-disabled. |
| `label` | `label` | `file-upload-label` | optional | Visible name (the label prop): text.sm, medium, ink, the slider's and the field's label. It wraps, never truncates. |
| `dropzone` | `div` | `file-upload-dropzone` |  | Ark's drop zone: role=button, focusable (the shared focus ring), radius card, a 1px dashed line-strong frame on the surface. A click, Enter or Space opens the system file dialog; files dropped on it are added. Holds the real EmptyState (size block): the upload-simple glyph in its tile, the title and the hint. Hovered by a mouse: color.hover; pressed: color.hover-2; while files are dragged over it: an accent frame on accent-tint. Disabled: half opacity, inert. |
| `hidden-input` | `input` | `file-upload-hidden-input` |  | Ark's hidden file input, carrying the files for a form (name). |
| `item-group` | `ul` | `file-upload-item-group` | optional, repeats | A list of files, one per line, 6px apart: the accepted files, then (Ark's rejected group) the refused ones. Each is absent when it has none. |
| `item` | `li` | `file-upload-item` | repeats | One file: the tag, then its size (text.xs, tabular figures, ink-2, never wrapped) at the end of the line, or for a refused file the reason in red. Keyed by the file itself, never by its position, so removing a file leaves the others in place. |
| `file` | `span` | `file-upload-file` | repeats | The real Tag (md, rounded, removable, with the file glyph as its icon), marked data-file-upload="file": the file's name, cut at the end with a title when the line is too narrow. Its remove button is named "Remove <name>" and removes the file. A refused file's tag is tone red. |
| `item-size` | `span` | `file-upload-item-size` | repeats | The file's size (1.2 MB) or, for a refused file, why it was refused (Too large, Not an accepted type, Too many files…) in red. |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `files` | `(File \| { name: string, size: number, type?: string })[]` |  | Bindable (React: `defaultFiles` and `onFilesChange`; Vue: `v-model:files`; Svelte: `bind:files`). The accepted files. Controlled: files with onFilesChange. Uncontrolled: defaultFiles. A plain { name, size, type } stands for a file (an upload already on the server, a fixture). |
| `label` | `string` |  | Visible label above the drop zone. |
| `title` | `string` | `"Drop files here"` | The drop zone's title. |
| `hint` | `string` | `"or click to choose"` | The drop zone's hint (what is accepted, the limits). |
| `accept` | `string[]` |  | Accepted types: MIME types (image/png, image/*) or extensions (.pdf). Anything else is refused. |
| `maxFiles` | `number` | `1` | How many files it takes; more are refused (Too many files). |
| `maxFileSize` | `number` |  | Largest file in bytes; larger ones are refused (Too large). |
| `name` | `string` |  | Form field name (the hidden input). |
| `disabled` | `boolean` | `false` | Inert at half opacity; the drop zone leaves the tab order. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `filesChange` | (files: File[]) the accepted files after a pick, a drop or a removal |
| `reject` | (rejections: { file: File, errors: string[] }[]) files that were refused |

## States

`hover`, `press`, `focus-visible`, `dragging`, `disabled`

## Keyboard and accessibility

Role: button.

Keyboard:

- Tab reaches the drop zone, then each file's remove button
- Enter or Space on the drop zone opens the system file dialog
- Enter or Space on a remove button removes that file

Notes:

- The drop zone is role=button named by its title and hint; the label names the hidden input.
- Each remove button is named "Remove <file name>".
- A refused file stays in the list, in red with its reason, until it is removed, so the reason can be read.

## Motion

- drop zone: background and frame colour over duration.fast (hover, press, files dragged over it), colour only
- files: a file added after the first paint makes its room as it fades in: its line opens from 0fr to 1fr over duration.reveal, easing.out-strong (the collapsible's reveal), while its opacity rises over duration.base, so what is below slides down instead of jumping; files present at first paint do not move, and removing a file never makes the others enter again
- remove buttons: the tag's own (the Button's press)

## Tokens

`color.surface`, `color.hover`, `color.hover-2`, `color.line-strong`, `color.accent`, `color.accent-tint`, `color.ink`, `color.ink-2`, `color.red`, `radius.card`, `text.sm`, `text.xs`, `duration.fast`, `duration.base`, `duration.reveal`, `easing.out-strong`

## Dependencies

- [Empty State](/components/empty-state.md)
- [Tag](/components/tag.md)

## Examples

### Import members from a CSV

React · `members-csv.tsx`

```tsx
import { FileUpload } from "@/components/ui/file-upload";

/* no label: the title names the drop zone; accept and maxFileSize reject the wrong file before it uploads */
export default function MembersCsv() {
  return (
    <FileUpload
      className="w-full max-w-80"
      title="Import members from a CSV"
      hint="Drop it here or click to choose · up to 2 MB"
      accept={["text/csv"]}
      maxFileSize={2_000_000}
    />
  );
}
```

Vue · `members-csv.vue`

```vue
<script setup lang="ts">
import { FileUpload } from "@/components/ui/file-upload";
</script>

<!-- no label: the title names the drop zone; accept and maxFileSize reject the wrong file before it uploads -->
<template>
  <FileUpload
    class="w-full max-w-80"
    title="Import members from a CSV"
    hint="Drop it here or click to choose · up to 2 MB"
    :accept="['text/csv']"
    :max-file-size="2_000_000"
  />
</template>
```

Svelte · `members-csv.svelte`

```svelte
<script lang="ts">
  import { FileUpload } from "$lib/components/ui/file-upload";
</script>

<!-- no label: the title names the drop zone; accept and maxFileSize reject the wrong file before it uploads -->
<FileUpload
  class="w-full max-w-80"
  title="Import members from a CSV"
  hint="Drop it here or click to choose · up to 2 MB"
  accept={["text/csv"]}
  maxFileSize={2_000_000}
/>
```

### Screenshots for a bug report

React · `bug-screenshots.tsx`

```tsx
import { FileUpload } from "@/components/ui/file-upload";

export default function BugScreenshots() {
  return (
    <FileUpload
      className="w-full max-w-sm"
      label="Screenshots"
      title="Drop screenshots here"
      hint="PNG or JPG, up to 4 files of 5 MB"
      accept={["image/png", "image/jpeg"]}
      maxFiles={4}
      maxFileSize={5_000_000}
    />
  );
}
```

Vue · `bug-screenshots.vue`

```vue
<script setup lang="ts">
import { FileUpload } from "@/components/ui/file-upload";
</script>

<template>
  <FileUpload
    class="w-full max-w-sm"
    label="Screenshots"
    title="Drop screenshots here"
    hint="PNG or JPG, up to 4 files of 5 MB"
    :accept="['image/png', 'image/jpeg']"
    :max-files="4"
    :max-file-size="5_000_000"
  />
</template>
```

Svelte · `bug-screenshots.svelte`

```svelte
<script lang="ts">
  import { FileUpload } from "$lib/components/ui/file-upload";
</script>

<FileUpload
  class="w-full max-w-sm"
  label="Screenshots"
  title="Drop screenshots here"
  hint="PNG or JPG, up to 4 files of 5 MB"
  accept={["image/png", "image/jpeg"]}
  maxFiles={4}
  maxFileSize={5_000_000}
/>
```

### Ticket with files attached

React · `ticket-attachments.tsx`

```tsx
import { useState } from "react";
import { FileUpload, type FileUploadFile } from "@/components/ui/file-upload";

/* controlled: files already attached to the ticket are listed and can be removed */
export default function TicketAttachments() {
  const [files, setFiles] = useState<FileUploadFile[]>([
    { name: "INV-2024-0117.pdf", size: 182_000, type: "application/pdf" },
    { name: "deploy-log-dpl_7f3a9c-billing-api-production.txt", size: 48_200, type: "text/plain" },
  ]);
  return (
    <div className="flex w-full max-w-sm flex-col gap-2">
      <FileUpload label="Attachments" maxFiles={3} files={files} onFilesChange={setFiles} />
      <p className="m-0 text-xs text-ink-2">{files.length} of 3 files</p>
    </div>
  );
}
```

Vue · `ticket-attachments.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { FileUpload, type FileUploadFile } from "@/components/ui/file-upload";

const files = ref<ReadonlyArray<FileUploadFile>>([
  { name: "INV-2024-0117.pdf", size: 182_000, type: "application/pdf" },
  { name: "deploy-log-dpl_7f3a9c-billing-api-production.txt", size: 48_200, type: "text/plain" },
]);
</script>

<!-- controlled: files already attached to the ticket are listed and can be removed -->
<template>
  <div class="flex w-full max-w-sm flex-col gap-2">
    <FileUpload v-model:files="files" label="Attachments" :max-files="3" />
    <p class="m-0 text-xs text-ink-2">{{ files.length }} of 3 files</p>
  </div>
</template>
```

Svelte · `ticket-attachments.svelte`

```svelte
<script lang="ts">
  import { FileUpload, type FileUploadFile } from "$lib/components/ui/file-upload";

  let files = $state<ReadonlyArray<FileUploadFile>>([
    { name: "INV-2024-0117.pdf", size: 182_000, type: "application/pdf" },
    { name: "deploy-log-dpl_7f3a9c-billing-api-production.txt", size: 48_200, type: "text/plain" },
  ]);
</script>

<!-- controlled: files already attached to the ticket are listed and can be removed -->
<div class="flex w-full max-w-sm flex-col gap-2">
  <FileUpload bind:files label="Attachments" maxFiles={3} />
  <p class="m-0 text-xs text-ink-2">{files.length} of 3 files</p>
</div>
```

### Locked on the Free plan

React · `free-plan.tsx`

```tsx
import { FileUpload } from "@/components/ui/file-upload";

/* disabled: the hint says what unlocks it */
export default function FreePlan() {
  return (
    <FileUpload
      className="w-full max-w-80"
      label="Build artifacts"
      title="Uploads are off on Free"
      hint="Upgrade to Team to keep build artifacts"
      disabled
    />
  );
}
```

Vue · `free-plan.vue`

```vue
<script setup lang="ts">
import { FileUpload } from "@/components/ui/file-upload";
</script>

<!-- disabled: the hint says what unlocks it -->
<template>
  <FileUpload
    class="w-full max-w-80"
    label="Build artifacts"
    title="Uploads are off on Free"
    hint="Upgrade to Team to keep build artifacts"
    disabled
  />
</template>
```

Svelte · `free-plan.svelte`

```svelte
<script lang="ts">
  import { FileUpload } from "$lib/components/ui/file-upload";
</script>

<!-- disabled: the hint says what unlocks it -->
<FileUpload
  class="w-full max-w-80"
  label="Build artifacts"
  title="Uploads are off on Free"
  hint="Upgrade to Team to keep build artifacts"
  disabled
/>
```

## Recipe

`file-upload.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* file-upload — drop files on a zone or choose them (Ark UI file-upload).
 *
 * The drop zone is a dashed frame holding the real EmptyState (size block: the icon tile, the
 * title, the hint); its look answers the pointer in colour only: hover (a real pointer) → hover,
 * press → hover-2, files dragged over it (the engine's data-dragging) → an accent frame on
 * accent-tint. A large surface presses with its background, as rows do, not with a scale.
 *
 * Each picked file is a line: the real Tag (removable, the file glyph as its icon; its classes
 * here are the `file` slot, passed as className so its name can be cut at the end of the line)
 * and its size at the end. A refused file is a red tag with its reason in red. A file added
 * after the first paint makes its room as it fades in, so what is below slides down instead of
 * jumping (arrivals make room as they fade): its one grid track opens from
 * minmax(0, 0fr) at @starting-style to minmax(0, 1fr) over duration.reveal (the collapsible's
 * reveal), while it fades in over duration.base. It clips while it opens, 4px of clip margin
 * leaving the remove button's focus ring whole. Only once the root has data-enter (after its
 * first paint, lib/paint.ts: the shared flag, it stays an attribute), so files there at first paint
 * do not move. The adapters key a file by its identity, never by its index, so removing one never
 * makes the files after it arrive again. */
export const fileUpload = tv({
  slots: {
    base: "flex flex-col gap-2 min-w-0 w-full data-disabled:opacity-50 data-disabled:pointer-events-none",
    label: "min-w-0 text-sm font-medium text-ink [overflow-wrap:anywhere] [unicode-bidi:plaintext]",
    dropzone: [
      "relative min-w-0 rounded-card border border-dashed border-line-strong bg-surface cursor-pointer",
      "motion-colors-fast",
      "hover:bg-hover active:bg-hover-2",
      "data-dragging:border-accent data-dragging:bg-accent-tint",
      "data-disabled:cursor-default",
    ],
    hiddenInput: "",
    itemGroup: "flex flex-col gap-1.5 min-w-0 m-0 p-0 list-none",
    /* the tag (as wide as its name, shrinking first) and the size at the end of the line.
     * fndn-check-ok: grid-template-rows is the sanctioned way to open room; one track, the line's own */
    item: [
      "grid grid-cols-[minmax(0,max-content)_1fr] grid-rows-[minmax(0,1fr)] items-center gap-x-2 min-w-0",
      "overflow-clip [overflow-clip-margin:4px] motion-size-reveal [[data-slot=file-upload][data-enter]_&]:starting:grid-rows-[minmax(0,0fr)]",
      "opacity-100 motion-opacity-base [[data-slot=file-upload][data-enter]_&]:starting:opacity-0",
    ],
    /* on the Tag: it may shrink, so its name is cut at the end of the line instead of pushing the size out */
    file: "min-w-0 max-w-full",
    itemSize: "justify-self-end text-xs tabular-nums whitespace-nowrap text-ink-2",
  },
  variants: {
    rejected: {
      true: { itemSize: "text-red" },
    },
  },
});

export type FileUploadRecipeProps = VariantProps<typeof fileUpload>;
```
