@smakss/react-scroll-direction 4.2.0-beta.0 → 4.2.0-beta.1

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/index.d.ts CHANGED
@@ -1,46 +1,3 @@
1
- import { Axis, Direction, ScrollInfo, ScrollProps } from './types';
2
- /**
3
- * useDetectScroll hook.
4
- *
5
- * This hook provides a mechanism to detect the scroll direction and position.
6
- * It will return the scroll direction as a string (up, down, left, right, or still) based on user scrolling,
7
- * as well as the scroll position from the top, bottom, left, and right edges of the page.
8
- *
9
- * @example
10
- *
11
- * import useDetectScroll, { Axis, Direction } from '@smakss/react-scroll-direction';
12
- *
13
- * function App() {
14
- * const customElementRef = useRef<HTMLDivElement>(null);
15
- * const [customElement, setCustomElement] = useState<HTMLDivElement>();
16
- *
17
- * const { scrollDir, scrollPosition } = useDetectScroll({
18
- * target: customElement,
19
- * thr: 100,
20
- * axis: Axis.Y,
21
- * scrollUp: Direction.Up,
22
- * scrollDown: Direction.Down,
23
- * still: Direction.Still
24
- * });
25
- *
26
- * useEffect(() => {
27
- * if (customElementRef.current) {
28
- * setCustomElement(customElementRef.current);
29
- * }
30
- * }, [customElementRef]);
31
- *
32
- * return (
33
- * <div>
34
- * <p>Current scroll direction: {scrollDir}</p>
35
- * <p>Scroll position - Top: {scrollPosition.top}, Bottom: {scrollPosition.bottom},
36
- * Left: {scrollPosition.left}, Right: {scrollPosition.right}</p>
37
- * </div>
38
- * );
39
- * }
40
- *
41
- * @param {ScrollProps} props - The properties related to scrolling.
42
- * @returns {ScrollInfo} - The current direction and position of scrolling.
43
- */
44
- declare function useDetectScroll(props?: ScrollProps): ScrollInfo;
45
- export { Axis, Direction };
1
+ import useDetectScroll from './useDetectScroll';
46
2
  export default useDetectScroll;
3
+ export { Axis, Direction } from './types';
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { useState, useRef, useCallback, useEffect } from 'react';
2
2
 
3
- /** Enumeration for axis values */
3
+ // Enumeration for axis values
4
4
  var Axis;
5
5
  (function (Axis) {
6
6
  /**
@@ -12,7 +12,7 @@ var Axis;
12
12
  */
13
13
  Axis["Y"] = "y";
14
14
  })(Axis || (Axis = {}));
15
- /** Enumeration for direction values */
15
+ // Enumeration for direction values
16
16
  var Direction;
