Updated sticky header logic.

This commit is contained in:
Mike Cao 2023-02-09 08:22:36 -08:00
parent 45c13da262
commit f062cdbed2
8 changed files with 31 additions and 24 deletions

View file

@ -1,27 +1,28 @@
import { useState, useEffect, useRef } from 'react';
export default function useSticky(defaultSticky = false) {
export default function useSticky(
element = document.getElementById('layout-body'),
defaultSticky = false,
) {
const [isSticky, setIsSticky] = useState(defaultSticky);
const ref = useRef(null);
const initialTop = useRef(0);
const initialTop = useRef(null);
useEffect(() => {
const handleScroll = () => {
if (window.pageYOffset > initialTop.current) {
setIsSticky(true);
} else {
setIsSticky(false);
}
setIsSticky(element.scrollTop > initialTop.current);
};
initialTop.current = ref.current.offsetTop;
if (initialTop.current === null) {
initialTop.current = ref.current.offsetTop;
}
window.addEventListener('scroll', handleScroll);
element.addEventListener('scroll', handleScroll);
return () => {
window.removeEventListener('scroll', handleScroll);
element.removeEventListener('scroll', handleScroll);
};
}, []);
}, [setIsSticky]);
return { ref, isSticky };
}