( ref: RefObject<HTMLElement>, enabled = true, )
| 10 | // block solid over its cover until a scroll; so the element is also |
| 11 | // measured once whenever the page changes size, never on scroll. |
| 12 | export const usePassedBlock = ( |
| 13 | ref: RefObject<HTMLElement>, |
| 14 | enabled = true, |
| 15 | ): boolean => { |
| 16 | const [passed, setPassed] = useState(false); |
| 17 | const blockBottom = useShellEdge(); |
| 18 | |
| 19 | useEffect(() => { |
| 20 | const element = ref.current; |
| 21 | if (!enabled || !element || typeof IntersectionObserver === 'undefined') { |
| 22 | setPassed(false); |
| 23 | return undefined; |
| 24 | } |
| 25 | |
| 26 | const observer = new IntersectionObserver( |
| 27 | ([entry]) => |
| 28 | setPassed( |
| 29 | !entry.isIntersecting && entry.boundingClientRect.top < blockBottom, |
| 30 | ), |
| 31 | { rootMargin: `-${blockBottom}px 0px 0px 0px` }, |
| 32 | ); |
| 33 | observer.observe(element); |
| 34 | const settle = |
| 35 | typeof ResizeObserver === 'undefined' |
| 36 | ? undefined |
| 37 | : new ResizeObserver(() => |
| 38 | setPassed(element.getBoundingClientRect().bottom <= blockBottom), |
| 39 | ); |
| 40 | settle?.observe(document.body); |
| 41 | |
| 42 | return () => { |
| 43 | observer.disconnect(); |
| 44 | settle?.disconnect(); |
| 45 | }; |
| 46 | }, [ref, enabled, blockBottom]); |
| 47 | |
| 48 | return passed; |
| 49 | }; |
| 50 | |
| 51 | // On a thing's page the block stays through the hero and a little past it, |
| 52 | // so the name, Follow and the segments are seen arriving in it on the way |
no test coverage detected