{dragId && (
-
+ {renderToken(findGroup(tokenGroups, dragId), false)}
)}
,
diff --git a/src/components/token/TokenBarToken.js b/src/components/token/TokenBarToken.js
index f6dd70f..7baa2fa 100644
--- a/src/components/token/TokenBarToken.js
+++ b/src/components/token/TokenBarToken.js
@@ -21,6 +21,7 @@ function TokenBarToken({ token }) {
width: "100%",
height: "100%",
objectFit: "cover",
+ pointerEvents: "none",
}}
alt={token.name}
title={token.name}
diff --git a/src/components/token/TokenBarTokenGroup.js b/src/components/token/TokenBarTokenGroup.js
index 5494fad..25dc12d 100644
--- a/src/components/token/TokenBarTokenGroup.js
+++ b/src/components/token/TokenBarTokenGroup.js
@@ -1,84 +1,131 @@
-import React, { useState } from "react";
-import { Grid, Flex } from "theme-ui";
+import React, { useState, useRef } from "react";
+import { Grid, Flex, Box } from "theme-ui";
import { useSpring, animated } from "react-spring";
+import { useDraggable } from "@dnd-kit/core";
import TokenImage from "./TokenImage";
import TokenBarToken from "./TokenBarToken";
import Draggable from "../drag/Draggable";
+import Vector2 from "../../helpers/Vector2";
+
import GroupIcon from "../../icons/GroupIcon";
-function TokenBarTokenGroup({ group, tokens }) {
+function TokenBarTokenGroup({ group, tokens, draggable }) {
+ const { attributes, listeners, setNodeRef, isDragging } = useDraggable({
+ id: draggable && group.id,
+ disabled: !draggable,
+ });
const [isOpen, setIsOpen] = useState(false);
const { height } = useSpring({
height: isOpen ? (tokens.length + 1) * 56 : 56,
});
+ function renderToken(token) {
+ if (draggable) {
+ return (
+
+
+
+ );
+ } else {
+ return ;
+ }
+ }
+
function renderTokens() {
if (isOpen) {
return (
- <>
+
setIsOpen(false)}
+ onClick={(e) => handleOpenClick(e, false)}
key="group"
alt={group.name}
title={group.name}
+ {...listeners}
+ {...attributes}
>
- {tokens.map((token) => (
-
-
-
- ))}
- >
+ {tokens.map(renderToken)}
+
);
} else {
- return tokens.slice(0, 4).map((token) => (
-
- ));
+ return (
+
+ {tokens.slice(0, 4).map((token) => (
+
+ ))}
+
+ );
+ }
+ }
+
+ // Reject the opening of a group if the pointer has moved
+ const clickDownPositionRef = useRef(new Vector2(0, 0));
+ function handleOpenDown(event) {
+ clickDownPositionRef.current = new Vector2(event.clientX, event.clientY);
+ }
+ function handleOpenClick(event, newOpen) {
+ const clickPosition = new Vector2(event.clientX, event.clientY);
+ const distance = Vector2.distance(
+ clickPosition,
+ clickDownPositionRef.current
+ );
+ if (distance < 5) {
+ setIsOpen(newOpen);
}
}
return (
- !isOpen && setIsOpen(true)}
- >
-
+ !isOpen && handleOpenClick(e, true)}
>
{renderTokens()}
-
-
+
+