Skip to content

Commit 2f87f3d

Browse files
committed
tooltip test data displayed
1 parent 8b5fbee commit 2f87f3d

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

src/app/components/ComponentMap.tsx

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import useForceUpdate from './useForceUpdate';
88
import LinkControls from './LinkControls';
99
import getLinkComponent from './getLinkComponent';
1010
import { localPoint } from '@visx/event';
11-
import { onHover, onHoverExit } from '../actions/actions';
11+
import { useTooltip, useTooltipInPortal, TooltipWithBounds } from '@visx/tooltip';
12+
import { onHover, onHoverExit } from '../actions/actions';
1213
import { useStoreContext } from '../store';
1314

1415
const root = hierarchy({
@@ -31,6 +32,7 @@ interface TreeNode {
3132
children?: TreeNode[];
3233
}
3334

35+
3436
type HierarchyNode = HierarchyPointNode<TreeNode>;
3537

3638
const defaultMargin = { top: 30, left: 30, right: 55, bottom: 70 };
@@ -91,8 +93,31 @@ export default function ComponentMap({
9193
}
9294
}
9395

96+
97+
//Tooltip stuff:
98+
const {
99+
tooltipData,
100+
tooltipLeft,
101+
tooltipTop,
102+
tooltipOpen,
103+
showTooltip,
104+
hideTooltip,
105+
} = useTooltip();
106+
107+
const { containerRef, TooltipInPortal } = useTooltipInPortal();
108+
94109
//mousing controls
95-
const handleMouseOver = () => console.log("mouse entered");
110+
const handleMouseOver = (event) => {
111+
console.log("mouse entered");
112+
const coords = localPoint(event.target.ownerSVGElement, event);
113+
console.log("I'm coords", coords);
114+
showTooltip({
115+
tooltipLeft: coords.x,
116+
tooltipTop: coords.y,
117+
tooltipData: "test"
118+
});
119+
}
120+
96121

97122
// controls for the map
98123
const LinkComponent = getLinkComponent({ layout, linkType, orientation });
@@ -109,7 +134,7 @@ export default function ComponentMap({
109134
setStepPercent={setStepPercent}
110135
/>
111136

112-
<svg width={totalWidth} height={totalHeight}>
137+
<svg ref={containerRef} width={totalWidth} height={totalHeight}>
113138
<LinearGradient id="links-gradient" from="#fd9b93" to="#fe6e9e" />
114139
<rect width={totalWidth} height={totalHeight} rx={14} fill="#242529" />
115140
<Group top={margin.top} left={margin.left}>
@@ -185,7 +210,7 @@ export default function ComponentMap({
185210
node.data.isExpanded = !node.data.isExpanded;
186211
forceUpdate();
187212
}}
188-
//check with recoil
213+
//Tooltip event handlers
189214
onMouseOver={handleMouseOver}
190215
onMouseOut={hideTooltip}
191216
/>
@@ -215,6 +240,16 @@ export default function ComponentMap({
215240
</Tree>
216241
</Group>
217242
</svg>
243+
tooltipOpen && tooltipData && (
244+
<TooltipInPortal
245+
// set this to random so it correctly updates with parent bounds
246+
key={Math.random()}
247+
top={tooltipTop}
248+
left={tooltipLeft}
249+
>
250+
Tooltip Data: <strong>{tooltipData}</strong>
251+
</TooltipInPortal>
252+
)
218253
</div>
219254
);
220255
}

0 commit comments

Comments
 (0)