import type { CSSProperties, RefObject } from 'react'; import { useEffect, useState } from 'react'; /* * Small motion helpers for our home page. Every one of them shows the finished state straight away for * visitors whose device asks for less motion. * * They check positions on scroll instead of using IntersectionObserver, because its margins were not * reliable in the embedded browser we test with (the storefront's reveal.tsx does the same). */ /** True when the visitor's device asks for less motion. */ export function lessMotion(): boolean { return ( typeof window !== 'undefined' && window.matchMedia('(prefers-reduced-motion: reduce)').matches ); } /** Runs `check` once now and then at most once a frame while the page scrolls or resizes. Returns a stop function. */ export function onScroll(check: () => void): () => void { let frame = 0; const run = () => { cancelAnimationFrame(frame); frame = requestAnimationFrame(check); }; check(); window.addEventListener('scroll', run, { passive: true }); window.addEventListener('resize', run); return () => { cancelAnimationFrame(frame); window.removeEventListener('scroll', run); window.removeEventListener('resize', run); }; } /** How long a revealed item waits after the one before it, so a row of cards comes in one by one. */ export function revealDelay(index: number): CSSProperties { return { '--d': `${index * 90}ms` } as CSSProperties; } /** * Items marked `data-reveal` inside `root` fade up when they come into view. Items already on the first * screen are left alone, so nothing the visitor can already see disappears. */ export function usePageReveal(root: RefObject): void { useEffect(() => { const page = root.current; if (!page || lessMotion()) { return; } let waiting = Array.from( page.querySelectorAll('[data-reveal]'), ).filter( (item) => item.getBoundingClientRect().top > window.innerHeight, ); waiting.forEach((item) => item.classList.add('h-reveal')); return onScroll(() => { waiting = waiting.filter((item) => { if ( item.getBoundingClientRect().top < window.innerHeight * 0.9 ) { item.classList.add('h-revealed'); return false; } return true; }); }); }, [root]); } /** True once the top of `ref` has come up to `at` of the screen's height (0.8 is 80% of the way down). Stays true. */ export function useSeen(ref: RefObject, at = 0.8): boolean { const [seen, setSeen] = useState(lessMotion); useEffect(() => { if (seen) { return; } return onScroll(() => { const element = ref.current; if ( element && element.getBoundingClientRect().top < window.innerHeight * at ) { setSeen(true); } }); }, [seen, ref, at]); return seen; } /** * Types `text` out letter by letter, like someone typing, after `delay` milliseconds. Returns how many * letters show and whether it has finished. With `enabled` false the whole text shows at once. */ export function useTyping( text: string, enabled: boolean, delay = 500, ): { shown: number; done: boolean } { const [shown, setShown] = useState(enabled ? 0 : text.length); useEffect(() => { if (!enabled) { return; } let count = 0; let timer = 0; const next = () => { count += 1; setShown(count); if (count < text.length) { // A short pause after a full stop or comma, otherwise a slightly uneven, human pace. const pause = /[.,]/.test(text[count - 1]) ? 240 : 16 + Math.random() * 22; timer = window.setTimeout(next, pause); } }; timer = window.setTimeout(next, delay); return () => window.clearTimeout(timer); }, [enabled, text, delay]); return { shown, done: shown >= text.length }; }