AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reveal.tsx 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { usePage } from '@inertiajs/react';
  2. import { useEffect } from 'react';
  3. /**
  4. * Gentle motion: sections below the first screen fade up as they come into view.
  5. *
  6. * Only sections the shopper cannot see yet are hidden, so the first screen is always complete, and
  7. * nothing moves for visitors who ask their device for less motion. A scroll check is used rather than
  8. * an intersection observer, because some embedded browsers do not report those reliably.
  9. */
  10. export function Reveal({ enabled }: { enabled: boolean }) {
  11. const { url } = usePage();
  12. useEffect(() => {
  13. if (
  14. !enabled ||
  15. window.matchMedia('(prefers-reduced-motion: reduce)').matches
  16. ) {
  17. return;
  18. }
  19. const bands = Array.from(
  20. document.querySelectorAll<HTMLElement>('.sf-band'),
  21. ).filter(
  22. // Only sections that start below the first screen. Anything already showing stays put.
  23. (band) => band.getBoundingClientRect().top >= window.innerHeight,
  24. );
  25. bands.forEach((band) => band.classList.add('sf-reveal'));
  26. let frame = 0;
  27. const check = () => {
  28. cancelAnimationFrame(frame);
  29. frame = requestAnimationFrame(() => {
  30. for (const band of bands) {
  31. if (
  32. band.getBoundingClientRect().top <
  33. window.innerHeight * 0.92
  34. ) {
  35. band.classList.add('sf-revealed');
  36. }
  37. }
  38. });
  39. };
  40. check();
  41. window.addEventListener('scroll', check, { passive: true });
  42. window.addEventListener('resize', check);
  43. return () => {
  44. cancelAnimationFrame(frame);
  45. window.removeEventListener('scroll', check);
  46. window.removeEventListener('resize', check);
  47. bands.forEach((band) =>
  48. band.classList.remove('sf-reveal', 'sf-revealed'),
  49. );
  50. };
  51. }, [enabled, url]);
  52. return null;
  53. }