Skip to content

Commit e7d4b12

Browse files
committed
feat(scroll-position): finalise the feature
1 parent 985e7f7 commit e7d4b12

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

Readme.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
![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)
44

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.
66

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.
88

99
## Demo
1010

11-
Experience `@smakss/react-scroll-direction` in action on CodeSandbox:
11+
Experience the extended capabilities of `@smakss/react-scroll-direction` on CodeSandbox:
1212

1313
[![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)
1414

@@ -47,30 +47,37 @@ import useDetectScroll, {
4747

4848
## Usage
4949

50-
The `useDetectScroll` hook takes an options object that can include the following properties:
50+
The `useDetectScroll` hook takes an options object with the following properties:
5151

5252
- `thr`: Threshold for scroll direction change detection (default: `0`, accepts only positive values).
5353
- `axis`: Defines the scroll axis (`"y"` or `"x"`, default: `"y"`).
5454
- `scrollUp`: Value returned when scrolling up (y-axis) or left (x-axis) (default: `"up"` for y-axis, `"left"` for x-axis).
5555
- `scrollDown`: Value returned when scrolling down (y-axis) or right (x-axis) (default: `"down"` for y-axis, `"right"` for x-axis).
5656
- `still`: Value returned when there's no scrolling activity (default: `"still"`).
5757

58+
The hook returns an object with two properties:
59+
60+
- `scrollDir`: Indicates the scroll direction (`"up"`, `"down"`, `"left"`, `"right"`, or `"still"`).
61+
- `scrollPosition`: An object containing distances from the top, bottom, left, and right edges of the viewport.
62+
5863
## Examples
5964

60-
To detect upward or downward scroll:
65+
To detect both scroll direction and position:
6166

6267
```js
63-
const scrollDir = useDetectScroll();
68+
const { scrollDir, scrollPosition } = useDetectScroll();
6469

65-
// Returns: "up", "down", or "still"
70+
// scrollDir: "up", "down", "left", "right", or "still"
71+
// scrollPosition: { top, bottom, left, right }
6672
```
6773

68-
To detect left or right scroll:
74+
To customize for horizontal scroll:
6975

7076
```js
71-
const scrollDir = useDetectScroll({ axis: 'x' });
77+
const { scrollDir, scrollPosition } = useDetectScroll({ axis: Axis.X });
7278

73-
// Returns: "left", "right", or "still"
79+
// scrollDir: "left", "right", or "still"
80+
// scrollPosition: { top, bottom, left, right }
7481
```
7582

7683
## Contributing

package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"bugs": {
44
"url": "https://github.com/SMAKSS/react-scroll-direction/issues"
55
},
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 both CommonJS and ES Modules.",
77
"devDependencies": {
88
"@commitlint/cli": "^18.4.3",
99
"@commitlint/config-conventional": "^18.4.3",
@@ -35,9 +35,13 @@
3535
"homepage": "https://github.com/SMAKSS/react-scroll-direction#readme",
3636
"keywords": [
3737
"react-scroll-direction",
38+
"scroll-position-react",
39+
"react-hook-scroll-position",
3840
"scroll-detection-react",
41+
"scroll-position-detection",
42+
"scroll-direction-detection",
3943
"react-hook-scroll",
40-
"direction-detection",
44+
"direction-and-position-detection",
4145
"scroll-hook",
4246
"npm",
4347
"yarn",
@@ -47,12 +51,14 @@
4751
"gatsby",
4852
"scroll",
4953
"direction",
54+
"position",
5055
"SMAKSS",
5156
"CommonJS",
5257
"EcmaScript",
5358
"TypeScript",
5459
"detect scroll in react",
55-
"react scroll"
60+
"react scroll position",
61+
"react scroll direction and position"
5662
],
5763
"license": "MIT",
5864
"main": "dist/cjs/index.js",
@@ -75,5 +81,5 @@
7581
"typecheck": "tsc -b ."
7682
},
7783
"type": "module",
78-
"version": "4.0.0-beta.0"
84+
"version": "4.0.0"
7985
}

0 commit comments

Comments
 (0)