|
2 | 2 |
|
3 | 3 |     |
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 | [](https://codesandbox.io/s/react-scroll-direction-tclwvp?fontsize=14&hidenavigation=1&theme=dark) |
14 | 14 |
|
@@ -47,30 +47,37 @@ import useDetectScroll, { |
47 | 47 |
|
48 | 48 | ## Usage |
49 | 49 |
|
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: |
51 | 51 |
|
52 | 52 | - `thr`: Threshold for scroll direction change detection (default: `0`, accepts only positive values). |
53 | 53 | - `axis`: Defines the scroll axis (`"y"` or `"x"`, default: `"y"`). |
54 | 54 | - `scrollUp`: Value returned when scrolling up (y-axis) or left (x-axis) (default: `"up"` for y-axis, `"left"` for x-axis). |
55 | 55 | - `scrollDown`: Value returned when scrolling down (y-axis) or right (x-axis) (default: `"down"` for y-axis, `"right"` for x-axis). |
56 | 56 | - `still`: Value returned when there's no scrolling activity (default: `"still"`). |
57 | 57 |
|
| 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 | + |
58 | 63 | ## Examples |
59 | 64 |
|
60 | | -To detect upward or downward scroll: |
| 65 | +To detect both scroll direction and position: |
61 | 66 |
|
62 | 67 | ```js |
63 | | -const scrollDir = useDetectScroll(); |
| 68 | +const { scrollDir, scrollPosition } = useDetectScroll(); |
64 | 69 |
|
65 | | -// Returns: "up", "down", or "still" |
| 70 | +// scrollDir: "up", "down", "left", "right", or "still" |
| 71 | +// scrollPosition: { top, bottom, left, right } |
66 | 72 | ``` |
67 | 73 |
|
68 | | -To detect left or right scroll: |
| 74 | +To customize for horizontal scroll: |
69 | 75 |
|
70 | 76 | ```js |
71 | | -const scrollDir = useDetectScroll({ axis: 'x' }); |
| 77 | +const { scrollDir, scrollPosition } = useDetectScroll({ axis: Axis.X }); |
72 | 78 |
|
73 | | -// Returns: "left", "right", or "still" |
| 79 | +// scrollDir: "left", "right", or "still" |
| 80 | +// scrollPosition: { top, bottom, left, right } |
74 | 81 | ``` |
75 | 82 |
|
76 | 83 | ## Contributing |
|
0 commit comments