1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.default = void 0;
|
5 |
|
6 | var _react = require("react");
|
7 |
|
8 | var _useStableMemo = _interopRequireDefault(require("./useStableMemo"));
|
9 |
|
10 | var _useIsomorphicEffect = _interopRequireDefault(require("./useIsomorphicEffect"));
|
11 |
|
12 | var _useEventCallback = _interopRequireDefault(require("./useEventCallback"));
|
13 |
|
14 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
15 |
|
16 | function useIntersectionObserver(element, callbackOrOptions, maybeOptions) {
|
17 | var callback;
|
18 | var options;
|
19 |
|
20 | if (typeof callbackOrOptions === 'function') {
|
21 | callback = callbackOrOptions;
|
22 | options = maybeOptions || {};
|
23 | } else {
|
24 | options = callbackOrOptions || {};
|
25 | }
|
26 |
|
27 | var _options = options,
|
28 | threshold = _options.threshold,
|
29 | root = _options.root,
|
30 | rootMargin = _options.rootMargin;
|
31 |
|
32 | var _useState = (0, _react.useState)(null),
|
33 | entries = _useState[0],
|
34 | setEntry = _useState[1];
|
35 |
|
36 | var handler = (0, _useEventCallback.default)(callback || setEntry);
|
37 |
|
38 | var observer = (0, _useStableMemo.default)(function () {
|
39 | return root !== null && typeof IntersectionObserver !== 'undefined' && new IntersectionObserver(handler, {
|
40 | threshold: threshold,
|
41 | root: root,
|
42 | rootMargin: rootMargin
|
43 | });
|
44 | }, [handler, root, rootMargin, threshold && JSON.stringify(threshold)]);
|
45 | (0, _useIsomorphicEffect.default)(function () {
|
46 | if (!element || !observer) return;
|
47 | observer.observe(element);
|
48 | return function () {
|
49 | observer.unobserve(element);
|
50 | };
|
51 | }, [observer, element]);
|
52 | return callback ? undefined : entries || [];
|
53 | }
|
54 |
|
55 | var _default = useIntersectionObserver;
|
56 | exports.default = _default; |
\ | No newline at end of file |