Skip to content

useDropZone ​

Hook that provides drop zone functionality

elements
test coverage
Last changed: 7 days ago

Installation ​

Library
CLI
Manual
typescript
import { useDropZone } from '@siberiacancode/reactuse';

Usage ​

typescript
const { overed, files } = useDropZone(ref, options);
// or
const { overed, files } = useDropZone(ref, () => console.log('callback'));
// or
const { ref, overed, files } = useDropZone(options);
// or
const { ref, overed, files } = useDropZone(() => console.log('callback'));

Demo ​

Api ​

Parameters

NameTypeDefaultNote
targetTarget-The target element drop zone's
options.dataTypes?DataTypes-The data types
options.multiple?boolean-The multiple mode
options.onDrop?(files: File[] | null, event: DragEvent) => void-The on drop callback function
options.onEnter?(event: DragEvent) => void-The on enter callback function
options.onLeave?(event: DragEvent) => void-The on leave callback function
options.onOver?(event: DragEvent) => void-The on over callback function

Returns

UseDropZoneReturn

Parameters

NameTypeDefaultNote
targetTarget-The target element drop zone's
callback?(files: File[] | null, event: DragEvent) => void-The callback function to be invoked on drop

Returns

UseDropZoneReturn

Parameters

NameTypeDefaultNote
options.dataTypes?DataTypes-The data types
options.multiple?boolean-The multiple mode
options.onDrop?(files: File[] | null, event: DragEvent) => void-The on drop callback function
options.onEnter?(event: DragEvent) => void-The on enter callback function
options.onLeave?(event: DragEvent) => void-The on leave callback function
options.onOver?(event: DragEvent) => void-The on over callback function

Returns

UseDropZoneReturn & { ref: StateRef<Target> }

Parameters

NameTypeDefaultNote
callback?(files: File[] | null, event: DragEvent) => void-The callback function to be invoked on drop

Returns

UseDropZoneReturn & { ref: StateRef<Target> }

Type declaration ​

typescript
import type { HookTarget } from '@/utils/helpers';

import type { StateRef } from '../useRefState/useRefState';

export type DropZoneDataTypes = ((types: string[]) => boolean) | string[];

export interface UseDropZoneOptions {
  /** The data types for drop zone */
  dataTypes?: DropZoneDataTypes;
  /** The multiple mode for drop zone */
  multiple?: boolean;
  /** The on drop callback */
  onDrop?: (files: File[] | null, event: DragEvent) => void;
  /** The on enter callback */
  onEnter?: (event: DragEvent) => void;
  /** The on leave callback */
  onLeave?: (event: DragEvent) => void;
  /** The on over callback */
  onOver?: (event: DragEvent) => void;
}

export interface UseDropZoneReturn {
  /** The files that was dropped in drop zone */
  files: File[] | null;
  /** The over drop zone status */
  overed: boolean;
}

export interface UseDropZone {
  (
    target: HookTarget,
    callback?: (files: File[] | null, event: DragEvent) => void
  ): UseDropZoneReturn;

  <Target extends Element>(
    callback?: (files: File[] | null, event: DragEvent) => void,
    target?: never
  ): UseDropZoneReturn & {
    ref: StateRef<Target>;
  };

  (target: HookTarget, options?: UseDropZoneOptions): UseDropZoneReturn;

  <Target extends Element>(
    options?: UseDropZoneOptions,
    target?: never
  ): UseDropZoneReturn & {
    ref: StateRef<Target>;
  };
}

Source ​

Source • Demo

Contributors ​

B
babin
babin
D
debabin
debabin
V
VLADISLAW9
VLADISLAW9

Released under the MIT License.