/*! Strand UI | MIT License | dillingerstaffing.com */ import type { JSX } from "preact"; import { forwardRef } from "preact/compat"; import { useEffect, useRef, useState } from "preact/hooks"; import { cx, mergeRefs } from "../../internal/index.js"; export interface ScrollRevealProps extends JSX.HTMLAttributes { /** IntersectionObserver threshold, 0 to 1. */ threshold?: number; /** Reveal once and stop observing. */ once?: boolean; } /** * Reveals its children when they scroll into view. * * @example *

Fades in on scroll.

*/ export const ScrollReveal = forwardRef(({ threshold = 0.1, once = true, className = "", children, ...rest }, ref) => { const own = useRef(null); const [visible, setVisible] = useState(false); useEffect(() => { const el = own.current; if (!el || typeof IntersectionObserver === "undefined") return; const observer = new IntersectionObserver( (entries) => { for (const entry of entries) { if (entry.isIntersecting) { setVisible(true); if (once) observer.unobserve(entry.target); } else if (!once) { setVisible(false); } } }, { threshold }, ); observer.observe(el); return () => observer.disconnect(); }, [threshold, once]); return (
{children}
); }); ScrollReveal.displayName = "ScrollReveal";