Docs
useDropzone
Drag and drop files into a dropzone with useDropzone, providing options to customize the dropzone.
Loading...
Installation
pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-dropzone
Usage
"use client"
import * as React from "react"
import { useDropZone } from "@/hooks/use-dropzone"
export function Component() {
const dropZoneRef = React.useRef<HTMLDivElement>(null)
const { files, isOverDropZone } = useDropZone(dropZoneRef, {
onDrop: (files) => {
console.log("Dropped files:", files)
},
})
return (
<div style={{ display: "flex", flexDirection: "column", gap: "16px" }}>
<div
ref={dropZoneRef}
style={{
borderRadius: "16px",
borderWidth: "2px",
borderStyle: "dashed",
padding: "32px",
borderColor: isOverDropZone ? "#2563EB" : "#D1D5DB",
backgroundColor: isOverDropZone
? "rgba(37, 99, 235, 0.1)"
: "rgba(209, 213, 219, 0.15)",
}}
>
{files ? (
<p>Dropped {files.length} files</p>
) : isOverDropZone ? (
<p>Drop files here</p>
) : (
<p>Drag files here</p>
)}
</div>
<p>Here is the filenames dropped:</p>
<ul>{files?.map((file) => <li key={file.name}>{file.name}</li>)}</ul>
</div>
)
}
API Reference
Parameters
Name | Type | Description | Default Value | Optional |
---|---|---|---|---|
target | RefObject<HTMLElement | null> | A React ref to the HTML element that will act as the drop zone. | - | No |
options | UseDropZoneOptions | UseDropZoneOptions["onDrop"] | Configuration options or a standalone onDrop callback function. | {} | Yes |
UseDropZoneOptions
Interface
Name | Type | Description | Default Value | Optional |
---|---|---|---|---|
dataTypes | string[] | ((types: readonly string[]) => boolean) | Allowed MIME types or a function to validate dragged item types. | undefined | Yes |
onDrop | (files: File[] | null, event: DragEvent) => void | Callback when files are dropped. | undefined | Yes |
onEnter | (files: File[] | null, event: DragEvent) => void | Callback when dragging enters the drop zone. | undefined | Yes |
onLeave | (files: File[] | null, event: DragEvent) => void | Callback when dragging leaves the drop zone. | undefined | Yes |
onOver | (files: File[] | null, event: DragEvent) => void | Callback when dragging is over the drop zone. | undefined | Yes |
multiple | boolean | Whether to allow multiple files to be dropped. | true | Yes |
preventDefaultForUnhandled | boolean | Whether to prevent default behavior for unhandled drag events. | false | Yes |
Return Values
Name | Type | Description |
---|---|---|
files | File[] | null | The array of dropped files, or null if no files have been dropped or the drop was invalid. |
isOverDropZone | boolean | Indicates whether the user is currently dragging an item over the drop zone. |