UNPKG

@ariakit/react-core

Version:

Ariakit React core

128 lines (113 loc) 4.12 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); var _DMTXWFDQcjs = require('./DMTXWFDQ.cjs'); var _XFOFWDPYcjs = require('./XFOFWDPY.cjs'); var _WBFXWJUHcjs = require('./WBFXWJUH.cjs'); var _MZ2HG624cjs = require('./MZ2HG624.cjs'); // src/collection/collection-item-offscreen.tsx var _misc = require('@ariakit/core/utils/misc'); var _react = require('react'); var _jsxruntime = require('react/jsx-runtime'); var TagName = "div"; function requestIdleCallback(callback) { if (typeof window.requestIdleCallback === "function") { return window.requestIdleCallback(callback); } callback(); return 0; } function cancelIdleCallback(id) { if (typeof window.cancelIdleCallback === "function") { window.cancelIdleCallback(id); } } function useCollectionItemOffscreen({ offscreenBehavior = "active", offscreenRoot, ...props }) { const id = _MZ2HG624cjs.useId.call(void 0, props.id); const [updated, forceUpdate] = _MZ2HG624cjs.useForceUpdate.call(void 0, ); const forcedUpdatesCountRef = _react.useRef.call(void 0, 0); const [_active, setActive] = _react.useState.call(void 0, offscreenBehavior === "active"); const active = _active || offscreenBehavior === "active"; const observerRef = _react.useRef.call(void 0, null); const idleCallbackIdRef = _react.useRef.call(void 0, 0); const ref = _react.useCallback.call(void 0, (element) => { var _a; if (!element || offscreenBehavior === "active") { cancelIdleCallback(idleCallbackIdRef.current); (_a = observerRef.current) == null ? void 0 : _a.disconnect(); return; } _misc.invariant.call(void 0, offscreenRoot !== void 0, process.env.NODE_ENV !== "production" && "The offscreenRoot prop must provided." ); const getOffscreenRoot = () => { if (!offscreenRoot) return null; if (typeof offscreenRoot === "function") { return offscreenRoot(element); } if ("current" in offscreenRoot) { return offscreenRoot.current; } return offscreenRoot; }; const root = getOffscreenRoot(); if (!root) { forcedUpdatesCountRef.current++; if (forcedUpdatesCountRef.current > 3) { throw new Error( "The offscreenRoot is not available. Please make sure the root element is mounted." ); } forceUpdate(); return; } if (!observerRef.current || observerRef.current.root !== root) { observerRef.current = new IntersectionObserver( ([entry]) => { cancelIdleCallback(idleCallbackIdRef.current); const isIntersecting = !!(entry == null ? void 0 : entry.isIntersecting); idleCallbackIdRef.current = requestIdleCallback(() => { if (!isIntersecting && offscreenBehavior === "lazy") return; setActive(isIntersecting); }); }, { root, rootMargin: "40%" } ); } observerRef.current.observe(element); }, [updated, offscreenBehavior, offscreenRoot] ); return { id, active, ref, "data-offscreen": !active || void 0 }; } var CollectionItem2 = _WBFXWJUHcjs.forwardRef.call(void 0, function CollectionItem3({ offscreenBehavior, offscreenRoot, ...props }) { const { active, ref, ...rest } = useCollectionItemOffscreen({ offscreenBehavior, offscreenRoot, ...props }); const allProps = { ...rest, ...props, ref: _MZ2HG624cjs.useMergeRefs.call(void 0, ref, props.ref) }; if (active) { return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _XFOFWDPYcjs.CollectionItem, { ...allProps }); } const { store, shouldRegisterItem, getItem, // @ts-expect-error This prop may come from a collection renderer. element, ...htmlProps } = allProps; const Component = _DMTXWFDQcjs.Role[TagName]; return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Component, { ...htmlProps }); }); exports.useCollectionItemOffscreen = useCollectionItemOffscreen; exports.CollectionItem = CollectionItem2;