<!-- generated by scripts/gen-docs.mjs from docs/system/file-drop.md. Edit the source and rerun. -->
# FileDrop

## Description
Takes files from a drag or from the device's picker, and shows each one's upload as it goes, so a person knows which files arrived, which are still on their way, and which failed and why.

`import { FileDrop, accepts, fileDropLabels, type DropFile } from "@/components/cobalt/ui/file-drop"`, source `registry/cobalt/ui/file-drop.tsx`, status `ready-with-gaps`.
Foundation: `hand-rolled`. Traps checked: `trap/native-control`, `trap/button-icon-name`, `trap/toast-errors`, `trap/loading-layout-shift`, `trap/live-region-double`, `trap/color-only-status`, `trap/numbers-shift`, `trap/disabled-still-hovers`, `trap/gesture-drag`.

Where each trap is answered: `trap/native-control` in Foundation, the file input stays hidden and a real `Button` opens it; `trap/gesture-drag` in Keyboard, the drop has a keyboard path, the button; `trap/button-icon-name` in ARIA, each Remove is named after its file; `trap/toast-errors` in `rule/file-drop-refusals-in-place`; `trap/loading-layout-shift` in States, drag-over changes only the edge and the tint, and a file's tile keeps its height from waiting to done (56px measured); `trap/live-region-double`, each row's word is one status region and the glyph's progress bar is not live; `trap/color-only-status` in States, every state has a word and a glyph; `trap/numbers-shift`, sizes and percentages are tabular; `trap/disabled-still-hovers`, a disabled area takes no drop and its buttons are disabled.

### Foundation
| Given by the foundation | Added by the team |
|---|---|
| The native `<input type="file">` with `accept` and `multiple`, the HTML drag and drop events and `DataTransfer.files` | A dashed area, the one place a dashed edge earns itself, in `--control-edge` on a 16px corner, that takes a drop and opens the picker on a press; a secondary `sm` "Choose files" button as the keyboard's way in, described by the hint; drag-over changes only the edge to `--primary` and the fill to `--primary-soft`, counted through nested enters and leaves; `accepts()` and `maxSize` answer at once under the area with the files refused and why; each file as a `FileTile`, the tile a message and a composer share: its kind's glyph through `FileTypeIcon`, which draws the upload on itself (grey, its colour rising as the share grows, breathing while the share is not known, the retry arrow once it failed), the name with its extension kept in view and the whole name in a tooltip when cut, and one line of state in words, "Waiting, 980 B", "Uploading, 40%", a check and "Uploaded, 1.8 MB" (sizes through `bytes()` in `registry/cobalt/lib/format.ts`), or the reason in `--destructive` with the glyph as Retry, as in every file tile, named "Retry uploading" and the file, beside the tile's Remove, which is named after the file; with no second button the failed row keeps its room for the name and the reason at 390 ("Group rooming list.csv" whole in its 202px line) |

## Examples
A booking's documents: one uploaded, one uploading, one failed with its glyph as Retry; dropping or choosing more adds rows that upload.

Real uses, 0 call sites (`rg -n "<FileDrop\b" app registry --glob '!registry/cobalt/ui/*'`): new in this run.

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/file-drop/default.tsx` | default | Documents for a booking, each showing its upload and its result, with Retry when one fails |
| `docs/system/examples/file-drop/one-file.tsx` | state:one-file | A CSV of funds to import into a research list, with the button worded for one file |
| `docs/system/examples/file-drop/state-disabled.tsx` | state:disabled | The booking is checked out, so no file can be added or removed |
| `docs/system/examples/file-drop/state-drag-over.tsx` | state:drag-over | The area while a file is dragged over it, held by `dragging` so it shows without a drag: the edge in the accent and the soft fill, and nothing moves |
| `docs/system/examples/file-drop/too-large.tsx` | state:refused | A photo past 200 KB is refused when picked or dropped, and the reason stays under the area until the next pick |
| Not applicable: default.tsx shows uploading, uploaded and failed tiles | state:uploading | A file's tile in each state |
| `docs/system/examples/file-drop/one-file.tsx` | composition:Form | Under a heading and over a note, among a form's fields; FileDrop names itself through its button and hint, so it takes no FieldLabel |

Example: Documents for a booking, each showing its upload and its result, with Retry when one fails (`docs/system/examples/file-drop/default.tsx`)

```tsx
import { useEffect, useRef, useState } from "react";

