@smakss/react-scroll-direction 4.1.0 → 4.2.0-beta.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 CHANGED
@@ -43,6 +43,7 @@ import useDetectScroll, {
43
43
 
44
44
  The `useDetectScroll` hook takes an options object with the following properties:
45
45
 
46
+ - `target`: The target scrollable element from which to detect scroll direction and position (default: `window`, must be an `HTMLDivElement`).
46
47
  - `thr`: Threshold for scroll direction change detection (default: `0`, accepts only positive values).
47
48
  - `axis`: Defines the scroll axis (`"y"` or `"x"`, default: `"y"`).
48
49
  - `scrollUp`: Value returned when scrolling up (y-axis) or left (x-axis) (default: `"up"` for y-axis, `"left"` for x-axis).
@@ -74,6 +75,21 @@ const { scrollDir, scrollPosition } = useDetectScroll({ axis: Axis.X });
74
75
  // scrollPosition: { top, bottom, left, right }
75
76
  ```
76
77
 
78
+ To use a custom scrollable element as a target rather than the default window:
79
+
80
+ ```js
81
+ const customElementRef = useRef<HTMLDivElement>(null);
82
+ const [customElement, setCustomElement] = useState<HTMLDivElement>();
83
+
84
+ const scrollDir = useDetectScroll({target: customElement});
85
+
86
+ useEffect(() => {
87
+ if(customElementRef.current) {
88
+ setHomepageElement(customElementRef.current);
89
+ }
90
+ }, [customElementRef])
91
+ ```
92
+
77
93
  ## Contributing
78
94
 
79
95
  Interested in making contributions to this project? Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines and details.
package/dist/index.d.ts CHANGED
@@ -1,89 +1,4 @@
1
- /** Enumeration for axis values */
2
- export declare enum Axis {
3
- /**
4
- * The x-axis represents the horizontal direction.
5
- */
6
- X = "x",
7
- /**
8
- * The y-axis represents the vertical direction.
9
- */
10
- Y = "y"
11
- }
12
- /** Enumeration for direction values */
13
- export declare enum Direction {
14
- /**
15
- * The up direction represents the scroll direction moving towards the top.
16
- */
17
- Up = "up",
18
- /**
19
- * The down direction represents the scroll direction moving towards the bottom.
20
- */
21
- Down = "down",
22
- /**
23
- * The left direction represents the scroll direction moving towards the left.
24
- */
25
- Left = "left",
26
- /**
27
- * The right direction represents the scroll direction moving towards the right.
28
- */
29
- Right = "right",
30
- /**
31
- * The still direction represents the scroll direction when the user is not scrolling.
32
- */
33
- Still = "still"
34
- }
35
- type ScrollPosition = {
36
- /**
37
- * The top position represents the distance from the top edge of the page.
38
- */
39
- top: number;
40
- /**
41
- * The bottom position represents the distance from the bottom edge of the page.
42
- */
43
- bottom: number;
44
- /**
45
- * The left position represents the distance from the left edge of the page.
46
- */
47
- left: number;
48
- /**
49
- * The right position represents the distance from the right edge of the page.
50
- */
51
- right: number;
52
- };
53
- /** Type declaration for the returned scroll information */
54
- type ScrollInfo = {
55
- /**
56
- * The scrollDir represents the current scroll direction.
57
- */
58
- scrollDir: Direction;
59
- /**
60
- * The scrollPosition represents the current scroll position.
61
- */
62
- scrollPosition: ScrollPosition;
63
- };
64
- /** Type declaration for scroll properties */
65
- type ScrollProps = {
66
- /**
67
- * The thr represents the threshold value for scroll detection.
68
- */
69
- thr?: number;
70
- /**
71
- * The axis represents the scroll axis (x or y).
72
- */
73
- axis?: Axis;
74
- /**
75
- * The scrollUp represents the scroll direction when moving up.
76
- */
77
- scrollUp?: Direction;
78
- /**
79
- * The scrollDown represents the scroll direction when moving down.
80
- */
81
- scrollDown?: Direction;
82
- /**
83
- * The still represents the scroll direction when the user is not scrolling.
84
- */
85
- still?: Direction;
86
- };
1
+ import { Axis, Direction, ScrollInfo, ScrollProps } from './types';
87
2
  /**
88
3
  * useDetectScroll hook.
89
4
  *
@@ -96,7 +11,11 @@ type ScrollProps = {
96
11
  * import useDetectScroll, { Axis, Direction } from '@smakss/react-scroll-direction';
97
12
  *
98
13
  * function App() {
14
+ * const customElementRef = useRef<HTMLDivElement>(null);
15
+ * const [customElement, setCustomElement] = useState<HTMLDivElement>();
16
+ *
99
17
  * const { scrollDir, scrollPosition } = useDetectScroll({
18
+ * target: customElement,
100
19
  * thr: 100,
101
20
  * axis: Axis.Y,
102
21
  * scrollUp: Direction.Up,
@@ -104,6 +23,12 @@ type ScrollProps = {
104
23
  * still: Direction.Still
105
24
  * });
106
25
  *
26
+ * useEffect(() => {
27
+ * if (customElementRef.current) {
28
+ * setCustomElement(customElementRef.current);
29
+ * }
30
+ * }, [customElementRef]);
31
+ *
107
32
  * return (
108
33
  * <div>
109
34
  * <p>Current scroll direction: {scrollDir}</p>
@@ -117,4 +42,5 @@ type ScrollProps = {
117
42
  * @returns {ScrollInfo} - The current direction and position of scrolling.
118
43
  */
119
44
  declare function useDetectScroll(props?: ScrollProps): ScrollInfo;
45
+ export { Axis, Direction };
120
46
  export default useDetectScroll;
package/dist/index.js CHANGED
@@ -36,6 +36,7 @@ var Direction;
36
36
  */
37
37
  Direction["Still"] = "still";
38
38
  })(Direction || (Direction = {}));
39
+
39
40
  /**
40
41
  * useDetectScroll hook.
41
42
  *
@@ -48,7 +49,11 @@ var Direction;
48
49
  * import useDetectScroll, { Axis, Direction } from '@smakss/react-scroll-direction';
49
50
  *
50
51
  * function App() {
52
+ * const customElementRef = useRef<HTMLDivElement>(null);
53
+ * const [customElement, setCustomElement] = useState<HTMLDivElement>();
54
+ *
51
55
  * const { scrollDir, scrollPosition } = useDetectScroll({
56
+ * target: customElement,
52
57
  * thr: 100,
53
58
  * axis: Axis.Y,
54
59
  * scrollUp: Direction.Up,
@@ -56,6 +61,12 @@ var Direction;
56
61
  * still: Direction.Still
57
62
  * });
58
63
  *
64
+ * useEffect(() => {
65
+ * if (customElementRef.current) {
66
+ * setCustomElement(customElementRef.current);
67
+ * }
68
+ * }, [customElementRef]);
69
+ *
59
70
  * return (
60
71
  * <div>
61
72
  * <p>Current scroll direction: {scrollDir}</p>
@@ -69,7 +80,7 @@ var Direction;
69
80
  * @returns {ScrollInfo} - The current direction and position of scrolling.
70
81
  */
71
82
  function useDetectScroll(props = {}) {
72
- 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;
83
+ const { target = typeof window !== 'undefined' ? window : undefined, 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;
73
84
  const [scrollDir, setScrollDir] = useState(still);
74
85
  const [scrollPosition, setScrollPosition] = useState({
75
86
  top: 0,
@@ -82,41 +93,69 @@ function useDetectScroll(props = {}) {
82
93
  const lastScroll = useRef(0);
83
94
  /** Function to update scroll direction */
84
95
  const updateScrollDir = useCallback(() => {
85
- const scroll = axis === Axis.Y ? window.scrollY : window.scrollX;
96
+ if (!target)
97
+ return;
98
+ let scroll;
99
+ if (target instanceof Window) {
100
+ scroll = axis === Axis.Y ? target.scrollY : target.scrollX;
101
+ }
102
+ else {
103
+ scroll = axis === Axis.Y ? target.scrollTop : target.scrollLeft;
104
+ }
86
105
  if (Math.abs(scroll - lastScroll.current) >= threshold) {
87
106
  setScrollDir(scroll > lastScroll.current ? scrollDown : scrollUp);
88
107
  lastScroll.current = Math.max(0, scroll);
89
108
  }
90
109
  ticking.current = false;
91
- }, [axis, threshold, scrollDown, scrollUp]);
110
+ }, [target, axis, threshold, scrollDown, scrollUp]);
92
111
  useEffect(() => {
112
+ if (!target) {
113
+ console.warn('useDetectScroll: target is not set. Falling back to window.');
114
+ return;
115
+ }
93
116
  /** Function to update scroll position */
94
117
  const updateScrollPosition = () => {
95
- const top = window.scrollY;
96
- const left = window.scrollX;
97
- const bottom = document.documentElement.scrollHeight - window.innerHeight - top;
98
- const right = document.documentElement.scrollWidth - window.innerWidth - left;
118
+ if (!target)
119
+ return;
120
+ const top = target instanceof Window ? target.scrollY : target.scrollTop;
121
+ const left = target instanceof Window ? target.scrollX : target.scrollLeft;
122
+ const bottom = (target instanceof Window
123
+ ? document.documentElement.scrollHeight - target.innerHeight
124
+ : target.scrollHeight - target.clientHeight) - top;
125
+ const right = (target instanceof Window
126
+ ? document.documentElement.scrollWidth - target.innerWidth
127
+ : target.scrollWidth - target.clientWidth) - left;
99
128
  setScrollPosition({ top, bottom, left, right });
100
129
  };
101
- /** Call the update function when the component mounts */
102
130
  updateScrollPosition();
103
- window.addEventListener('scroll', updateScrollPosition);
131
+ const targetElement = target;
132
+ targetElement.addEventListener('scroll', updateScrollPosition);
104
133
  return () => {
105
- window.removeEventListener('scroll', updateScrollPosition);
134
+ targetElement.removeEventListener('scroll', updateScrollPosition);
106
135
  };
107
- }, []);
136
+ }, [target]);
108
137
  useEffect(() => {
109
- lastScroll.current = axis === Axis.Y ? window.scrollY : window.scrollX;
110
- /** Function to handle onScroll event */
138
+ if (!target) {
139
+ console.warn('useDetectScroll: target is not set. Falling back to window.');
140
+ return;
141
+ }
142
+ if (target instanceof Window) {
143
+ lastScroll.current = axis === Axis.Y ? target.scrollY : target.scrollX;
144
+ }
145
+ else {
146
+ lastScroll.current =
147
+ axis === Axis.Y ? target.scrollTop : target.scrollLeft;
148
+ }
111
149
  const onScroll = () => {
112
150
  if (!ticking.current) {
113
151
  window.requestAnimationFrame(updateScrollDir);
114
152
  ticking.current = true;
115
153
  }
116
154
  };
117
- window.addEventListener('scroll', onScroll);
118
- return () => window.removeEventListener('scroll', onScroll);
119
- }, [axis, updateScrollDir]);
155
+ const targetElement = target;
156
+ targetElement.addEventListener('scroll', onScroll);
157
+ return () => targetElement.removeEventListener('scroll', onScroll);
158
+ }, [target, axis, updateScrollDir]);
120
159
  return { scrollDir, scrollPosition };
121
160
  }
122
161
 
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import { useState, useEffect, useCallback, useRef } from 'react';\n\n/** Enumeration for axis values */\nexport enum Axis {\n /**\n * The x-axis represents the horizontal direction.\n */\n X = 'x',\n /**\n * The y-axis represents the vertical direction.\n */\n Y = 'y'\n}\n\n/** Enumeration for direction values */\nexport enum Direction {\n /**\n * The up direction represents the scroll direction moving towards the top.\n */\n Up = 'up',\n /**\n * The down direction represents the scroll direction moving towards the bottom.\n */\n Down = 'down',\n /**\n * The left direction represents the scroll direction moving towards the left.\n */\n Left = 'left',\n /**\n * The right direction represents the scroll direction moving towards the right.\n */\n Right = 'right',\n /**\n * The still direction represents the scroll direction when the user is not scrolling.\n */\n Still = 'still'\n}\n\ntype ScrollPosition = {\n /**\n * The top position represents the distance from the top edge of the page.\n */\n top: number;\n /**\n * The bottom position represents the distance from the bottom edge of the page.\n */\n bottom: number;\n /**\n * The left position represents the distance from the left edge of the page.\n */\n left: number;\n /**\n * The right position represents the distance from the right edge of the page.\n */\n right: number;\n};\n\n/** Type declaration for the returned scroll information */\ntype ScrollInfo = {\n /**\n * The scrollDir represents the current scroll direction.\n */\n scrollDir: Direction;\n /**\n * The scrollPosition represents the current scroll position.\n */\n scrollPosition: ScrollPosition;\n};\n\n/** Type declaration for scroll properties */\ntype ScrollProps = {\n /**\n * The thr represents the threshold value for scroll detection.\n */\n thr?: number;\n /**\n * The axis represents the scroll axis (x or y).\n */\n axis?: Axis;\n /**\n * The scrollUp represents the scroll direction when moving up.\n */\n scrollUp?: Direction;\n /**\n * The scrollDown represents the scroll direction when moving down.\n */\n scrollDown?: Direction;\n /**\n * The still represents the scroll direction when the user is not scrolling.\n */\n still?: Direction;\n};\n\n/**\n * useDetectScroll hook.\n *\n * This hook provides a mechanism to detect the scroll direction and position.\n * It will return the scroll direction as a string (up, down, left, right, or still) based on user scrolling,\n * as well as the scroll position from the top, bottom, left, and right edges of the page.\n *\n * @example\n *\n * import useDetectScroll, { Axis, Direction } from '@smakss/react-scroll-direction';\n *\n * function App() {\n * const { scrollDir, scrollPosition } = 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: {scrollDir}</p>\n * <p>Scroll position - Top: {scrollPosition.top}, Bottom: {scrollPosition.bottom},\n * Left: {scrollPosition.left}, Right: {scrollPosition.right}</p>\n * </div>\n * );\n * }\n *\n * @param {ScrollProps} props - The properties related to scrolling.\n * @returns {ScrollInfo} - The current direction and position of scrolling.\n */\nfunction useDetectScroll(props: ScrollProps = {}): ScrollInfo {\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 const [scrollPosition, setScrollPosition] = useState<ScrollPosition>({\n top: 0,\n bottom: 0,\n left: 0,\n right: 0\n });\n\n const threshold = Math.max(0, thr);\n const ticking = useRef(false);\n const lastScroll = useRef(0);\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.current) >= threshold) {\n setScrollDir(scroll > lastScroll.current ? scrollDown : scrollUp);\n lastScroll.current = Math.max(0, scroll);\n }\n ticking.current = false;\n }, [axis, threshold, scrollDown, scrollUp]);\n\n useEffect(() => {\n /** Function to update scroll position */\n const updateScrollPosition = () => {\n const top = window.scrollY;\n const left = window.scrollX;\n const bottom =\n document.documentElement.scrollHeight - window.innerHeight - top;\n const right =\n document.documentElement.scrollWidth - window.innerWidth - left;\n\n setScrollPosition({ top, bottom, left, right });\n };\n\n /** Call the update function when the component mounts */\n updateScrollPosition();\n\n window.addEventListener('scroll', updateScrollPosition);\n\n return () => {\n window.removeEventListener('scroll', updateScrollPosition);\n };\n }, []);\n\n useEffect(() => {\n lastScroll.current = axis === Axis.Y ? window.scrollY : window.scrollX;\n\n /** Function to handle onScroll event */\n const onScroll = () => {\n if (!ticking.current) {\n window.requestAnimationFrame(updateScrollDir);\n ticking.current = true;\n }\n };\n\n window.addEventListener('scroll', onScroll);\n\n return () => window.removeEventListener('scroll', onScroll);\n }, [axis, updateScrollDir]);\n\n return { scrollDir, scrollPos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,GAAG,GAAG,CAAC;AACnE,YAAA,MAAM,KAAK,GACT,QAAQ,CAAC,eAAe,CAAC,WAAW,GAAG,MAAM,CAAC,UAAU,GAAG,IAAI,CAAC;YAElE,iBAAiB,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;AAClD,SAAC,CAAC;;AAGF,QAAA,oBAAoB,EAAE,CAAC;AAEvB,QAAA,MAAM,CAAC,gBAAgB,CAAC,QAAQ,EAAE,oBAAoB,CAAC,CAAC;AAExD,QAAA,OAAO,MAAK;AACV,YAAA,MAAM,CAAC,mBAAmB,CAAC,QAAQ,EAAE,oBAAoB,CAAC,CAAC;AAC7D,SAAC,CAAC;KACH,EAAE,EAAE,CAAC,CAAC;IAEP,SAAS,CAAC,MAAK;QACb,UAAU,CAAC,OAAO,GAAG,IAAI,KAAK,IAAI,CAAC,CAAC,GAAG,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;;QAGvE,MAAM,QAAQ,GAAG,MAAK;AACpB,YAAA,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE;AACpB,gBAAA,MAAM,CAAC,qBAAqB,CAAC,eAAe,CAAC,CAAC;AAC9C,gBAAA,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;aACxB;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,IAAI,EAAE,eAAe,CAAC,CAAC,CAAC;AAE5B,IAAA,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,CAAC;AACvC;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../src/types.ts","../src/index.ts"],"sourcesContent":["/** Enumeration for axis values */\nexport enum Axis {\n /**\n * The x-axis represents the horizontal direction.\n */\n X = 'x',\n /**\n * The y-axis represents the vertical direction.\n */\n Y = 'y'\n}\n\n/** Enumeration for direction values */\nexport enum Direction {\n /**\n * The up direction represents the scroll direction moving towards the top.\n */\n Up = 'up',\n /**\n * The down direction represents the scroll direction moving towards the bottom.\n */\n Down = 'down',\n /**\n * The left direction represents the scroll direction moving towards the left.\n */\n Left = 'left',\n /**\n * The right direction represents the scroll direction moving towards the right.\n */\n Right = 'right',\n /**\n * The still direction represents the scroll direction when the user is not scrolling.\n */\n Still = 'still'\n}\n\nexport type ScrollPosition = {\n /**\n * The top position represents the distance from the top edge of the page.\n */\n top: number;\n /**\n * The bottom position represents the distance from the bottom edge of the page.\n */\n bottom: number;\n /**\n * The left position represents the distance from the left edge of the page.\n */\n left: number;\n /**\n * The right position represents the distance from the right edge of the page.\n */\n right: number;\n};\n\n/** Type declaration for the returned scroll information */\nexport type ScrollInfo = {\n /**\n * The scrollDir represents the current scroll direction.\n */\n scrollDir: Direction;\n /**\n * The scrollPosition represents the current scroll position.\n */\n scrollPosition: ScrollPosition;\n};\n\n/** Type declaration for scroll properties */\nexport type ScrollProps = {\n /**\n * The target represents the scrollable element to check for scroll detection.\n */\n target?: HTMLDivElement | Window;\n /**\n * The thr represents the threshold value for scroll detection.\n */\n thr?: number;\n /**\n * The axis represents the scroll axis (x or y).\n */\n axis?: Axis;\n /**\n * The scrollUp represents the scroll direction when moving up.\n */\n scrollUp?: Direction;\n /**\n * The scrollDown represents the scroll direction when moving down.\n */\n scrollDown?: Direction;\n /**\n * The still represents the scroll direction when the user is not scrolling.\n */\n still?: Direction;\n};\n","import { useState, useEffect, useCallback, useRef } from 'react';\nimport {\n Axis,\n Direction,\n ScrollInfo,\n ScrollPosition,\n ScrollProps\n} from './types';\n\n/**\n * useDetectScroll hook.\n *\n * This hook provides a mechanism to detect the scroll direction and position.\n * It will return the scroll direction as a string (up, down, left, right, or still) based on user scrolling,\n * as well as the scroll position from the top, bottom, left, and right edges of the page.\n *\n * @example\n *\n * import useDetectScroll, { Axis, Direction } from '@smakss/react-scroll-direction';\n *\n * function App() {\n * const customElementRef = useRef<HTMLDivElement>(null);\n * const [customElement, setCustomElement] = useState<HTMLDivElement>();\n *\n * const { scrollDir, scrollPosition } = useDetectScroll({\n * target: customElement,\n * thr: 100,\n * axis: Axis.Y,\n * scrollUp: Direction.Up,\n * scrollDown: Direction.Down,\n * still: Direction.Still\n * });\n *\n * useEffect(() => {\n * if (customElementRef.current) {\n * setCustomElement(customElementRef.current);\n * }\n * }, [customElementRef]);\n *\n * return (\n * <div>\n * <p>Current scroll direction: {scrollDir}</p>\n * <p>Scroll position - Top: {scrollPosition.top}, Bottom: {scrollPosition.bottom},\n * Left: {scrollPosition.left}, Right: {scrollPosition.right}</p>\n * </div>\n * );\n * }\n *\n * @param {ScrollProps} props - The properties related to scrolling.\n * @returns {ScrollInfo} - The current direction and position of scrolling.\n */\nfunction useDetectScroll(props: ScrollProps = {}): ScrollInfo {\n const {\n target = typeof window !== 'undefined' ? window : undefined,\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 const [scrollPosition, setScrollPosition] = useState<ScrollPosition>({\n top: 0,\n bottom: 0,\n left: 0,\n right: 0\n });\n\n const threshold = Math.max(0, thr);\n const ticking = useRef(false);\n const lastScroll = useRef(0);\n\n /** Function to update scroll direction */\n const updateScrollDir = useCallback(() => {\n if (!target) return;\n\n let scroll: number;\n if (target instanceof Window) {\n scroll = axis === Axis.Y ? target.scrollY : target.scrollX;\n } else {\n scroll = axis === Axis.Y ? target.scrollTop : target.scrollLeft;\n }\n\n if (Math.abs(scroll - lastScroll.current) >= threshold) {\n setScrollDir(scroll > lastScroll.current ? scrollDown : scrollUp);\n lastScroll.current = Math.max(0, scroll);\n }\n ticking.current = false;\n }, [target, axis, threshold, scrollDown, scrollUp]);\n\n useEffect(() => {\n if (!target) {\n console.warn(\n 'useDetectScroll: target is not set. Falling back to window.'\n );\n return;\n }\n\n /** Function to update scroll position */\n const updateScrollPosition = () => {\n if (!target) return;\n\n const top = target instanceof Window ? target.scrollY : target.scrollTop;\n const left =\n target instanceof Window ? target.scrollX : target.scrollLeft;\n\n const bottom =\n (target instanceof Window\n ? document.documentElement.scrollHeight - target.innerHeight\n : target.scrollHeight - target.clientHeight) - top;\n const right =\n (target instanceof Window\n ? document.documentElement.scrollWidth - target.innerWidth\n : target.scrollWidth - target.clientWidth) - left;\n\n setScrollPosition({ top, bottom, left, right });\n };\n\n updateScrollPosition();\n\n const targetElement = target as EventTarget;\n targetElement.addEventListener('scroll', updateScrollPosition);\n\n return () => {\n targetElement.removeEventListener('scroll', updateScrollPosition);\n };\n }, [target]);\n\n useEffect(() => {\n if (!target) {\n console.warn(\n 'useDetectScroll: target is not set. Falling back to window.'\n );\n return;\n }\n\n if (target instanceof Window) {\n lastScroll.current = axis === Axis.Y ? target.scrollY : target.scrollX;\n } else {\n lastScroll.current =\n axis === Axis.Y ? target.scrollTop : target.scrollLeft;\n }\n\n const onScroll = () => {\n if (!ticking.current) {\n window.requestAnimationFrame(updateScrollDir);\n ticking.current = true;\n }\n };\n\n const targetElement = target as EventTarget;\n targetElement.addEventListener('scroll', onScroll);\n\n return () => targetElement.removeEventListener('scroll', onScroll);\n }, [target, axis, updateScrollDir]);\n\n return { scrollDir, scrollPosition };\n}\n\nexport { Axis, Direction };\nexport default useDetectScroll;\n"],"names":[],"mappings":";;AAAA;IACY,KASX;AATD,CAAA,UAAY,IAAI,EAAA;AACd;;AAEG;AACH,IAAA,IAAA,CAAA,GAAA,CAAA,GAAA,GAAO,CAAA;AACP;;AAEG;AACH,IAAA,IAAA,CAAA,GAAA,CAAA,GAAA,GAAO,CAAA;AACT,CAAC,EATW,IAAI,KAAJ,IAAI,GASf,EAAA,CAAA,CAAA,CAAA;AAED;IACY,UAqBX;AArBD,CAAA,UAAY,SAAS,E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aAAM;AACL,YAAA,UAAU,CAAC,OAAO;AAChB,gBAAA,IAAI,KAAK,IAAI,CAAC,CAAC,GAAG,MAAM,CAAC,SAAS,GAAG,MAAM,CAAC,UAAU,CAAC;SAC1D;QAED,MAAM,QAAQ,GAAG,MAAK;AACpB,YAAA,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE;AACpB,gBAAA,MAAM,CAAC,qBAAqB,CAAC,eAAe,CAAC,CAAC;AAC9C,gBAAA,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;aACxB;AACH,SAAC,CAAC;QAEF,MAAM,aAAa,GAAG,MAAqB,CAAC;AAC5C,QAAA,aAAa,CAAC,gBAAgB,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;QAEnD,OAAO,MAAM,aAAa,CAAC,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;KACpE,EAAE,CAAC,MAAM,EAAE,IAAI,EAAE,eAAe,CAAC,CAAC,CAAC;AAEpC,IAAA,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,CAAC;AACvC;;;;"}
@@ -0,0 +1,90 @@
1
+ /** Enumeration for axis values */
2
+ export declare enum Axis {
3
+ /**
4
+ * The x-axis represents the horizontal direction.
5
+ */
6
+ X = "x",
7
+ /**
8
+ * The y-axis represents the vertical direction.
9
+ */
10
+ Y = "y"
11
+ }
12
+ /** Enumeration for direction values */
13
+ export declare enum Direction {
14
+ /**
15
+ * The up direction represents the scroll direction moving towards the top.
16
+ */
17
+ Up = "up",
18
+ /**
19
+ * The down direction represents the scroll direction moving towards the bottom.
20
+ */
21
+ Down = "down",
22
+ /**
23
+ * The left direction represents the scroll direction moving towards the left.
24
+ */
25
+ Left = "left",
26
+ /**
27
+ * The right direction represents the scroll direction moving towards the right.
28
+ */
29
+ Right = "right",
30
+ /**
31
+ * The still direction represents the scroll direction when the user is not scrolling.
32
+ */
33
+ Still = "still"
34
+ }
35
+ export type ScrollPosition = {
36
+ /**
37
+ * The top position represents the distance from the top edge of the page.
38
+ */
39
+ top: number;
40
+ /**
41
+ * The bottom position represents the distance from the bottom edge of the page.
42
+ */
43
+ bottom: number;
44
+ /**
45
+ * The left position represents the distance from the left edge of the page.
46
+ */
47
+ left: number;
48
+ /**
49
+ * The right position represents the distance from the right edge of the page.
50
+ */
51
+ right: number;
52
+ };
53
+ /** Type declaration for the returned scroll information */
54
+ export type ScrollInfo = {
55
+ /**
56
+ * The scrollDir represents the current scroll direction.
57
+ */
58
+ scrollDir: Direction;
59
+ /**
60
+ * The scrollPosition represents the current scroll position.
61
+ */
62
+ scrollPosition: ScrollPosition;
63
+ };
64
+ /** Type declaration for scroll properties */
65
+ export type ScrollProps = {
66
+ /**
67
+ * The target represents the scrollable element to check for scroll detection.
68
+ */
69
+ target?: HTMLDivElement | Window;
70
+ /**
71
+ * The thr represents the threshold value for scroll detection.
72
+ */
73
+ thr?: number;
74
+ /**
75
+ * The axis represents the scroll axis (x or y).
76
+ */
77
+ axis?: Axis;
78
+ /**
79
+ * The scrollUp represents the scroll direction when moving up.
80
+ */
81
+ scrollUp?: Direction;
82
+ /**
83
+ * The scrollDown represents the scroll direction when moving down.
84
+ */
85
+ scrollDown?: Direction;
86
+ /**
87
+ * The still represents the scroll direction when the user is not scrolling.
88
+ */
89
+ still?: Direction;
90
+ };
package/package.json CHANGED
@@ -80,5 +80,5 @@
80
80
  },
81
81
  "type": "module",
82
82
  "types": "./dist/index.d.ts",
83
- "version": "4.1.0"
83
+ "version": "4.2.0-beta.0"
84
84
  }