@smakss/react-scroll-direction 1.1.0-beta → 2.0.0
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/Readme.md +28 -24
- package/dist/cjs/index.js +40 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +40 -1
- package/dist/esm/index.js.map +1 -1
- package/package.json +2 -1
package/Readme.md
CHANGED
|
@@ -1,61 +1,65 @@
|
|
|
1
|
-
#
|
|
1
|
+
# React Scroll Direction Hook
|
|
2
2
|
|
|
3
3
|
    
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Detect scroll direction in your React applications effortlessly using `@smakss/react-scroll-direction`, a custom React Hook with an adjustable threshold.
|
|
6
6
|
|
|
7
|
-
This package
|
|
7
|
+
This package was inspired by a [popular StackOverflow answer](https://stackoverflow.com/a/62497293/11908502), crafted to be a ready-to-use solution for detecting scroll direction in your React applications.
|
|
8
8
|
|
|
9
9
|
## Demo
|
|
10
10
|
|
|
11
|
+
Click the button below to view a demo on CodeSandbox:
|
|
12
|
+
|
|
11
13
|
[](https://codesandbox.io/s/react-scroll-direction-tclwvp?fontsize=14&hidenavigation=1&theme=dark)
|
|
12
14
|
|
|
13
|
-
##
|
|
15
|
+
## Installation
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
Install the package using npm or yarn:
|
|
16
18
|
|
|
17
19
|
```bash
|
|
18
|
-
npm
|
|
19
|
-
or
|
|
20
|
+
npm install @smakss/react-scroll-direction
|
|
21
|
+
# or
|
|
20
22
|
yarn add @smakss/react-scroll-direction
|
|
21
23
|
```
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
To include it in your project, use:
|
|
26
|
+
|
|
27
|
+
CommonJS:
|
|
24
28
|
|
|
25
29
|
```js
|
|
26
|
-
|
|
30
|
+
const { useDetectScroll } = require("@smakss/react-scroll-direction");
|
|
27
31
|
```
|
|
28
32
|
|
|
29
|
-
|
|
33
|
+
ES Module:
|
|
30
34
|
|
|
31
35
|
```js
|
|
32
36
|
import { useDetectScroll } from "@smakss/react-scroll-direction";
|
|
33
37
|
```
|
|
34
38
|
|
|
35
|
-
|
|
39
|
+
## Usage
|
|
36
40
|
|
|
37
|
-
The useDetectScroll custom hook
|
|
41
|
+
The `useDetectScroll` custom hook accepts an options object with the following properties:
|
|
38
42
|
|
|
39
|
-
- `thr
|
|
40
|
-
- `axis
|
|
41
|
-
- `scrollUp
|
|
42
|
-
- `scrollDown
|
|
43
|
-
- `still
|
|
43
|
+
- `thr`: Threshold to trigger scroll direction change (default: `0`, only accepts positive values).
|
|
44
|
+
- `axis`: Scroll axis (`"y"` or `"x"`, default: `"y"`).
|
|
45
|
+
- `scrollUp`: Output value when scrolling up or left (default: `"up"` for y-axis, `"left"` for x-axis).
|
|
46
|
+
- `scrollDown`: Output value when scrolling down or right (default: `"down"` for y-axis, `"right"` for x-axis).
|
|
47
|
+
- `still`: Output value when no scrolling is detected (default: `"still"`).
|
|
44
48
|
|
|
45
|
-
## Examples
|
|
49
|
+
## Examples
|
|
46
50
|
|
|
47
|
-
|
|
51
|
+
Detecting scroll up/down:
|
|
48
52
|
|
|
49
53
|
```js
|
|
50
|
-
const
|
|
54
|
+
const scrollDir = useDetectScroll({});
|
|
51
55
|
|
|
52
|
-
//
|
|
56
|
+
// Outputs: "up", "down", or "still"
|
|
53
57
|
```
|
|
54
58
|
|
|
55
|
-
|
|
59
|
+
Detecting scroll left/right:
|
|
56
60
|
|
|
57
61
|
```js
|
|
58
|
-
const
|
|
62
|
+
const scrollDir = useDetectScroll({ axis: "x" });
|
|
59
63
|
|
|
60
|
-
//
|
|
64
|
+
// Outputs: "left", "right", or "still"
|
|
61
65
|
```
|
package/dist/cjs/index.js
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
var react = require('react');
|
|
4
4
|
|
|
5
|
+
/** Enumeration for axis values */
|
|
5
6
|
var Axis;
|
|
6
7
|
(function (Axis) {
|
|
7
8
|
Axis["X"] = "x";
|
|
8
9
|
Axis["Y"] = "y";
|
|
9
10
|
})(Axis || (Axis = {}));
|
|
11
|
+
/** Enumeration for direction values */
|
|
10
12
|
var Direction;
|
|
11
13
|
(function (Direction) {
|
|
12
14
|
Direction["Up"] = "up";
|
|
@@ -15,12 +17,48 @@ var Direction;
|
|
|
15
17
|
Direction["Right"] = "right";
|
|
16
18
|
Direction["Still"] = "still";
|
|
17
19
|
})(Direction || (Direction = {}));
|
|
20
|
+
/**
|
|
21
|
+
* useDetectScroll hook.
|
|
22
|
+
*
|
|
23
|
+
* This hook provides a mechanism to detect the scroll direction.
|
|
24
|
+
* It will return the scroll direction as a string (up, down, left, right, or still) based on user scrolling.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
*
|
|
28
|
+
* import useDetectScroll, { Axis, Direction } from './useDetectScroll';
|
|
29
|
+
*
|
|
30
|
+
* function App() {
|
|
31
|
+
* const scrollDirection = useDetectScroll({
|
|
32
|
+
* thr: 100,
|
|
33
|
+
* axis: Axis.Y,
|
|
34
|
+
* scrollUp: Direction.Up,
|
|
35
|
+
* scrollDown: Direction.Down,
|
|
36
|
+
* still: Direction.Still
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* return (
|
|
40
|
+
* <div>
|
|
41
|
+
* <p>Current scroll direction: {scrollDirection}</p>
|
|
42
|
+
* </div>
|
|
43
|
+
* );
|
|
44
|
+
* }
|
|
45
|
+
*
|
|
46
|
+
* @param {ScrollProps} props - The properties related to scrolling.
|
|
47
|
+
* @property {number} props.thr - The threshold value which the scroll difference must exceed to update scroll direction.
|
|
48
|
+
* @property {Axis} props.axis - The axis along which to detect scroll. Can be 'x' or 'y'.
|
|
49
|
+
* @property {Direction} props.scrollUp - The direction to set when scrolling up or left. By default, 'up' for y-axis and 'left' for x-axis.
|
|
50
|
+
* @property {Direction} props.scrollDown - The direction to set when scrolling down or right. By default, 'down' for y-axis and 'right' for x-axis.
|
|
51
|
+
* @property {Direction} props.still - The direction to set when there is no scrolling. By default, 'still'.
|
|
52
|
+
*
|
|
53
|
+
* @returns {Direction} - The current direction of scrolling.
|
|
54
|
+
*/
|
|
18
55
|
function useDetectScroll(props) {
|
|
19
56
|
const { thr = 0, axis = Axis.Y, scrollUp = axis === Axis.Y ? Direction.Up : Direction.Left, scrollDown = axis === Axis.Y ? Direction.Down : Direction.Right, still = Direction.Still, } = props;
|
|
20
57
|
const [scrollDir, setScrollDir] = react.useState(still);
|
|
21
58
|
const threshold = Math.max(0, thr);
|
|
22
59
|
let ticking = false;
|
|
23
60
|
let lastScroll = axis === Axis.Y ? window.scrollY : window.scrollX;
|
|
61
|
+
/** Function to update scroll direction */
|
|
24
62
|
const updateScrollDir = react.useCallback(() => {
|
|
25
63
|
const scroll = axis === Axis.Y ? window.scrollY : window.scrollX;
|
|
26
64
|
if (Math.abs(scroll - lastScroll) >= threshold) {
|
|
@@ -30,6 +68,7 @@ function useDetectScroll(props) {
|
|
|
30
68
|
ticking = false;
|
|
31
69
|
}, [axis, threshold, scrollDown, scrollUp]);
|
|
32
70
|
react.useEffect(() => {
|
|
71
|
+
/** Function to handle onScroll event */
|
|
33
72
|
const onScroll = () => {
|
|
34
73
|
if (!ticking) {
|
|
35
74
|
window.requestAnimationFrame(updateScrollDir);
|
|
@@ -39,7 +78,7 @@ function useDetectScroll(props) {
|
|
|
39
78
|
window.addEventListener("scroll", onScroll);
|
|
40
79
|
return () => window.removeEventListener("scroll", onScroll);
|
|
41
80
|
}, [updateScrollDir]);
|
|
42
|
-
return
|
|
81
|
+
return scrollDir;
|
|
43
82
|
}
|
|
44
83
|
|
|
45
84
|
exports.useDetectScroll = useDetectScroll;
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/hooks/useDetectScroll.ts"],"sourcesContent":["import { useState, useEffect, useCallback } from \"react\";\n\nenum Axis {\n X = \"x\",\n Y = \"y\",\n}\n\nenum Direction {\n Up = \"up\",\n Down = \"down\",\n Left = \"left\",\n Right = \"right\",\n Still = \"still\",\n}\n\ntype ScrollProps = {\n thr?: number;\n axis?: Axis;\n scrollUp?:
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/hooks/useDetectScroll.ts"],"sourcesContent":["import { useState, useEffect, useCallback } from \"react\";\n\n/** Enumeration for axis values */\nenum Axis {\n X = \"x\",\n Y = \"y\",\n}\n\n/** Enumeration for direction values */\nenum Direction {\n Up = \"up\",\n Down = \"down\",\n Left = \"left\",\n Right = \"right\",\n Still = \"still\",\n}\n\n/** Type declaration for scroll properties */\ntype ScrollProps = {\n thr?: number;\n axis?: Axis;\n scrollUp?: Direction;\n scrollDown?: Direction;\n still?: Direction;\n};\n\n/**\n * useDetectScroll hook.\n *\n * This hook provides a mechanism to detect the scroll direction.\n * It will return the scroll direction as a string (up, down, left, right, or still) based on user scrolling.\n *\n * @example\n *\n * import useDetectScroll, { Axis, Direction } from './useDetectScroll';\n *\n * function App() {\n * const scrollDirection = useDetectScroll({\n * thr: 100,\n * axis: Axis.Y,\n * scrollUp: Direction.Up,\n * scrollDown: Direction.Down,\n * still: Direction.Still\n * });\n *\n * return (\n * <div>\n * <p>Current scroll direction: {scrollDirection}</p>\n * </div>\n * );\n * }\n *\n * @param {ScrollProps} props - The properties related to scrolling.\n * @property {number} props.thr - The threshold value which the scroll difference must exceed to update scroll direction.\n * @property {Axis} props.axis - The axis along which to detect scroll. Can be 'x' or 'y'.\n * @property {Direction} props.scrollUp - The direction to set when scrolling up or left. By default, 'up' for y-axis and 'left' for x-axis.\n * @property {Direction} props.scrollDown - The direction to set when scrolling down or right. By default, 'down' for y-axis and 'right' for x-axis.\n * @property {Direction} props.still - The direction to set when there is no scrolling. By default, 'still'.\n *\n * @returns {Direction} - The current direction of scrolling.\n */\nfunction useDetectScroll(props: ScrollProps): Direction {\n const {\n thr = 0,\n axis = Axis.Y,\n scrollUp = axis === Axis.Y ? Direction.Up : Direction.Left,\n scrollDown = axis === Axis.Y ? Direction.Down : Direction.Right,\n still = Direction.Still,\n } = props;\n\n const [scrollDir, setScrollDir] = useState<Direction>(still);\n\n const threshold = Math.max(0, thr);\n let ticking = false;\n let lastScroll: number = axis === Axis.Y ? window.scrollY : window.scrollX;\n\n /** Function to update scroll direction */\n const updateScrollDir = useCallback(() => {\n const scroll = axis === Axis.Y ? window.scrollY : window.scrollX;\n\n if (Math.abs(scroll - lastScroll) >= threshold) {\n setScrollDir(scroll > lastScroll ? scrollDown : scrollUp);\n lastScroll = Math.max(0, scroll);\n }\n ticking = false;\n }, [axis, threshold, scrollDown, scrollUp]);\n\n useEffect(() => {\n /** Function to handle onScroll event */\n const onScroll = () => {\n if (!ticking) {\n window.requestAnimationFrame(updateScrollDir);\n ticking = true;\n }\n };\n\n window.addEventListener(\"scroll\", onScroll);\n\n return () => window.removeEventListener(\"scroll\", onScroll);\n }, [updateScrollDir]);\n\n return scrollDir;\n}\n\nexport default useDetectScroll;\n"],"names":["useState","useCallback","useEffect"],"mappings":";;;;AAEA;AACA,IAAK,IAGJ,CAAA;AAHD,CAAA,UAAK,IAAI,EAAA;AACP,IAAA,IAAA,CAAA,GAAA,CAAA,GAAA,GAAO,CAAA;AACP,IAAA,IAAA,CAAA,GAAA,CAAA,GAAA,GAAO,CAAA;AACT,CAAC,EAHI,IAAI,KAAJ,IAAI,GAGR,EAAA,CAAA,CAAA,CAAA;AAED;AACA,IAAK,SAMJ,CAAA;AAND,CAAA,UAAK,SAAS,EAAA;AACZ,IAAA,SAAA,CAAA,IAAA,CAAA,GAAA,IAAS,CAAA;AACT,IAAA,SAAA,CAAA,MAAA,CAAA,GAAA,MAAa,CAAA;AACb,IAAA,SAAA,CAAA,MAAA,CAAA,GAAA,MAAa,CAAA;AACb,IAAA,SAAA,CAAA,OAAA,CAAA,GAAA,OAAe,CAAA;AACf,IAAA,SAAA,CAAA,OAAA,CAAA,GAAA,OAAe,CAAA;AACjB,CAAC,EANI,SAAS,KAAT,SAAS,GAMb,EAAA,CA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AAE,QAAQ,CAAC,CAAC,CAAC;IAE5CC,eAAS,CAAC,MAAK;;QAEb,MAAM,QAAQ,GAAG,MAAK;YACpB,IAAI,CAAC,OAAO,EAAE;AACZ,gBAAA,MAAM,CAAC,qBAAqB,CAAC,eAAe,CAAC,CAAC;gBAC9C,OAAO,GAAG,IAAI,CAAC;AAChB,aAAA;AACH,SAAC,CAAC;AAEF,QAAA,MAAM,CAAC,gBAAgB,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;QAE5C,OAAO,MAAM,MAAM,CAAC,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;AAC9D,KAAC,EAAE,CAAC,eAAe,CAAC,CAAC,CAAC;AAEtB,IAAA,OAAO,SAAS,CAAC;AACnB;;;;"}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { useState, useCallback, useEffect } from 'react';
|
|
2
2
|
|
|
3
|
+
/** Enumeration for axis values */
|
|
3
4
|
var Axis;
|
|
4
5
|
(function (Axis) {
|
|
5
6
|
Axis["X"] = "x";
|
|
6
7
|
Axis["Y"] = "y";
|
|
7
8
|
})(Axis || (Axis = {}));
|
|
9
|
+
/** Enumeration for direction values */
|
|
8
10
|
var Direction;
|
|
9
11
|
(function (Direction) {
|
|
10
12
|
Direction["Up"] = "up";
|
|
@@ -13,12 +15,48 @@ var Direction;
|
|
|
13
15
|
Direction["Right"] = "right";
|
|
14
16
|
Direction["Still"] = "still";
|
|
15
17
|
})(Direction || (Direction = {}));
|
|
18
|
+
/**
|
|
19
|
+
* useDetectScroll hook.
|
|
20
|
+
*
|
|
21
|
+
* This hook provides a mechanism to detect the scroll direction.
|
|
22
|
+
* It will return the scroll direction as a string (up, down, left, right, or still) based on user scrolling.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
*
|
|
26
|
+
* import useDetectScroll, { Axis, Direction } from './useDetectScroll';
|
|
27
|
+
*
|
|
28
|
+
* function App() {
|
|
29
|
+
* const scrollDirection = useDetectScroll({
|
|
30
|
+
* thr: 100,
|
|
31
|
+
* axis: Axis.Y,
|
|
32
|
+
* scrollUp: Direction.Up,
|
|
33
|
+
* scrollDown: Direction.Down,
|
|
34
|
+
* still: Direction.Still
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* return (
|
|
38
|
+
* <div>
|
|
39
|
+
* <p>Current scroll direction: {scrollDirection}</p>
|
|
40
|
+
* </div>
|
|
41
|
+
* );
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* @param {ScrollProps} props - The properties related to scrolling.
|
|
45
|
+
* @property {number} props.thr - The threshold value which the scroll difference must exceed to update scroll direction.
|
|
46
|
+
* @property {Axis} props.axis - The axis along which to detect scroll. Can be 'x' or 'y'.
|
|
47
|
+
* @property {Direction} props.scrollUp - The direction to set when scrolling up or left. By default, 'up' for y-axis and 'left' for x-axis.
|
|
48
|
+
* @property {Direction} props.scrollDown - The direction to set when scrolling down or right. By default, 'down' for y-axis and 'right' for x-axis.
|
|
49
|
+
* @property {Direction} props.still - The direction to set when there is no scrolling. By default, 'still'.
|
|
50
|
+
*
|
|
51
|
+
* @returns {Direction} - The current direction of scrolling.
|
|
52
|
+
*/
|
|
16
53
|
function useDetectScroll(props) {
|
|
17
54
|
const { thr = 0, axis = Axis.Y, scrollUp = axis === Axis.Y ? Direction.Up : Direction.Left, scrollDown = axis === Axis.Y ? Direction.Down : Direction.Right, still = Direction.Still, } = props;
|
|
18
55
|
const [scrollDir, setScrollDir] = useState(still);
|
|
19
56
|
const threshold = Math.max(0, thr);
|
|
20
57
|
let ticking = false;
|
|
21
58
|
let lastScroll = axis === Axis.Y ? window.scrollY : window.scrollX;
|
|
59
|
+
/** Function to update scroll direction */
|
|
22
60
|
const updateScrollDir = useCallback(() => {
|
|
23
61
|
const scroll = axis === Axis.Y ? window.scrollY : window.scrollX;
|
|
24
62
|
if (Math.abs(scroll - lastScroll) >= threshold) {
|
|
@@ -28,6 +66,7 @@ function useDetectScroll(props) {
|
|
|
28
66
|
ticking = false;
|
|
29
67
|
}, [axis, threshold, scrollDown, scrollUp]);
|
|
30
68
|
useEffect(() => {
|
|
69
|
+
/** Function to handle onScroll event */
|
|
31
70
|
const onScroll = () => {
|
|
32
71
|
if (!ticking) {
|
|
33
72
|
window.requestAnimationFrame(updateScrollDir);
|
|
@@ -37,7 +76,7 @@ function useDetectScroll(props) {
|
|
|
37
76
|
window.addEventListener("scroll", onScroll);
|
|
38
77
|
return () => window.removeEventListener("scroll", onScroll);
|
|
39
78
|
}, [updateScrollDir]);
|
|
40
|
-
return
|
|
79
|
+
return scrollDir;
|
|
41
80
|
}
|
|
42
81
|
|
|
43
82
|
export { useDetectScroll };
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/hooks/useDetectScroll.ts"],"sourcesContent":["import { useState, useEffect, useCallback } from \"react\";\n\nenum Axis {\n X = \"x\",\n Y = \"y\",\n}\n\nenum Direction {\n Up = \"up\",\n Down = \"down\",\n Left = \"left\",\n Right = \"right\",\n Still = \"still\",\n}\n\ntype ScrollProps = {\n thr?: number;\n axis?: Axis;\n scrollUp?:
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/hooks/useDetectScroll.ts"],"sourcesContent":["import { useState, useEffect, useCallback } from \"react\";\n\n/** Enumeration for axis values */\nenum Axis {\n X = \"x\",\n Y = \"y\",\n}\n\n/** Enumeration for direction values */\nenum Direction {\n Up = \"up\",\n Down = \"down\",\n Left = \"left\",\n Right = \"right\",\n Still = \"still\",\n}\n\n/** Type declaration for scroll properties */\ntype ScrollProps = {\n thr?: number;\n axis?: Axis;\n scrollUp?: Direction;\n scrollDown?: Direction;\n still?: Direction;\n};\n\n/**\n * useDetectScroll hook.\n *\n * This hook provides a mechanism to detect the scroll direction.\n * It will return the scroll direction as a string (up, down, left, right, or still) based on user scrolling.\n *\n * @example\n *\n * import useDetectScroll, { Axis, Direction } from './useDetectScroll';\n *\n * function App() {\n * const scrollDirection = useDetectScroll({\n * thr: 100,\n * axis: Axis.Y,\n * scrollUp: Direction.Up,\n * scrollDown: Direction.Down,\n * still: Direction.Still\n * });\n *\n * return (\n * <div>\n * <p>Current scroll direction: {scrollDirection}</p>\n * </div>\n * );\n * }\n *\n * @param {ScrollProps} props - The properties related to scrolling.\n * @property {number} props.thr - The threshold value which the scroll difference must exceed to update scroll direction.\n * @property {Axis} props.axis - The axis along which to detect scroll. Can be 'x' or 'y'.\n * @property {Direction} props.scrollUp - The direction to set when scrolling up or left. By default, 'up' for y-axis and 'left' for x-axis.\n * @property {Direction} props.scrollDown - The direction to set when scrolling down or right. By default, 'down' for y-axis and 'right' for x-axis.\n * @property {Direction} props.still - The direction to set when there is no scrolling. By default, 'still'.\n *\n * @returns {Direction} - The current direction of scrolling.\n */\nfunction useDetectScroll(props: ScrollProps): Direction {\n const {\n thr = 0,\n axis = Axis.Y,\n scrollUp = axis === Axis.Y ? Direction.Up : Direction.Left,\n scrollDown = axis === Axis.Y ? Direction.Down : Direction.Right,\n still = Direction.Still,\n } = props;\n\n const [scrollDir, setScrollDir] = useState<Direction>(still);\n\n const threshold = Math.max(0, thr);\n let ticking = false;\n let lastScroll: number = axis === Axis.Y ? window.scrollY : window.scrollX;\n\n /** Function to update scroll direction */\n const updateScrollDir = useCallback(() => {\n const scroll = axis === Axis.Y ? window.scrollY : window.scrollX;\n\n if (Math.abs(scroll - lastScroll) >= threshold) {\n setScrollDir(scroll > lastScroll ? scrollDown : scrollUp);\n lastScroll = Math.max(0, scroll);\n }\n ticking = false;\n }, [axis, threshold, scrollDown, scrollUp]);\n\n useEffect(() => {\n /** Function to handle onScroll event */\n const onScroll = () => {\n if (!ticking) {\n window.requestAnimationFrame(updateScrollDir);\n ticking = true;\n }\n };\n\n window.addEventListener(\"scroll\", onScroll);\n\n return () => window.removeEventListener(\"scroll\", onScroll);\n }, [updateScrollDir]);\n\n return scrollDir;\n}\n\nexport default useDetect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ript",
|
|
38
39
|
"detect scroll in react",
|
|
39
40
|
"react scroll",
|
|
40
41
|
"scroll direction"
|
|
@@ -56,5 +57,5 @@
|
|
|
56
57
|
"lint:fix": "eslint src/**/*.ts --fix"
|
|
57
58
|
},
|
|
58
59
|
"type": "module",
|
|
59
|
-
"version": "
|
|
60
|
+
"version": "2.0.0"
|
|
60
61
|
}
|