FileDropzone
Data inputPicks files: <FileDropzone> with bind:files (a File[]) takes files dropped on it or chosen by browsing (a click, or Enter or Space on its focused input). It turns away files that don't match accept, are larger than maxSize or exceed maxFiles, and says why; onReject hears it too. Chosen files are listed with a remove button, and with a bar for those in progress (a Map of file to 0โ1). The app does the upload; with name, a form posts the chosen files. Inside a Field it takes its id, label, description, error and invalid and disabled state from the Field.
import { FileDropzone } from '@limerdev/zest'; Input with type="file". Uploading inside the component: upload from bind:files in the app, and show it with progress.Examples
Basic
In a Field: images or PDFs up to 10 MB. Drop files on it, or click or press Enter to browse.
Images or PDFs, up to 10 MB each, five at most.
<script lang="ts">
import { Field, FileDropzone } from '@limerdev/zest';
let receipts: File[] = $state([]);
</script>
<Field.Root class="w-full max-w-md">
<Field.Label>Receipts</Field.Label>
<FileDropzone
name="receipts"
bind:files={receipts}
multiple
accept="image/*,.pdf"
maxSize={10_000_000}
maxFiles={5}
/>
<Field.Description>Images or PDFs, up to 10 MB each, five at most.</Field.Description>
</Field.Root> Upload
The app uploads: here a timer stands in for the request, and a SvelteMap of progress draws a bar per file.
<script lang="ts">
import { Button, FileDropzone } from '@limerdev/zest';
import { SvelteMap } from 'svelte/reactivity';
let files: File[] = $state([]);
const progress = new SvelteMap<File, number>();
function upload(): void {
for (const file of files) {
if (progress.has(file)) continue;
progress.set(file, 0);
const timer = setInterval(() => {
const next = Math.min(1, (progress.get(file) ?? 0) + 0.1);
progress.set(file, next);
if (next === 1) clearInterval(timer);
}, 200);
}
}
</script>
<div class="flex w-full max-w-md flex-col gap-3">
<FileDropzone bind:files multiple {progress} aria-label="Attachments" />
<Button class="self-end" disabled={files.length === 0} onclick={upload}>Upload</Button>
</div> States
One CSV at a time (a new one replaces it), with its own label text; and disabled in its Field.
Locked once the invoice is sent.
<script lang="ts">
import { Field, FileDropzone } from '@limerdev/zest';
</script>
<div class="grid w-full max-w-md gap-6">
<Field.Root>
<Field.Label>Import</Field.Label>
<FileDropzone
name="import"
accept=".csv,text/csv"
label="Drop a CSV file, or click to browse"
/>
</Field.Root>
<Field.Root disabled>
<Field.Label>Contract</Field.Label>
<FileDropzone name="contract" accept=".pdf" />
<Field.Description>Locked once the invoice is sent.</Field.Description>
</Field.Root>
</div> Props
Every part also takes class (merged over its defaults), ref (bindable: the element), children, and child (a snippet that
renders your own element, with props to spread on it).
FileDropzone
| Prop | Type | Default | Description |
|---|---|---|---|
| accept | string | undefined | The file types it takes, as for an input: image/*, .pdf, application/pdf. |
| files bindable | File[] | [] | The chosen files, in the order they were added. Bindable. |
| label | string | the Config's `strings.dropFiles`, else 'Drop files here, or click to browse' | The text in the drop area. |
| locale | string | the Config's `defaultLocale`, else the browser's | The locale for file sizes (1,5 MB in sv-SE). |
| maxFiles | number | undefined | How many files it holds at most; more are turned away. |
| maxSize | number | undefined | The largest file it takes, in bytes. |
| multiple | boolean | false | Takes several files. Without it a new file replaces the chosen one. |
| onReject | ((rejections: FileRejection[]) => void) | undefined | Called with the files turned away when files are added. |
| progress | Map<File, number> | undefined | Each file's upload progress, from 0 to 1, for files the app is uploading. A file without an entry shows no bar. |
| rejectionMessage | ((rejection: FileRejection, maxSize: string) => string) | the Config's `strings.fileRejected`, else English messages: "report.exe isn't a type this takes." | The message for a turned-away file, given the rejection and the largest size, formatted. |
| removeLabel | ((name: string) => string) | the Config's `strings.removeFile`, else (name) => `Remove ${name} | The remove button's accessible name, given the file's name. |