Community JavaScript Snippet

A File Drop Zone Without a Library

The drop zone I keep instead of pulling in react-dropzone (60+kB minified): drag-over visuals, multi-file drops, folder uploads via DataTransferItem, and the Safari quirk where dragleave fires on every child enter.

A File Drop Zone Without a Library

The drop zone I keep instead of pulling in react-dropzone (60+kB minified): drag-over visuals, multi-file drops, folder uploads via DataTransferItem, and the Safari quirk where dragleave fires on every child enter.

JavaScript
Frontend
4 snippets
js-dom
code-template
frontend
utility
isabellarashid

By @isabellarashid

December 6, 2025

·

Updated May 18, 2026

323 views

1

4.4 (13)

The depth counter is the part nobody warns you about. The MDN docs show a single boolean isOver, which works fine until your zone has a button or icon inside it; then every hover over the inner element fires dragleave on the parent and your highlight flashes. Counting dragenter minus dragleave and only flipping the visual at the boundary makes the highlight stable. The dragover listener calling preventDefault() is non-negotiable: without it the browser's default behavior is dropEffect = 'none', so the cursor shows the no-drop icon and drop never fires. The synthetic harness at the bottom is how I unit-test the FSM in CI without spinning up a browser.