Skip to main content

useDrop

function useDrop(ref: RefObject<HTMLElement | null>, options: DropOptions): Drop;

interface DropOptions {
onFiles?: ((files: File[], event: DragEvent) => void) | undefined;
onUri?: ((uri: string, event: DragEvent) => void) | undefined;
onText?: ((text: string, event: DragEvent) => void) | undefined;
enabled?: boolean | undefined; // default true
}

interface Drop {
readonly isOver: boolean;
}

Turns the element held by ref into a drop zone for files, links and text. isOver says whether something it handles is being dragged over it. Only kinds of data with a callback are handled, and each drop goes to one callback: onFiles if it carries files, otherwise onUri with the first URL of a link, otherwise onText. Other drags are ignored and left to the browser. Moving across the element's children doesn't make isOver flicker, and a handled drop isn't opened by the browser. The latest callbacks are always used; listeners follow the ref to a new element and are removed on unmount or while enabled is false. Disabling it ends any drag in progress. To accept only some file types, use useFileDrop.

Example​

import { useRef } from "react";
import { useDrop } from "react-honey-hooks";

function Bookmarks({ onAdd }: { onAdd: (url: string) => void }) {
const zone = useRef<HTMLDivElement>(null);
const { isOver } = useDrop(zone, { onUri: onAdd });

return (
<div ref={zone} className={isOver ? "zone zone--over" : "zone"}>
Drag a link here to bookmark it
</div>
);
}