live.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. "use client";
  2. import * as React from "react";
  3. const STILLNESS = "(prefers-reduced-motion: reduce)";
  4. const MARGIN = "120px 0px";
  5. const LONGEST_FRAME = 1 / 30;
  6. export const asksForStillness = () => typeof window !== "undefined" && window.matchMedia(STILLNESS).matches;
  7. export function useStillness() {
  8. return React.useSyncExternalStore(
  9. (changed) => {
  10. const query = window.matchMedia(STILLNESS);
  11. query.addEventListener("change", changed);
  12. return () => query.removeEventListener("change", changed);
  13. },
  14. asksForStillness,
  15. () => false,
  16. );
  17. }
  18. export function useInView<T extends Element>(ref: React.RefObject<T | null>) {
  19. const [inView, setInView] = React.useState(false);
  20. React.useEffect(() => {
  21. const node = ref.current;
  22. if (!node) return;
  23. const observer = new IntersectionObserver((entries) => setInView(Boolean(entries.at(-1)?.isIntersecting)), { rootMargin: MARGIN });
  24. observer.observe(node);
  25. return () => observer.disconnect();
  26. }, [ref]);
  27. return inView;
  28. }
  29. export function Stage({ className, children }: { className: string; children: React.ReactNode }) {
  30. const ref = React.useRef<HTMLDivElement>(null);
  31. const inView = useInView(ref);
  32. return (
  33. <div ref={ref} className={className} data-inview={inView ? "" : undefined}>
  34. {children}
  35. </div>
  36. );
  37. }
  38. export function useReadout<T extends Element>() {
  39. const anchor = React.useRef<T>(null);
  40. const slot = React.useRef<Element | null>(null);
  41. const said = React.useRef("");
  42. React.useEffect(() => {
  43. slot.current = anchor.current?.closest(".iso-plate")?.querySelector("[data-readout]") ?? null;
  44. }, []);
  45. const say = React.useCallback((text: string) => {
  46. if (text === said.current) return;
  47. said.current = text;
  48. if (slot.current) slot.current.textContent = text;
  49. }, []);
  50. return { anchor, say };
  51. }
  52. export function useLoop(tick: (dt: number, calm: boolean) => boolean, running: boolean) {
  53. const latest = React.useRef(tick);
  54. latest.current = tick;
  55. const frame = React.useRef(0);
  56. const last = React.useRef(0);
  57. const kick = React.useCallback(() => {
  58. if (frame.current) return;
  59. last.current = 0;
  60. const step = (now: number) => {
  61. const dt = last.current ? Math.min((now - last.current) / 1000, LONGEST_FRAME) : 1 / 60;
  62. last.current = now;
  63. const more = latest.current(dt, asksForStillness());
  64. frame.current = more ? requestAnimationFrame(step) : 0;
  65. };
  66. frame.current = requestAnimationFrame(step);
  67. }, []);
  68. React.useEffect(() => {
  69. if (running) kick();
  70. else {
  71. cancelAnimationFrame(frame.current);
  72. frame.current = 0;
  73. }
  74. return () => {
  75. cancelAnimationFrame(frame.current);
  76. frame.current = 0;
  77. };
  78. }, [running, kick]);
  79. return kick;
  80. }
  81. export function follow(current: number, target: number, dt: number, seconds: number, calm = false) {
  82. if (calm) return target;
  83. const next = current + (target - current) * (1 - Math.exp(-dt / seconds));
  84. return Math.abs(target - next) < 1e-3 ? target : next;
  85. }
  86. export interface Spring {
  87. at: number;
  88. rate: number;
  89. }
  90. export function spring(state: Spring, target: number, dt: number, stiffness: number, damping: number, calm = false) {
  91. const drag = calm ? 2 * Math.sqrt(stiffness) : damping;
  92. const steps = Math.max(Math.ceil(dt * 240), 1);
  93. const slice = dt / steps;
  94. for (let step = 0; step < steps; step++) {
  95. state.rate += (target - state.at) * stiffness * slice - state.rate * drag * slice;
  96. state.at += state.rate * slice;
  97. }
  98. if (Math.abs(target - state.at) < 5e-4 && Math.abs(state.rate) < 5e-4) {
  99. state.at = target;
  100. state.rate = 0;
  101. }
  102. return state.at === target && state.rate === 0;
  103. }
  104. export function useEased(target: number, seconds = 0.12) {
  105. const [value, setValue] = React.useState(target);
  106. const shown = React.useRef(target);
  107. useLoop((dt, calm) => {
  108. shown.current = follow(shown.current, target, dt, seconds, calm);
  109. setValue(shown.current);
  110. return shown.current !== target;
  111. }, shown.current !== target);
  112. return value;
  113. }