Skip to content

Commit b49c4b0

Browse files
committed
feat: add JSDoc to the app
Also, update the Readme.md file.
1 parent 37b7e1d commit b49c4b0

File tree

3 files changed

+76
-31
lines changed

3 files changed

+76
-31
lines changed

Readme.md

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,65 @@
1-
# Detect react scroll direction
1+
# React Scroll Direction Hook
22

33
![npm](https://img.shields.io/npm/v/@smakss/react-scroll-direction) ![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/@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-
This is a custom hook for react which is useful to detect scroll direction in React applications in an efficient way with a custom threshold.
5+
Detect scroll direction in your React applications effortlessly using `@smakss/react-scroll-direction`, a custom React Hook with an adjustable threshold.
66

7-
This package is created on behalf of a [StackOverflow answer](https://stackoverflow.com/a/62497293/11908502) which draws some attention to itself, so if someone just wants something to work with right away, they can access it easily here.
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.
88

99
## Demo
1010

11+
Click the button below to view a demo on CodeSandbox:
12+
1113
[![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)
1214

13-
## How it works?
15+
## Installation
1416

15-
To install it you can simply do the following command:
17+
Install the package using npm or yarn:
1618

1719
```bash
18-
npm i @smakss/react-scroll-direction
19-
or
20+
npm install @smakss/react-scroll-direction
21+
# or
2022
yarn add @smakss/react-scroll-direction
2123
```
2224

23-
to include it with common js module you should do this:
25+
To include it in your project, use:
26+
27+
CommonJS:
2428

2529
```js
26-
var { useDetectScroll } = require("@smakss/react-scroll-direction");
30+
const { useDetectScroll } = require("@smakss/react-scroll-direction");
2731
```
2832

29-
and to include it with ECMAscript module you can simply do this one:
33+
ES Module:
3034

3135
```js
3236
import { useDetectScroll } from "@smakss/react-scroll-direction";
3337
```
3438

35-
then to use it within your application you can do it just like below:
39+
## Usage
3640

37-
The useDetectScroll custom hook will accept 3 input parameter:
41+
The `useDetectScroll` custom hook accepts an options object with the following properties:
3842

39-
- `thr` (`number`): A number to indicate the threshold of firing scroll direction event, which is `0` by default and only accepts a positive numeric value. If it gets a higher value the steps will be longer.
40-
- `axis` (`string`): Indicate the page scroll axis, whether, in the `y` or `x` axes, it is `y` by default.
41-
- `scrollUp` (`string`): A string value for the output of the custom hook if the scroll direction is upward. The default value is `up` if the axis is `y` and `left` if the axis is `x`.
42-
- `scrollDown` (`string`): A string value for the output of the custom hook if the scroll direction is downward. The default value is `down` if the axis is `y` and `right` if the axis is `x`.
43-
- `still` (`string`): default value for the direction when there is no scrolling happening on the page. The default value is `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"`).
4448

45-
## Examples of usage
49+
## Examples
4650

47-
### If the scroll goes upward/downward
51+
Detecting scroll up/down:
4852

4953
```js
50-
const [scrollDir] = useDetectScroll({});
54+
const scrollDir = useDetectScroll({});
5155

52-
// scrollDir: "up"/"down"
56+
// Outputs: "up", "down", or "still"
5357
```
5458

55-
### If the scroll goes left/right
59+
Detecting scroll left/right:
5660

5761
```js
58-
const [scrollDir] = useDetectScroll({ axis: "x" });
62+
const scrollDir = useDetectScroll({ axis: "x" });
5963

60-
// scrollDir: "left"/"right"
64+
// Outputs: "left", "right", or "still"
6165
```

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"SMAKSS",
3636
"CommonJS",
3737
"EcmaScript",
38+
"TypeScript",
3839
"detect scroll in react",
3940
"react scroll",
4041
"scroll direction"
@@ -56,5 +57,5 @@
5657
"lint:fix": "eslint src/**/*.ts --fix"
5758
},
5859
"type": "module",
59-
"version": "1.1.0-beta"
60+
"version": "1.1.0"
6061
}

