import { useCallback , useEffect , useRef } from "react" ; import useResizeObserver from "use-resize-observer/polyfilled" ; import { useCoralContext } from "coral-framework/lib/bootstrap" ; import resizePopup from "../dom/resizePopup" ; export default function useResizePopup ( ) { const { window } = useCoralContext ( ) ; const polling = useRef ( true ) ; const pollTimeout = useRef < number | null > ( null ) ; const pollPopupHeight = useCallback ( ( interval = 200 ) => { if ( ! polling . current ) { return ; } pollTimeout . current = window . setTimeout ( ( ) => { window . requestAnimationFrame ( ( ) => { resizePopup ( window ) ; pollPopupHeight ( interval ) ; } ) ; } , interval ) ; } , [ window ] ) ; useEffect ( ( ) => { pollPopupHeight ( ) ; return ( ) => { if ( pollTimeout . current ) { window . clearTimeout ( pollTimeout . current ) ; pollTimeout . current = null ; polling . current = false ; } } ; } , [ pollPopupHeight , window ] ) ; const { ref } = useResizeObserver < HTMLDivElement > ( { onResize : ( ) => resizePopup ( window ) , } ) ; return ref ; }