Skip to content

Commit 41a72a7

Browse files
authored
Merge pull request #53 from SMAKSS/feature/migrate-to-ts
feat: change the project folder hierarchy
2 parents fae32e5 + 536dca8 commit 41a72a7

File tree

5 files changed

+132
-125
lines changed

5 files changed

+132
-125
lines changed

Readme.md

+26-17
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,73 @@
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-
Detect scroll direction in your React applications effortlessly using `@smakss/react-scroll-direction`, a custom React Hook with an adjustable threshold.
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.
66

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

99
## Demo
1010

11-
Click the button below to view a demo on CodeSandbox:
11+
Experience `@smakss/react-scroll-direction` in action 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

1515
## Installation
1616

17-
Install the package using npm or yarn:
17+
Install `@smakss/react-scroll-direction` via npm or yarn:
1818

1919
```bash
2020
npm install @smakss/react-scroll-direction
2121
# or
2222
yarn add @smakss/react-scroll-direction
2323
```
2424

25-
To include it in your project, use:
25+
Then, import it into your project:
2626

2727
CommonJS:
2828

2929
```js
30-
const { useDetectScroll } = require("@smakss/react-scroll-direction");
30+
const useDetectScroll = require("@smakss/react-scroll-direction");
3131
```
3232

3333
ES Module:
3434

3535
```js
36-
import { useDetectScroll } from "@smakss/react-scroll-direction";
36+
import useDetectScroll from "@smakss/react-scroll-direction";
37+
```
38+
39+
For TypeScript projects, import the hook and its types:
40+
41+
```ts
42+
import useDetectScroll, {
43+
Axis,
44+
Direction,
45+
} from "@smakss/react-scroll-direction";
3746
```
3847

3948
## Usage
4049

41-
The `useDetectScroll` custom hook accepts an options object with the following properties:
50+
The `useDetectScroll` hook takes an options object that can include the following properties:
4251

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"`).
52+
- `thr`: Threshold for scroll direction change detection (default: `0`, accepts only positive values).
53+
- `axis`: Defines the scroll axis (`"y"` or `"x"`, default: `"y"`).
54+
- `scrollUp`: Value returned when scrolling up (y-axis) or left (x-axis) (default: `"up"` for y-axis, `"left"` for x-axis).
55+
- `scrollDown`: Value returned when scrolling down (y-axis) or right (x-axis) (default: `"down"` for y-axis, `"right"` for x-axis).
56+
- `still`: Value returned when there's no scrolling activity (default: `"still"`).
4857

4958
## Examples
5059

51-
Detecting scroll up/down:
60+
To detect upward or downward scroll:
5261

5362
```js
5463
const scrollDir = useDetectScroll({});
5564

56-
// Outputs: "up", "down", or "still"
65+
// Returns: "up", "down", or "still"
5766
```
5867

59-
Detecting scroll left/right:
68+
To detect left or right scroll:
6069

6170
```js
6271
const scrollDir = useDetectScroll({ axis: "x" });
6372

64-
// Outputs: "left", "right", or "still"
73+
// Returns: "left", "right", or "still"
6574
```

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@
5757
"lint:fix": "eslint src/**/*.ts --fix"
5858
},
5959
"type": "module",
60-
"version": "2.0.0"
60+
"version": "2.1.0"
6161
}

src/hooks/index.ts

-1
This file was deleted.

src/hooks/useDetectScroll.ts

-105
This file was deleted.

src/index.ts

+105-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,105 @@
1-
export * from "./hooks";
1+
import { useState, useEffect, useCallback } from "react";
2+
3+
/** Enumeration for axis values */
4+
export enum Axis {
5+
X = "x",
6+
Y = "y",
7+
}
8+
9+
/** Enumeration for direction values */
10+
export enum Direction {
11+
Up = "up",
12+
Down = "down",
13+
Left = "left",
14+
Right = "right",
15+
Still = "still",
16+
}
17+
18+
/** Type declaration for scroll properties */
19+
type ScrollProps = {
20+
thr?: number;
21+
axis?: Axis;
22+
scrollUp?: Direction;
23+
scrollDown?: Direction;
24+
still?: Direction;
25+
};
26+
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 '@smakss/react-scroll-direction';
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 {
63+
const {
64+
thr = 0,
65+
axis = Axis.Y,
66+
scrollUp = axis === Axis.Y ? Direction.Up : Direction.Left,
67+
scrollDown = axis === Axis.Y ? Direction.Down : Direction.Right,
68+
still = Direction.Still,
69+
} = props;
70+
71+
const [scrollDir, setScrollDir] = useState<Direction>(still);
72+
73+
const threshold = Math.max(0, thr);
74+
let ticking = false;
75+
let lastScroll: number = axis === Axis.Y ? window.scrollY : window.scrollX;
76+
77+
/** Function to update scroll direction */
78+
const updateScrollDir = useCallback(() => {
79+
const scroll = axis === Axis.Y ? window.scrollY : window.scrollX;
80+
81+
if (Math.abs(scroll - lastScroll) >= threshold) {
82+
setScrollDir(scroll > lastScroll ? scrollDown : scrollUp);
83+
lastScroll = Math.max(0, scroll);
84+
}
85+
ticking = false;
86+
}, [axis, threshold, scrollDown, scrollUp]);
87+
88+
useEffect(() => {
89+
/** Function to handle onScroll event */
90+
const onScroll = () => {
91+
if (!ticking) {
92+
window.requestAnimationFrame(updateScrollDir);
93+
ticking = true;
94+
}
95+
};
96+
97+
window.addEventListener("scroll", onScroll);
98+
99+
return () => window.removeEventListener("scroll", onScroll);
100+
}, [updateScrollDir]);
101+
102+
return scrollDir;
103+
}
104+
105+
export default useDetectScroll;

0 commit comments

Comments
 (0)