x
This commit is contained in:
1 parent
ce2f4cf0c8
commit
5a87e7f77d
21 files changed
+1083
-88
No files matched your search
@@ -0,0 +1,37 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
|
||||
interface UseScrollRevealOptions {
|
||||
threshold?: number;
|
||||
rootMargin?: string;
|
||||
once?: boolean;
|
||||
}
|
||||
|
||||
export function useScrollReveal<T extends HTMLElement = HTMLDivElement>(
|
||||
options: UseScrollRevealOptions = {}
|
||||
) {
|
||||
const { threshold = 0.1, rootMargin = '0px 0px -60px 0px', once = true } = options;
|
||||
const ref = useRef<T>(null);
|
||||
const [isVisible, setIsVisible] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const el = ref.current;
|
||||
if (!el) return;
|
||||
|
||||
const observer = new IntersectionObserver(
|
||||
([entry]) => {
|
||||
if (entry.isIntersecting) {
|
||||
setIsVisible(true);
|
||||
if (once) observer.disconnect();
|
||||
} else if (!once) {
|
||||
setIsVisible(false);
|
||||
}
|
||||
},
|
||||
{ threshold, rootMargin }
|
||||
);
|
||||
|
||||
observer.observe(el);
|
||||
return () => observer.disconnect();
|
||||
}, [threshold, rootMargin, once]);
|
||||
|
||||
return { ref, isVisible };
|
||||
}
|
||||
Reference in new issue
Block a user