@smakss/react-scroll-direction 4.0.0-beta.0 → 4.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 CHANGED
@@ -2,13 +2,13 @@
2
2
 
3
3
  ![npm](https://img.shields.io/npm/v/@smakss/react-scroll-direction) ![NPM](https://img.shields.io/npm/l/@smakss/react-scroll-direction) ![npm](https://img.shields.io/npm/dt/@smakss/react-scroll-direction) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/min/@smakss/react-scroll-direction)
4
4
 
5
- `@smakss/react-scroll-direction` is a versatile, lightweight React hook that detects scroll direction in your application with ease. You can fine-tune its sensitivity using an adjustable threshold, catering to your application's unique needs.
5
+ `@smakss/react-scroll-direction` is a versatile, lightweight React hook that not only detects the scroll direction but also provides the scroll position in your application with ease. This enhanced functionality includes detecting distances from the top, bottom, left, and right edges of the viewport, making it an ideal solution for advanced scroll-based interactions in your React applications.
6
6
 
7
- This package originated from a [popular StackOverflow response](https://stackoverflow.com/a/62497293/11908502) and has been developed further into a ready-to-implement solution for managing scroll direction detection in your React applications.
7
+ Originally inspired by a [popular StackOverflow response](https://stackoverflow.com/a/62497293/11908502), this package has evolved into a comprehensive tool for managing scroll detection in React applications.
8
8
 
9
9
  ## Demo
10
10
 
11
- Experience `@smakss/react-scroll-direction` in action on CodeSandbox:
11
+ Experience the extended capabilities of `@smakss/react-scroll-direction` on CodeSandbox:
12
12
 
13
13
  [![View @smakss/search](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/react-scroll-direction-tclwvp?fontsize=14&hidenavigation=1&theme=dark)
14
14
 
@@ -24,12 +24,6 @@ yarn add @smakss/react-scroll-direction
24
24
 
25
25
  Then, import it into your project:
26
26
 
27
- CommonJS:
28
-
29
- ```js
30
- const useDetectScroll = require('@smakss/react-scroll-direction');
31
- ```
32
-
33
27
  ES Module:
34
28
 
35
29
  ```js
@@ -47,7 +41,7 @@ import useDetectScroll, {
47
41
 
48
42
  ## Usage
49
43
 
50
- The `useDetectScroll` hook takes an options object that can include the following properties:
44
+ The `useDetectScroll` hook takes an options object with the following properties:
51
45
 
52
46
  - `thr`: Threshold for scroll direction change detection (default: `0`, accepts only positive values).
53
47
  - `axis`: Defines the scroll axis (`"y"` or `"x"`, default: `"y"`).
@@ -55,22 +49,29 @@ The `useDetectScroll` hook takes an options object that can include the followin
55
49
  - `scrollDown`: Value returned when scrolling down (y-axis) or right (x-axis) (default: `"down"` for y-axis, `"right"` for x-axis).
56
50
  - `still`: Value returned when there's no scrolling activity (default: `"still"`).
57
51
 
52
+ The hook returns an object with two properties:
53
+
54
+ - `scrollDir`: Indicates the scroll direction (`"up"`, `"down"`, `"left"`, `"right"`, or `"still"`).
55
+ - `scrollPosition`: An object containing distances from the top, bottom, left, and right edges of the viewport.
56
+
58
57
  ## Examples
59
58
 
60
- To detect upward or downward scroll:
59
+ To detect both scroll direction and position:
61
60
 
62
61
  ```js
63
- const scrollDir = useDetectScroll();
62
+ const { scrollDir, scrollPosition } = useDetectScroll();
64
63
 
65
- // Returns: "up", "down", or "still"
64
+ // scrollDir: "up", "down", "left", "right", or "still"
65
+ // scrollPosition: { top, bottom, left, right }
66
66
  ```
67
67
 
68
- To detect left or right scroll:
68
+ To customize for horizontal scroll:
69
69
 
70
70
  ```js
71
- const scrollDir = useDetectScroll({ axis: 'x' });
71
+ const { scrollDir, scrollPosition } = useDetectScroll({ axis: Axis.X });
72
72
 
73
- // Returns: "left", "right", or "still"
73
+ // scrollDir: "left", "right", or "still"
74
+ // scrollPosition: { top, bottom, left, right }
74
75
  ```
75
76
 
76
77
  ## Contributing
package/package.json CHANGED
@@ -3,11 +3,10 @@
3
3
  "bugs": {
4
4
  "url": "https://github.com/SMAKSS/react-scroll-direction/issues"
5
5
  },
6
- "description": "Effortlessly detect scroll direction in React apps with @smakss/react-scroll-direction. Perfect for React, Remix, Next.js, and Gatsby projects, offering adjustable sensitivity.",
6
+ "description": "Enhance your React apps with advanced scroll detection using @smakss/react-scroll-direction. This powerful hook not only detects scroll direction but also provides scroll position information. Ideal for React, Remix, Next.js, and Gatsby projects, it comes with adjustable sensitivity and supports ES Modules.",
7
7
  "devDependencies": {
8
8
  "@commitlint/cli": "^18.4.3",
9
9
  "@commitlint/config-conventional": "^18.4.3",
10
- "@rollup/plugin-commonjs": "^25.0.7",
11
10
  "@rollup/plugin-node-resolve": "^15.2.3",
12
11
  "@rollup/plugin-typescript": "^11.1.5",
13
12
  "@types/react": "^18.2.46",
@@ -35,9 +34,13 @@
35
34
  "homepage": "https://github.com/SMAKSS/react-scroll-direction#readme",
36
35
  "keywords": [
37
36
  "react-scroll-direction",
37
+ "scroll-position-react",
38
+ "react-hook-scroll-position",
38
39
  "scroll-detection-react",
40
+ "scroll-position-detection",
41
+ "scroll-direction-detection",
39
42
  "react-hook-scroll",
40
- "direction-detection",
43
+ "direction-and-position-detection",
41
44
  "scroll-hook",
42
45
  "npm",
43
46
  "yarn",
@@ -47,12 +50,13 @@
47
50
  "gatsby",
48
51
  "scroll",
49
52
  "direction",
53
+ "position",
50
54
  "SMAKSS",
51
- "CommonJS",
52
55
  "EcmaScript",
53
56
  "TypeScript",
54
57
  "detect scroll in react",
55
- "react scroll"
58
+ "react scroll position",
59
+ "react scroll direction and position"
56
60
  ],
57
61
  "license": "MIT",
58
62
  "main": "dist/cjs/index.js",
@@ -75,5 +79,5 @@
75
79
  "typecheck": "tsc -b ."
76
80
  },
77
81
  "type": "module",
78
- "version": "4.0.0-beta.0"
82
+ "version": "4.0.0"
79
83
  }
@@ -1,66 +0,0 @@
1
- /** Enumeration for axis values */
2
- export declare enum Axis {
3
- X = "x",
4
- Y = "y"
5
- }
6
- /** Enumeration for direction values */
7
- export declare enum Direction {
8
- Up = "up",
9
- Down = "down",
10
- Left = "left",
11
- Right = "right",
12
- Still = "still"
13
- }
14
- type ScrollPosition = {
15
- top: number;
16
- bottom: number;
17
- left: number;
18
- right: number;
19
- };
20
- /** Type declaration for the returned scroll information */
21
- type ScrollInfo = {
22
- scrollDir: Direction;
23
- scrollPosition: ScrollPosition;
24
- };
25
- /** Type declaration for scroll properties */
26
- type ScrollProps = {
27
- thr?: number;
28
- axis?: Axis;
29
- scrollUp?: Direction;
30
- scrollDown?: Direction;
31
- still?: Direction;
32
- };
33
- /**
34
- * useDetectScroll hook.
35
- *
36
- * This hook provides a mechanism to detect the scroll direction and position.
37
- * It will return the scroll direction as a string (up, down, left, right, or still) based on user scrolling,
38
- * as well as the scroll position from the top, bottom, left, and right edges of the page.
39
- *
40
- * @example
41
- *
42
- * import useDetectScroll, { Axis, Direction } from '@smakss/react-scroll-direction';
43
- *
44
- * function App() {
45
- * const { scrollDir, scrollPosition } = useDetectScroll({
46
- * thr: 100,
47
- * axis: Axis.Y,
48
- * scrollUp: Direction.Up,
49
- * scrollDown: Direction.Down,
50
- * still: Direction.Still
51
- * });
52
- *
53
- * return (
54
- * <div>
55
- * <p>Current scroll direction: {scrollDir}</p>
56
- * <p>Scroll position - Top: {scrollPosition.top}, Bottom: {scrollPosition.bottom},
57
- * Left: {scrollPosition.left}, Right: {scrollPosition.right}</p>
58
- * </div>
59
- * );
60
- * }
61
- *
62
- * @param {ScrollProps} props - The properties related to scrolling.
63
- * @returns {ScrollInfo} - The current direction and position of scrolling.
64
- */
65
- declare function useDetectScroll(props?: ScrollProps): ScrollInfo;
66
- export default useDetectScroll;
package/dist/cjs/index.js DELETED
@@ -1,107 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var react = require('react');
6
-
7
- /** Enumeration for axis values */
8
- exports.Axis = void 0;
9
- (function (Axis) {
10
- Axis["X"] = "x";
11
- Axis["Y"] = "y";
12
- })(exports.Axis || (exports.Axis = {}));
13
- /** Enumeration for direction values */
14
- exports.Direction = void 0;
15
- (function (Direction) {
16
- Direction["Up"] = "up";
17
- Direction["Down"] = "down";
18
- Direction["Left"] = "left";
19
- Direction["Right"] = "right";
20
- Direction["Still"] = "still";
21
- })(exports.Direction || (exports.Direction = {}));
22
- /**
23
- * useDetectScroll hook.
24
- *
25
- * This hook provides a mechanism to detect the scroll direction and position.
26
- * It will return the scroll direction as a string (up, down, left, right, or still) based on user scrolling,
27
- * as well as the scroll position from the top, bottom, left, and right edges of the page.
28
- *
29
- * @example
30
- *
31
- * import useDetectScroll, { Axis, Direction } from '@smakss/react-scroll-direction';
32
- *
33
- * function App() {
34
- * const { scrollDir, scrollPosition } = useDetectScroll({
35
- * thr: 100,
36
- * axis: Axis.Y,
37
- * scrollUp: Direction.Up,
38
- * scrollDown: Direction.Down,
39
- * still: Direction.Still
40
- * });
41
- *
42
- * return (
43
- * <div>
44
- * <p>Current scroll direction: {scrollDir}</p>
45
- * <p>Scroll position - Top: {scrollPosition.top}, Bottom: {scrollPosition.bottom},
46
- * Left: {scrollPosition.left}, Right: {scrollPosition.right}</p>
47
- * </div>
48
- * );
49
- * }
50
- *
51
- * @param {ScrollProps} props - The properties related to scrolling.
52
- * @returns {ScrollInfo} - The current direction and position of scrolling.
53
- */
54
- function useDetectScroll(props = {}) {
55
- const { thr = 0, axis = exports.Axis.Y, scrollUp = axis === exports.Axis.Y ? exports.Direction.Up : exports.Direction.Left, scrollDown = axis === exports.Axis.Y ? exports.Direction.Down : exports.Direction.Right, still = exports.Direction.Still } = props;
56
- const [scrollDir, setScrollDir] = react.useState(still);
57
- const [scrollPosition, setScrollPosition] = react.useState({
58
- top: 0,
59
- bottom: 0,
60
- left: 0,
61
- right: 0
62
- });
63
- const threshold = Math.max(0, thr);
64
- let ticking = false;
65
- let lastScroll = 0;
66
- /** Function to update scroll direction */
67
- const updateScrollDir = react.useCallback(() => {
68
- const scroll = axis === exports.Axis.Y ? window.scrollY : window.scrollX;
69
- if (Math.abs(scroll - lastScroll) >= threshold) {
70
- setScrollDir(scroll > lastScroll ? scrollDown : scrollUp);
71
- lastScroll = Math.max(0, scroll);
72
- }
73
- ticking = false;
74
- }, [axis, threshold, scrollDown, scrollUp]);
75
- react.useEffect(() => {
76
- /** Function to update scroll position */
77
- const updateScrollPosition = () => {
78
- const top = window.scrollY;
79
- const left = window.scrollX;
80
- const bottom = document.documentElement.scrollHeight - window.innerHeight - top;
81
- const right = document.documentElement.scrollWidth - window.innerWidth - left;
82
- setScrollPosition({ top, bottom, left, right });
83
- };
84
- /** Call the update function when the component mounts */
85
- updateScrollPosition();
86
- window.addEventListener('scroll', updateScrollPosition);
87
- return () => {
88
- window.removeEventListener('scroll', updateScrollPosition);
89
- };
90
- }, []);
91
- react.useEffect(() => {
92
- lastScroll = axis === exports.Axis.Y ? window.scrollY : window.scrollX;
93
- /** Function to handle onScroll event */
94
- const onScroll = () => {
95
- if (!ticking) {
96
- window.requestAnimationFrame(updateScrollDir);
97
- ticking = true;
98
- }
99
- };
100
- window.addEventListener('scroll', onScroll);
101
- return () => window.removeEventListener('scroll', onScroll);
102
- }, [updateScrollDir]);
103
- return { scrollDir, scrollPosition };
104
- }
105
-
106
- exports.default = useDetectScroll;
107
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["import { useState, useEffect, useCallback } from 'react';\n\n/** Enumeration for axis values */\nexport enum Axis {\n X = 'x',\n Y = 'y'\n}\n\n/** Enumeration for direction values */\nexport enum Direction {\n Up = 'up',\n Down = 'down',\n Left = 'left',\n Right = 'right',\n Still = 'still'\n}\n\ntype ScrollPosition = {\n top: number;\n bottom: number;\n left: number;\n right: number;\n};\n\n/** Type declaration for the returned scroll information */\ntype ScrollInfo = {\n scrollDir: Direction;\n scrollPosition: ScrollPosition;\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 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 let ticking = false;\n let lastScroll = 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) >= 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 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 = axis === Axis.Y ? window.scrollY : window.scrollX;\n\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, scrollPosition };\n}\n\nexport default useDetectScroll;\n"],"names":["Axis","Direction","useState","useCallback","useEffect"],"mappings":";;;;;;AAEA;AACYA,sBAGX;AAHD,CAAA,UAAY,IAAI,EAAA;AACd,IAAA,IAAA,CAAA,GAAA,CAAA,GAAA,GAAO,CAAA;AACP,IAAA,IAAA,CAAA,GAAA,CAAA,GAAA,GAAO,CAAA;AACT,CAAC,EAHWA,YAAI,KAAJA,YAAI,GAGf,EAAA,CAAA,CAAA,CAAA;AAED;AACYC,2BAMX;AAND,CAAA,UAAY,SAAS,EAAA;AACnB,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,EANWA,iBAAS,KAATA,iBAAS,GAMpB,EAAA,CAAA,CAAA,CAAA;AAwBD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BG;AACH,SAAS,eAAe,CAAC,KAAA,GAAqB,EAAE,EAAA;IAC9C,MAAM,EACJ,GAAG,GAAG,CAAC,EACP,IAAI,GAAGD,YAAI,CAAC,CAAC,EACb,QAAQ,GAAG,IAAI,KAAKA,YAAI,CAAC,CAAC,GAAGC,iBAAS,CAAC,EAAE,GAAGA,iBAAS,CAAC,IAAI,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