Skip to content

Upload Drop Area

A drag-and-drop file area with a click-to-browse fallback. Built on a native <input type="file">, so the browser owns file selection, keyboard operation and form participation.

The drag target itself is the generic dropArea action (exported from the package): it wires the dragover/dragleave/drop trio, reflects the state on a data-dragover attribute and hands the raw DataTransfer to onDrop. It is reusable on any element — a TreeView item, a kanban column — and it is a pointer-only enhancement by design: drag-and-drop has no keyboard or assistive-tech contract, so always pair it with an accessible alternative (here, the click-to-browse input).

<script>
import UploadDropArea from "@invisible-ui/svelte/UploadDropArea.svelte";
</script>

In Elements, picked and dropped files arrive in the bubbling files event, and the files the accept filter leaves out in the bubbling reject event:

<ds-upload-drop-area accept="image/*" multiple name="photos"></ds-upload-drop-area>
<script type="module">
const area = document.querySelector("ds-upload-drop-area");
area.addEventListener("files", (event) => upload(event.detail.files));
area.addEventListener("reject", (event) => log(event.detail.rejected));
</script>
PropTypeDefaultDescription
acceptstring | undefinedundefinedAccepted file types (the input's accept attribute), e.g. "image/*".
multiplebooleanfalseAllow selecting/dropping more than one file.
disabledbooleanfalse
namestring | undefinedundefinedForm field name (the underlying file input).
captionstring | undefinedundefinedOptional grey caption under the text (e.g. accepted formats / max size).
onFiles((files: File[]) => void) | undefinedundefinedCalled with the selected/dropped files.
onRejected((rejected: UploadRejection[]) => void) | undefinedundefinedCalled with the files the area left out: a type accept does not name, or a file beyond the first in an area without multiple.
iconSnippet | undefinedundefinedReplaces the built-in upload icon.

Dropped and picked files are forwarded through the same onFiles callback.

Picked and dropped files go through the same filter. accept has the format of the file input’s accept attribute: extensions (.pdf), media types (application/pdf) and type families (image/*), compared without case. A file with no media type matches by extension only. Without multiple, the area takes the first file that passes the filter.

The files the filter leaves out never reach onFiles and never join the form. The area names each one in a message under it, with the hazard icon, and reports them to onRejected (reject in Elements) as { file, reason }, where reason is "type" or "count". The message is an alert, and the file input points at it through aria-describedby. It stays until the next pick or drop. A drop with no file to take leaves the files the input already holds.

The filter is a convenience. The browser’s picker can still offer every file, and a file’s name and media type can be wrong, so the app validates the files it receives, on the server too.

<UploadDropArea
accept=".pdf,image/*"
multiple
onFiles={(files) => upload(files)}
onRejected={(rejected) => track(rejected.map(({ file, reason }) => `${file.name}: ${reason}`))}
/>

The messages come from the catalog: uploadDropArea.rejectedType and uploadDropArea.rejectedCount, both with a {name} placeholder.

<UploadDropArea accept="image/*" multiple onFiles={(files) => upload(files)} />
<!-- The generic action, on any element: -->
<script>
import { dropArea } from "@invisible-ui/svelte";
</script>
<li use:dropArea={{ onDrop: (data) => moveInto(node, data) }}>Documents</li>

In React, children replace the prompt and icon the glyph, and useDropArea returns the props for a drop target of your own:

<UploadDropArea accept="image/*" multiple onFiles={upload} />;
function Folder({ node }) {
const { dropAreaProps } = useDropArea({ onDrop: (data) => moveInto(node, data) });
return <li {...dropAreaProps}>Documents</li>;
}

Themeable via --ds-upload-drop-area-*.

  • The native file input is the control: Tab reaches it, Enter or Space opens the picker, and the prompt is its name.
  • Drag and drop is a pointer enhancement; the picker offers the same files.
  • Files the filter leaves out are named in an alert after the area, with the hazard icon, so the message never relies on color. The input points at it through aria-describedby.
  • Assistive technology: not tested.