Skip to content

Commit 60635aa

Browse files
committed
feat: add the new x axis support
1 parent f2a7289 commit 60635aa

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

Readme.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,28 @@ then to use it within your application you can do it just like below:
3030

3131
The useDetectScroll custom hook will accept 3 input parameter:
3232

33-
- `thr` (`Number`): A number to indicate the threshold of firing scroll direction event, which is `0` by default and it is only accept a positive numeric value. If it gets higher value the steps will be longer.
34-
- `up` (`string`): A string value for the output of custom hook if the scroll direction is upward. The default value is `up`.
35-
- `down` (`string`): A string value for the output of custom hook if the scroll direction is downward. The default value is `down`.
33+
- `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.
34+
- `axis` (`string`): Indicate the page scroll axis, whether, in the `y` or `x` axes, it is `y` by default.
35+
- `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`.
36+
- `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`.
37+
- `still` (`string`): default value for the direction when there is no scrolling happening on the page. The default value is `still`.
3638

3739
## Examples of usage
3840

39-
if scroll goes upward:
41+
### If the scroll goes upward/downward
4042

4143
```js
4244
const [scrollDir] = useDetectScroll();
4345

44-
// scrollDir: "up"
46+
// scrollDir: "up"/"down"
4547
```
4648

47-
if scroll goes downward:
49+
### If the scroll goes left/right
4850

4951
```js
50-
const [scrollDir] = useDetectScroll();
52+
const [scrollDir] = useDetectScroll({ axis: "x" });
5153

52-
// scrollDir: "down"
54+
// scrollDir: "left"/"right"
5355
```
5456

5557
## Demo

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@
4646
"generate": "rollup -c"
4747
},
4848
"type": "module",
49-
"version": "0.0.6"
49+
"version": "0.0.7"
5050
}

src/hooks/useDetectScroll.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
11
import { useState, useEffect } from "react";
22
import PropTypes from "prop-types";
33

4-
function useDetectScroll({ thr, up = "up", down = "down" }) {
5-
const [scrollDir, setScrollDir] = useState(down);
4+
function useDetectScroll(props) {
5+
const {
6+
thr = 0,
7+
axis = "y",
8+
scrollUp = axis === "y" ? "up" : "left",
9+
scrollDown = axis === "y" ? "down" : "right",
10+
still = "still",
11+
} = props;
12+
const [scrollDir, setScrollDir] = useState(still);
613

714
useEffect(() => {
8-
const threshold = thr ? (thr > 0 ? thr : 0) : 0;
9-
let lastScrollY = window.pageYOffset;
15+
const threshold = thr > 0 ? thr : 0;
1016
let ticking = false;
17+
let lastScroll = undefined;
18+
19+
axis === "y"
20+
? (lastScroll = window.pageYOffset)
21+
: (lastScroll = window.pageXOffset);
1122

1223
const updateScrollDir = () => {
13-
const scrollY = window.pageYOffset;
24+
let scroll = undefined;
25+
26+
axis === "y"
27+
? (scroll = window.pageYOffset)
28+
: (scroll = window.pageXOffset);
1429

15-
if (Math.abs(scrollY - lastScrollY) < threshold) {
30+
if (Math.abs(scroll - lastScroll) < threshold) {
1631
ticking = false;
1732
return;
1833
}
19-
setScrollDir(scrollY > lastScrollY ? down : up);
20-
lastScrollY = scrollY > 0 ? scrollY : 0;
34+
setScrollDir(scroll > lastScroll ? scrollDown : scrollUp);
35+
lastScroll = scroll > 0 ? scrollY : 0;
2136
ticking = false;
2237
};
2338

@@ -40,6 +55,8 @@ export default useDetectScroll;
4055

4156
useDetectScroll.propTypes = {
4257
thr: PropTypes.number,
43-
up: PropTypes.string,
44-
down: PropTypes.string,
58+
axis: PropTypes.string,
59+
scrollUp: PropTypes.string,
60+
scrollDown: PropTypes.string,
61+
still: PropTypes.string,
4562
};

0 commit comments

Comments
 (0)