Skip to content

Commit

Permalink
Fix: dropzone doesn't get drop events
Browse files Browse the repository at this point in the history
  • Loading branch information
OEvgeny committed Jan 28, 2025
1 parent 1f5e557 commit 866e8b9
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/fluent-theme/src/components/dropZone/DropZone.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { hooks } from 'botframework-webchat-component';
import cx from 'classnames';
import React, { memo, useCallback, useEffect, useRef, useState, type DragEventHandler } from 'react';
import React, {
memo,
useCallback,
useEffect,
useRef,
useState,
type DragEventHandler,
type DragEvent as ReactDragEvent
} from 'react';
import { useRefFrom } from 'use-ref-from';

import { AddDocumentIcon } from '../../icons';
Expand All @@ -10,7 +18,7 @@ import { useStyles } from '../../styles';

const { useLocalizer } = hooks;

const handleDragOver: DragEventHandler<HTMLDivElement> = event => {
const handleDragOver = (event: ReactDragEvent<unknown> | DragEvent) => {
// This is for preventing the browser from opening the dropped file in a new tab.
event.preventDefault();
};
Expand Down Expand Up @@ -47,6 +55,8 @@ const DropZone = (props: { readonly onFilesAdded: (files: File[]) => void }) =>
let entranceCounter = 0;

const handleDragEnter = (event: DragEvent) => {
document.addEventListener('dragover', handleDragOver);

entranceCounter++;

if (isFilesTransferEvent(event)) {
Expand All @@ -63,7 +73,10 @@ const DropZone = (props: { readonly onFilesAdded: (files: File[]) => void }) =>
const handleDragLeave = () => --entranceCounter <= 0 && setDropZoneState(false);

const handleDragEnd = () => {
document.removeEventListener('dragover', handleDragOver);

entranceCounter = 0;

setDropZoneState(false);
};

Expand All @@ -83,6 +96,7 @@ const DropZone = (props: { readonly onFilesAdded: (files: File[]) => void }) =>
document.removeEventListener('dragleave', handleDragLeave);
document.removeEventListener('dragend', handleDragEnd);
document.removeEventListener('drop', handleDocumentDrop);
document.removeEventListener('dragover', handleDragOver);
};
}, [setDropZoneState]);

Expand Down

0 comments on commit 866e8b9

Please sign in to comment.