mirror of
https://github.com/umami-software/umami.git
synced 2025-12-06 01:18:00 +01:00
Updated sticky header logic.
This commit is contained in:
parent
8532c673fe
commit
f3e1f18e1b
7 changed files with 41 additions and 76 deletions
|
|
@ -1,25 +1,23 @@
|
|||
import { useState, useEffect, useRef } from 'react';
|
||||
|
||||
export default function useSticky({ scrollElement = document, defaultSticky = false }) {
|
||||
export default function useSticky({ defaultSticky = false, enabled = true }) {
|
||||
const [isSticky, setIsSticky] = useState(defaultSticky);
|
||||
const ref = useRef(null);
|
||||
const initialTop = useRef(null);
|
||||
|
||||
useEffect(() => {
|
||||
const handleScroll = () => {
|
||||
setIsSticky((scrollElement?.scrollTop ?? window.scrollY) > initialTop.current);
|
||||
};
|
||||
let observer;
|
||||
const handler = ([entry]) => setIsSticky(entry.intersectionRatio < 1);
|
||||
|
||||
if (initialTop.current === null) {
|
||||
initialTop.current = ref?.current?.offsetTop;
|
||||
if (enabled && ref.current) {
|
||||
observer = new IntersectionObserver(handler, { threshold: [1] });
|
||||
observer.observe(ref.current);
|
||||
}
|
||||
|
||||
scrollElement.addEventListener('scroll', handleScroll, true);
|
||||
|
||||
return () => {
|
||||
scrollElement.removeEventListener('scroll', handleScroll, true);
|
||||
if (observer) {
|
||||
observer.disconnect();
|
||||
}
|
||||
};
|
||||
}, [ref, setIsSticky, scrollElement]);
|
||||
}, [ref]);
|
||||
|
||||
return { ref, isSticky };
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue