| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import { usePage } from '@inertiajs/react';
- import { useEffect } from 'react';
-
- /**
- * Gentle motion: sections below the first screen fade up as they come into view.
- *
- * Only sections the shopper cannot see yet are hidden, so the first screen is always complete, and
- * nothing moves for visitors who ask their device for less motion. A scroll check is used rather than
- * an intersection observer, because some embedded browsers do not report those reliably.
- */
- export function Reveal({ enabled }: { enabled: boolean }) {
- const { url } = usePage();
-
- useEffect(() => {
- if (
- !enabled ||
- window.matchMedia('(prefers-reduced-motion: reduce)').matches
- ) {
- return;
- }
-
- const bands = Array.from(
- document.querySelectorAll<HTMLElement>('.sf-band'),
- ).filter(
- // Only sections that start below the first screen. Anything already showing stays put.
- (band) => band.getBoundingClientRect().top >= window.innerHeight,
- );
-
- bands.forEach((band) => band.classList.add('sf-reveal'));
-
- let frame = 0;
- const check = () => {
- cancelAnimationFrame(frame);
- frame = requestAnimationFrame(() => {
- for (const band of bands) {
- if (
- band.getBoundingClientRect().top <
- window.innerHeight * 0.92
- ) {
- band.classList.add('sf-revealed');
- }
- }
- });
- };
-
- check();
- window.addEventListener('scroll', check, { passive: true });
- window.addEventListener('resize', check);
-
- return () => {
- cancelAnimationFrame(frame);
- window.removeEventListener('scroll', check);
- window.removeEventListener('resize', check);
- bands.forEach((band) =>
- band.classList.remove('sf-reveal', 'sf-revealed'),
- );
- };
- }, [enabled, url]);
-
- return null;
- }
|