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.

motion.ts 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import type { CSSProperties, RefObject } from 'react';
  2. import { useEffect, useState } from 'react';
  3. /*
  4. * Small motion helpers for our home page. Every one of them shows the finished state straight away for
  5. * visitors whose device asks for less motion.
  6. *
  7. * They check positions on scroll instead of using IntersectionObserver, because its margins were not
  8. * reliable in the embedded browser we test with (the storefront's reveal.tsx does the same).
  9. */
  10. /** True when the visitor's device asks for less motion. */
  11. export function lessMotion(): boolean {
  12. return (
  13. typeof window !== 'undefined' &&
  14. window.matchMedia('(prefers-reduced-motion: reduce)').matches
  15. );
  16. }
  17. /** Runs `check` once now and then at most once a frame while the page scrolls or resizes. Returns a stop function. */
  18. export function onScroll(check: () => void): () => void {
  19. let frame = 0;
  20. const run = () => {
  21. cancelAnimationFrame(frame);
  22. frame = requestAnimationFrame(check);
  23. };
  24. check();
  25. window.addEventListener('scroll', run, { passive: true });
  26. window.addEventListener('resize', run);
  27. return () => {
  28. cancelAnimationFrame(frame);
  29. window.removeEventListener('scroll', run);
  30. window.removeEventListener('resize', run);
  31. };
  32. }
  33. /** How long a revealed item waits after the one before it, so a row of cards comes in one by one. */
  34. export function revealDelay(index: number): CSSProperties {
  35. return { '--d': `${index * 90}ms` } as CSSProperties;
  36. }
  37. /**
  38. * Items marked `data-reveal` inside `root` fade up when they come into view. Items already on the first
  39. * screen are left alone, so nothing the visitor can already see disappears.
  40. */
  41. export function usePageReveal(root: RefObject<HTMLElement | null>): void {
  42. useEffect(() => {
  43. const page = root.current;
  44. if (!page || lessMotion()) {
  45. return;
  46. }
  47. let waiting = Array.from(
  48. page.querySelectorAll<HTMLElement>('[data-reveal]'),
  49. ).filter(
  50. (item) => item.getBoundingClientRect().top > window.innerHeight,
  51. );
  52. waiting.forEach((item) => item.classList.add('h-reveal'));
  53. return onScroll(() => {
  54. waiting = waiting.filter((item) => {
  55. if (
  56. item.getBoundingClientRect().top <
  57. window.innerHeight * 0.9
  58. ) {
  59. item.classList.add('h-revealed');
  60. return false;
  61. }
  62. return true;
  63. });
  64. });
  65. }, [root]);
  66. }
  67. /** 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. */
  68. export function useSeen(ref: RefObject<HTMLElement | null>, at = 0.8): boolean {
  69. const [seen, setSeen] = useState(lessMotion);
  70. useEffect(() => {
  71. if (seen) {
  72. return;
  73. }
  74. return onScroll(() => {
  75. const element = ref.current;
  76. if (
  77. element &&
  78. element.getBoundingClientRect().top < window.innerHeight * at
  79. ) {
  80. setSeen(true);
  81. }
  82. });
  83. }, [seen, ref, at]);
  84. return seen;
  85. }
  86. /**
  87. * Types `text` out letter by letter, like someone typing, after `delay` milliseconds. Returns how many
  88. * letters show and whether it has finished. With `enabled` false the whole text shows at once.
  89. */
  90. export function useTyping(
  91. text: string,
  92. enabled: boolean,
  93. delay = 500,
  94. ): { shown: number; done: boolean } {
  95. const [shown, setShown] = useState(enabled ? 0 : text.length);
  96. useEffect(() => {
  97. if (!enabled) {
  98. return;
  99. }
  100. let count = 0;
  101. let timer = 0;
  102. const next = () => {
  103. count += 1;
  104. setShown(count);
  105. if (count < text.length) {
  106. // A short pause after a full stop or comma, otherwise a slightly uneven, human pace.
  107. const pause = /[.,]/.test(text[count - 1])
  108. ? 240
  109. : 16 + Math.random() * 22;
  110. timer = window.setTimeout(next, pause);
  111. }
  112. };
  113. timer = window.setTimeout(next, delay);
  114. return () => window.clearTimeout(timer);
  115. }, [enabled, text, delay]);
  116. return { shown, done: shown >= text.length };
  117. }