src/hooks/useDetectScroll.ts

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { useState, useEffect, useCallback } from "react";
22

3+
/** Enumeration for axis values */
34
enum Axis {
45
X = "x",
56
Y = "y",
67
}
78

9+
/** Enumeration for direction values */
810
enum Direction {
911
Up = "up",
1012
Down = "down",
@@ -13,15 +15,51 @@ enum Direction {
1315
Still = "still",
1416
}
1517

18+
/** Type declaration for scroll properties */
1619
type ScrollProps = {
1720
thr?: number;
1821
axis?: Axis;
19-
scrollUp?: string;
20-
scrollDown?: string;
21-
still?: string;
22+
scrollUp?: Direction;
23+
scrollDown?: Direction;
24+
still?: Direction;
2225
};
2326

24-
function useDetectScroll(props: ScrollProps) {
27+
/**
28+
* useDetectScroll hook.
29+
*
30+
* This hook provides a mechanism to detect the scroll direction.
31+
* It will return the scroll direction as a string (up, down, left, right, or still) based on user scrolling.
32+
*
33+
* @example
34+
*
35+
* import useDetectScroll, { Axis, Direction } from './useDetectScroll';
36+
*
37+
* function App() {
38+
* const scrollDirection = useDetectScroll({
39+
* thr: 100,
40+
* axis: Axis.Y,
41+
* scrollUp: Direction.Up,
42+
* scrollDown: Direction.Down,
43+
* still: Direction.Still
44+
* });
45+
*
46+
* return (
47+
* <div>
48+
* <p>Current scroll direction: {scrollDirection}</p>
49+
* </div>
50+
* );
51+
* }
52+
*
53+
* @param {ScrollProps} props - The properties related to scrolling.
54+
* @property {number} props.thr - The threshold value which the scroll difference must exceed to update scroll direction.
55+
* @property {Axis} props.axis - The axis along which to detect scroll. Can be 'x' or 'y'.
56+
* @property {Direction} props.scrollUp - The direction to set when scrolling up or left. By default, 'up' for y-axis and 'left' for x-axis.
57+
* @property {Direction} props.scrollDown - The direction to set when scrolling down or right. By default, 'down' for y-axis and 'right' for x-axis.
58+
* @property {Direction} props.still - The direction to set when there is no scrolling. By default, 'still'.
59+
*
60+
* @returns {Direction} - The current direction of scrolling.
61+
*/
62+
function useDetectScroll(props: ScrollProps): Direction {
2563
const {
2664
thr = 0,
2765
axis = Axis.Y,
@@ -30,12 +68,13 @@ function useDetectScroll(props: ScrollProps) {
3068
still = Direction.Still,
3169
} = props;
3270

33-
const [scrollDir, setScrollDir] = useState(still);
71+
const [scrollDir, setScrollDir] = useState<Direction>(still);
3472

3573
const threshold = Math.max(0, thr);
3674
let ticking = false;
3775
let lastScroll: number = axis === Axis.Y ? window.scrollY : window.scrollX;
3876

77+
/** Function to update scroll direction */
3978
const updateScrollDir = useCallback(() => {
4079
const scroll = axis === Axis.Y ? window.scrollY : window.scrollX;
4180

@@ -47,6 +86,7 @@ function useDetectScroll(props: ScrollProps) {
4786
}, [axis, threshold, scrollDown, scrollUp]);
4887

4988
useEffect(() => {
89+
/** Function to handle onScroll event */
5090
const onScroll = () => {
5191
if (!ticking) {
5292
window.requestAnimationFrame(updateScrollDir);
@@ -59,7 +99,7 @@ function useDetectScroll(props: ScrollProps) {
5999
return () => window.removeEventListener("scroll", onScroll);
60100
}, [updateScrollDir]);
61101

62-
return [scrollDir];
102+
return scrollDir;
63103
}
64104

65105
export default useDetectScroll;

0 commit comments

Comments
 (0)