UNPKG

@kiwicom/orbit-components

Version:

Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.

36 lines (33 loc) 936 B
import { useState, useEffect, useRef, useCallback } from "react"; const useIntersect = ({ root = null, rootMargin, threshold = 0 } = {}) => { const [entry, updateEntry] = useState(null); const [node, setNode] = useState(null); const ref = useCallback(el => { setNode(el); }, []); const observer = useRef(null); useEffect(() => { if ("IntersectionObserver" in window === false) { return () => {}; } observer.current = new window.IntersectionObserver(([ent]) => updateEntry(ent), { root, rootMargin, threshold }); const { current: currentObs } = observer; if (node) currentObs === null || currentObs === void 0 ? void 0 : currentObs.observe(node); return () => currentObs === null || currentObs === void 0 ? void 0 : currentObs.disconnect(); }, [node, root, rootMargin, threshold]); return { ref, entry }; }; export default useIntersect;