Skip to content

Commit

Permalink
front: Add tooltip to map regions.
Browse files Browse the repository at this point in the history
Added Material-UI Tooltip to RegionMap component to display region names when
hovering over the map regions. This enhances user interactivity by providing
immediate context about each region. Adjusted event handlers for mouse
movements to control tooltip visibility and content based on the region under
the cursor.

Issue: #186

Signed-off-by: Nikolay Martyanov <[email protected]>
  • Loading branch information
OhmSpectator committed Jan 1, 2024
1 parent 85d88dd commit 8ae160c
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions frontend/src/components/RegionMap.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useEffect, useRef, useState } from 'react';
import maplibregl from 'maplibre-gl';
import Tooltip from '@mui/material/Tooltip';
import * as turf from '@turf/turf';
import { useNavigation } from './NavigationContext';
import {
Expand All @@ -22,6 +23,8 @@ function MapComponent() {
const [error, setError] = useState(null);
const [renderedFeatures, setRenderedFeatures] = useState([]);
const selectedRegionRef = useRef(selectedRegion);
const [tooltipContent, setTooltipContent] = useState('');
const [tooltipOpen, setTooltipOpen] = useState(false);

// Function to handle region click on the map
const handleRegionClick = (e) => {
Expand Down Expand Up @@ -226,12 +229,24 @@ function MapComponent() {
// Set up click event handler for selecting regions
map.current.on('click', 'polygon', handleRegionClick);

// Optional: Change the cursor to a pointer when over clickable regions
map.current.on('mouseenter', 'polygon', () => {
map.current.getCanvas().style.cursor = 'pointer';
// Show a tooltip with the region name when hovering over regions
map.current.on('mousemove', 'polygon', (e) => {
const featuresUnder = map.current.queryRenderedFeatures(e.point, {
layers: ['polygon'],
});
if (featuresUnder.length > 0) {
const hoveredRegion = featuresUnder[0].properties;
map.current.getCanvas().style.cursor = 'pointer';
setTooltipContent(hoveredRegion.name);
setTooltipOpen(true);
} else {
setTooltipOpen(false);
}
});

map.current.on('mouseleave', 'polygon', () => {
map.current.getCanvas().style.cursor = '';
setTooltipOpen(false);
});
}
};
Expand All @@ -251,7 +266,9 @@ function MapComponent() {
return (
<div>
{error && <div style={{ color: 'red' }}>{error}</div>}
<div ref={mapContainer} style={{ width: '100%', height: '400px' }} />
<Tooltip title={tooltipContent} open={tooltipOpen} placement="right" followCursor>
<div ref={mapContainer} style={{ width: '100%', height: '400px' }} />
</Tooltip>
</div>
);
}
Expand Down

0 comments on commit 8ae160c

Please sign in to comment.