2020-05-21 20:57:52 +10:00
|
|
|
import React, { useRef, useEffect, useState } from "react";
|
|
|
|
|
import { Rect, Text, Group } from "react-konva";
|
|
|
|
|
|
2020-07-20 19:30:09 +10:00
|
|
|
const maxTokenSize = 3;
|
|
|
|
|
|
2020-05-21 20:57:52 +10:00
|
|
|
function TokenLabel({ tokenState, width, height }) {
|
2020-07-20 19:30:09 +10:00
|
|
|
const fontSize =
|
|
|
|
|
(height / 6 / tokenState.size) * Math.min(tokenState.size, maxTokenSize);
|
|
|
|
|
const paddingY =
|
|
|
|
|
(height / 16 / tokenState.size) * Math.min(tokenState.size, maxTokenSize);
|
|
|
|
|
const paddingX =
|
|
|
|
|
(height / 8 / tokenState.size) * Math.min(tokenState.size, maxTokenSize);
|
2020-05-21 20:57:52 +10:00
|
|
|
|
|
|
|
|
const [rectWidth, setRectWidth] = useState(0);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const text = textRef.current;
|
|
|
|
|
if (text && tokenState.label) {
|
|
|
|
|
setRectWidth(text.getTextWidth() + paddingX);
|
|
|
|
|
} else {
|
|
|
|
|
setRectWidth(0);
|
|
|
|
|
}
|
2020-09-08 14:31:44 +10:00
|
|
|
}, [tokenState.label, paddingX, width]);
|
2020-05-21 20:57:52 +10:00
|
|
|
|
|
|
|
|
const textRef = useRef();
|
2020-04-13 00:24:03 +10:00
|
|
|
|
|
|
|
|
return (
|
2020-05-21 20:57:52 +10:00
|
|
|
<Group y={height - (fontSize + paddingY) / 2}>
|
|
|
|
|
<Rect
|
|
|
|
|
y={-paddingY / 2}
|
|
|
|
|
width={rectWidth}
|
|
|
|
|
offsetX={width / 2}
|
|
|
|
|
x={width - rectWidth / 2}
|
|
|
|
|
height={fontSize + paddingY}
|
|
|
|
|
fill="hsla(230, 25%, 18%, 0.8)"
|
|
|
|
|
cornerRadius={(fontSize + paddingY) / 2}
|
|
|
|
|
/>
|
|
|
|
|
<Text
|
|
|
|
|
ref={textRef}
|
|
|
|
|
width={width}
|
|
|
|
|
text={tokenState.label}
|
|
|
|
|
fontSize={fontSize}
|
|
|
|
|
lineHeight={1}
|
|
|
|
|
align="center"
|
|
|
|
|
verticalAlign="bottom"
|
|
|
|
|
fill="white"
|
|
|
|
|
paddingX={paddingX}
|
|
|
|
|
paddingY={paddingY}
|
|
|
|
|
wrap="none"
|
|
|
|
|
ellipsis={true}
|
2020-05-29 07:50:03 +10:00
|
|
|
hitFunc={() => {}}
|
2020-05-21 20:57:52 +10:00
|
|
|
/>
|
|
|
|
|
</Group>
|
2020-04-13 00:24:03 +10:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default TokenLabel;
|