pages/ValuePartnership/useNavHeight.jsjavascript
import { useEffect, useState } from "react";
// every mounted bottom bar and its current height, in mount order
const bars = new Map();
const publish = () => {
const root = document.documentElement;
const heights = [...bars.values()];
if (heights.length === 0) root.style.removeProperty("--vp-nav-height");
else root.style.setProperty("--vp-nav-height", `${heights[heights.length - 1]}px`);
};
/**
* Publishes a bottom bar's real height as --vp-nav-height, so the side drawers
* (CategoryMenu, ValueCheckboxPanel) end flush on top of it. The height is
* content-driven and differs per device, so a fixed em offset in the drawers
* kept leaving gaps or overlaps.
*
* Two bars take turns in the same slot: ValuePartnershipNav and, while a globe
* marker is selected, GlobeBottomNav. Their mount and unmount land in
* different commits (the parent hides ValuePartnershipNav in an effect after
* GlobeBottomNav has mounted), so a bar unmounting must not just clear the
* variable: the most recently mounted bar that is still there wins.
*
* offsetHeight = layout px, unaffected by the Stage's transform scale.
*
* @returns {function(?HTMLElement): void} Callback ref for the bar - works
* for bars that render `null` at first, too.
*/
export const useNavHeight = () => {
const [element, setElement] = useState(null);
useEffect(() => {
if (!element) return;
const key = {};
const measure = () => {
bars.set(key, element.offsetHeight);
publish();
};
measure();
const observer = new ResizeObserver(measure);
observer.observe(element);
return () => {
observer.disconnect();
bars.delete(key);
publish();
};
}, [element]);
return setElement;
};