import { type DropFile, FileDrop } from "@/components/cobalt/ui/file-drop";

// Stands in for your upload. Each file climbs 20 percent every 400ms, and fails while the browser is
// offline or the address has ?network=offline.
const offline = () => !navigator.onLine || new URLSearchParams(location.search).get("network") === "offline";

const start: DropFile[] = [
  { id: "a", name: "Rental agreement, Harbour suite.pdf", size: 1_840_000, status: "success" },
  { id: "b", name: "Deposit invoice BK-48215.pdf", size: 412_000, status: "pending", progress: 40 },
  {
    id: "c",
    name: "Group rooming list.csv",
    size: 96_000,
    status: "failure",
    error: "The connection dropped at 60%.",
  },
];

export default function Example() {
  const [files, setFiles] = useState(start);
  const next = useRef(0);
  // Moves every uploading file on, and settles it at 100.
  useEffect(() => {
    const timer = setInterval(() => {
      setFiles((list) =>
        list.map((file) => {
          if (file.status !== "pending") return file;
          if (offline()) return { ...file, status: "failure", error: "No connection. Retry when you are back online." };
          const progress = Math.min(100, (file.progress ?? 0) + 20);
          return progress === 100 ? { ...file, status: "success", progress } : { ...file, progress };
        }),
      );
    }, 400);
    return () => clearInterval(timer);
  }, []);
  const upload = (id: string) =>
    setFiles((list) => list.map((file) => (file.id === id ? { ...file, status: "pending", progress: 0 } : file)));
  return (
    <FileDrop
      className="w-full max-w-md"
      accept=".pdf,.csv,image/*"
      maxSize={10_000_000}
      hint="PDF, CSV or an image, up to 10 MB each"
      files={files}
      onAdd={(added) =>
        setFiles((list) => [
          ...list,
          ...added.map((file) => ({
            id: `new-${(next.current += 1)}`,
            name: file.name,
            size: file.size,
            status: "pending" as const,
            progress: 0,
          })),
        ])
      }
      onRemove={(id) => setFiles((list) => list.filter((file) => file.id !== id))}
      onRetry={upload}
    />
  );
}
```

Example: Under a heading and over a note, among a form's fields; FileDrop names itself through its button and hint, so it takes no FieldLabel (`docs/system/examples/file-drop/one-file.tsx`)

```tsx
import { useState } from "react";

import { type DropFile, FileDrop } from "@/components/cobalt/ui/file-drop";

export default function Example() {
  const [files, setFiles] = useState<DropFile[]>([]);
  return (
    <div className="flex w-full max-w-md flex-col gap-1.5">
      <h3 className="text-sm font-medium">Fund list</h3>
      <FileDrop
        multiple={false}
        accept=".csv"
        maxSize={5_000_000}
        hint="A CSV with one fund per row, up to 5 MB"
        files={files}
        // A local file reads at once, so it is listed as done; an upload would start pending.
        onAdd={([file]) => file && setFiles([{ id: file.name, name: file.name, size: file.size, status: "success" }])}
        onRemove={() => setFiles([])}
      />
      <p className="text-xs text-muted-foreground">The first row names the columns: fund, adviser, gross assets.</p>
    </div>
  );
}
```

## Variants
Not applicable: FileDrop has one look. `multiple` words the button for one file or several, shown in States.

## States
| State | Trigger | What the user can do | Shown by, besides color | Checked by |
|---|---|---|---|---|
| idle | no drag, no files | drop, or press Choose files | the dashed area, the glyph, the prompt, the button and the hint | test |
| drag-over | a drag enters the area, or the host passes `dragging` while a file is over its window | drop | the edge in `--primary` and the `--primary-soft` fill; nothing moves (measured the area at the same size during the drag) | screenshot |
| refused | a dropped or chosen file fails `accept` or `maxSize` | try another file | "Floor plan.pdf is larger than 10 MB." under the area, as an alert, linked to the button | test |
| waiting | a file's `status` is idle | remove it | its glyph in its kind's colour and "Waiting" with its size | test |
| uploading | `pending` | remove it to cancel | the glyph grey with its colour rising to the share, or breathing while the share is not known; "Uploading" with its size, or "Uploading, 40%" with "Uploading" for a screen reader | test |
| uploaded | `success` | remove it | the glyph in its kind's colour, a check in `--good` and "Uploaded" with its size | test |
| failed | `failure` | press the glyph to retry, or remove it | the glyph grey with the retry arrow, a button with the tooltip "Retry", and the reason in `--destructive` on up to two lines | test |
| disabled | `disabled` | nothing | the area at half opacity, Choose files, Retry and Remove disabled | test |

Example: A CSV of funds to import into a research list, with the button worded for one file (`docs/system/examples/file-drop/one-file.tsx`)

```tsx
import { useState } from "react";

