vael-ui v0.5.7

Drag & drop

useFileDrop

Turns any element into a drop target for files, with a drag-over state that holds steady over child elements and ignores dragged text or links. Use it to build your own drop zone.

The drag-and-drop half of FileUpload. It makes any element a drop target for files from the desktop, tracks whether a drag is over it, and passes the dropped files to onFiles. It counts nested dragenter and dragleave events, so moving over a child element doesn't flicker the state. It doesn't validate anything: check type, size and count yourself, as FileUpload does with accept, maxSize and maxFiles. It doesn't open a file picker either, so pair it with an <input type="file"> for keyboard and touch users, who can't drag files.

Install

import { useFileDrop } from 'vael-ui'

Examples

Parameters

NameTypeDescription
targetElRef<HTMLElement | null>The drop target. The listeners follow it when the ref changes.
onFiles(files: File[]) => voidReceives every dropped file. A drop with no files, such as dragged text, doesn't call it.
disabledMaybeRefOrGetter<boolean>Ignores every drag event while true. The browser then handles a drop itself, which usually opens the file in the tab.

Returns

NameTypeDescription
isDragOverRef<boolean>true while a drag with files is over the target. Other drags, such as selected text, are ignored, and turning disabled on clears it.