Skip to content

Commit

Permalink
Disable DnD hooks when DnDContext is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
TimboKZ committed Nov 20, 2020
1 parent 89fc042 commit f1bc3ea
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
selectFileActionIds,
selectIsDnDDisabled,
} from '../../redux/selectors';
import { useDndContextAvailable } from '../../util/dnd-fallback';
import { elementIsInsideButton } from '../../util/helpers';
import { makeGlobalChonkyStyles } from '../../util/styles';
import { useContextMenuTrigger } from '../external/FileContextMenu-hooks';
Expand Down Expand Up @@ -60,13 +61,14 @@ export const ChonkyPresentationLayer: React.FC<ChonkyPresentationLayerProps> = (
[fileActionIds]
);

const dndContextAvailable = useDndContextAvailable();
const showContextMenu = useContextMenuTrigger();

const classes = useStyles();
return (
<ClickAwayListener onClickAway={handleClickAway}>
<Box className={classes.chonkyRoot} onContextMenu={showContextMenu}>
{!dndDisabled && <DnDFileListDragLayer />}
{!dndDisabled && dndContextAvailable && <DnDFileListDragLayer />}
{hotkeyListenerComponents}
{children ? children : null}
</Box>
Expand Down
26 changes: 26 additions & 0 deletions packages/chonky/src/util/dnd-fallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useCallback, useContext } from 'react';
import { DndContext, useDrag, useDrop } from 'react-dnd';

export const useDndContextAvailable = () => {
const dndContext = useContext(DndContext);
const { dragDropManager } = dndContext;
return !!dragDropManager;
};

export const useDragIfAvailable: typeof useDrag = (...args) => {
// This is an ugly hack to make `useDrag` not throw if a `DndContext` is not available.
// See: https://github.com/react-dnd/react-dnd/issues/2212
const dndContextAvailable = useDndContextAvailable();
const mockHook = useCallback(() => [{} as any, () => null, () => null], []);
// @ts-ignore
const useHook: typeof useDrag = dndContextAvailable ? useDrag : mockHook;
return useHook(...args);
};

export const useDropIfAvailable: typeof useDrop = (...args) => {
const dndContextAvailable = useDndContextAvailable();
const mockHook = useCallback(() => [{} as any, () => null], []);
// @ts-ignore
const useHook: typeof useDrop = dndContextAvailable ? useDrop : mockHook;
return useHook(...args);
};
7 changes: 4 additions & 3 deletions packages/chonky/src/util/dnd.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback, useEffect, useMemo } from 'react';
import { DragSourceMonitor, DropTargetMonitor, useDrag, useDrop } from 'react-dnd';
import { DragSourceMonitor, DropTargetMonitor } from 'react-dnd';
import { getEmptyImage } from 'react-dnd-html5-backend';
import { useDispatch, useSelector, useStore } from 'react-redux';
import { ExcludeKeys, Nullable } from 'tsdef';
Expand All @@ -21,6 +21,7 @@ import {
} from '../types/dnd.types';
import { DndEntryState } from '../types/file-list.types';
import { FileData } from '../types/file.types';
import { useDragIfAvailable, useDropIfAvailable } from './dnd-fallback';
import { FileHelper } from './file-helper';
import { useInstanceVariable } from './hooks-helpers';

Expand Down Expand Up @@ -88,7 +89,7 @@ export const useFileDrag = (file: Nullable<FileData>) => {
(monitor) => ({ isDragging: monitor.isDragging() }),
[]
);
const [{ isDragging: dndIsDragging }, drag, preview] = useDrag({
const [{ isDragging: dndIsDragging }, drag, preview] = useDragIfAvailable({
item,
canDrag,
begin: onDragStart,
Expand Down Expand Up @@ -168,7 +169,7 @@ export const useFileDrop = ({
const [
{ isOver: dndIsOver, isOverCurrent: dndIsOverCurrent, canDrop: dndCanDrop },
drop,
] = useDrop({
] = useDropIfAvailable({
accept: ChonkyDndFileEntryType,
drop: onDrop,
canDrop,
Expand Down

0 comments on commit f1bc3ea

Please sign in to comment.