const { useState, useEffect } = React; const useOnScreen = (ref) => { const [isIntersecting, setIntersecting] = useState(false); useEffect(() => { const observer = new IntersectionObserver(([entry]) => setIntersecting(entry.isIntersecting)); if (ref.current) observer.observe(ref.current); return () => { if (ref.current) observer.unobserve(ref.current); }; }, []); return isIntersecting; }; window.useOnScreen = useOnScreen;