UNPKG

@open-condo/miniapp-utils

Version:

A set of helper functions / components / hooks used to build new condo apps fast

76 lines (75 loc) 2.77 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/hooks/useIntersectionObserver.ts var useIntersectionObserver_exports = {}; __export(useIntersectionObserver_exports, { useIntersectionObserver: () => useIntersectionObserver }); module.exports = __toCommonJS(useIntersectionObserver_exports); var import_react = require("react"); function useIntersectionObserver({ threshold = 0, root = null, rootMargin = "0%", onChange } = {}) { const [ref, setRef] = (0, import_react.useState)(null); const [state, setState] = (0, import_react.useState)(() => ({ isIntersecting: false, entry: void 0 })); const callbackRef = (0, import_react.useRef)(); callbackRef.current = onChange; (0, import_react.useEffect)(() => { if (!ref) return; if (!("IntersectionObserver" in window)) return; const observer = new IntersectionObserver((entries) => { const thresholds = observer.thresholds; entries.forEach((entry) => { const isIntersecting = entry.isIntersecting && thresholds.some((threshold2) => entry.intersectionRatio >= threshold2); setState({ isIntersecting, entry }); if (callbackRef.current) { callbackRef.current(isIntersecting, entry); } }); }, { threshold, root, rootMargin }); observer.observe(ref); return () => { observer.disconnect(); }; }, [ref, root, rootMargin, threshold]); const prevRef = (0, import_react.useRef)(null); (0, import_react.useEffect)(() => { var _a; if (!ref && ((_a = state.entry) == null ? void 0 : _a.target) && prevRef.current !== state.entry.target) { prevRef.current = state.entry.target; setState({ isIntersecting: false, entry: void 0 }); } }, [ref, state.entry]); return { entry: state.entry, ref: setRef, isIntersecting: state.isIntersecting }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useIntersectionObserver }); //# sourceMappingURL=useIntersectionObserver.js.map