-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseHeaderDisplay.js
34 lines (27 loc) · 1005 Bytes
/
useHeaderDisplay.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { useState, useEffect } from "react";
import { debounce } from "../lib/debounce";
//Only working on mobile screen??
export default function useHeaderDisplay() {
const [prevScrollPos, setPrevScrollPos] = useState(0);
const [headerVisible, setVisible] = useState(true);
const handleScroll = debounce(() => {
const currentScrollPos = window.scrollY;
/* last || -> if just a little scroll down, keep Header in place. */
setVisible(
(prevState) =>
(prevScrollPos > currentScrollPos &&
prevScrollPos - currentScrollPos > 30) ||
currentScrollPos < 10 ||
(prevState && currentScrollPos - prevScrollPos < 80) /* 100 */
);
//console.log(headerVisible);
setPrevScrollPos(currentScrollPos);
}, 200);
useEffect(() => {
window.addEventListener("scroll", handleScroll);
return () => window.removeEventListener("scroll", handleScroll);
}, [prevScrollPos, headerVisible, handleScroll]);
return {
headerVisible,
};
}