|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect } from "react"; |
| 4 | + |
| 5 | +export default function HomeInteractions() { |
| 6 | + useEffect(() => { |
| 7 | + const root = document.documentElement; |
| 8 | + const sectionLinks = [...document.querySelectorAll("[data-section-link]")]; |
| 9 | + const revealTargets = [...document.querySelectorAll("[data-reveal]")]; |
| 10 | + const pageSections = [...document.querySelectorAll("main section[id]")]; |
| 11 | + const reducedMotionQuery = window.matchMedia("(prefers-reduced-motion: reduce)"); |
| 12 | + const cleanups = []; |
| 13 | + |
| 14 | + const setReducedMotion = (matches) => { |
| 15 | + root.classList.toggle("reduced-motion", matches); |
| 16 | + }; |
| 17 | + |
| 18 | + setReducedMotion(reducedMotionQuery.matches); |
| 19 | + |
| 20 | + const onReducedMotionChange = (event) => setReducedMotion(event.matches); |
| 21 | + if (typeof reducedMotionQuery.addEventListener === "function") { |
| 22 | + reducedMotionQuery.addEventListener("change", onReducedMotionChange); |
| 23 | + cleanups.push(() => reducedMotionQuery.removeEventListener("change", onReducedMotionChange)); |
| 24 | + } |
| 25 | + |
| 26 | + if (!reducedMotionQuery.matches) { |
| 27 | + const revealObserver = new IntersectionObserver( |
| 28 | + (entries) => { |
| 29 | + entries.forEach((entry) => { |
| 30 | + if (entry.isIntersecting) { |
| 31 | + entry.target.classList.add("is-visible"); |
| 32 | + revealObserver.unobserve(entry.target); |
| 33 | + } |
| 34 | + }); |
| 35 | + }, |
| 36 | + { |
| 37 | + threshold: 0.2, |
| 38 | + rootMargin: "0px 0px -8% 0px", |
| 39 | + } |
| 40 | + ); |
| 41 | + |
| 42 | + revealTargets.forEach((target) => revealObserver.observe(target)); |
| 43 | + cleanups.push(() => revealObserver.disconnect()); |
| 44 | + } else { |
| 45 | + revealTargets.forEach((target) => target.classList.add("is-visible")); |
| 46 | + } |
| 47 | + |
| 48 | + const sectionObserver = new IntersectionObserver( |
| 49 | + (entries) => { |
| 50 | + const visibleEntry = entries |
| 51 | + .filter((entry) => entry.isIntersecting) |
| 52 | + .sort((left, right) => right.intersectionRatio - left.intersectionRatio)[0]; |
| 53 | + |
| 54 | + if (!visibleEntry) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + const currentId = visibleEntry.target.id; |
| 59 | + sectionLinks.forEach((link) => { |
| 60 | + link.classList.toggle("is-active", link.dataset.sectionLink === currentId); |
| 61 | + }); |
| 62 | + }, |
| 63 | + { |
| 64 | + threshold: [0.3, 0.5, 0.7], |
| 65 | + rootMargin: "-20% 0px -45% 0px", |
| 66 | + } |
| 67 | + ); |
| 68 | + |
| 69 | + pageSections.forEach((section) => sectionObserver.observe(section)); |
| 70 | + cleanups.push(() => sectionObserver.disconnect()); |
| 71 | + |
| 72 | + const syncScrollProgress = () => { |
| 73 | + const scrollable = document.documentElement.scrollHeight - window.innerHeight; |
| 74 | + const progress = scrollable > 0 ? window.scrollY / scrollable : 0; |
| 75 | + root.style.setProperty("--scroll", progress.toFixed(4)); |
| 76 | + }; |
| 77 | + |
| 78 | + syncScrollProgress(); |
| 79 | + window.addEventListener("scroll", syncScrollProgress, { passive: true }); |
| 80 | + window.addEventListener("resize", syncScrollProgress); |
| 81 | + cleanups.push(() => { |
| 82 | + window.removeEventListener("scroll", syncScrollProgress); |
| 83 | + window.removeEventListener("resize", syncScrollProgress); |
| 84 | + }); |
| 85 | + |
| 86 | + if (!reducedMotionQuery.matches) { |
| 87 | + const onPointerMove = (event) => { |
| 88 | + const x = `${((event.clientX / window.innerWidth) * 100).toFixed(2)}%`; |
| 89 | + const y = `${((event.clientY / window.innerHeight) * 100).toFixed(2)}%`; |
| 90 | + root.style.setProperty("--pointer-x", x); |
| 91 | + root.style.setProperty("--pointer-y", y); |
| 92 | + }; |
| 93 | + |
| 94 | + window.addEventListener("pointermove", onPointerMove, { passive: true }); |
| 95 | + cleanups.push(() => window.removeEventListener("pointermove", onPointerMove)); |
| 96 | + } |
| 97 | + |
| 98 | + return () => cleanups.forEach((cleanup) => cleanup()); |
| 99 | + }, []); |
| 100 | + |
| 101 | + return null; |
| 102 | +} |
0 commit comments