mirror of
https://github.com/umami-software/umami.git
synced 2026-02-11 16:17:13 +01:00
Sticky metrics header. CSS updates.
This commit is contained in:
parent
a65f637df2
commit
9c5762b8a2
16 changed files with 193 additions and 104 deletions
31
components/hooks/useSticky.js
Normal file
31
components/hooks/useSticky.js
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import { useState, useEffect, useCallback, useRef } from 'react';
|
||||
|
||||
export default function useSticky(enabled) {
|
||||
const [node, setNode] = useState(null);
|
||||
const [sticky, setSticky] = useState(false);
|
||||
const offsetTop = useRef(0);
|
||||
|
||||
const ref = useCallback(node => {
|
||||
offsetTop.current = node?.offsetTop;
|
||||
setNode(node);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const checkPosition = () => {
|
||||
const state = window.pageYOffset > offsetTop.current;
|
||||
if (node && sticky !== state) {
|
||||
setSticky(state);
|
||||
}
|
||||
};
|
||||
|
||||
if (enabled) {
|
||||
window.addEventListener('scroll', checkPosition);
|
||||
}
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('scroll', checkPosition);
|
||||
};
|
||||
}, [node, sticky, enabled]);
|
||||
|
||||
return [ref, sticky];
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue