# All files / src useDropzone.ts

96.03% Statements 218/227
88.88% Branches 8/9
75% Functions 6/8
96.03% Lines 218/227

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    2281x
    1x
    1x
    1x
    1x
    8x
    8x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    3x

    3x
    3x

    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x

    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    16x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    16x
    3x
    3x
    3x
    3x
    3x
    3x
    16x
    4x
    4x
    4x
    4x
    4x
    4x
    16x
    16x
    16x
     "use client";
    import type { DragEvent } from "react";
    import { useEffect, useRef, useState } from "react";

    const noop = (): void => {
      // do nothing
    };

    /**
     * @remarks
     * \@since 5.1.3
     * \@since 6.0.0 The element type is dynamically inferred on each handler
     * instead of the `DropzoneHandlers` type.
     */
    export interface DropzoneHandlers {
      onDrop<E extends HTMLElement>(event: DragEvent<E>): void;
      onDragEnter?<E extends HTMLElement>(event: DragEvent<E>): void;
      onDragOver?<E extends HTMLElement>(event: DragEvent<E>): void;
      onDragLeave?<E extends HTMLElement>(event: DragEvent<E>): void;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface DropzoneOptions extends DropzoneHandlers {
      /**
       * Set this to `true` if you do not need to capture drag events from outside
       * the window. i.e. Dragging files into the dropzone.
       *
       * @defaultValue `false`
       * @see {@link DropzoneHookReturnValue.isDragging}
       */
      disableDragging?: boolean;
    }

    /**
     * @remarks
     * \@since 2.9.0
     * \@since 6.0.0 Returns an object instead of an ordered array of
     * `[isOver: boolean, dropzoneHandlers: DropzoneHandlers]`. Also returns a new
     * `isDragging` state.
     */
    export interface DropzoneHookReturnValue {
      /**
       * This will be `true` when the user is dragging something over the dropzone
       * target.
       */
      isOver: boolean;

      /**
       * This will be `true` when the user is dragging anything within the document.
       * The main use case for this is detecting when a user is dragging a file into
       * the document so you can help highlight the dropzone area.
       *
       * This will always be `false` if {@link DropzoneOptions.disableDragging} is
       * `true`.
       */
      isDragging: boolean;

      /**
       * The event handlers that should be passed to the dropzone target.
       */
      dropzoneHandlers: Required<DropzoneHandlers>;
    }

    /**
     * @example
     * Simple Example
     * ```tsx
     * import { useDropzone, useFileUpload } from "@react-md/core"
     * import type { CSSProperties, ReactElement } from "react";
     *
     * const style: CSSProperties = {
     *   border: '1px solid blue',
     * };
     *
     * function Example(): ReactElement {
     *   const { onDrop } = useFileUpload()
     *   const { isOver, dropzoneHandlers } = useDropzone({
     *     onDrop(event) {
     *       // normally use the `onDrop` behavior from `useFileUpload` to upload
     *       // files:
     *       // onDrop(event);
     *     },
     *     disableDragging: true,
     *   });
     *
     *   return (
     *     <div {...dropzoneHandlers} style={isOver ? style : {}}>
     *       Drag and drop some files!
     *       {isOver && <UploadSVGIcon />}
     *     </div>
     *   );
     * }
     * ```
     *
     * @example
     * Dragging Example
     * ```tsx
     * import { useDropzone, useFileUpload } from "@react-md/core"
     * import type { CSSProperties, ReactElement } from "react";
     *
     * const draggingStyle: CSSProperties = {
     *   backgroundColor: "orange",
     * };
     * const overStyle: CSSProperties = {
     *   border: '1px solid blue',
     * };
     *
     * function Example(): ReactElement {
     *   const { onDrop } = useFileUpload()
     *   const { isOver, isDragging, dropzoneHandlers } = useDropzone({
     *     onDrop(event) {
     *       // normally use the `onDrop` behavior from `useFileUpload` to upload
     *       // files:
     *       // onDrop(event);
     *     },
     *   });
     *
     *   return (
     *     <div
     *       {...dropzoneHandlers}
     *       style={{
     *         ...(isDragging && draggingStyle),
     *         ...(isOver && overStyle),
     *       }}
     *     >
     *       Drag and drop some files!
     *       {isOver && <UploadSVGIcon />}
     *     </div>
     *   );
     * }
     * ```
     *
     * @remarks
     * \@since 2.9.0
     * \@since 6.0.0 Supports document-level dragging flag;
     */
    export function useDropzone(options: DropzoneOptions): DropzoneHookReturnValue {
      const {
        onDrop,
        onDragOver = noop,
        onDragEnter = noop,
        onDragLeave = noop,
        disableDragging = false,
      } = options;

      const [isOver, setOver] = useState(false);
      const [isDragging, setDragging] = useState(false);
      const draggingTimeout = useRef<number | undefined>();

      useEffect(() => {
        if (disableDragging) {
          return;
        }

        const startDragging = (): void => {
          setDragging(true);
        };

        // Browsers sometimes don't trigger a dragleave event for the entire
        // document, so we have to work around that by using the `dragover` event
        // instead. The `dragover` event will continually fire within the window
        // until the user drops the file or moves the file outside of the window.
        //
        // So we can consider the
        const handler = (): void => {
          window.clearTimeout(draggingTimeout.current);
          draggingTimeout.current = window.setTimeout(() => {
            setDragging(false);
          }, 100);
        };

        window.addEventListener("dragenter", startDragging);
        window.addEventListener("dragover", handler);
        return () => {
          window.clearTimeout(draggingTimeout.current);
          window.removeEventListener("dragenter", startDragging);
          window.removeEventListener("dragover", handler);
        };
      }, [disableDragging]);

      return {
        isOver,
        isDragging,
        dropzoneHandlers: {
          // Note: need to call `event.stopPropagation()` and
          // `event.preventDefault())` for each of these handlers to prevent the
          // default browser behavior when dropping. Only calling within `onDrop`
          // does not work.
          //
          // i.e. dropping an image would preview that image in the current
          // window/tab instead of triggering the drop event.
          onDrop(event) {
            event.preventDefault();
            event.stopPropagation();

            window.clearTimeout(draggingTimeout.current);
            onDrop(event);
            setOver(false);
            setDragging(false);
          },
          onDragOver(event) {
            event.preventDefault();
            event.stopPropagation();

            window.clearTimeout(draggingTimeout.current);
            onDragOver(event);
            setOver(true);
          },
          onDragEnter(event) {
            event.preventDefault();
            event.stopPropagation();

            onDragEnter(event);
            setOver(true);
          },
          onDragLeave(event) {
            event.preventDefault();
            event.stopPropagation();

            onDragLeave(event);
            setOver(false);
          },
        },
      };
    }