Files

160 lines
3.6 KiB
TypeScript

"use client";
import {
useEffect,
useRef,
useState,
type CSSProperties,
type HTMLAttributes,
type ReactNode,
} from "react";
const REDUCED_MOTION_QUERY = "(prefers-reduced-motion: reduce)";
export type RevealVariant = "fade-in" | "fade-up";
type RevealOptions = {
amount?: number;
delay?: number;
once?: boolean;
rootMargin?: string;
variant?: RevealVariant;
};
export type RevealProps = Omit<HTMLAttributes<HTMLDivElement>, "children"> &
RevealOptions & {
children: ReactNode;
};
type RevealStyle = CSSProperties & {
"--reveal-delay"?: string;
};
function clampThreshold(amount: number) {
if (!Number.isFinite(amount)) return 0.2;
return Math.min(1, Math.max(0, amount));
}
function useReveal({
amount,
once,
rootMargin,
}: Required<Pick<RevealOptions, "amount" | "once" | "rootMargin">>) {
const elementRef = useRef<HTMLDivElement>(null);
const hasRevealedRef = useRef(false);
const [isVisible, setIsVisible] = useState(false);
useEffect(() => {
const element = elementRef.current;
if (!element) return;
if (once && hasRevealedRef.current) {
setIsVisible(true);
return;
}
const motionPreference =
typeof window.matchMedia === "function"
? window.matchMedia(REDUCED_MOTION_QUERY)
: null;
let observer: IntersectionObserver | undefined;
const observe = () => {
observer?.disconnect();
if (
motionPreference?.matches ||
!("IntersectionObserver" in window) ||
(once && hasRevealedRef.current)
) {
hasRevealedRef.current = true;
setIsVisible(true);
return;
}
const threshold = clampThreshold(amount);
observer = new IntersectionObserver(
([entry]) => {
if (!entry) return;
if (entry.isIntersecting && entry.intersectionRatio >= threshold) {
hasRevealedRef.current = true;
setIsVisible(true);
if (once) observer?.unobserve(entry.target);
return;
}
if (!once) setIsVisible(false);
},
{ rootMargin, threshold },
);
observer.observe(element);
};
observe();
motionPreference?.addEventListener("change", observe);
return () => {
observer?.disconnect();
motionPreference?.removeEventListener("change", observe);
};
}, [amount, once, rootMargin]);
return { elementRef, isVisible };
}
export function Reveal({
amount = 0.2,
children,
className = "",
delay = 0,
once = true,
rootMargin = "0px 0px -8% 0px",
style,
variant = "fade-up",
...props
}: RevealProps) {
const { elementRef, isVisible } = useReveal({ amount, once, rootMargin });
const classes = [
"reveal",
`reveal--${variant}`,
isVisible ? "is-visible" : "",
"motion-reduce:!transform-none motion-reduce:!opacity-100 motion-reduce:!transition-none",
className,
]
.filter(Boolean)
.join(" ");
const revealStyle: RevealStyle = {
...style,
"--reveal-delay": `${Number.isFinite(delay) ? Math.max(0, delay) : 0}ms`,
};
return (
<div
{...props}
ref={elementRef}
className={classes}
data-reveal-state={isVisible ? "visible" : "hidden"}
style={revealStyle}
>
{children}
</div>
);
}
export type ImageRevealProps = Omit<RevealProps, "variant">;
export function ImageReveal({ children, className = "", ...props }: ImageRevealProps) {
const classes = ["image-reveal", className].filter(Boolean).join(" ");
return (
<Reveal {...props} variant="fade-in" className={classes}>
<div className="image-reveal-mask">{children}</div>
</Reveal>
);
}