Skip to content

Commit 5e01376

Browse files
authored
Merge pull request #31156 from paultsimura/fix/29469-map-rezoom
fix: Distance - reset map boundaries on resize
2 parents 0b0518d + 68233f4 commit 5e01376

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/components/MapView/MapView.web.tsx

+15-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ import utils from './utils';
1919
const MapView = forwardRef<MapViewHandle, MapViewProps>(
2020
({style, styleURL, waypoints, mapPadding, accessToken, directionCoordinates, initialState = {location: CONST.MAPBOX.DEFAULT_COORDINATE, zoom: CONST.MAPBOX.DEFAULT_ZOOM}}, ref) => {
2121
const [mapRef, setMapRef] = useState<MapRef | null>(null);
22+
const [shouldResetBoundaries, setShouldResetBoundaries] = useState<boolean>(false);
2223
const setRef = useCallback((newRef: MapRef | null) => setMapRef(newRef), []);
2324

24-
useEffect(() => {
25+
const resetBoundaries = useCallback(() => {
2526
if (!waypoints || waypoints.length === 0) {
2627
return;
2728
}
@@ -47,13 +48,26 @@ const MapView = forwardRef<MapViewHandle, MapViewProps>(
4748
map.fitBounds([northEast, southWest], {padding: mapPadding});
4849
}, [waypoints, mapRef, mapPadding, directionCoordinates]);
4950

51+
useEffect(resetBoundaries, [resetBoundaries]);
52+
53+
useEffect(() => {
54+
if (!shouldResetBoundaries) {
55+
return;
56+
}
57+
58+
resetBoundaries();
59+
setShouldResetBoundaries(false);
60+
// eslint-disable-next-line react-hooks/exhaustive-deps -- this effect only needs to run when the boundaries reset is forced
61+
}, [shouldResetBoundaries]);
62+
5063
useEffect(() => {
5164
if (!mapRef) {
5265
return;
5366
}
5467

5568
const resizeObserver = new ResizeObserver(() => {
5669
mapRef.resize();
70+
setShouldResetBoundaries(true);
5771
});
5872
resizeObserver.observe(mapRef.getContainer());
5973

0 commit comments

Comments
 (0)