47 lines
1.0 KiB
TypeScript
47 lines
1.0 KiB
TypeScript
"use client";
|
|
|
|
import { type CSSProperties, type ReactNode, useEffect, useRef, useState } from "react";
|
|
|
|
interface RevealOnViewProps {
|
|
children: ReactNode;
|
|
className?: string;
|
|
delay?: number;
|
|
}
|
|
|
|
export function RevealOnView({
|
|
children,
|
|
className = "",
|
|
delay = 0,
|
|
}: RevealOnViewProps) {
|
|
const ref = useRef<HTMLDivElement>(null);
|
|
const [isVisible, setIsVisible] = useState(false);
|
|
|
|
useEffect(() => {
|
|
const element = ref.current;
|
|
if (!element) return;
|
|
|
|
const observer = new IntersectionObserver(
|
|
([entry]) => {
|
|
if (entry.isIntersecting) {
|
|
setIsVisible(true);
|
|
observer.unobserve(element);
|
|
}
|
|
},
|
|
{ rootMargin: "0px 0px 18% 0px", threshold: 0.01 }
|
|
);
|
|
|
|
observer.observe(element);
|
|
return () => observer.disconnect();
|
|
}, []);
|
|
|
|
return (
|
|
<div
|
|
ref={ref}
|
|
className={`reveal-on-view ${isVisible ? "is-visible" : ""} ${className}`}
|
|
style={{ "--reveal-delay": `${delay}ms` } as CSSProperties}
|
|
>
|
|
{children}
|
|
</div>
|
|
);
|
|
}
|