forge
Explore
Log in

Don't you dare go Hollow

hezarfen_landing

Clone
★ 0

0 watching0 forksTypeScript581.6 KiB94b1804Burak Boduroğlu2 months ago15 commits

Reveal.tsx43 lines, 1.2 KiBHistoryRawDownload
1import { createSignal, onCleanup, onMount, type JSX } from "solid-js";
2
3/**
4 * Scroll-triggered entrance. `armed` is only ever set on the client, so the
5 * server-rendered markup stays readable when the script never arrives, and
6 * `shown` latches the first time the block enters the viewport. `--i`
7 * staggers siblings sharing one grid.
8 */
9export default function Reveal(props: {
10 index?: number;
11 class?: string;
12 children: JSX.Element;
13}) {
14 const [armed, setArmed] = createSignal(false);
15 const [shown, setShown] = createSignal(false);
16 let el!: HTMLDivElement;
17
18 onMount(() => {
19 if (!("IntersectionObserver" in window)) return;
20 setArmed(true);
21 const observer = new IntersectionObserver(
22 entries => {
23 if (!entries.some(entry => entry.isIntersecting)) return;
24 setShown(true);
25 observer.disconnect();
26 },
27 { threshold: 0.12, rootMargin: "0px 0px -6% 0px" },
28 );
29 observer.observe(el);
30 onCleanup(() => observer.disconnect());
31 });
32
33 return (
34 <div
35 ref={el}
36 class={`hz-reveal ${props.class ?? ""}`}
37 classList={{ "is-armed": armed(), "is-in": shown() }}
38 style={{ "--i": String(props.index ?? 0) }}
39 >
40 {props.children}
41 </div>
42 );
43}