UNPKG

@josmangarsal/pragmatic-scheduler

Version:
43 lines (42 loc) 1.55 kB
"use strict"; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useVisibilityObserver = void 0; var react_1 = require("react"); function useVisibilityObserver(_a) { var root = _a.root, targetRef = _a.targetRef, _b = _a.threshold, threshold = _b === void 0 ? 1.0 : _b; var _c = __read((0, react_1.useState)(false), 2), isVisible = _c[0], setIsVisible = _c[1]; (0, react_1.useEffect)(function () { var target = targetRef.current; if (!target || !root) return; var observer = new IntersectionObserver(function (_a) { var _b = __read(_a, 1), entry = _b[0]; setIsVisible(entry.intersectionRatio >= threshold); }, { root: root, threshold: threshold, }); observer.observe(target); return function () { observer.disconnect(); }; }, [root, targetRef, threshold]); return { isVisible: isVisible }; } exports.useVisibilityObserver = useVisibilityObserver;