- {dragId && (
-
- {renderToken(findGroup(tokenGroups, dragId), false)}
-
- )}
+
+ {dragId && renderToken(findGroup(tokenGroups, dragId), false)}
,
document.body
)}
-
+
);
}
diff --git a/src/contexts/DragContext.js b/src/contexts/DragContext.js
new file mode 100644
index 0000000..2614b0f
--- /dev/null
+++ b/src/contexts/DragContext.js
@@ -0,0 +1,75 @@
+// eslint-disable-next-line no-unused-vars
+import React, { useRef, ReactNode } from "react";
+import {
+ DndContext,
+ useDndContext,
+ useDndMonitor,
+ // eslint-disable-next-line no-unused-vars
+ DragEndEvent,
+} from "@dnd-kit/core";
+
+/**
+ * Wrap a dnd-kit DndContext with a position monitor to get the
+ * active drag element on drag end
+ * TODO: use look into fixing this upstream
+ * Related: https://github.com/clauderic/dnd-kit/issues/238
+ */
+
+/**
+ * @typedef DragEndOverlayEvent
+ * @property {DOMRect} overlayNodeClientRect
+ *
+ * @typedef {DragEndEvent & DragEndOverlayEvent} DragEndWithOverlayProps
+ */
+
+/**
+ * @callback DragEndWithOverlayEvent
+ * @param {DragEndWithOverlayProps} props
+ */
+
+/**
+ * @typedef CustomDragProps
+ * @property {DragEndWithOverlayEvent=} onDragEnd
+ * @property {ReactNode} children
+ */
+
+/**
+ * @param {CustomDragProps} props
+ */
+function DragPositionMonitor({ children, onDragEnd }) {
+ const { overlayNode } = useDndContext();
+
+ const overlayNodeClientRectRef = useRef();
+ function handleDragMove() {
+ if (overlayNode?.nodeRef?.current) {
+ overlayNodeClientRectRef.current = overlayNode.nodeRef.current.getBoundingClientRect();
+ }
+ }
+
+ function handleDragEnd(props) {
+ onDragEnd &&
+ onDragEnd({
+ ...props,
+ overlayNodeClientRect: overlayNodeClientRectRef.current,
+ });
+ }
+ useDndMonitor({ onDragEnd: handleDragEnd, onDragMove: handleDragMove });
+
+ return children;
+}
+
+/**
+ * TODO: Import Props interface from dnd-kit with conversion to Typescript
+ * @param {CustomDragProps} props
+ */
+function DragContext({ children, onDragEnd, ...props }) {
+ return (
+
+
+ {children}
+
+
+ );
+}
+
+export default DragContext;
diff --git a/src/contexts/TileDragContext.js b/src/contexts/TileDragContext.js
index 2e8c229..6f6644e 100644
--- a/src/contexts/TileDragContext.js
+++ b/src/contexts/TileDragContext.js
@@ -1,6 +1,5 @@
import React, { useState, useContext } from "react";
import {
- DndContext,
MouseSensor,
TouchSensor,
KeyboardSensor,
@@ -9,6 +8,8 @@ import {
closestCenter,
} from "@dnd-kit/core";
+import DragContext from "./DragContext";
+
import { useGroup } from "./GroupContext";
import { moveGroupsInto, moveGroups, ungroup } from "../helpers/group";
@@ -108,7 +109,7 @@ export function TileDragProvider({
}
function handleDragEnd(event) {
- const { active, over } = event;
+ const { active, over, overlayNodeClientRect } = event;
setDragId();
setOverId();
@@ -143,7 +144,9 @@ export function TileDragProvider({
}
onGroupsChange(newGroups);
} else if (over.id === ADD_TO_MAP_ID) {
- onDragAdd && onDragAdd(selectedGroupIds, over.rect);
+ onDragAdd &&
+ overlayNodeClientRect &&
+ onDragAdd(selectedGroupIds, overlayNodeClientRect);
} else if (!filter) {
// Hanlde tile move only if we have no filter
const overGroupIndex = activeGroups.findIndex(
@@ -210,7 +213,7 @@ export function TileDragProvider({
const value = { dragId, overId, dragCursor };
return (
-
{children}
-
+
);
}
diff --git a/src/modals/SelectTokensModal.js b/src/modals/SelectTokensModal.js
index 42606b4..421e0e9 100644
--- a/src/modals/SelectTokensModal.js
+++ b/src/modals/SelectTokensModal.js
@@ -144,8 +144,8 @@ function SelectTokensModal({ isOpen, onRequestClose, onMapTokensStateCreate }) {
const mapStageRef = useMapStage();
function handleTokensAddToMap(groupIds, rect) {
let clientPosition = new Vector2(
- rect.width / 2 + rect.offsetLeft,
- rect.height / 2 + rect.offsetTop
+ rect.width / 2 + rect.left,
+ rect.height / 2 + rect.top
);
const mapStage = mapStageRef.current;
if (!mapStage) {