17
17
  (function (Direction) {
18
18
  /**
package/dist/index.js.map CHANGED
@@ -1 +1 @@
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,EAAA;AACnB;;AAEG;AACH,IAAA,SAAA,CAAA,IAAA,CAAA,GAAA,IAAS,CAAA;AACT;;AAEG;AACH,IAAA,SAAA,CAAA,MAAA,CAAA,GAAA,MAAa,CAAA;AACb;;AAEG;AACH,IAAA,SAAA,CAAA,MAAA,CAAA,GAAA,MAAa,CAAA;AACb;;AAEG;AACH,IAAA,SAAA,CAAA,OAAA,CAAA,GAAA,OAAe,CAAA;AACf;;AAEG;AACH,IAAA,SAAA,CAAA,OAAA,CAAA,GAAA,OAAe,CAAA;AACjB,CAAC,EArBW,SAAS,KAAT,SAAS,GAqBpB,EAAA,CAAA,CAAA;;ACzBD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCG;AACH,SAAS,eAAe,CAAC,KAAA,GAAqB,EAAE,EAAA;AAC9C,IAAA,MAAM,EACJ,MAAM,GAAG,OAAO,MAAM,KAAK,WAAW,GAAG,MAAM,GAAG,SAAS,EAC3D,GAAG,GAAG,CAAC,EACP,IAAI,GAAG,IAAI,CAAC,CAAC,EACb,QAAQ,GAAG,IAAI,KAAK,IAAI,CAAC,CAAC,GAAG,SAAS,CAAC,EAAE,GAAG,SAAS,CAAC,IAAI,EAC1D,UAAU,GAAG,IAAI,KAAK,IAAI,CAAC,CAAC,GAAG,SAAS,CAAC,IAAI,GAAG,SAAS,CAAC,KAAK,EAC/D,KAAK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E,oBAAoB,CAAC,CAAC;AACpE,SAAC,CAAC;AACJ,KAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;IAEb,SAAS,CAAC,MAAK;QACb,IAAI,CAAC,MAAM,EAAE;AACX,YAAA,OAAO,CAAC,IAAI,CACV,6DAA6D,CAC9D,CAAC;YACF,OAAO;SACR;AAED,QAAA,IAAI,MAAM,YAAY,MAAM,EAAE;YAC5B,UAAU,CAAC,OAAO,GAAG,IAAI,KAAK,IAAI,CAAC,CAAC,GAAG,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;SACxE;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;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../src/types.ts","../src/useDetectScroll.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\n// Type declaration for scroll position\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 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,EAAA;AACnB;;AAEG;AACH,IAAA,SAAA,CAAA,IAAA,CAAA,GAAA,IA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AAI,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;;;;"}
package/dist/types.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /** Enumeration for axis values */
2
1
  export declare enum Axis {
3
2
  /**
4
3
  * The x-axis represents the horizontal direction.
@@ -9,7 +8,6 @@ export declare enum Axis {
9
8
  */
10
9
  Y = "y"
11
10
  }
12
- /** Enumeration for direction values */
13
11
  export declare enum Direction {
14
12
  /**
15
13
  * The up direction represents the scroll direction moving towards the top.
@@ -50,7 +48,6 @@ export type ScrollPosition = {
50
48
  */
51
49
  right: number;
52
50
  };
53
- /** Type declaration for the returned scroll information */
54
51
  export type ScrollInfo = {
55
52
  /**
56
53
  * The scrollDir represents the current scroll direction.
@@ -61,7 +58,6 @@ export type ScrollInfo = {
61
58
  */
62
59
  scrollPosition: ScrollPosition;
63
60
  };
64
- /** Type declaration for scroll properties */
65
61
  export type ScrollProps = {
66
62
  /**
67
63
  * The target represents the scrollable element to check for scroll detection.
@@ -0,0 +1,45 @@
1
+ import { ScrollInfo, ScrollProps } from './types';
2
+ /**
3
+ * useDetectScroll hook.
4
+ *
5
+ * This hook provides a mechanism to detect the scroll direction and position.
6
+ * It will return the scroll direction as a string (up, down, left, right, or still) based on user scrolling,
7
+ * as well as the scroll position from the top, bottom, left, and right edges of the page.
8
+ *
9
+ * @example
10
+ *
11
+ * import useDetectScroll, { Axis, Direction } from '@smakss/react-scroll-direction';
12
+ *
13
+ * function App() {
14
+ * const customElementRef = useRef<HTMLDivElement>(null);
15
+ * const [customElement, setCustomElement] = useState<HTMLDivElement>();
16
+ *
17
+ * const { scrollDir, scrollPosition } = useDetectScroll({
18
+ * target: customElement,
19
+ * thr: 100,
20
+ * axis: Axis.Y,
21
+ * scrollUp: Direction.Up,
22
+ * scrollDown: Direction.Down,
23
+ * still: Direction.Still
24
+ * });
25
+ *
26
+ * useEffect(() => {
27
+ * if (customElementRef.current) {
28
+ * setCustomElement(customElementRef.current);
29
+ * }
30
+ * }, [customElementRef]);
31
+ *
32
+ * return (
33
+ * <div>
34
+ * <p>Current scroll direction: {scrollDir}</p>
35
+ * <p>Scroll position - Top: {scrollPosition.top}, Bottom: {scrollPosition.bottom},
36
+ * Left: {scrollPosition.left}, Right: {scrollPosition.right}</p>
37
+ * </div>
38
+ * );
39
+ * }
40
+ *
41
+ * @param {ScrollProps} props - The properties related to scrolling.
42
+ * @returns {ScrollInfo} - The current direction and position of scrolling.
43
+ */
44
+ declare function useDetectScroll(props?: ScrollProps): ScrollInfo;
45
+ export default useDetectScroll;
package/package.json CHANGED
@@ -80,5 +80,5 @@
80
80
  },
81
81
  "type": "module",
82
82
  "types": "./dist/index.d.ts",
83
- "version": "4.2.0-beta.0"
83
+ "version": "4.2.0-beta.1"
84
84
  }