Skip to content

Commit efff26d

Browse files
committed
improve hook for dropzone monitoring, make it more stable by removing processing of unnecessary event calls
1 parent 9c33568 commit efff26d

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ This is a major release, and it might be not compatible with your current usage
5959
- toggling on/off the `<HandleTools/>` was corrected, they kept displayed after re-entering with the cursor
6060
- `<Pagination/>`
6161
- change text overflow for selectors to `clip` because Firefox rendered `ellipsis` a bit too early
62+
- `<ApplicationContainer />`:
63+
- `useDropzoneMonitor` helper hook process was improved so that less events are processed and the dropzone monitoring is more stable
6264

6365
### Changed
6466

src/components/Application/helper.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,48 @@ export const useApplicationHeaderOverModals = (elevate: boolean, className: stri
2828
export const useDropzoneMonitor = (enabledTypes: string[]) => {
2929
React.useEffect(() => {
3030
const monitor = window.document.body;
31+
let timestampMonitorEnabled = 0;
32+
let processDragleave: any;
3133

3234
const addMonitor = (event: DragEvent) => {
35+
// stop default, so that also no files cannot executed by browser without demand
36+
event.preventDefault();
37+
38+
if (processDragleave) {
39+
// stop removeMonitor process if it happend shortly before
40+
clearTimeout(processDragleave);
41+
}
42+
43+
// only process if monitor is not already enabled
44+
if (timestampMonitorEnabled > 0) {
45+
return;
46+
}
47+
48+
// stop the event here to prevent double execution
49+
event.stopImmediatePropagation();
50+
51+
// enable monitoring only for supported types of dragged elements
3352
const types = event.dataTransfer?.types || [];
3453
const monitorTypes = [...new Set(types.filter((type) => enabledTypes.includes(type)))];
3554
if (monitorTypes.length > 0 && !monitor.dataset.monitorDropzone) {
3655
monitor.dataset.monitorDropzone = monitorTypes.join(" ");
3756
}
38-
event.preventDefault();
57+
58+
timestampMonitorEnabled = Date.now();
3959
};
4060

4161
const removeMonitor = (event: DragEvent) => {
42-
if (event.type === "drop" || monitor === event.target) {
62+
const removeAction = () => {
4363
delete monitor.dataset.monitorDropzone;
4464
event.preventDefault();
65+
timestampMonitorEnabled = 0;
66+
};
67+
68+
if (event.type === "dragleave") {
69+
// use timeout function for dragleave to prevent useless removeMonitor actions
70+
processDragleave = setTimeout(removeAction, 250);
71+
} else {
72+
removeAction();
4573
}
4674
};
4775

0 commit comments

Comments
 (0)