@koine/react 1.1.3 → 1.1.4
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/hooks/index.d.ts
CHANGED
|
@@ -14,6 +14,7 @@ export * from "./useNavigateAway";
|
|
|
14
14
|
export * from "./usePrevious";
|
|
15
15
|
export * from "./usePreviousRef";
|
|
16
16
|
export * from "./useScrollPosition";
|
|
17
|
+
export * from "./useScrollThreshold";
|
|
17
18
|
export * from "./useSmoothScroll";
|
|
18
19
|
export * from "./useTraceUpdate";
|
|
19
20
|
export * from "./useUpdateEffect";
|
package/hooks/index.js
CHANGED
|
@@ -14,6 +14,7 @@ export * from "./useNavigateAway";
|
|
|
14
14
|
export * from "./usePrevious";
|
|
15
15
|
export * from "./usePreviousRef";
|
|
16
16
|
export * from "./useScrollPosition";
|
|
17
|
+
export * from "./useScrollThreshold";
|
|
17
18
|
// export * from "./useScrollTo";
|
|
18
19
|
export * from "./useSmoothScroll";
|
|
19
20
|
export * from "./useTraceUpdate";
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
import { noop } from "@koine/utils";
|
|
3
|
+
import { /* listenScroll, */ on } from "@koine/dom";
|
|
4
|
+
export var useScrollThreshold = function (threshold, callback) {
|
|
5
|
+
var _a = useState(false), isBelow = _a[0], setIsBelow = _a[1];
|
|
6
|
+
var handler = useCallback(function () {
|
|
7
|
+
if (threshold) {
|
|
8
|
+
var posY = window.scrollY; // * -1;
|
|
9
|
+
var isAbove = posY < threshold;
|
|
10
|
+
var isBelow_1 = posY > threshold;
|
|
11
|
+
// console.log("useScrollThreshold setIsBelow", isBelow, posY, threshold);
|
|
12
|
+
setIsBelow(isBelow_1);
|
|
13
|
+
if (callback)
|
|
14
|
+
callback(isAbove, isBelow_1);
|
|
15
|
+
}
|
|
16
|
+
}, [threshold, callback]);
|
|
17
|
+
useEffect(function () {
|
|
18
|
+
if (threshold) {
|
|
19
|
+
// const listener = listenScroll(handler, 50);
|
|
20
|
+
var listener = on(window, "scroll", handler, { passive: true });
|
|
21
|
+
handler();
|
|
22
|
+
return listener;
|
|
23
|
+
}
|
|
24
|
+
return noop;
|
|
25
|
+
}, [threshold, handler]);
|
|
26
|
+
return isBelow;
|
|
27
|
+
};
|
|
28
|
+
export default useScrollThreshold;
|
package/node/hooks/index.js
CHANGED
|
@@ -17,6 +17,7 @@ tslib_1.__exportStar(require("./useNavigateAway"), exports);
|
|
|
17
17
|
tslib_1.__exportStar(require("./usePrevious"), exports);
|
|
18
18
|
tslib_1.__exportStar(require("./usePreviousRef"), exports);
|
|
19
19
|
tslib_1.__exportStar(require("./useScrollPosition"), exports);
|
|
20
|
+
tslib_1.__exportStar(require("./useScrollThreshold"), exports);
|
|
20
21
|
// export * from "./useScrollTo";
|
|
21
22
|
tslib_1.__exportStar(require("./useSmoothScroll"), exports);
|
|
22
23
|
tslib_1.__exportStar(require("./useTraceUpdate"), exports);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useScrollThreshold = void 0;
|
|
4
|
+
var react_1 = require("react");
|
|
5
|
+
var utils_1 = require("@koine/utils");
|
|
6
|
+
var dom_1 = require("@koine/dom");
|
|
7
|
+
var useScrollThreshold = function (threshold, callback) {
|
|
8
|
+
var _a = (0, react_1.useState)(false), isBelow = _a[0], setIsBelow = _a[1];
|
|
9
|
+
var handler = (0, react_1.useCallback)(function () {
|
|
10
|
+
if (threshold) {
|
|
11
|
+
var posY = window.scrollY; // * -1;
|
|
12
|
+
var isAbove = posY < threshold;
|
|
13
|
+
var isBelow_1 = posY > threshold;
|
|
14
|
+
// console.log("useScrollThreshold setIsBelow", isBelow, posY, threshold);
|
|
15
|
+
setIsBelow(isBelow_1);
|
|
16
|
+
if (callback)
|
|
17
|
+
callback(isAbove, isBelow_1);
|
|
18
|
+
}
|
|
19
|
+
}, [threshold, callback]);
|
|
20
|
+
(0, react_1.useEffect)(function () {
|
|
21
|
+
if (threshold) {
|
|
22
|
+
// const listener = listenScroll(handler, 50);
|
|
23
|
+
var listener = (0, dom_1.on)(window, "scroll", handler, { passive: true });
|
|
24
|
+
handler();
|
|
25
|
+
return listener;
|
|
26
|
+
}
|
|
27
|
+
return utils_1.noop;
|
|
28
|
+
}, [threshold, handler]);
|
|
29
|
+
return isBelow;
|
|
30
|
+
};
|
|
31
|
+
exports.useScrollThreshold = useScrollThreshold;
|
|
32
|
+
exports.default = exports.useScrollThreshold;
|