Appearance
useDropZone
Create a zone where files can be dropped
WARNING
Due to Safari browser limitations, file type validation is only possible during the drop event, not during drag events. As a result, the isOverDropZone value will always be true during drag operations in Safari, regardless of file type.
Demo
Usage
tsx
import { useDropZone } from '@reause/core'
import { useRef } from 'react'
function Component() {
const dropZoneRef = useRef<HTMLDivElement>(null)
function onDrop(files: File[] | null) {
// called when files are dropped on zone
}
const { isOverDropZone } = useDropZone(dropZoneRef, {
onDrop,
// specify the types of data to be received.
dataTypes: ['image/jpeg'],
// control multi-file drop
multiple: true,
// whether to prevent default behavior for unhandled events
preventDefaultForUnhandled: false,
})
return (
<div ref={dropZoneRef}>
Drop files here
</div>
)
}Type Declarations
ts
export type UseDropZoneCallback = (files: File[] | null, event: DragEvent) => void
export interface UseDropZoneOptions {
dataTypes?: readonly string[] | ((types: readonly string[]) => boolean);
checkValidity?: (items: DataTransferItemList) => boolean;
onDrop?: UseDropZoneCallback;
onEnter?: UseDropZoneCallback;
onLeave?: UseDropZoneCallback;
onOver?: UseDropZoneCallback;
multiple?: boolean;
preventDefaultForUnhandled?: boolean;
}
export interface UseDropZoneReturn {
isOverDropZone: boolean;
files: File[] | null;
onDrop: (fn: UseDropZoneCallback) => () => void;
onDragEnter: (fn: UseDropZoneCallback) => () => void;
onDragLeave: (fn: UseDropZoneCallback) => () => void;
}
export function useDropZone(target: RefObject<HTMLElement | Document | null | undefined>, options?: UseDropZoneOptions | UseDropZoneOptions['onDrop']): UseDropZoneReturn