Auto-pan map to keep popup fully visible in viewport
When a property popup opens near the edge of the map, the map now automatically pans so the entire popup is visible with 20px padding.
This commit is contained in:
parent
f6f0aa8d14
commit
6489012e8f
1 changed files with 20 additions and 0 deletions
|
|
@ -381,6 +381,26 @@ export function Map(props: MapProps) {
|
||||||
.setMaxWidth("450px")
|
.setMaxWidth("450px")
|
||||||
.addTo(mapRef.current);
|
.addTo(mapRef.current);
|
||||||
popup.on('close', () => root.unmount());
|
popup.on('close', () => root.unmount());
|
||||||
|
|
||||||
|
// Pan map to ensure popup is fully visible
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
const popupEl = popup.getElement();
|
||||||
|
if (!popupEl || !mapRef.current) return;
|
||||||
|
const popupRect = popupEl.getBoundingClientRect();
|
||||||
|
const mapRect = mapRef.current.getContainer().getBoundingClientRect();
|
||||||
|
const padding = 20;
|
||||||
|
let dx = 0, dy = 0;
|
||||||
|
if (popupRect.left < mapRect.left + padding)
|
||||||
|
dx = popupRect.left - mapRect.left - padding;
|
||||||
|
if (popupRect.right > mapRect.right - padding)
|
||||||
|
dx = popupRect.right - mapRect.right + padding;
|
||||||
|
if (popupRect.top < mapRect.top + padding)
|
||||||
|
dy = popupRect.top - mapRect.top - padding;
|
||||||
|
if (popupRect.bottom > mapRect.bottom - padding)
|
||||||
|
dy = popupRect.bottom - mapRect.bottom + padding;
|
||||||
|
if (dx !== 0 || dy !== 0)
|
||||||
|
mapRef.current.panBy([dx, dy], { duration: 300 });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue