@peculiar/react-components 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/hooks/index.js +3 -1
- package/dist/cjs/hooks/index.js.map +1 -1
- package/dist/cjs/hooks/use_intersection_observer.js +52 -0
- package/dist/cjs/hooks/use_intersection_observer.js.map +1 -0
- package/dist/cjs/utils/intersection_observer.js +54 -0
- package/dist/cjs/utils/intersection_observer.js.map +1 -0
- package/dist/esm/hooks/index.js +1 -0
- package/dist/esm/hooks/index.js.map +1 -1
- package/dist/esm/hooks/use_intersection_observer.js +45 -0
- package/dist/esm/hooks/use_intersection_observer.js.map +1 -0
- package/dist/esm/utils/intersection_observer.js +51 -0
- package/dist/esm/utils/intersection_observer.js.map +1 -0
- package/dist/esnext/hooks/index.js +1 -0
- package/dist/esnext/hooks/index.js.map +1 -1
- package/dist/esnext/hooks/use_intersection_observer.js +45 -0
- package/dist/esnext/hooks/use_intersection_observer.js.map +1 -0
- package/dist/esnext/utils/intersection_observer.js +50 -0
- package/dist/esnext/utils/intersection_observer.js.map +1 -0
- package/dist/types/hooks/index.d.ts +1 -0
- package/dist/types/hooks/use_intersection_observer.d.ts +34 -0
- package/dist/types/utils/intersection_observer.d.ts +12 -0
- package/package.json +2 -2
package/dist/cjs/hooks/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useEventCallback = exports.useAutocomplete = exports.useWindowEventListener = exports.useImage = exports.useId = exports.useControllableState = exports.useClipboard = exports.useMergedRef = exports.useMediaQuery = void 0;
|
|
3
|
+
exports.useEventCallback = exports.useAutocomplete = exports.useIntersectionObserver = exports.useWindowEventListener = exports.useImage = exports.useId = exports.useControllableState = exports.useClipboard = exports.useMergedRef = exports.useMediaQuery = void 0;
|
|
4
4
|
var use_media_query_1 = require("./use_media_query");
|
|
5
5
|
Object.defineProperty(exports, "useMediaQuery", { enumerable: true, get: function () { return use_media_query_1.useMediaQuery; } });
|
|
6
6
|
var use_merged_ref_1 = require("./use_merged_ref");
|
|
@@ -15,6 +15,8 @@ var use_image_1 = require("./use_image");
|
|
|
15
15
|
Object.defineProperty(exports, "useImage", { enumerable: true, get: function () { return use_image_1.useImage; } });
|
|
16
16
|
var use_window_event_listener_1 = require("./use_window_event_listener");
|
|
17
17
|
Object.defineProperty(exports, "useWindowEventListener", { enumerable: true, get: function () { return use_window_event_listener_1.useWindowEventListener; } });
|
|
18
|
+
var use_intersection_observer_1 = require("./use_intersection_observer");
|
|
19
|
+
Object.defineProperty(exports, "useIntersectionObserver", { enumerable: true, get: function () { return use_intersection_observer_1.useIntersectionObserver; } });
|
|
18
20
|
var use_autocomplete_1 = require("./use_autocomplete");
|
|
19
21
|
Object.defineProperty(exports, "useAutocomplete", { enumerable: true, get: function () { return use_autocomplete_1.useAutocomplete; } });
|
|
20
22
|
var use_event_callback_1 = require("./use_event_callback");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/hooks/index.ts"],"names":[],"mappings":";;;AAAA,qDAAkD;AAAzC,gHAAA,aAAa,OAAA;AACtB,mDAAgD;AAAvC,8GAAA,YAAY,OAAA;AACrB,iDAAoE;AAA3D,6GAAA,YAAY,OAAA;AACrB,uDAAqF;AAA5E,wHAAA,oBAAoB,OAAA;AAC7B,mCAAiC;AAAxB,+FAAA,KAAK,OAAA;AACd,yCAAuC;AAA9B,qGAAA,QAAQ,OAAA;AACjB,yEAAqE;AAA5D,mIAAA,sBAAsB,OAAA;AAC/B,uDAQ4B;AAP1B,mHAAA,eAAe,OAAA;AAQjB,2DAAwD;AAA/C,sHAAA,gBAAgB,OAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/hooks/index.ts"],"names":[],"mappings":";;;AAAA,qDAAkD;AAAzC,gHAAA,aAAa,OAAA;AACtB,mDAAgD;AAAvC,8GAAA,YAAY,OAAA;AACrB,iDAAoE;AAA3D,6GAAA,YAAY,OAAA;AACrB,uDAAqF;AAA5E,wHAAA,oBAAoB,OAAA;AAC7B,mCAAiC;AAAxB,+FAAA,KAAK,OAAA;AACd,yCAAuC;AAA9B,qGAAA,QAAQ,OAAA;AACjB,yEAAqE;AAA5D,mIAAA,sBAAsB,OAAA;AAC/B,yEAAsE;AAA7D,oIAAA,uBAAuB,OAAA;AAChC,uDAQ4B;AAP1B,mHAAA,eAAe,OAAA;AAQjB,2DAAwD;AAA/C,sHAAA,gBAAgB,OAAA"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.useIntersectionObserver = void 0;
|
|
7
|
+
var react_1 = __importDefault(require("react"));
|
|
8
|
+
var intersection_observer_1 = require("../utils/intersection_observer");
|
|
9
|
+
/**
|
|
10
|
+
* React sensor hook that tracks the changes in
|
|
11
|
+
* the intersection of a target element with an ancestor element
|
|
12
|
+
* or with a top-level document's viewport.
|
|
13
|
+
*
|
|
14
|
+
* @returns
|
|
15
|
+
* the refCallback function to determine which item to observe
|
|
16
|
+
* @returns
|
|
17
|
+
* the isIntersecting value to detect observing item is intersecting
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
*
|
|
21
|
+
* ```jsx
|
|
22
|
+
* function App(){
|
|
23
|
+
* const [refIntersecting, { isIntersecting }] = useIntersectionObserver();
|
|
24
|
+
* return (
|
|
25
|
+
* <div
|
|
26
|
+
* ref={refIntersecting}
|
|
27
|
+
* >
|
|
28
|
+
* {isIntersecting ? 'I\'m in the viewport' : null}
|
|
29
|
+
* </div>
|
|
30
|
+
* );
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
function useIntersectionObserver() {
|
|
35
|
+
var nodeRef = react_1.default.useRef(null);
|
|
36
|
+
var _a = react_1.default.useState(false), isIntersecting = _a[0], setIntersecting = _a[1];
|
|
37
|
+
react_1.default.useEffect(function () { return function () {
|
|
38
|
+
if (nodeRef.current) {
|
|
39
|
+
intersection_observer_1.intersectionObserver.remove(nodeRef.current);
|
|
40
|
+
}
|
|
41
|
+
}; }, []);
|
|
42
|
+
var refCallback = react_1.default.useCallback(function (node) {
|
|
43
|
+
if (node) {
|
|
44
|
+
intersection_observer_1.intersectionObserver.init();
|
|
45
|
+
nodeRef.current = node;
|
|
46
|
+
intersection_observer_1.intersectionObserver.add(node, setIntersecting);
|
|
47
|
+
}
|
|
48
|
+
}, []);
|
|
49
|
+
return [refCallback, { isIntersecting: isIntersecting }];
|
|
50
|
+
}
|
|
51
|
+
exports.useIntersectionObserver = useIntersectionObserver;
|
|
52
|
+
//# sourceMappingURL=use_intersection_observer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use_intersection_observer.js","sourceRoot":"","sources":["../../../src/hooks/use_intersection_observer.ts"],"names":[],"mappings":";;;;;;AAAA,gDAA0B;AAC1B,wEAAsE;AAetE;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,SAAgB,uBAAuB;IACrC,IAAM,OAAO,GAAG,eAAK,CAAC,MAAM,CAA0C,IAAI,CAAC,CAAC;IACtE,IAAA,KAAoC,eAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAxD,cAAc,QAAA,EAAE,eAAe,QAAyB,CAAC;IAEhE,eAAK,CAAC,SAAS,CAAC,cAAM,OAAA;QACpB,IAAI,OAAO,CAAC,OAAO,EAAE;YACnB,4CAAoB,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;SAC9C;IACH,CAAC,EAJqB,CAIrB,EAAE,EAAE,CAAC,CAAC;IAEP,IAAM,WAAW,GAAG,eAAK,CAAC,WAAW,CACnC,UAAC,IAAI;QACH,IAAI,IAAI,EAAE;YACR,4CAAoB,CAAC,IAAI,EAAE,CAAC;YAC5B,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;YACvB,4CAAoB,CAAC,GAAG,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC;SACjD;IACH,CAAC,EACD,EAAE,CACH,CAAC;IAEF,OAAO,CAAC,WAAW,EAAE,EAAE,cAAc,gBAAA,EAAE,CAAC,CAAC;AAC3C,CAAC;AAtBD,0DAsBC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.intersectionObserver = void 0;
|
|
4
|
+
var IntersectionObserverDirective = /** @class */ (function () {
|
|
5
|
+
function IntersectionObserverDirective() {
|
|
6
|
+
// As each observable child attaches itself to the parent observer, we need to
|
|
7
|
+
// map Elements to Callbacks so that when an Element's intersection changes,
|
|
8
|
+
// we'll know which callback to invoke. For this, we'll use an ES6 Map.
|
|
9
|
+
this.mapping = new Map();
|
|
10
|
+
}
|
|
11
|
+
// I initialize the intersection observer parent directive.
|
|
12
|
+
IntersectionObserverDirective.prototype.init = function () {
|
|
13
|
+
var _this = this;
|
|
14
|
+
if (!this.observer) {
|
|
15
|
+
this.observer = new IntersectionObserver(function (entries) {
|
|
16
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
17
|
+
for (var _i = 0, entries_1 = entries; _i < entries_1.length; _i++) {
|
|
18
|
+
var entry = entries_1[_i];
|
|
19
|
+
var callback = _this.mapping.get(entry.target);
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
21
|
+
(callback && callback(entry.isIntersecting));
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return this.observer;
|
|
26
|
+
};
|
|
27
|
+
IntersectionObserverDirective.prototype.assertObserver = function () {
|
|
28
|
+
if (!this.observer) {
|
|
29
|
+
throw new Error('You need to initialize the observer before');
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
// I add the given Element for intersection observation. When the intersection status
|
|
33
|
+
// changes, the given callback is invoked with the new status.
|
|
34
|
+
IntersectionObserverDirective.prototype.add = function (element, callback) {
|
|
35
|
+
this.assertObserver();
|
|
36
|
+
this.mapping.set(element, callback);
|
|
37
|
+
this.observer.observe(element);
|
|
38
|
+
};
|
|
39
|
+
// I get called once when the host element is being destroyed.
|
|
40
|
+
IntersectionObserverDirective.prototype.destroy = function () {
|
|
41
|
+
this.assertObserver();
|
|
42
|
+
this.mapping.clear();
|
|
43
|
+
this.observer.disconnect();
|
|
44
|
+
};
|
|
45
|
+
// I remove the given Element from intersection observation.
|
|
46
|
+
IntersectionObserverDirective.prototype.remove = function (element) {
|
|
47
|
+
this.assertObserver();
|
|
48
|
+
this.mapping.delete(element);
|
|
49
|
+
this.observer.unobserve(element);
|
|
50
|
+
};
|
|
51
|
+
return IntersectionObserverDirective;
|
|
52
|
+
}());
|
|
53
|
+
exports.intersectionObserver = new IntersectionObserverDirective();
|
|
54
|
+
//# sourceMappingURL=intersection_observer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"intersection_observer.js","sourceRoot":"","sources":["../../../src/utils/intersection_observer.ts"],"names":[],"mappings":";;;AAAA;IAKE;QACE,8EAA8E;QAC9E,4EAA4E;QAC5E,uEAAuE;QACvE,IAAI,CAAC,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;IAC3B,CAAC;IAED,2DAA2D;IACpD,4CAAI,GAAX;QAAA,iBAgBC;QAfC,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;YAClB,IAAI,CAAC,QAAQ,GAAG,IAAI,oBAAoB,CACtC,UAAC,OAAoC;gBACnC,gDAAgD;gBAChD,KAAoB,UAAO,EAAP,mBAAO,EAAP,qBAAO,EAAP,IAAO,EAAE;oBAAxB,IAAM,KAAK,gBAAA;oBACd,IAAM,QAAQ,GAAG,KAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;oBAEhD,oEAAoE;oBACpE,CAAC,QAAQ,IAAI,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC;iBAC9C;YACH,CAAC,CACF,CAAC;SACH;QAED,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAEO,sDAAc,GAAtB;QACE,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;YAClB,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;SAC/D;IACH,CAAC;IAED,qFAAqF;IACrF,8DAA8D;IACvD,2CAAG,GAAV,UAAW,OAAoB,EAAE,QAAkB;QACjD,IAAI,CAAC,cAAc,EAAE,CAAC;QAEtB,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QACpC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC;IAED,8DAA8D;IACvD,+CAAO,GAAd;QACE,IAAI,CAAC,cAAc,EAAE,CAAC;QAEtB,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACrB,IAAI,CAAC,QAAQ,CAAC,UAAU,EAAE,CAAC;IAC7B,CAAC;IAED,4DAA4D;IACrD,8CAAM,GAAb,UAAc,OAAoB;QAChC,IAAI,CAAC,cAAc,EAAE,CAAC;QAEtB,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAC7B,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IACnC,CAAC;IACH,oCAAC;AAAD,CAAC,AA7DD,IA6DC;AAEY,QAAA,oBAAoB,GAAG,IAAI,6BAA6B,EAAE,CAAC"}
|
package/dist/esm/hooks/index.js
CHANGED
|
@@ -5,6 +5,7 @@ export { useControllableState } from './use_controllable';
|
|
|
5
5
|
export { useId } from './use_id';
|
|
6
6
|
export { useImage } from './use_image';
|
|
7
7
|
export { useWindowEventListener } from './use_window_event_listener';
|
|
8
|
+
export { useIntersectionObserver } from './use_intersection_observer';
|
|
8
9
|
export { useAutocomplete, } from './use_autocomplete';
|
|
9
10
|
export { useEventCallback } from './use_event_callback';
|
|
10
11
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAuB,MAAM,iBAAiB,CAAC;AACpE,OAAO,EAAE,oBAAoB,EAA6B,MAAM,oBAAoB,CAAC;AACrF,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EACL,eAAe,GAOhB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAuB,MAAM,iBAAiB,CAAC;AACpE,OAAO,EAAE,oBAAoB,EAA6B,MAAM,oBAAoB,CAAC;AACrF,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AACtE,OAAO,EACL,eAAe,GAOhB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { intersectionObserver } from '../utils/intersection_observer';
|
|
3
|
+
/**
|
|
4
|
+
* React sensor hook that tracks the changes in
|
|
5
|
+
* the intersection of a target element with an ancestor element
|
|
6
|
+
* or with a top-level document's viewport.
|
|
7
|
+
*
|
|
8
|
+
* @returns
|
|
9
|
+
* the refCallback function to determine which item to observe
|
|
10
|
+
* @returns
|
|
11
|
+
* the isIntersecting value to detect observing item is intersecting
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
*
|
|
15
|
+
* ```jsx
|
|
16
|
+
* function App(){
|
|
17
|
+
* const [refIntersecting, { isIntersecting }] = useIntersectionObserver();
|
|
18
|
+
* return (
|
|
19
|
+
* <div
|
|
20
|
+
* ref={refIntersecting}
|
|
21
|
+
* >
|
|
22
|
+
* {isIntersecting ? 'I\'m in the viewport' : null}
|
|
23
|
+
* </div>
|
|
24
|
+
* );
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function useIntersectionObserver() {
|
|
29
|
+
var nodeRef = React.useRef(null);
|
|
30
|
+
var _a = React.useState(false), isIntersecting = _a[0], setIntersecting = _a[1];
|
|
31
|
+
React.useEffect(function () { return function () {
|
|
32
|
+
if (nodeRef.current) {
|
|
33
|
+
intersectionObserver.remove(nodeRef.current);
|
|
34
|
+
}
|
|
35
|
+
}; }, []);
|
|
36
|
+
var refCallback = React.useCallback(function (node) {
|
|
37
|
+
if (node) {
|
|
38
|
+
intersectionObserver.init();
|
|
39
|
+
nodeRef.current = node;
|
|
40
|
+
intersectionObserver.add(node, setIntersecting);
|
|
41
|
+
}
|
|
42
|
+
}, []);
|
|
43
|
+
return [refCallback, { isIntersecting: isIntersecting }];
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=use_intersection_observer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use_intersection_observer.js","sourceRoot":"","sources":["../../../src/hooks/use_intersection_observer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,oBAAoB,EAAE,MAAM,gCAAgC,CAAC;AAetE;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,uBAAuB;IACrC,IAAM,OAAO,GAAG,KAAK,CAAC,MAAM,CAA0C,IAAI,CAAC,CAAC;IACtE,IAAA,KAAoC,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAxD,cAAc,QAAA,EAAE,eAAe,QAAyB,CAAC;IAEhE,KAAK,CAAC,SAAS,CAAC,cAAM,OAAA;QACpB,IAAI,OAAO,CAAC,OAAO,EAAE;YACnB,oBAAoB,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;SAC9C;IACH,CAAC,EAJqB,CAIrB,EAAE,EAAE,CAAC,CAAC;IAEP,IAAM,WAAW,GAAG,KAAK,CAAC,WAAW,CACnC,UAAC,IAAI;QACH,IAAI,IAAI,EAAE;YACR,oBAAoB,CAAC,IAAI,EAAE,CAAC;YAC5B,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;YACvB,oBAAoB,CAAC,GAAG,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC;SACjD;IACH,CAAC,EACD,EAAE,CACH,CAAC;IAEF,OAAO,CAAC,WAAW,EAAE,EAAE,cAAc,gBAAA,EAAE,CAAC,CAAC;AAC3C,CAAC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
var IntersectionObserverDirective = /** @class */ (function () {
|
|
2
|
+
function IntersectionObserverDirective() {
|
|
3
|
+
// As each observable child attaches itself to the parent observer, we need to
|
|
4
|
+
// map Elements to Callbacks so that when an Element's intersection changes,
|
|
5
|
+
// we'll know which callback to invoke. For this, we'll use an ES6 Map.
|
|
6
|
+
this.mapping = new Map();
|
|
7
|
+
}
|
|
8
|
+
// I initialize the intersection observer parent directive.
|
|
9
|
+
IntersectionObserverDirective.prototype.init = function () {
|
|
10
|
+
var _this = this;
|
|
11
|
+
if (!this.observer) {
|
|
12
|
+
this.observer = new IntersectionObserver(function (entries) {
|
|
13
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
14
|
+
for (var _i = 0, entries_1 = entries; _i < entries_1.length; _i++) {
|
|
15
|
+
var entry = entries_1[_i];
|
|
16
|
+
var callback = _this.mapping.get(entry.target);
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
18
|
+
(callback && callback(entry.isIntersecting));
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
return this.observer;
|
|
23
|
+
};
|
|
24
|
+
IntersectionObserverDirective.prototype.assertObserver = function () {
|
|
25
|
+
if (!this.observer) {
|
|
26
|
+
throw new Error('You need to initialize the observer before');
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
// I add the given Element for intersection observation. When the intersection status
|
|
30
|
+
// changes, the given callback is invoked with the new status.
|
|
31
|
+
IntersectionObserverDirective.prototype.add = function (element, callback) {
|
|
32
|
+
this.assertObserver();
|
|
33
|
+
this.mapping.set(element, callback);
|
|
34
|
+
this.observer.observe(element);
|
|
35
|
+
};
|
|
36
|
+
// I get called once when the host element is being destroyed.
|
|
37
|
+
IntersectionObserverDirective.prototype.destroy = function () {
|
|
38
|
+
this.assertObserver();
|
|
39
|
+
this.mapping.clear();
|
|
40
|
+
this.observer.disconnect();
|
|
41
|
+
};
|
|
42
|
+
// I remove the given Element from intersection observation.
|
|
43
|
+
IntersectionObserverDirective.prototype.remove = function (element) {
|
|
44
|
+
this.assertObserver();
|
|
45
|
+
this.mapping.delete(element);
|
|
46
|
+
this.observer.unobserve(element);
|
|
47
|
+
};
|
|
48
|
+
return IntersectionObserverDirective;
|
|
49
|
+
}());
|
|
50
|
+
export var intersectionObserver = new IntersectionObserverDirective();
|
|
51
|
+
//# sourceMappingURL=intersection_observer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"intersection_observer.js","sourceRoot":"","sources":["../../../src/utils/intersection_observer.ts"],"names":[],"mappings":"AAAA;IAKE;QACE,8EAA8E;QAC9E,4EAA4E;QAC5E,uEAAuE;QACvE,IAAI,CAAC,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;IAC3B,CAAC;IAED,2DAA2D;IACpD,4CAAI,GAAX;QAAA,iBAgBC;QAfC,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;YAClB,IAAI,CAAC,QAAQ,GAAG,IAAI,oBAAoB,CACtC,UAAC,OAAoC;gBACnC,gDAAgD;gBAChD,KAAoB,UAAO,EAAP,mBAAO,EAAP,qBAAO,EAAP,IAAO,EAAE;oBAAxB,IAAM,KAAK,gBAAA;oBACd,IAAM,QAAQ,GAAG,KAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;oBAEhD,oEAAoE;oBACpE,CAAC,QAAQ,IAAI,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC;iBAC9C;YACH,CAAC,CACF,CAAC;SACH;QAED,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAEO,sDAAc,GAAtB;QACE,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;YAClB,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;SAC/D;IACH,CAAC;IAED,qFAAqF;IACrF,8DAA8D;IACvD,2CAAG,GAAV,UAAW,OAAoB,EAAE,QAAkB;QACjD,IAAI,CAAC,cAAc,EAAE,CAAC;QAEtB,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QACpC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC;IAED,8DAA8D;IACvD,+CAAO,GAAd;QACE,IAAI,CAAC,cAAc,EAAE,CAAC;QAEtB,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACrB,IAAI,CAAC,QAAQ,CAAC,UAAU,EAAE,CAAC;IAC7B,CAAC;IAED,4DAA4D;IACrD,8CAAM,GAAb,UAAc,OAAoB;QAChC,IAAI,CAAC,cAAc,EAAE,CAAC;QAEtB,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAC7B,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IACnC,CAAC;IACH,oCAAC;AAAD,CAAC,AA7DD,IA6DC;AAED,MAAM,CAAC,IAAM,oBAAoB,GAAG,IAAI,6BAA6B,EAAE,CAAC"}
|
|
@@ -5,6 +5,7 @@ export { useControllableState } from './use_controllable';
|
|
|
5
5
|
export { useId } from './use_id';
|
|
6
6
|
export { useImage } from './use_image';
|
|
7
7
|
export { useWindowEventListener } from './use_window_event_listener';
|
|
8
|
+
export { useIntersectionObserver } from './use_intersection_observer';
|
|
8
9
|
export { useAutocomplete, } from './use_autocomplete';
|
|
9
10
|
export { useEventCallback } from './use_event_callback';
|
|
10
11
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAuB,MAAM,iBAAiB,CAAC;AACpE,OAAO,EAAE,oBAAoB,EAA6B,MAAM,oBAAoB,CAAC;AACrF,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EACL,eAAe,GAOhB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAuB,MAAM,iBAAiB,CAAC;AACpE,OAAO,EAAE,oBAAoB,EAA6B,MAAM,oBAAoB,CAAC;AACrF,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AACtE,OAAO,EACL,eAAe,GAOhB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { intersectionObserver } from '../utils/intersection_observer';
|
|
3
|
+
/**
|
|
4
|
+
* React sensor hook that tracks the changes in
|
|
5
|
+
* the intersection of a target element with an ancestor element
|
|
6
|
+
* or with a top-level document's viewport.
|
|
7
|
+
*
|
|
8
|
+
* @returns
|
|
9
|
+
* the refCallback function to determine which item to observe
|
|
10
|
+
* @returns
|
|
11
|
+
* the isIntersecting value to detect observing item is intersecting
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
*
|
|
15
|
+
* ```jsx
|
|
16
|
+
* function App(){
|
|
17
|
+
* const [refIntersecting, { isIntersecting }] = useIntersectionObserver();
|
|
18
|
+
* return (
|
|
19
|
+
* <div
|
|
20
|
+
* ref={refIntersecting}
|
|
21
|
+
* >
|
|
22
|
+
* {isIntersecting ? 'I\'m in the viewport' : null}
|
|
23
|
+
* </div>
|
|
24
|
+
* );
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function useIntersectionObserver() {
|
|
29
|
+
const nodeRef = React.useRef(null);
|
|
30
|
+
const [isIntersecting, setIntersecting] = React.useState(false);
|
|
31
|
+
React.useEffect(() => () => {
|
|
32
|
+
if (nodeRef.current) {
|
|
33
|
+
intersectionObserver.remove(nodeRef.current);
|
|
34
|
+
}
|
|
35
|
+
}, []);
|
|
36
|
+
const refCallback = React.useCallback((node) => {
|
|
37
|
+
if (node) {
|
|
38
|
+
intersectionObserver.init();
|
|
39
|
+
nodeRef.current = node;
|
|
40
|
+
intersectionObserver.add(node, setIntersecting);
|
|
41
|
+
}
|
|
42
|
+
}, []);
|
|
43
|
+
return [refCallback, { isIntersecting }];
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=use_intersection_observer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use_intersection_observer.js","sourceRoot":"","sources":["../../../src/hooks/use_intersection_observer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,oBAAoB,EAAE,MAAM,gCAAgC,CAAC;AAetE;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,uBAAuB;IACrC,MAAM,OAAO,GAAG,KAAK,CAAC,MAAM,CAA0C,IAAI,CAAC,CAAC;IAC5E,MAAM,CAAC,cAAc,EAAE,eAAe,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAEhE,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE;QACzB,IAAI,OAAO,CAAC,OAAO,EAAE;YACnB,oBAAoB,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;SAC9C;IACH,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,MAAM,WAAW,GAAG,KAAK,CAAC,WAAW,CACnC,CAAC,IAAI,EAAE,EAAE;QACP,IAAI,IAAI,EAAE;YACR,oBAAoB,CAAC,IAAI,EAAE,CAAC;YAC5B,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;YACvB,oBAAoB,CAAC,GAAG,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC;SACjD;IACH,CAAC,EACD,EAAE,CACH,CAAC;IAEF,OAAO,CAAC,WAAW,EAAE,EAAE,cAAc,EAAE,CAAC,CAAC;AAC3C,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
class IntersectionObserverDirective {
|
|
2
|
+
mapping;
|
|
3
|
+
observer;
|
|
4
|
+
constructor() {
|
|
5
|
+
// As each observable child attaches itself to the parent observer, we need to
|
|
6
|
+
// map Elements to Callbacks so that when an Element's intersection changes,
|
|
7
|
+
// we'll know which callback to invoke. For this, we'll use an ES6 Map.
|
|
8
|
+
this.mapping = new Map();
|
|
9
|
+
}
|
|
10
|
+
// I initialize the intersection observer parent directive.
|
|
11
|
+
init() {
|
|
12
|
+
if (!this.observer) {
|
|
13
|
+
this.observer = new IntersectionObserver((entries) => {
|
|
14
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
15
|
+
for (const entry of entries) {
|
|
16
|
+
const callback = this.mapping.get(entry.target);
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
18
|
+
(callback && callback(entry.isIntersecting));
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
return this.observer;
|
|
23
|
+
}
|
|
24
|
+
assertObserver() {
|
|
25
|
+
if (!this.observer) {
|
|
26
|
+
throw new Error('You need to initialize the observer before');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// I add the given Element for intersection observation. When the intersection status
|
|
30
|
+
// changes, the given callback is invoked with the new status.
|
|
31
|
+
add(element, callback) {
|
|
32
|
+
this.assertObserver();
|
|
33
|
+
this.mapping.set(element, callback);
|
|
34
|
+
this.observer.observe(element);
|
|
35
|
+
}
|
|
36
|
+
// I get called once when the host element is being destroyed.
|
|
37
|
+
destroy() {
|
|
38
|
+
this.assertObserver();
|
|
39
|
+
this.mapping.clear();
|
|
40
|
+
this.observer.disconnect();
|
|
41
|
+
}
|
|
42
|
+
// I remove the given Element from intersection observation.
|
|
43
|
+
remove(element) {
|
|
44
|
+
this.assertObserver();
|
|
45
|
+
this.mapping.delete(element);
|
|
46
|
+
this.observer.unobserve(element);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
export const intersectionObserver = new IntersectionObserverDirective();
|
|
50
|
+
//# sourceMappingURL=intersection_observer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"intersection_observer.js","sourceRoot":"","sources":["../../../src/utils/intersection_observer.ts"],"names":[],"mappings":"AAAA,MAAM,6BAA6B;IACzB,OAAO,CAAyB;IAEhC,QAAQ,CAAwB;IAExC;QACE,8EAA8E;QAC9E,4EAA4E;QAC5E,uEAAuE;QACvE,IAAI,CAAC,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;IAC3B,CAAC;IAED,2DAA2D;IACpD,IAAI;QACT,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;YAClB,IAAI,CAAC,QAAQ,GAAG,IAAI,oBAAoB,CACtC,CAAC,OAAoC,EAAE,EAAE;gBACvC,gDAAgD;gBAChD,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE;oBAC3B,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;oBAEhD,oEAAoE;oBACpE,CAAC,QAAQ,IAAI,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC;iBAC9C;YACH,CAAC,CACF,CAAC;SACH;QAED,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAEO,cAAc;QACpB,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;YAClB,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;SAC/D;IACH,CAAC;IAED,qFAAqF;IACrF,8DAA8D;IACvD,GAAG,CAAC,OAAoB,EAAE,QAAkB;QACjD,IAAI,CAAC,cAAc,EAAE,CAAC;QAEtB,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QACpC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC;IAED,8DAA8D;IACvD,OAAO;QACZ,IAAI,CAAC,cAAc,EAAE,CAAC;QAEtB,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACrB,IAAI,CAAC,QAAQ,CAAC,UAAU,EAAE,CAAC;IAC7B,CAAC;IAED,4DAA4D;IACrD,MAAM,CAAC,OAAoB;QAChC,IAAI,CAAC,cAAc,EAAE,CAAC;QAEtB,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAC7B,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IACnC,CAAC;CACF;AAED,MAAM,CAAC,MAAM,oBAAoB,GAAG,IAAI,6BAA6B,EAAE,CAAC"}
|
|
@@ -5,5 +5,6 @@ export { useControllableState, UseControllableStateProps } from './use_controlla
|
|
|
5
5
|
export { useId } from './use_id';
|
|
6
6
|
export { useImage } from './use_image';
|
|
7
7
|
export { useWindowEventListener } from './use_window_event_listener';
|
|
8
|
+
export { useIntersectionObserver } from './use_intersection_observer';
|
|
8
9
|
export { useAutocomplete, UseAutocompleteProps, UseAutocompleteReturnType, AutocompleteValue, AutocompleteGroupedOption, AutocompleteChangeReason, AutocompleteChangeDetails, } from './use_autocomplete';
|
|
9
10
|
export { useEventCallback } from './use_event_callback';
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export declare type IntersectionObserverHookRefCallbackNode = HTMLElement | null;
|
|
2
|
+
export declare type IntersectionObserverHookRefCallback = (node: IntersectionObserverHookRefCallbackNode) => void;
|
|
3
|
+
export declare type IntersectionObserverHookResult = [
|
|
4
|
+
IntersectionObserverHookRefCallback,
|
|
5
|
+
{
|
|
6
|
+
isIntersecting: boolean;
|
|
7
|
+
}
|
|
8
|
+
];
|
|
9
|
+
/**
|
|
10
|
+
* React sensor hook that tracks the changes in
|
|
11
|
+
* the intersection of a target element with an ancestor element
|
|
12
|
+
* or with a top-level document's viewport.
|
|
13
|
+
*
|
|
14
|
+
* @returns
|
|
15
|
+
* the refCallback function to determine which item to observe
|
|
16
|
+
* @returns
|
|
17
|
+
* the isIntersecting value to detect observing item is intersecting
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
*
|
|
21
|
+
* ```jsx
|
|
22
|
+
* function App(){
|
|
23
|
+
* const [refIntersecting, { isIntersecting }] = useIntersectionObserver();
|
|
24
|
+
* return (
|
|
25
|
+
* <div
|
|
26
|
+
* ref={refIntersecting}
|
|
27
|
+
* >
|
|
28
|
+
* {isIntersecting ? 'I\'m in the viewport' : null}
|
|
29
|
+
* </div>
|
|
30
|
+
* );
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function useIntersectionObserver(): IntersectionObserverHookResult;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
declare class IntersectionObserverDirective {
|
|
2
|
+
private mapping;
|
|
3
|
+
private observer?;
|
|
4
|
+
constructor();
|
|
5
|
+
init(): IntersectionObserver;
|
|
6
|
+
private assertObserver;
|
|
7
|
+
add(element: HTMLElement, callback: Function): void;
|
|
8
|
+
destroy(): void;
|
|
9
|
+
remove(element: HTMLElement): void;
|
|
10
|
+
}
|
|
11
|
+
export declare const intersectionObserver: IntersectionObserverDirective;
|
|
12
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peculiar/react-components",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.2",
|
|
5
5
|
"author": "PeculiarVentures Team",
|
|
6
6
|
"description": "A simple and customizable component library to build faster, beautiful, and more accessible React applications.",
|
|
7
7
|
"keywords": [
|
|
@@ -96,5 +96,5 @@
|
|
|
96
96
|
"node": ">=12.x"
|
|
97
97
|
},
|
|
98
98
|
"license": "MIT",
|
|
99
|
-
"gitHead": "
|
|
99
|
+
"gitHead": "b596b2bd3ade5707280920d92f96fe65bee1367e"
|
|
100
100
|
}
|