Skip to content

Commit f1bc3ea

Browse files
committed
Disable DnD hooks when DnDContext is not available
1 parent 89fc042 commit f1bc3ea

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

packages/chonky/src/components/internal/ChonkyPresentationLayer.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
selectFileActionIds,
1616
selectIsDnDDisabled,
1717
} from '../../redux/selectors';
18+
import { useDndContextAvailable } from '../../util/dnd-fallback';
1819
import { elementIsInsideButton } from '../../util/helpers';
1920
import { makeGlobalChonkyStyles } from '../../util/styles';
2021
import { useContextMenuTrigger } from '../external/FileContextMenu-hooks';
@@ -60,13 +61,14 @@ export const ChonkyPresentationLayer: React.FC<ChonkyPresentationLayerProps> = (
6061
[fileActionIds]
6162
);
6263

64+
const dndContextAvailable = useDndContextAvailable();
6365
const showContextMenu = useContextMenuTrigger();
6466

6567
const classes = useStyles();
6668
return (
6769
<ClickAwayListener onClickAway={handleClickAway}>
6870
<Box className={classes.chonkyRoot} onContextMenu={showContextMenu}>
69-
{!dndDisabled && <DnDFileListDragLayer />}
71+
{!dndDisabled && dndContextAvailable && <DnDFileListDragLayer />}
7072
{hotkeyListenerComponents}
7173
{children ? children : null}
7274
</Box>
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { useCallback, useContext } from 'react';
2+
import { DndContext, useDrag, useDrop } from 'react-dnd';
3+
4+
export const useDndContextAvailable = () => {
5+
const dndContext = useContext(DndContext);
6+
const { dragDropManager } = dndContext;
7+
return !!dragDropManager;
8+
};
9+
10+
export const useDragIfAvailable: typeof useDrag = (...args) => {
11+
// This is an ugly hack to make `useDrag` not throw if a `DndContext` is not available.
12+
// See: https://github.com/react-dnd/react-dnd/issues/2212
13+
const dndContextAvailable = useDndContextAvailable();
14+
const mockHook = useCallback(() => [{} as any, () => null, () => null], []);
15+
// @ts-ignore
16+
const useHook: typeof useDrag = dndContextAvailable ? useDrag : mockHook;
17+
return useHook(...args);
18+
};
19+
20+
export const useDropIfAvailable: typeof useDrop = (...args) => {
21+
const dndContextAvailable = useDndContextAvailable();
22+
const mockHook = useCallback(() => [{} as any, () => null], []);
23+
// @ts-ignore
24+
const useHook: typeof useDrop = dndContextAvailable ? useDrop : mockHook;
25+
return useHook(...args);
26+
};

packages/chonky/src/util/dnd.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useCallback, useEffect, useMemo } from 'react';
2-
import { DragSourceMonitor, DropTargetMonitor, useDrag, useDrop } from 'react-dnd';
2+
import { DragSourceMonitor, DropTargetMonitor } from 'react-dnd';
33
import { getEmptyImage } from 'react-dnd-html5-backend';
44
import { useDispatch, useSelector, useStore } from 'react-redux';
55
import { ExcludeKeys, Nullable } from 'tsdef';
@@ -21,6 +21,7 @@ import {
2121
} from '../types/dnd.types';
2222
import { DndEntryState } from '../types/file-list.types';
2323
import { FileData } from '../types/file.types';
24+
import { useDragIfAvailable, useDropIfAvailable } from './dnd-fallback';
2425
import { FileHelper } from './file-helper';
2526
import { useInstanceVariable } from './hooks-helpers';
2627

@@ -88,7 +89,7 @@ export const useFileDrag = (file: Nullable<FileData>) => {
8889
(monitor) => ({ isDragging: monitor.isDragging() }),
8990
[]
9091
);
91-
const [{ isDragging: dndIsDragging }, drag, preview] = useDrag({
92+
const [{ isDragging: dndIsDragging }, drag, preview] = useDragIfAvailable({
9293
item,
9394
canDrag,
9495
begin: onDragStart,
@@ -168,7 +169,7 @@ export const useFileDrop = ({
168169
const [
169170
{ isOver: dndIsOver, isOverCurrent: dndIsOverCurrent, canDrop: dndCanDrop },
170171
drop,
171-
] = useDrop({
172+
] = useDropIfAvailable({
172173
accept: ChonkyDndFileEntryType,
173174
drop: onDrop,
174175
canDrop,

0 commit comments

Comments
 (0)