Files
grungnet/src/components/map/MapToken.js
T

69 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-04-13 00:24:03 +10:00
import React, { useRef } from "react";
import { Box, Image } from "theme-ui";
import TokenLabel from "../token/TokenLabel";
import TokenStatus from "../token/TokenStatus";
2020-04-13 00:24:03 +10:00
import usePreventTouch from "../../helpers/usePreventTouch";
import useDataSource from "../../helpers/useDataSource";
import { tokenSources } from "../../tokens";
2020-04-13 00:24:03 +10:00
function MapToken({ token, tokenSizePercent, className }) {
const imageSource = useDataSource(token, tokenSources);
2020-04-13 00:24:03 +10:00
const imageRef = useRef();
// Stop touch to prevent 3d touch gesutre on iOS
usePreventTouch(imageRef);
return (
<Box
style={{
transform: `translate(${token.x * 100}%, ${token.y * 100}%)`,
width: "100%",
height: "100%",
}}
sx={{
position: "absolute",
pointerEvents: "none",
}}
>
<Box
style={{
width: `${tokenSizePercent * (token.size || 1)}%`,
}}
sx={{
position: "absolute",
pointerEvents: "all",
}}
>
<Box
sx={{
position: "absolute",
display: "flex", // Set display to flex to fix height being calculated wrong
width: "100%",
flexDirection: "column",
}}
>
2020-04-13 00:24:03 +10:00
<Image
className={className}
sx={{
userSelect: "none",
touchAction: "none",
width: "100%",
}}
src={imageSource}
// pass id into the dom element which is then used by the ProxyToken
2020-04-13 00:24:03 +10:00
data-id={token.id}
ref={imageRef}
/>
{token.statuses && <TokenStatus statuses={token.statuses} />}
2020-04-13 00:24:03 +10:00
{token.label && <TokenLabel label={token.label} />}
</Box>
</Box>
</Box>
);
}
export default MapToken;