@deepdub/react-arborist
Version:
44 lines (43 loc) • 1.51 kB
JavaScript
import { useDrop } from "react-dnd";
import { useTreeApi } from "../context";
import { computeDrop } from "./compute-drop";
import { actions as dnd } from "../state/dnd-slice";
export function useOuterDrop() {
const tree = useTreeApi();
// In case we drop an item at the bottom of the list
const [, drop] = useDrop(() => ({
accept: "NODE",
canDrop: (_item, m) => {
if (!m.isOver({ shallow: true }))
return false;
return tree.canDrop();
},
hover: (_item, m) => {
if (!m.isOver({ shallow: true }))
return;
const offset = m.getClientOffset();
if (!tree.listEl.current || !offset)
return;
const { cursor, drop } = computeDrop({
element: tree.listEl.current,
offset: offset,
indent: tree.indent,
hitAreaHeight: tree.props.rowHitAreaHeight,
disableReorder: tree.props.disableReorder,
node: null,
prevNode: tree.visibleNodes[tree.visibleNodes.length - 1],
nextNode: null,
});
if (drop)
tree.dispatch(dnd.hovering(drop.parentId, drop.index));
if (m.canDrop()) {
if (cursor)
tree.showCursor(cursor);
}
else {
tree.hideCursor();
}
},
}), [tree]);
drop(tree.listEl);
}