mirror of
https://github.com/umami-software/umami.git
synced 2025-12-08 05:12:36 +01:00
More refactoring and clean-up.
This commit is contained in:
parent
f062cdbed2
commit
c815e7cd51
16 changed files with 101 additions and 134 deletions
|
|
@ -1,20 +1,19 @@
|
|||
import { useState, useEffect, useRef } from 'react';
|
||||
|
||||
export default function useSticky(
|
||||
element = document.getElementById('layout-body'),
|
||||
defaultSticky = false,
|
||||
) {
|
||||
export default function useSticky({ scrollElementId, defaultSticky = false }) {
|
||||
const [isSticky, setIsSticky] = useState(defaultSticky);
|
||||
const ref = useRef(null);
|
||||
const initialTop = useRef(null);
|
||||
|
||||
useEffect(() => {
|
||||
const element = scrollElementId ? document.getElementById(scrollElementId) : window;
|
||||
|
||||
const handleScroll = () => {
|
||||
setIsSticky(element.scrollTop > initialTop.current);
|
||||
};
|
||||
|
||||
if (initialTop.current === null) {
|
||||
initialTop.current = ref.current.offsetTop;
|
||||
initialTop.current = ref?.current?.offsetTop;
|
||||
}
|
||||
|
||||
element.addEventListener('scroll', handleScroll);
|
||||
|
|
@ -22,7 +21,7 @@ export default function useSticky(
|
|||
return () => {
|
||||
element.removeEventListener('scroll', handleScroll);
|
||||
};
|
||||
}, [setIsSticky]);
|
||||
}, [ref, setIsSticky]);
|
||||
|
||||
return { ref, isSticky };
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue