Dropzone
A place to drop files, or to choose them with a click or the keyboard.
Installation
pnpm dlx @human-kit/design add dropzonenpx @human-kit/design add dropzoneyarn dlx @human-kit/design add dropzonebunx @human-kit/design add dropzoneThe command copies the files to src/lib/components/ui/dropzone. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as Dropzone from '$lib/components/ui/dropzone';
</script>
<Dropzone.Root aria-label="Lab results" accept=".pdf,image/*" multiple onFilesPicked={upload} /> Examples
Reading the files
onFilesPicked hands you the native File objects, from the picker, a drop or a paste alike. The zone stores nothing: keep the list yourself, and say what happened in announcement.
Your own content
Give children to draw something else inside the zone. The snippet receives dragging, so the words can change while a file is held over it.
Guidelines
- Use
outlinewhere the zone sits on the page or in an empty form: the dashed edge says it takes something. - Use
ghoston a card, where a dashed edge would be one frame too many. - Use
inlineat the foot of a card or under a list of files already added, where a tall zone would push the content down. - Say in
hintwhat it takes and how much: "PDF or images, up to 10 MB". Give the same rule inaccept, so a dropped file of another kind is filtered out. - Handle
onFilesRejectedwhen the kinds are strict: a file filtered out without a word reads as a zone that did not work.
Accessibility
- The zone is a native
<button>: Tab reaches it, and Enter or Space opens the file picker. That is the way in for anyone who cannot drag. - A paste while the zone has the focus counts as a drop.
- Give it a name with
aria-label, oraria-labelledbypointing at a visible label. - A drop does not move the focus, so a screen reader hears nothing on its own. Put the result in
announcement, "2 files added", and it is read out politely. - The focus ring shows for the keyboard only.
disabledtakes the zone out of the tab order.
API reference
| Prop | Type | Default |
|---|---|---|
variant | 'outline' | 'ghost' | 'inline' The box around the content. | 'outline' |
title | string The line that says what to do. | 'Drop files here, or click to choose' |
hint | string A quieter line under it: the kinds of file, the size it takes. | — |
accept | string The kinds of file it takes, as the native | — |
multiple | boolean Takes more than one file. Without it, only the first file is sent. | false |
onFilesPicked | (files: File[]) => void Called with the accepted files of a pick, a drop or a paste. | — |
onFilesRejected | (files: File[]) => void Called with the files that | — |
announcement | string A message for a screen reader, read out in a polite live region. | — |
disabled | boolean Turns the zone off and takes it out of the tab order. | false |
children | Snippet<[DropzoneRenderState]> Your own content in place of the icon, title and hint. It receives | — |
element | HTMLButtonElement | null The rendered element. Bindable. | — |
Plus every attribute of a native <button> but type, which is always button.