Forms

DropZone

Takes files dropped anywhere in the window, showing an overlay while a file is dragged over the page, and optionally files pasted from the clipboard.

import { DropZone } from '@lostgradient/cinder';
formuploaddrag-and-drop
01

Overview

Reference usage

DropZone takes files dropped anywhere in the window. While a file is dragged over the page it shows an overlay with a label, and with paste set it also takes files pasted from the clipboard. It hands over File objects only: upload progress and transport stay in the page.

Usage

svelte
<script lang="ts">
  import { DropZone } from '@lostgradient/cinder';

  let names = $state<string[]>([]);
  let problems = $state<string[]>([]);

  function addFiles(files: File[]) {
    names = [...names, ...files.map((file) => file.name)];
  }
</script>

<DropZone
  accept="image/*"
  paste
  label="Drop images to attach"
  onFiles={addFiles}
  onReject={(rejected) => (problems = rejected.map((entry) => entry.message))}
/>

{#each problems as problem (problem)}
  <p role="alert">{problem}</p>
{/each}

<ul>
  {#each names as name (name)}
    <li>{name}</li>
  {/each}
</ul>

onFiles receives only the files accept allows, using the same filter as FileUpload: extensions, media types and type/* patterns. It is not called when none of the files are allowed, and a drop the filter rejects is still cancelled, so the browser does not open the file in place of the page.

Only a drag that carries files shows the overlay. Dragging text or a link over the window leaves it hidden. The overlay never takes the pointer, so a drop lands on whatever is beneath it. When a nested drop target such as a FileUpload takes the drop first, DropZone leaves it to that target.

onReject receives the files accept refuses, from a drop or a paste, as the same RejectedFile records FileUpload reports, each with the wrong-type reason and a message that names the file. Say what happened in the page's own words. It is not called when every file is allowed, and onFiles is not called when every file is refused.

The label defaults to "Drop files to add them", which suits any intake: set label to name what the page does with them.

Set disabled to turn the zone off, for instance while a modal dialog is open. A disabled zone attaches no listeners: no overlay appears, nothing is delivered or rejected, and the browser keeps its own behavior for a file dropped on the page. Turning it off mid-drag closes the overlay.

With paste set, a paste that carries files calls onFiles. A paste while focus is in an input, a textarea or an editable element is left to that field, so typing and pasting text work as usual.

The window listeners attach when the component mounts and detach when it is destroyed. Dragging is a pointer gesture, so keep a FileUpload or a file input on the page for keyboard users.

Live preview

Page-wide drop and paste

Drop or paste an image anywhere on this page.

02

When to use

Use when
  • A whole page is a drop target, such as a gallery or a conversation that takes screenshots.
  • Files pasted from the clipboard should join the same intake as files dropped.
Avoid when
03

Examples

Page-wide drop and paste

Drag image files over the window to see the overlay, drop them anywhere, or paste a copied image. Only the images the accept filter allows reach onFiles, and onReject names the rest.

04

Props

Props for drop-zone
Name
Type
Default
Description
onFiles required (files: File[]) => void Called with the files a drop, or a paste when paste is set, delivered and accept allows. It is not called when none of them are allowed.
onReject (files: RejectedFile[]) => void Called with the files accept refuses, from a drop or a paste, each with FileUpload's wrong-type reason and a message that names the file. It is not called when every file is allowed, and not for a drop a nested target already took or a paste into a text field.
accept text Native accept filter, as on FileUpload: extensions, media types and type/* patterns. Any file is taken when omitted.
label text 'Drop files to add them' Words the overlay shows while a file is dragged over the window.
paste boolean false Also takes files pasted from the clipboard, unless focus is in an input, a textarea or an editable element.
disabled boolean false Turns the zone off: it attaches no listeners, so no overlay appears and nothing is delivered or rejected. Set it while a modal dialog is open.