import { type DropFile, FileDrop } from "@/components/cobalt/ui/file-drop";

export default function Example() {
  const [files, setFiles] = useState<DropFile[]>([]);
  return (
    <div className="flex w-full max-w-md flex-col gap-1.5">
      <h3 className="text-sm font-medium">Fund list</h3>
      <FileDrop
        multiple={false}
        accept=".csv"
        maxSize={5_000_000}
        hint="A CSV with one fund per row, up to 5 MB"
        files={files}
        // A local file reads at once, so it is listed as done; an upload would start pending.
        onAdd={([file]) => file && setFiles([{ id: file.name, name: file.name, size: file.size, status: "success" }])}
        onRemove={() => setFiles([])}
      />
      <p className="text-xs text-muted-foreground">The first row names the columns: fund, adviser, gross assets.</p>
    </div>
  );
}
```

Example: The booking is checked out, so no file can be added or removed (`docs/system/examples/file-drop/state-disabled.tsx`)

```tsx
import { FileDrop } from "@/components/cobalt/ui/file-drop";

export default function Example() {
  return (
    <FileDrop
      className="w-full max-w-md"
      disabled
      hint="Documents close when the guest checks out"
      files={[{ id: "a", name: "Final invoice BK-48201.pdf", size: 268_000, status: "success" }]}
      onAdd={() => {}}
      onRemove={() => {}}
    />
  );
}
```

Example: The area while a file is dragged over it, held by `dragging` so it shows without a drag: the edge in the accent and the soft fill, and nothing moves (`docs/system/examples/file-drop/state-drag-over.tsx`)

```tsx
import { useState } from "react";

import { type DropFile, FileDrop } from "@/components/cobalt/ui/file-drop";

export default function Example() {
  const [files, setFiles] = useState<DropFile[]>([
    { id: "a", name: "Rental agreement, Harbour suite.pdf", size: 1_840_000, status: "success" },
  ]);
  return (
    <FileDrop
      className="w-full max-w-md"
      accept=".pdf,.csv,image/*"
      maxSize={10_000_000}
      hint="PDF, CSV or an image, up to 10 MB each"
      dragging
      files={files}
      onAdd={() => {}}
      onRemove={(id) => setFiles((list) => list.filter((file) => file.id !== id))}
    />
  );
}
```

Example: A photo past 200 KB is refused when picked or dropped, and the reason stays under the area until the next pick (`docs/system/examples/file-drop/too-large.tsx`)

```tsx
import { useState } from "react";

import { type DropFile, FileDrop } from "@/components/cobalt/ui/file-drop";

