/** * Site-wide motion: Lenis smooth scrolling (desktop; touch keeps native * scrolling) driven by GSAP's ticker, plus ScrollTrigger reveals. * * data-reveal fade + rise when scrolled into view * data-reveal="tilt" same, with a slight paper-like rotation * data-reveal-stagger reveal direct children one after another * data-parallax="0.2" drift vertically while scrolling (desktop only) * * Everything is skipped when the visitor prefers reduced motion. */ import Lenis from 'lenis'; import 'lenis/dist/lenis.css'; import { gsap } from 'gsap'; import { ScrollTrigger } from 'gsap/ScrollTrigger'; gsap.registerPlugin(ScrollTrigger); const reduce = window.matchMedia('(prefers-reduced-motion: reduce)'); let lenis: Lenis | null = null; let ctx: gsap.Context | null = null; function startLenis() { if (lenis || reduce.matches) return; lenis = new Lenis({ lerp: 0.12, wheelMultiplier: 1, autoRaf: false }); lenis.on('scroll', ScrollTrigger.update); gsap.ticker.add((time) => lenis?.raf(time * 1000)); gsap.ticker.lagSmoothing(0); } function setup() { ctx?.revert(); ctx = gsap.context(() => { if (reduce.matches) return; gsap.utils.toArray('[data-reveal]').forEach((el) => { const targets = el.hasAttribute('data-reveal-stagger') ? Array.from(el.children) : [el]; gsap.from(targets, { y: 36, autoAlpha: 0, rotate: el.dataset.reveal === 'tilt' ? -2.5 : 0, duration: 0.85, ease: 'power3.out', stagger: 0.07, scrollTrigger: { trigger: el, start: 'top 90%', once: true }, }); }); const mm = gsap.matchMedia(); mm.add('(min-width: 52rem)', () => { gsap.utils.toArray('[data-parallax]').forEach((el) => { const amount = parseFloat(el.dataset.parallax || '0.15'); gsap.to(el, { yPercent: -100 * amount, ease: 'none', scrollTrigger: { trigger: el, start: 'top bottom', end: 'bottom top', scrub: true }, }); }); }); }); lenis?.resize(); ScrollTrigger.refresh(); } document.addEventListener('astro:page-load', () => { startLenis(); setup(); }); document.addEventListener('astro:before-swap', () => { ctx?.revert(); ctx = null; }); // Expose for menus that need to lock scrolling. (window as unknown as { __lenis?: () => Lenis | null }).__lenis = () => lenis;