More deletes. Fixed sticky header.

This commit is contained in:
Mike Cao 2023-02-08 23:14:11 -08:00
parent 87bbaa7f1d
commit 45c13da262
23 changed files with 69 additions and 582 deletions

27
hooks/useSticky.js Normal file
View file

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