import { useLayoutEffect, useRef, type RefObject } from "react"; const BOTTOM_GAP = 12; const MIN_HEIGHT = 448; /** Sizes an element to reach the bottom of the window from wherever it starts, so the map uses the whole page. */ export function useFillHeight(): RefObject { const ref = useRef(null); useLayoutEffect(() => { const element = ref.current; if (!element) return; let frame = 0; const fit = () => { cancelAnimationFrame(frame); frame = requestAnimationFrame(() => { const top = element.getBoundingClientRect().top + window.scrollY; const height = `${Math.max(MIN_HEIGHT, window.innerHeight - top - BOTTOM_GAP)}px`; if (element.style.height !== height) element.style.height = height; }); }; const first = element.getBoundingClientRect().top + window.scrollY; element.style.height = `${Math.max(MIN_HEIGHT, window.innerHeight - first - BOTTOM_GAP)}px`; window.addEventListener("resize", fit); const above = new ResizeObserver(fit); if (element.parentElement) above.observe(element.parentElement); return () => { cancelAnimationFrame(frame); window.removeEventListener("resize", fit); above.disconnect(); }; }, []); return ref; }