Skip to content

Commit

Permalink
perf: listen to fullscreenchange instead of onRender resize (#729)
Browse files Browse the repository at this point in the history
  • Loading branch information
orionna319 authored Oct 23, 2024
1 parent fac33dd commit b8e8fe1
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/components/RunMap/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MapboxLanguage from '@mapbox/mapbox-gl-language';
import React, {useRef, useCallback, useState} from 'react';
import React, {useRef, useCallback, useState, useEffect} from 'react';
import Map, {Layer, Source, FullscreenControl, NavigationControl, MapRef} from 'react-map-gl';
import {MapInstance} from "react-map-gl/src/types/lib";
import useActivities from '@/hooks/useActivities';
Expand Down Expand Up @@ -128,6 +128,18 @@ const RunMap = ({
opacity: 0.3,
};

useEffect(() => {
const handleFullscreenChange = () => {
if (mapRef.current) {
mapRef.current.getMap().resize();
}
};
document.addEventListener('fullscreenchange', handleFullscreenChange);
return () => {
document.removeEventListener('fullscreenchange', handleFullscreenChange);
};
}, []);

return (
<Map
{...viewState}
Expand All @@ -136,7 +148,6 @@ const RunMap = ({
mapStyle="mapbox://styles/mapbox/dark-v10"
ref={mapRefCallback}
mapboxAccessToken={MAPBOX_TOKEN}
onRender={(event) => event.target.resize()}
>
<RunMapButtons changeYear={changeYear} thisYear={thisYear} />
<Source id="data" type="geojson" data={geoData}>
Expand Down

0 comments on commit b8e8fe1

Please sign in to comment.