mirror of
https://github.com/DeBrosOfficial/orama.git
synced 2026-06-17 03:14:12 +00:00
26 lines
595 B
TypeScript
26 lines
595 B
TypeScript
import { useEffect, useRef, useState } from "react";
|
|
|
|
export function useInView(threshold = 0.1) {
|
|
const ref = useRef<HTMLDivElement>(null);
|
|
const [isInView, setIsInView] = useState(false);
|
|
|
|
useEffect(() => {
|
|
const el = ref.current;
|
|
if (!el) return;
|
|
|
|
const observer = new IntersectionObserver(
|
|
([entry]) => {
|
|
if (entry.isIntersecting) {
|
|
setIsInView(true);
|
|
observer.unobserve(el);
|
|
}
|
|
},
|
|
{ threshold },
|
|
);
|
|
observer.observe(el);
|
|
return () => observer.disconnect();
|
|
}, [threshold]);
|
|
|
|
return { ref, isInView };
|
|
}
|