Drag and Drop Upload Zone
Build an accessible drag-and-drop zone that also works with keyboard and click, validates dropped files, and avoids the common dropoutside bug.
On this page
Introduction
This guide covers accessible drag-and-drop upload zone, a recurring concern in the file-upload scenario. A drop zone that only works with a mouse excludes keyboard users and screen readers, and a naive dragenter counter breaks the highlight when a child element is entered. Building the zone with a hidden input, a label, and a corrected drag counter makes it work for everyone and stay visually correct.
Back with a label and input
Put a hidden input inside a label so clicking the zone or focusing and pressing Enter opens the picker, which is the keyboard path screen readers expect.
The trade-off worth naming is that put a hidden input inside a label so clicking the zone or focusing and pressing enter opens the picker, which is the keyboard path screen readers expect adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
<label class="zone" tabindex="0">
Drop files here or click to choose
<input type="file" multiple hidden />
</label>
Count drag events
Increment a counter on dragenter and decrement on dragleave, and clear the highlight only at zero, so moving over a child does not flicker the zone.
The trade-off worth naming is that increment a counter on dragenter and decrement on dragleave, and clear the highlight only at zero, so moving over a child does not flicker the zone adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
let depth = 0;
zone.addEventListener('dragenter', (e) => { e.preventDefault(); depth++; zone.classList.add('over'); });
zone.addEventListener('dragleave', () => { if (--depth === 0) zone.classList.remove('over'); });
zone.addEventListener('dragover', (e) => e.preventDefault());
zone.addEventListener('drop', (e) => { depth = 0; zone.classList.remove('over'); handle(e.dataTransfer.files); });
Validate on drop
Check each file size and type before accepting, and show an inline error for the rejected ones, so a 5 GB drop does not start uploading before the user sees it was refused.
The trade-off worth naming is that check each file size and type before accepting, and show an inline error for the rejected ones, so a 5 gb drop does not start uploading before the user sees it was refused adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Announce state
Use aria-live to announce when files are dragged over and when they are accepted, so a screen reader user hears the same state a sighted user sees.
The trade-off worth naming is that use aria-live to announce when files are dragged over and when they are accepted, so a screen reader user hears the same state a sighted user sees adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test all three paths
Verify the picker opens on click, on Enter, and on drop, and that a rejected file shows an error in each path, so the zone is not mouse-only.
The trade-off worth naming is that verify the picker opens on click, on enter, and on drop, and that a rejected file shows an error in each path, so the zone is not mouse-only adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
Sources: HTML drag and drop, WAI-ARIA practices.
FAQ
- Why does my drop zone highlight flicker?
- dragenter and dragleave fire for child elements too, so use a counter that increments on enter and decrements on leave, and only clear the highlight when it reaches zero.
- How do I make the zone keyboard accessible?
- Wrap it in a label tied to a hidden input, so focus and Enter open the file picker without a custom key handler that screen readers miss.
Report a problem with this articleOpens a pre-filled issue on GitHub.