UNPKG

@roadiehq/backstage-plugin-jira

Version:
50 lines (47 loc) 1.4 kB
import React, { useRef } from 'react'; import { useDrop, useDrag } from 'react-dnd'; const ItemType = "CARD"; const DraggableCard = ({ id, index, moveCard, children }) => { const ref = useRef(null); const [, drop] = useDrop({ accept: ItemType, hover(item, monitor) { if (!ref.current) { return; } const dragIndex = item.index; const hoverIndex = index; if (dragIndex === hoverIndex) { return; } const hoverBoundingRect = ref.current?.getBoundingClientRect(); const hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2; const clientOffset = monitor.getClientOffset(); const hoverClientY = clientOffset.y - hoverBoundingRect.top; if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) { return; } if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) { return; } moveCard(dragIndex, hoverIndex); item.index = hoverIndex; } }); const [{ isDragging }, drag] = useDrag({ type: ItemType, item: { id, index }, collect: (monitor) => ({ isDragging: monitor.isDragging() }) }); drag(drop(ref)); return /* @__PURE__ */ React.createElement("div", { ref, style: { opacity: isDragging ? 0.5 : 1 } }, children); }; export { DraggableCard as default }; //# sourceMappingURL=DraggableCard.esm.js.map