Skip to the content
human-kit design
06 — Form

Dropzone

A place to drop files, or to choose them with a click or the keyboard.

Installation

pnpm dlx @human-kit/design add dropzone
npx @human-kit/design add dropzone
yarn dlx @human-kit/design add dropzone
bunx @human-kit/design add dropzone

The 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 outline where the zone sits on the page or in an empty form: the dashed edge says it takes something.
  • Use ghost on a card, where a dashed edge would be one frame too many.
  • Use inline at the foot of a card or under a list of files already added, where a tall zone would push the content down.
  • Say in hint what it takes and how much: "PDF or images, up to 10 MB". Give the same rule in accept, so a dropped file of another kind is filtered out.
  • Handle onFilesRejected when 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, or aria-labelledby pointing 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. disabled takes the zone out of the tab order.

API reference

PropTypeDefault
variant'outline' | 'ghost' | 'inline'

The box around the content. inline lays it out on one line.

'outline'
titlestring

The line that says what to do.

'Drop files here, or click to choose'
hintstring

A quieter line under it: the kinds of file, the size it takes.

—
acceptstring

The kinds of file it takes, as the native accept. It also filters drops and pastes. Add extensions (.pdf) next to MIME types: a file with no type only matches an extension.

—
multipleboolean

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 accept filtered out.

—
announcementstring

A message for a screen reader, read out in a polite live region.

—
disabledboolean

Turns the zone off and takes it out of the tab order.

false
childrenSnippet<[DropzoneRenderState]>

Your own content in place of the icon, title and hint. It receives dragging, hovered, focused, focusVisible and disabled.

—
elementHTMLButtonElement | null

The rendered element. Bindable.

—

Plus every attribute of a native <button> but type, which is always button.