|
2 | 2 |
|
3 | 3 |     
|
4 | 4 |
|
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. |
6 | 6 |
|
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. |
8 | 8 |
|
9 | 9 | ## Demo
|
10 | 10 |
|
11 |
| -Click the button below to view a demo on CodeSandbox: |
| 11 | +Experience `@smakss/react-scroll-direction` in action on CodeSandbox: |
12 | 12 |
|
13 | 13 | [](https://codesandbox.io/s/react-scroll-direction-tclwvp?fontsize=14&hidenavigation=1&theme=dark)
|
14 | 14 |
|
15 | 15 | ## Installation
|
16 | 16 |
|
17 |
| -Install the package using npm or yarn: |
| 17 | +Install `@smakss/react-scroll-direction` via npm or yarn: |
18 | 18 |
|
19 | 19 | ```bash
|
20 | 20 | npm install @smakss/react-scroll-direction
|
21 | 21 | # or
|
22 | 22 | yarn add @smakss/react-scroll-direction
|
23 | 23 | ```
|
24 | 24 |
|
25 |
| -To include it in your project, use: |
| 25 | +Then, import it into your project: |
26 | 26 |
|
27 | 27 | CommonJS:
|
28 | 28 |
|
29 | 29 | ```js
|
30 |
| -const { useDetectScroll } = require("@smakss/react-scroll-direction"); |
| 30 | +const useDetectScroll = require("@smakss/react-scroll-direction"); |
31 | 31 | ```
|
32 | 32 |
|
33 | 33 | ES Module:
|
34 | 34 |
|
35 | 35 | ```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"; |
37 | 46 | ```
|
38 | 47 |
|
39 | 48 | ## Usage
|
40 | 49 |
|
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: |
42 | 51 |
|
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"`). |
48 | 57 |
|
49 | 58 | ## Examples
|
50 | 59 |
|
51 |
| -Detecting scroll up/down: |
| 60 | +To detect upward or downward scroll: |
52 | 61 |
|
53 | 62 | ```js
|
54 | 63 | const scrollDir = useDetectScroll({});
|
55 | 64 |
|
56 |
| -// Outputs: "up", "down", or "still" |
| 65 | +// Returns: "up", "down", or "still" |
57 | 66 | ```
|
58 | 67 |
|
59 |
| -Detecting scroll left/right: |
| 68 | +To detect left or right scroll: |
60 | 69 |
|
61 | 70 | ```js
|
62 | 71 | const scrollDir = useDetectScroll({ axis: "x" });
|
63 | 72 |
|
64 |
| -// Outputs: "left", "right", or "still" |
| 73 | +// Returns: "left", "right", or "still" |
65 | 74 | ```
|
0 commit comments