UNPKG

organism-react-scroll-animate

Version:
34 lines (32 loc) 852 B
//@ts-check import { useRef, useEffect } from "react"; import build from "reshow-build"; import { win } from "win-doc"; /** * @param {React.ReactElement|string} component * @param {IntersectionObserverCallback} onIntersect * @param {object} options * @returns {React.ReactElement} */ var useIntersectionObserver = function useIntersectionObserver(component, onIntersect, options) { if (options === void 0) { options = {}; } var lastEl = useRef(); var el = build(component)({ ref: lastEl }); useEffect(function () { var dom = lastEl.current; if (!dom) { return; } var observer = new (win().IntersectionObserver)(onIntersect, options); observer.observe(dom); return function () { observer.unobserve(dom); }; }, [lastEl.current]); return el; }; export default useIntersectionObserver;