Skip to content

Commit

Permalink
fix tooltip position
Browse files Browse the repository at this point in the history
  • Loading branch information
SimNed committed Jun 4, 2024
1 parent dab6b22 commit 2350807
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
12 changes: 8 additions & 4 deletions src/components/TooltipMouseTracker/TooltipMouseTracker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import React, { useState, useEffect } from 'react';
interface MouseTrackerProps {
children: React.ReactNode;
offset?: { x: number; y: number };
isVisible: boolean;
}

const MouseTracker = ({ children, offset = { x: -750, y: -250 } }: MouseTrackerProps) => {
const MouseTracker = ({ children, offset = { x: 0, y: 0 }, isVisible }: MouseTrackerProps) => {
const [position, setPosition] = useState({ x: 0, y: 0 });

useEffect(() => {
Expand All @@ -24,13 +25,16 @@ const MouseTracker = ({ children, offset = { x: -750, y: -250 } }: MouseTrackerP

return (
<div
className="mouse-tracker"
style={{
position: 'absolute',
padding: '2rem',
top: 0,
left: 0,
padding: '1rem 2rem',
display: isVisible ? 'block' : 'none',
background: 'white',
boxShadow: 'rgba(149, 157, 165, 0.2) 0px 8px 24px',
transform: `translate(${position.x}px, ${position.y}px)`,
pointerEvents: 'none', // This ensures the div does not interfere with mouse events
pointerEvents: 'none',
}}
>
{children}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import styles from './DashboardStatsMap.module.css';
import TooltipMouseTracker from 'src/components/TooltipMouseTracker/TooltipMouseTracker';

const DashboardStatsMap = () => {
const [tooltipPosition, setTooltipPosition] = React.useState({ x: 0, y: 0 });
const [isTooltipVisible, setIsTooltipVisible] = React.useState(false);
const [tooltipData, setTooltipData] = React.useState('');

Expand All @@ -19,10 +18,9 @@ const DashboardStatsMap = () => {
{({ geographies }) =>
geographies.map((geo) => (
<Geography
onMouseEnter={(e) => {
setTooltipPosition({ x: e.clientX, y: e.clientY });
setIsTooltipVisible(true);
onMouseOver={() => {
setTooltipData(geo.rsmKey);
setIsTooltipVisible(true);
}}
onMouseLeave={() => setIsTooltipVisible(false)}
key={geo.rsmKey}
Expand All @@ -38,14 +36,14 @@ const DashboardStatsMap = () => {
hover: {
fill: '#edf2fb',
stroke: '#000',
strokeWidth: '.25',
strokeWidth: '.2',
outline: 'none',
cursor: 'pointer',
},
pressed: {
fill: 'white',
stroke: '#000',
strokeWidth: '.25',
strokeWidth: '.2',
outline: 'none',
},
}}
Expand All @@ -55,7 +53,7 @@ const DashboardStatsMap = () => {
</Geographies>
</ZoomableGroup>
</ComposableMap>
{isTooltipVisible && <TooltipMouseTracker>{tooltipData}</TooltipMouseTracker>}
<TooltipMouseTracker isVisible={isTooltipVisible}>{tooltipData}</TooltipMouseTracker>
<div className={styles.legendContainer}>
<h2>Légende:</h2>
<ul>
Expand Down

0 comments on commit 2350807

Please sign in to comment.