Skip to content

Commit

Permalink
make polylines in the singapore mrt map easier to click
Browse files Browse the repository at this point in the history
Signed-off-by: Vu Van Dung <[email protected]>
  • Loading branch information
joulev committed Jun 8, 2024
1 parent 625ef26 commit 3b15b4a
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions src/app/blogs/walking-on-singapore-mrt-lines/map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function MapPolyline({
sessionIndex: number;
}) {
const polylineRef = useRef<google.maps.Polyline | null>(null);
const clickedPolylineRef = useRef<google.maps.Polyline | null>(null);

const { activeSession, setActiveSession, setPanelIsExpanded } = useActiveSession();

Expand All @@ -79,7 +80,7 @@ function MapPolyline({
}, [setPanelIsExpanded, setActiveSession, lineIndex, sessionIndex]);

const refreshStyling = useCallback(() => {
if (!polylineRef.current) return;
if (!polylineRef.current || !clickedPolylineRef.current) return;
polylineRef.current.setOptions({
strokeColor:
isActive || isHover
Expand All @@ -89,28 +90,47 @@ function MapPolyline({
: "#555555",
zIndex:
isActive || isHover
? 9999
? 8999
: lineIsActive
? 9997
? 8997
: lineZIndex[data[lineIndex].lineCode as keyof typeof lineZIndex],
strokeOpacity: 1,
strokeWeight: 2,
});
clickedPolylineRef.current.setOptions({
strokeOpacity: 0,
strokeWeight: 10,
zIndex:
isActive || isHover
? 9999
: lineIsActive
? 9997
: lineZIndex[data[lineIndex].lineCode as keyof typeof lineZIndex],
});
}, [isActive, isHover, lineIsActive, activeSession.lineIndex, lineIndex]);

useEffect(() => refreshStyling(), [refreshStyling]);

return (
<Polyline
onLoad={polyline => {
polylineRef.current = polyline;
refreshStyling();
}}
path={coordinates}
onMouseOver={onHoverEnter}
onMouseOut={onHoverLeave}
onClick={onClick}
/>
<>
<Polyline
onLoad={polyline => {
polylineRef.current = polyline;
refreshStyling();
}}
path={coordinates}
/>
<Polyline
onLoad={polyLine => {
clickedPolylineRef.current = polyLine;
refreshStyling();
}}
path={coordinates}
onMouseOver={onHoverEnter}
onMouseOut={onHoverLeave}
onClick={onClick}
/>
</>
);
}

Expand Down

0 comments on commit 3b15b4a

Please sign in to comment.