umami/hooks/useSticky.js
2023-03-22 14:05:55 -07:00

24 lines
635 B
JavaScript

import { useState, useEffect, useRef } from 'react';
export default 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 };
}