Skip to content

Commit

Permalink
Added debounce to map padding calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
davenquinn committed Nov 5, 2024
1 parent e52d86a commit 6acf3a2
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/map-interface/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
useMapDispatch,
useMapStatus,
} from "@macrostrat/mapbox-react";
import { useRef } from "react";
import { useMemo, useRef } from "react";
import { debounce } from "underscore";
import useResizeObserver from "use-resize-observer";

Expand Down Expand Up @@ -34,24 +34,31 @@ interface MapPaddingManagerProps {
containerRef: React.RefObject<HTMLDivElement>;
parentRef: React.RefObject<HTMLDivElement>;
infoMarkerPosition: mapboxgl.LngLatLike;
debounceTime?: number;
}

export function MapPaddingManager({
containerRef,
parentRef,
infoMarkerPosition,
debounceTime = 200,
}: MapPaddingManagerProps) {
const mapRef = useMapRef();

const [padding, setPadding] = useState(
getMapPadding(containerRef, parentRef)
);

const updateMapPadding = useCallback(() => {
const _updateMapPadding = useCallback(() => {
const newPadding = getMapPadding(containerRef, parentRef);
setPadding(newPadding);
}, [containerRef.current, parentRef.current]);

const updateMapPadding = useMemo(
() => debounce(_updateMapPadding, debounceTime),
[_updateMapPadding, debounceTime]
);

useEffect(() => {
const map = mapRef.current;
if (map == null) return;
Expand All @@ -64,6 +71,9 @@ export function MapPaddingManager({
onResize(sz) {
updateMapPadding();
},
round(n) {
return Math.round(n);
},
});

// Ideally, we would not have to do this when we know the infobox is loaded
Expand Down

0 comments on commit 6acf3a2

Please sign in to comment.