export default function Example() {
  const [files, setFiles] = useState<DropFile[]>([]);
  return (
    <div className="flex w-full max-w-md flex-col gap-1.5">
      <h3 className="text-sm font-medium">Logo on invoices</h3>
      <FileDrop
        multiple={false}
        accept="image/png,image/svg+xml"
        maxSize={200_000}
        hint="A PNG or SVG, up to 200 KB"
        files={files}
        // A small image reads at once, so it is listed as done; an upload would start pending.
        onAdd={([file]) => file && setFiles([{ id: file.name, name: file.name, size: file.size, status: "success" }])}
        onRemove={() => setFiles([])}
      />
    </div>
  );
}
```

### State precedence
- Disabled and drag-over: disabled wins. The area takes no drop and keeps its look.
- Refused and uploading: both show. The refusal names the files that were left out, and the rest upload.
- Failed and uploading: Not applicable: a file's `status` is one value.
- Drag-over and refused: drag-over wins while the drag lasts; the refusal from the last drop stays under the area until the next.

### Motion
| Trigger | Kind | Preset | Properties | Reduced motion |
|---|---|---|---|---|
| a drag enters or leaves | announce | `--t-press` on `--curve` | border-color, background-color | the same colour change |
| an upload's progress | announce | `FileTypeIcon`'s fill, `duration-settle` on `ease-out` | transform of the clip that reveals the colour | no transition; the fill jumps to the share |
| an upload whose share is not known | announce | `animate-pulse` on the grey sheet | opacity | still under reduced motion; the word carries the state |
| a file is removed or added | announce | `Presence`: Leave on `--t-leave` with the height on the settle spring, Grow on the layout spring | opacity, height | the fade only; the rows below close up after it |

## Props

<!-- Props generated from registry/cobalt/ui/file-drop.tsx with TypeScript by scripts/props-table.mjs -->
| Prop | Type | Default | Purpose |
|---|---|---|---|
| `files` | `DropFile[]` |  |  |
| `onAdd` | `(files: File[]) => void` |  | The files that passed `accept` and `maxSize`; the rest are named under the area at once. |
| `onRemove` | `(id: string) => void` |  |  |
| `accept?` | `string` |  | The types it takes, as an input's `accept`: ".pdf,.csv" or "image/*". |
| `className?` | `string` |  |  |
| `disabled?` | `boolean` | `false` |  |
| `dragging?` | `boolean` | `false` | Draws the drag-over look while true, for a host that lights the area as soon as a file is dragged anywhere over the window, so a person sees where to drop before they reach it. |
| `hint?` | `ReactNode` |  | What it takes, such as "PDF or CSV, up to 10 MB", under the button. |
| `labels?` | `Partial<{ prompt: string; choose: string; chooseOne: string; files: string; waiting: string; uploading: string; uploaded: string; failed: string; retry: string; remove: (name: string) => string; tooLarge: (name: string, max: string) => string; wrongType: (name: string) => string; }>` |  |  |
| `maxSize?` | `number` |  | The largest file it takes, in bytes. |
| `multiple?` | `boolean` | `true` |  |
| `onRetry?` | `(id: string) => void` |  |  |

`files` is the host's list of `DropFile`s: `id`, `name`, `size` in bytes, `status`, `progress` from 0 to 100, `error`. `onAdd` gets the files that passed `accept` and `maxSize`; `onRemove` and `onRetry` get an id. `accept` takes extensions, exact types and wildcards, as an input's. `multiple` is true by default. `hint` says what it takes. `dragging` draws the drag-over look while true, for a host that lights the area as soon as a file is dragged anywhere over the window; a drag over the area itself draws it either way, and a disabled area never does. `disabled` stops new files and removal. `labels` replaces every string.

## Usage

### When to use
- A booking's documents, a contract, an import: files that go to a server and take a moment each.
- A person may add several files and needs to see which arrived.

### When not to use
- A single avatar, logo or cover, placed before it is saved. Use ImageField instead, which crops and uploads one picture in place.
- Text a person could paste, such as a list of tickers. Use Textarea instead.

### Rules
- `rule/file-drop-refusals-in-place`: When a file fails `accept` or `maxSize`, let `FileDrop` name it under the area at once, instead of calling `toast()` or waiting for the server, because the person is looking at the area they dropped on and the reason is known before any request. Evidence: person D39; principle heuristic: error prevention. Check: test `file-drop.test.tsx` on `accepts`; review of every `onAdd`.
  - Don't: `onAdd={(files) => files.some((f) => f.size > max) && toast("File too large")}`
  - Do: `<FileDrop accept=".pdf,.csv" maxSize={10_000_000} files={files} onAdd={upload} onRemove={remove} />`
- `rule/file-drop-status-per-file`: When files upload, give each its own `status` and `progress` in `files`, instead of one spinner for the batch, because one failure among five must say which file failed and keep the four that arrived. Evidence: person D2; single use `docs/system/examples/file-drop/default.tsx:11-19`. Check: test `file-drop.test.tsx` expects a status word per row.
  - Don't: `{uploading && <Spinner label="Uploading files" />}`
  - Do: `files={[{ id, name, size, status: "pending", progress: 40 }]}`

### Content
- `rule/file-drop-hint-says-what-it-takes`: When the area takes some types or sizes only, write them in `hint` as the person knows them, such as "PDF, CSV or an image, up to 10 MB each", instead of the `accept` string, because ".pdf,.csv,image/*" is for the browser. Evidence: app 5/5 examples write the hint in words (`docs/system/examples/file-drop/default.tsx:46`); principle heuristic: match between system and the real world. Check: review.
  - Don't: `hint=".pdf,.csv,image/* (max 10485760)"`
  - Do: `hint="PDF, CSV or an image, up to 10 MB each"`

### Anti-slop
- `rule/file-drop-keyboard-way-in`: When taking files, keep the Choose files button that `FileDrop` renders, instead of a drop area that only a pointer can use, because a drag has no keyboard equivalent and the button is the only way in for a keyboard or a screen reader. Evidence: principle wcag: 2.1.1 Keyboard, every function works from a keyboard. Check: test `file-drop.test.tsx` expects the button, described by the hint.
  - Don't: `<div onDrop={take}>Drop files here</div>`
  - Do: `<FileDrop files={files} onAdd={upload} onRemove={remove} />`

### Limits
- `rule/file-drop-names-cut-at-the-end`: When file names can pass 30 characters, accept the cut, which keeps the extension in view and the whole name in the tile's tooltip, instead of a renderer that wraps names, because the tile's fixed 56px keeps a change of state from moving the files below, and at 390px the name's base has 176px. Evidence: measured an 83-character name's base cut at 176px at 390 and at 314px at 1440, every tile 56px in each state and 68px for a two-line failure, `.design-system/evidence/file-drop/tile-2026-10-17.json`. Check: probe /stress/file-drop?data=worst.
  - Don't: `labels` or CSS that sets the name to `whitespace-normal`
  - Do: `<FileDrop files={files} />` with the name as it is

## Accessibility
Rests on a native hidden file input opened by the system `Button`, and native buttons for Retry and Remove inside each `FileTile`.

### Keyboard
| Key | Where focus is | Effect | Focus after | Checked by |
|---|---|---|---|---|
| Tab | before the area | focuses Choose files, then each row's Retry and Remove | the button | by hand |
| Enter or Space | Choose files | opens the device's picker | the picker, then the button | by hand |
| Enter or Space | the glyph, as Retry | starts the file's upload again | the tile's Remove once the glyph stops being a button, since FileTile hands the focus on when a pressed button leaves | by hand |
| Enter or Space | Remove | removes the file | the next file's Remove, the one before, or Choose files when none is left | by hand |

### ARIA
| Part | Role | Accessible name from | States and properties | Announced | Checked by |
|---|---|---|---|---|---|
| Choose files | button | its text | `aria-describedby` the hint and a refusal | the name and the hint | test |
| refusal | alert | its text | none | at once | test |
| list | list | "Files" | none | the count of files on entry | test |
| state word | status | "Waiting", "Uploading", "Uploaded" or the reason | none | each change, once | test |
| glyph while uploading | progressbar | "Uploading" and the file's name | `aria-valuenow` once the share is known | the value on request | test |
| Remove | button | "Remove" and the file's name | `disabled` when the area is | the name | test |

## Tokens
| Part | State | Token |
|---|---|---|
| area | rest | `--control-edge` dashed, `--input` at 30 percent, the `--r-2xl` corner, `--muted-foreground` |
| area | drag-over | `--primary`, `--primary-soft` |
| size and percentage | all | `--muted-foreground`, tabular figures |
| check | uploaded | `--good` |
| reason | failed | `--destructive` |
| glyph | all | the `--file-*` kind colours and `--file-ink` through `FileTypeIcon`, `--file-other` grey while uploading or failed |
| tile | all | `--card` (`--muted` in the dark theme), the `--r-xl` corner, through `FileTile` |

Measured at 1440 and 390 (`.design-system/evidence/file-drop/tile-2026-10-17.json`, on /stress/file-drop?data=worst): the area 206px tall at 1440 and 222px at 390 with its long German hint, every file's tile 56px in each state, and 68px while a failure's reason takes two lines. Measured again once the rows became `FileTile`s (`.design-system/evidence/file-drop/fd-cut.mjs`): no state line is cut at 1440 or 390, on /components/file-drop or the stress page; "Uploading, 60%" and "Uploading, 99%" show whole in a 340px line at 1440 and a 202px line at 390, where "Uploading" was cut before.

## Related
- FileTile: one file's tile, the one each file here is drawn with, also for a message or a composer.
- FileTypeIcon: the glyph that draws a file's upload on itself.
- ImageField: one picture, cropped and uploaded in place.
- Button: the way in for the keyboard.
- Field: holds a form's other controls; FileDrop names itself through its button and hint, so it sits under a heading instead.
- Input: one line of typed text; a file comes through FileDrop, never a styled `type="file"` field.
