mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 17:55:08 +01:00
26 lines
654 B
JavaScript
26 lines
654 B
JavaScript
import { useState, useEffect, useRef } from 'react';
|
|
|
|
export function useSticky({ enabled = true, threshold = 1 }) {
|
|
const [isSticky, setIsSticky] = useState(false);
|
|
const ref = useRef(null);
|
|
|
|
useEffect(() => {
|
|
let observer;
|
|
const handler = ([entry]) => setIsSticky(entry.intersectionRatio < threshold);
|
|
|
|
if (enabled && ref.current) {
|
|
observer = new IntersectionObserver(handler, { threshold: [threshold] });
|
|
observer.observe(ref.current);
|
|
}
|
|
return () => {
|
|
if (observer) {
|
|
observer.disconnect();
|
|
}
|
|
};
|
|
}, [ref, enabled, threshold]);
|
|
|
|
return { ref, isSticky };
|
|
}
|
|
|
|
export default useSticky;
|