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

NameTypeDescriptionDefault ValueOptional
targetRefObject<HTMLElement | null>A React ref to the HTML element that will act as the drop zone.-No
optionsUseDropZoneOptions | UseDropZoneOptions["onDrop"]Configuration options or a standalone onDrop callback function.{}Yes

UseDropZoneOptions Interface

NameTypeDescriptionDefault ValueOptional
dataTypesstring[] | ((types: readonly string[]) => boolean)Allowed MIME types or a function to validate dragged item types.undefinedYes
onDrop(files: File[] | null, event: DragEvent) => voidCallback when files are dropped.undefinedYes
onEnter(files: File[] | null, event: DragEvent) => voidCallback when dragging enters the drop zone.undefinedYes
onLeave(files: File[] | null, event: DragEvent) => voidCallback when dragging leaves the drop zone.undefinedYes
onOver(files: File[] | null, event: DragEvent) => voidCallback when dragging is over the drop zone.undefinedYes
multiplebooleanWhether to allow multiple files to be dropped.trueYes
preventDefaultForUnhandledbooleanWhether to prevent default behavior for unhandled drag events.falseYes

Return Values

NameTypeDescription
filesFile[] | nullThe array of dropped files, or null if no files have been dropped or the drop was invalid.
isOverDropZonebooleanIndicates whether the user is currently dragging an item over the drop zone.