A hook that turns any element into a file drop target. It returns handlers to spread on the element and an isDragging flag. Nested children do not make the flag flicker, files are filtered by accept, and rejected ones come back with a reason.
Preview
Drag a few images from your desktop onto the box. Other files are rejected with a toast.
Drag images here
Nested elements do not make the highlight flicker
Installation
Usage
"use client"
import { useFileDrop } from "@/hooks/use-file-drop"
export default function Example() {
const { isDragging, dropProps } = useFileDrop({
accept: "image/*",
multiple: true,
onDrop: (files) => upload(files),
onReject: (files, reason) => console.warn(reason, files),
})
return (
<div
{...dropProps}
className={isDragging ? "border-primary bg-primary/5" : "border-dashed"}
>
Drag images here
</div>
)
}Examples
Drop onto an existing card
The hook does not render anything, so the drop target can be any block: here a note card shows an overlay while a text file is over it. multiple is off, so two files at once are rejected with the count reason.
Notes
import { Textarea } from "@/components/ui/textarea"
const { isDragging, dropProps } = useFileDrop({
accept: [".txt", ".md", "text/plain"],
onDrop: async ([file]) => setNotes(await file.text()),
onReject: (_files, reason) =>
toast.error(reason === "count" ? "One file at a time" : "Not a text file"),
})
<div {...dropProps} className="relative">
<Textarea value={notes} onChange={(e) => setNotes(e.target.value)} />
{isDragging && <div className="absolute inset-0">Drop to replace the text</div>}
</div>Checking files yourself
matchesAccept is exported too. Use it for files from a file picker or the clipboard, so every source follows the same rules.
import { matchesAccept } from "@/hooks/use-file-drop"
function onPaste(event: React.ClipboardEvent) {
const files = Array.from(event.clipboardData.files)
const images = files.filter((file) => matchesAccept(file, "image/*"))
if (images.length > 0) upload(images)
}Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| accept | string | string[] | - | MIME types, wildcards and extensions, like the accept attribute: "image/*,.pdf" |
| multiple | boolean | false | Accept more than one file per drop |
| disabled | boolean | false | Ignore drags and drops |
| onDrop | (files: File[]) => void | - | Called with the accepted files |
| onReject | (files: File[], reason: "type" | "count") => void | - | Called with rejected files and the reason: "type" or "count" |
Return value
| Name | Type | Description |
|---|---|---|
| isDragging | boolean | true while files are dragged over the element |
| dropProps | FileDropProps | onDragEnter, onDragOver, onDragLeave, onDrop and data-dragging for the element |
Features
- No flicker: dragenter and dragleave fire for every child, so the hook counts depth instead of toggling a flag
- Files only: dragging text or links over the element is ignored
- Recovery: a drop or a cancelled drag elsewhere on the page resets the state
- Fresh callbacks: onDrop and onReject can change between renders without re-binding