mirror of
https://github.com/zoriya/v10.git
synced 2026-08-11 00:19:28 +00:00
Co-authored-by: Darius Cepulis <dcepulis@mux.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
28 lines
781 B
TypeScript
28 lines
781 B
TypeScript
import { useEffect, useRef, useState } from 'react';
|
|
|
|
export function useIntersection(options?: IntersectionObserverInit): [React.RefCallback<Element>, boolean] {
|
|
const [isIntersecting, setIsIntersecting] = useState(false);
|
|
const observerRef = useRef<IntersectionObserver | null>(null);
|
|
|
|
useEffect(() => {
|
|
return () => observerRef.current?.disconnect();
|
|
}, []);
|
|
|
|
const ref = (node: Element | null) => {
|
|
observerRef.current?.disconnect();
|
|
|
|
if (!node || isIntersecting) return;
|
|
|
|
observerRef.current = new IntersectionObserver(([entry]) => {
|
|
if (entry?.isIntersecting) {
|
|
setIsIntersecting(true);
|
|
observerRef.current?.disconnect();
|
|
}
|
|
}, options);
|
|
|
|
observerRef.current.observe(node);
|
|
};
|
|
|
|
return [ref, isIntersecting];
|
|
}
|