import { RefObject, useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react'; type Point = { left: number; top: number }; type Snapshot = { trigger: Point; overlay: Point }; const toPoint = ({ left, top }: DOMRect): Point => ({ left, top }); const samePoint = (a: Point, b: Point) => a.left === b.left && a.top === b.top; const sameRect = (a: DOMRect, b: DOMRect) => a.left === b.left && a.top === b.top && a.width === b.width && a.height === b.height; const offset = ({ trigger, overlay }: Snapshot): Point => ({ left: trigger.left - overlay.left, top: trigger.top - overlay.top, }); /** * Calls `onMove` whenever `element` changes position in the viewport, whatever caused it (a layout shift anywhere * on the page, a scroll, a transform). Ported from floating-ui's `autoUpdate` (`layoutShift` option). * * An `IntersectionObserver` is created with a root margin that shrinks the viewport down to exactly the element's * current box, so the element fully intersects it (ratio 1). Any movement changes the ratio, which fires the * callback; the observer is then re-created around the new position. Nothing runs while the element is still. * * Returns a function that stops observing. */ function observeMove(element: Element, onMove: () => void): () => void { const root = element.ownerDocument.documentElement; let observer: IntersectionObserver | null = null; let timeoutId: ReturnType | undefined; function cleanup() { clearTimeout(timeoutId); observer?.disconnect(); observer = null; } function refresh(skip = false, threshold = 1) { cleanup(); const rectAtCreation = element.getBoundingClientRect(); const { left, top, width, height } = rectAtCreation; if (!skip) onMove(); if (!width || !height) return; const insetTop = Math.floor(top); const insetRight = Math.floor(root.clientWidth - (left + width)); const insetBottom = Math.floor(root.clientHeight - (top + height)); const insetLeft = Math.floor(left); const options: IntersectionObserverInit = { rootMargin: `${-insetTop}px ${-insetRight}px ${-insetBottom}px ${-insetLeft}px`, threshold: Math.max(0, Math.min(1, threshold)) || 1, }; let isFirstUpdate = true; function handleObserve(entries: IntersectionObserverEntry[]) { const ratio = entries[0].intersectionRatio; if (ratio !== threshold) { if (!isFirstUpdate) return refresh(); if (!ratio) { // The element is clipped (e.g. scrolled out of a container), so the ratio is 0. Throttle the refresh // to avoid an endless loop of updates while it stays clipped. timeoutId = setTimeout(() => refresh(false, 1e-7), 1000); } else { // Partially off-screen: watch for any change from the current ratio instead of from 1. refresh(false, ratio); } } // Under load the browser can report a ratio of 1 after the element has already moved out of the observed // box; comparing against the rect the observer was created around catches that. if (ratio === 1 && !sameRect(rectAtCreation, element.getBoundingClientRect())) refresh(); isFirstUpdate = false; } try { // Using the document as root handles the element being inside an