File drop
A drop zone and file picker that feeds a file list.
Demo
Usage
import { Field, FileDrop, FileItem, FileList } from '@tuttn/slant'
// It reports files and keeps no list: show them yourself, in a FileList.
const [files, setFiles] = useState<File[]>([])
<FileDrop multiple accept="image/*,.pdf" maxSize={2_000_000} maxFiles={4}
onFiles={added => setFiles(list => [...list, ...added])}
onReject={rejections => warn(rejections)} // [{ file, reason: 'type' | 'size' | 'count' }]
hint="Images or PDFs, up to 2 MB each" />
<FileList>{files.map(file => <FileItem key={file.name} name={file.name} type="PDF" progress={100} />)}</FileList>
// In a form: with a name, the input keeps the files it let in, dropped or
// picked, and the form submits them.
<form method="post" encType="multipart/form-data">
<FileDrop name="attachments" multiple />
</form>
// In a Field: the label names it (before the prompt), and the description and
// error describe it.
<Field invalid={missing} required>
<Field.Label>Attachments</Field.Label>
<FileDrop name="attachments" multiple size="sm" />
<Field.Error>Add at least one file.</Field.Error>
</Field>
// The whole window: while files are dragged anywhere over the page, an overlay
// shows where to drop them, and a drop anywhere goes through the same filter.
// The zone stays the way in for the keyboard and the picker.
<FileDrop scope="window" multiple accept="image/*" onFiles={add} hint="PNG or JPG, up to 5 MB" />Props
| Prop | Type | Default | Description |
|---|---|---|---|
onFiles | (files: File[]) => void | — | Called with the files a drop or a pick brings in that pass `accept`, `maxSize` and `maxFiles`; not called when none do. |
onReject | (rejections: FileRejection[]) => void | — | Called with the files a drop or a pick turned away, each with its reason; not called when there are none. |
maxSize | number | — | The largest file accepted, in bytes. |
maxFiles | number | 1 without `multiple`, else no limit | The most files one drop or pick may bring in; the ones past it are rejected with the reason `count`. |
hint | ReactNode | — | A line under the prompt, such as the types and size allowed. It describes the input. |
children | ReactNode | — | Replaces the prompt (“Choose files or drop them here”); it also names the input. |
size | 'sm' | 'md' | 'md' | `md` is a roomy zone with the icon over the prompt; `sm` is one compact row. |
invalid | boolean | — | Sets aria-invalid and a danger border. Inside a Field, the field's `invalid` does this. |
scope | 'zone' | 'window' | 'zone' | Where files can be dropped. `zone` is the zone alone. `window` is the whole page too: while files are dragged anywhere over it, an overlay across the window shows where to drop them (with the prompt, or children, and the hint), and a drop anywhere is taken as a drop on the zone. Disabled, it leaves drags over the page alone. |
…rest | InputHTMLAttributes<HTMLInputElement> | — | The ref and every other attribute (accept, multiple, name, form, required, aria-*) reach the <input type="file">; className and style go on the zone, a <label>. |
Accessibility
- The zone is a
<label>around a native<input type="file">, which is its one tab stop: Enter or Space opens the picker, and the focus ring shows on the zone. - The prompt names the input (after the Field’s label, inside a Field) and the
hintdescribes it, so a long hint isn’t read as part of the name. - What a drop or a pick did is announced in the root’s polite live region: “2 files added. 1 file not added”.
- The window overlay is
aria-hidden: dragging files is pointer-only, so it is there only to show where they go. Keyboard and screen reader users pick files through the zone, which takes the same files the same way.
The zone is a <label> around a visually hidden file input, so a click anywhere on it opens the file picker, and the input is the one tab stop: Enter or Space opens the picker, and the focus ring shows on the zone. Dragging files over it marks it data-dragging and the prompt reads “Drop to add”.
Dropped and picked files go through the same filter: accept (extensions such as .pdf, MIME types, and wildcards such as image/*), then maxSize in bytes, then maxFiles (one without multiple). The ones that pass reach onFiles, the rest onReject with a reason, and the root’s live region announces the outcome (“2 files added. 1 file not added”, from messages.filesAdded and filesRejected).
The prompt names the input and the hint describes it. Given a name, the input holds the files it let in, so a plain form submits them; without one, it empties after each pick, so the same file can be picked again. Show the files with FileList.
Dropping anywhere on the page
With scope="window" the zone renders as before, and the page takes drops too. While a drag that carries files is over the page, an overlay covers the window: a tint, an inset dashed frame, and the prompt (your children, else “Drop to add”) with the hint. A drop anywhere goes through the same filter as a drop on the zone, once, fills the input and is announced, and the browser doesn’t open the file. Text and links dragged over the page are left alone. The overlay goes when the drag leaves the window, ends, or is called off with Escape.
The overlay renders in a portal that carries the root’s theme, so it works inside a Dialog too, above the panel. It fades in, except under reduced motion. A disabled FileDrop doesn’t listen to the page at all. Use one window FileDrop per page: two would both take the same drop.