-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disable DnD hooks when DnDContext is not available
- Loading branch information
Showing
3 changed files
with
33 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters