gridland
Hooks

useFileDrop

Listen for file drops onto the canvas

The useFileDrop hook listens for file drops onto the canvas and provides drag-over state for visual feedback.

Import
import { useFileDrop } from "@gridland/web"

Usage

Basic
function FileDropZone() {
  const { isDragOver } = useFileDrop((file) => {
    console.log(`Dropped: ${file.name} (${file.size} bytes)`)
    console.log(file.content)
  })

  return (
    <box border borderColor={isDragOver ? "green" : "gray"}>
      <text>{isDragOver ? "Drop here!" : "Drag a file here"}</text>
    </box>
  )
}

Examples

File Type Filtering

Check the MIME type before processing.

Accept only JSON
function JsonDropZone() {
  const [data, setData] = useState<string | null>(null)

  const { isDragOver } = useFileDrop((file) => {
    if (file.type === "application/json") {
      setData(file.content)
    }
  })

  return (
    <box border borderColor={isDragOver ? "cyan" : "gray"}>
      <text>{isDragOver ? "Drop JSON file..." : data ?? "Drop a .json file here"}</text>
    </box>
  )
}

Show File Info

Display metadata about the dropped file.

File info
function FileInfo() {
  const [file, setFile] = useState<DroppedFile | null>(null)

  const { isDragOver } = useFileDrop((dropped) => {
    setFile(dropped)
  })

  return (
    <box border borderColor={isDragOver ? "green" : "gray"} padding={1}>
      {file ? (
        <>
          <text>Name: {file.name}</text>
          <text>Type: {file.type}</text>
          <text>Size: {file.size} bytes</text>
        </>
      ) : (
        <text>{isDragOver ? "Release to drop" : "Drag a file here"}</text>
      )}
    </box>
  )
}

Parameters

ParamTypeDescription
callback(file: DroppedFile) => voidCalled when a file is dropped

Returns

PropertyTypeDescription
isDragOverbooleantrue while a file is being dragged over the canvas

DroppedFile

PropertyTypeDescription
namestringFile name
contentstringFile content as a string
typestringMIME type
sizenumberFile size in bytes