Drop Zones

Drop Zones

Drop images here, or click to choose them
<div class="dropzone">
<input type="file" accept="image/*" multiple aria-label="Upload images" />
<i class="icon icon-upload" aria-hidden="true"></i>
<span>Drop images here, or click to choose them</span>
</div>

The markup is a box around a file input

The file input covers the box, so a click anywhere opens the file picker.

<div class="dropzone">
<input type="file" name="images" accept="image/*" multiple aria-label="Upload images" />
<i class="icon icon-upload" aria-hidden="true"></i>
<span>Drop images here, or click to choose them</span>
</div>

Dropped files are put on the input and its change event fires. The page uploads and shows the files.

In a field, taking one PNG or SVG file of up to 1 MB

Drop a file here, or click to choose one

PNG or SVG, up to 1 MB.

<div class="field">
<label for="demo-dropzone-logo">Store logo</label>
<div class="dropzone" data-max-size="1048576">
<input type="file" id="demo-dropzone-logo" accept=".png,.svg" aria-describedby="demo-dropzone-logo-help" />
<i class="icon icon-upload" aria-hidden="true"></i>
<span>Drop a file here, or click to choose one</span>
</div>
<p id="demo-dropzone-logo-help">PNG or SVG, up to 1 MB.</p>
</div>

As a tile beside the images already uploaded

Upload
<div class="flex flex-wrap gap-3">
<img src="/demo-assets/products/headphones.jpg" alt="" class="size-24 rounded-lg border object-cover" />
<img src="/demo-assets/products/mug.jpg" alt="" class="size-24 rounded-lg border object-cover" />
<div class="dropzone size-24 p-2">
<input type="file" accept="image/*" multiple aria-label="Upload product images" />
<i class="icon icon-upload" aria-hidden="true"></i>
<span>Upload</span>
</div>
</div>

The page shows a preview of each chosen image

Upload
<div class="flex flex-wrap gap-3" id="demo-dropzone-preview">
<div class="dropzone size-24 p-2">
<input type="file" accept="image/*" multiple aria-label="Upload product images" />
<i class="icon icon-upload" aria-hidden="true"></i>
<span>Upload</span>
</div>
</div>
<script>
document.addEventListener("DOMContentLoaded", () => {
const zone = document.querySelector("#demo-dropzone-preview > .dropzone");
zone.addEventListener("change", (event) => {
Array.from(event.target.files).forEach((file) => {
const image = document.createElement("img");
image.className = "size-24 rounded-lg border object-cover";
image.alt = file.name;
image.src = URL.createObjectURL(file);
// The address holds the file in memory until it is released.
image.addEventListener("load", () => URL.revokeObjectURL(image.src), { once: true });
zone.before(image);
});
});
});
</script>

While files are dragged over it

Drop files here, or click to choose them
<div class="dropzone" data-dragging="true">
<input type="file" aria-label="Upload files" />
<i class="icon icon-upload" aria-hidden="true"></i>
<span>Drop files here, or click to choose them</span>
</div>

Disabled, and invalid

Uploads are off for this store
Drop a file here, or click to choose one
<div class="grid gap-4 sm:grid-cols-2">
<div class="dropzone">
<input type="file" aria-label="Upload files" disabled />
<i class="icon icon-upload" aria-hidden="true"></i>
<span>Uploads are off for this store</span>
</div>
<div class="field" data-invalid>
<div class="dropzone">
<input type="file" aria-label="Upload a document" aria-invalid="true" aria-describedby="demo-dropzone-error" />
<i class="icon icon-upload" aria-hidden="true"></i>
<span>Drop a file here, or click to choose one</span>
</div>
<p id="demo-dropzone-error" role="alert">Choose a file before saving.</p>
</div>
</div>

The page reads the files from the change event, and the refused ones from the rejected event

Drop images of up to 2 MB here, or click to choose them

No files yet.

<div class="flex flex-col gap-3">
<div class="dropzone" id="demo-dropzone-events" data-max-size="2097152">
<input type="file" accept="image/*" multiple aria-label="Upload images" />
<i class="icon icon-upload" aria-hidden="true"></i>
<span>Drop images of up to 2 MB here, or click to choose them</span>
</div>
<p class="text-muted-foreground text-sm" id="demo-dropzone-events-result">No files yet.</p>
</div>
<script>
document.addEventListener("DOMContentLoaded", () => {
const zone = document.getElementById("demo-dropzone-events");
const result = document.getElementById("demo-dropzone-events-result");
zone.addEventListener("change", (event) => {
result.textContent = "Taken: " + Array.from(event.target.files, (file) => file.name).join(", ");
});
zone.addEventListener("rejected", (event) => {
result.textContent = "Rejected: " + event.detail.rejected.map((entry) => entry.file.name + " (" + entry.reason + ")").join(", ");
});
});
</script>

In a form, the input's name sends the files, dropped or picked

Drop documents here, or click to choose them

Nothing sent yet.

<form class="flex flex-col gap-3" id="demo-dropzone-form" onsubmit="event.preventDefault(); document.getElementById('demo-dropzone-form-result').textContent = 'Sent: ' + (new FormData(this).getAll('documents').filter((file) => file.name).map((file) => file.name).join(', ') || 'no files')">
<div class="dropzone">
<input type="file" name="documents" multiple aria-label="Upload documents" />
<i class="icon icon-upload" aria-hidden="true"></i>
<span>Drop documents here, or click to choose them</span>
</div>
<button type="submit" class="btn self-start">Save</button>
<p class="text-muted-foreground text-sm" id="demo-dropzone-form-result">Nothing sent yet.</p>
</form>

API Reference

Attributes

Attribute On Values Default Description
accept the input file types any The file types let through: ".png", "image/png" or "image/*", separated by commas. Checked for dropped and for picked files.
multiple the input present or absent absent Takes several files. Without it only the first dropped file is taken.
name the input text none Sends the files with the form the drop zone is in.
disabled the input present or absent absent Takes no files and dims the box.
aria-invalid the input "true" none Marks the box as invalid, as a field does.
data-max-size .dropzone bytes none The largest file let through.
data-dragging .dropzone "true" set by the script Present while files are dragged over the box. Write it by hand only to show that state.

Events

Event On Detail Description
change the input, and it bubbles to the drop zone none The input's own event, sent after a pick and after a drop. event.target.files holds the files let through.
rejected .dropzone, and it bubbles { rejected } A list of { file, reason }, where reason is "type" or "size". Those files are left off the input.

A drop that keeps no file leaves the files the input already holds.

The drop zone has no methods of its own: read and clear the files through the input.

Once the script has started it, the element carries data-dropzone-initialized="true" and sends tsugi:initialized. The methods and properties above are on the element itself.