More deletes. Fixed sticky header.

This commit is contained in:
Mike Cao committed 2023-02-08 23:14:11 -08:00
1 parent 87bbaa7f1d
commit 45c13da262
23 files changed
+69 -582

No files matched your search

+27
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 };
}