Skip to content

Commit

Permalink
Reset map boundaries on resize
Browse files Browse the repository at this point in the history
  • Loading branch information
paultsimura committed Nov 9, 2023
1 parent 12966ae commit 95c9b7e
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/components/MapView/MapView.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import utils from './utils';
const MapView = forwardRef<MapViewHandle, MapViewProps>(
({style, styleURL, waypoints, mapPadding, accessToken, directionCoordinates, initialState = {location: CONST.MAPBOX.DEFAULT_COORDINATE, zoom: CONST.MAPBOX.DEFAULT_ZOOM}}, ref) => {
const [mapRef, setMapRef] = useState<MapRef | null>(null);
const [shouldResetBoundaries, setShouldResetBoundaries] = useState<boolean>(false);
const setRef = useCallback((newRef: MapRef | null) => setMapRef(newRef), []);

useEffect(() => {
const resetBoundaries = useCallback(() => {
if (!waypoints || waypoints.length === 0) {
return;
}
Expand All @@ -47,13 +48,27 @@ const MapView = forwardRef<MapViewHandle, MapViewProps>(
map.fitBounds([northEast, southWest], {padding: mapPadding});
}, [waypoints, mapRef, mapPadding, directionCoordinates]);

// Reset boundaries when waypoints change
useEffect(resetBoundaries, [resetBoundaries]);

useEffect(() => {
if (!shouldResetBoundaries) {
return;
}

resetBoundaries();
setShouldResetBoundaries(false);
// eslint-disable-next-line react-hooks/exhaustive-deps -- this effect only needs to run when the boundaries reset is forced
}, [shouldResetBoundaries]);

useEffect(() => {
if (!mapRef) {
return;
}

const resizeObserver = new ResizeObserver(() => {
mapRef.resize();
setShouldResetBoundaries(true);
});
resizeObserver.observe(mapRef.getContainer());

Expand Down

0 comments on commit 95c9b7e

Please sign in to comment.