FileDrop
A place to drop files from the computer, with a button for those who prefer to choose them. It lists what was chosen with its size, and says why a file was refused.
import { FileDrop } from '@sypnia/ui/react'
One file, with rules
Takes one CSV of up to 5 MB. Try another kind of file: it says what it takes instead.
A CSV file of up to 5 MB, with the names of the columns in the first line.
Code
import { useState } from 'react';
import { Field, FileDrop } from '@sypnia/ui/react';
export default function Basic() {
const [files, setFiles] = useState<File[]>([]);
return (
<Field label="Rows to import" hint={files.length ? 'The first line must be the names of the columns.' : 'A CSV file of up to 5 MB, with the names of the columns in the first line.'}>
<FileDrop files={files} onChange={setFiles} accept={['.csv']} maxSize={5 * 1024 * 1024} />
</Field>
);
} Several files
Each new file is added to the list, and each can be removed.
Pictures or PDF, up to 10 MB each.
Code
import { Field, FileDrop } from '@sypnia/ui/react';
export default function Several() {
return (
<Field label="Attachments" hint="Pictures or PDF, up to 10 MB each.">
<FileDrop multiple accept={['image/*', '.pdf']} maxSize={10 * 1024 * 1024}>Drop the screenshots here or choose them</FileDrop>
</Field>
);
} Disabled
It says why nothing can be dropped.
An import is already running. Wait for it to finish.
Code
import { Field, FileDrop } from '@sypnia/ui/react';
export default function States() {
return (
<Field label="Rows to import" hint="An import is already running. Wait for it to finish.">
<FileDrop disabled accept={['.csv']} />
</Field>
);
} When to use it
Use it when
- The person brings a file from their computer: a CSV to import, a logo, a certificate.
- There are rules about what is taken:
acceptfor the kind of file,maxSizefor its weight.
Do not use it when
- The file is a picture that should be seen and cropped: ImageUpload or AvatarUpload.
- It is one small file in a tight form: FileInput takes one line.
- The content is short text the person could paste: Textarea or CodeInput.
For everyone
- Everything can be done without dragging: the button opens the file chooser of the device, and it is the part that takes the focus.
- A refused file is announced at once as an alert that names the file, says what is wrong and what to choose instead.
- Each chosen file has its own button, named «Remove» plus the name of the file.
- While a file is dragged over it, the zone lights up, so it is clear where to let go.
- Inside a Field it takes the label, the hint and the error by itself; alone, give it an
aria-label.
Props
| FileDrop | Type | Default |
|---|---|---|
acceptWhat it takes: extensions (`.csv`) or types (`text/csv`, `image/*`). Anything else is refused with a message. | string[] | |
aria-describedby | string | |
aria-labelThe name for screen readers when there is no Field around it. | string | |
aria-labelledby | string | |
childrenThe sentence inside the zone. By default «Drop a file here or choose one». | ReactNode | |
className | string | |
disabled | boolean | |
filesThe chosen files. Leave it out to let the component keep them. | File[] | |
idInside a Field it comes from the Field. | string | |
invalidMarks the value as wrong. Inside a `<Field error>` it is set for you. | boolean | |
maxSizeThe largest file it takes, in bytes. | number | |
multipleTakes several files. With one, a new file replaces the old one. | boolean | |
onChange | ((files: File[]) => void) |