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>
);
}import {
FileTrigger,
DropZone,
FileUploadList,
FileRow,
} from "@senvenli/react/form";Examples
From a button
FileTrigger opens the system picker from any Button.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.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
- One picture must be framed to a fixed shape before it is saved. Image cropper
- A link or path to the file is typed instead of picked. Input
Accessibility
Keyboard
| Keys | What happens |
|---|---|
| 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 | Description |
|---|---|---|---|
acceptedFileTypes | readonly string[] | – | MIME types or extensions: "image/jpeg", "image/*", ".heic", "application/pdf". |
maxSize | number | – | Bytes. |
onSelect | (files: File[]) => void | – | Files that passed the rules. |
onReject | (rejected: RejectedFile[]) => void | – | Files that did not, each with its reason. |
acceptedFileTypesreadonly string[]MIME types or extensions: "image/jpeg", "image/*", ".heic", "application/pdf".
maxSizenumberBytes.
onSelect(files: File[]) => voidFiles that passed the rules.
onReject(rejected: RejectedFile[]) => voidFiles that did not, each with its reason.
DropZone
| Prop | Type | Default | Description |
|---|---|---|---|
acceptedFileTypes | readonly string[] | – | MIME types or extensions: "image/jpeg", "image/*", ".heic", "application/pdf". |
maxSize | number | – | Bytes. |
labelRequired | string | – | What to drop, as a call to action: "Drag your photos here". |
typesLabel | string | – | The accepted types as people read them ("JPG, PNG, HEIC"); the size limit is added. |
allowsMultiple | boolean | – | |
onSelect | (files: File[]) => void | – | |
onReject | (rejected: RejectedFile[]) => void | – | |
isDisabled | boolean | – |
acceptedFileTypesreadonly string[]MIME types or extensions: "image/jpeg", "image/*", ".heic", "application/pdf".
maxSizenumberBytes.
labelRequiredstringWhat to drop, as a call to action: "Drag your photos here".
typesLabelstringThe accepted types as people read them ("JPG, PNG, HEIC"); the size limit is added.
allowsMultiplebooleanonSelect(files: File[]) => voidonReject(rejected: RejectedFile[]) => voidisDisabledboolean
FileUploadList
| Prop | Type | Default | Description |
|---|---|---|---|
childrenRequired | React.ReactNode | – | |
aria-label | string | – |
childrenRequiredReact.ReactNodearia-labelstring
FileRow
| Prop | Type | Default | Description |
|---|---|---|---|
file | File | – | The picked file; photos get a thumbnail at once, before upload ends. |
name | string | – | Shown name and size when there is no File object (a stored document). |
size | number | – | |
progress | number | – | 0–1 while uploading; omit when done. |
error | string | – | |
onCancel | () => void | – | |
onRemove | () => void | – | |
onReplace | () => void | – |
fileFileThe picked file; photos get a thumbnail at once, before upload ends.
namestringShown name and size when there is no File object (a stored document).
sizenumberprogressnumber0–1 while uploading; omit when done.
errorstringonCancel() => voidonRemove() => voidonReplace() => void