Skip to content

Commit

Permalink
Merge pull request Expensify#31156 from paultsimura/fix/29469-map-rezoom
Browse files Browse the repository at this point in the history
fix: Distance - reset map boundaries on resize
  • Loading branch information
deetergp authored Nov 16, 2023
2 parents 0b0518d + 68233f4 commit 5e01376
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 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,26 @@ const MapView = forwardRef<MapViewHandle, MapViewProps>(
map.fitBounds([northEast, southWest], {padding: mapPadding});
}, [waypoints, mapRef, mapPadding, directionCoordinates]);

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 5e01376

Please sign in to comment.