UNPKG

@atlaskit/pragmatic-drag-and-drop-react-beautiful-dnd-migration

Version:

An optional Pragmatic drag and drop package that enables rapid migration from react-beautiful-dnd to Pragmatic drag and drop

49 lines (47 loc) 1.42 kB
import { useEffect } from 'react'; // eslint-disable-next-line import/no-extraneous-dependencies import { attachClosestEdge } from '@atlaskit/pragmatic-drag-and-drop-hitbox/closest-edge'; import { dropTargetForElements } from '@atlaskit/pragmatic-drag-and-drop/element/adapter'; import { rbdInvariant } from '../drag-drop-context/rbd-invariant'; import { isDraggableData } from '../draggable/data'; export function useDropTargetForDraggable({ elementRef, data, direction, contextId, isDropDisabled, type }) { useEffect(() => { const element = elementRef.current; rbdInvariant(element instanceof HTMLElement); return dropTargetForElements({ element, getIsSticky() { return true; }, canDrop({ source }) { if (!isDraggableData(source.data)) { // not dragging something from the migration layer // we should not allow dropping return false; } if (isDropDisabled) { return false; } return source.data.type === type && source.data.contextId === contextId; }, getData({ input }) { return attachClosestEdge(data, { element, input, allowedEdges: direction === 'vertical' ? ['top', 'bottom'] : ['left', 'right'] }); } }); }, [data, direction, contextId, isDropDisabled, type, elementRef]); }