cursor-lens.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. "use client";
  2. import { useEffect, useRef, useState, useSyncExternalStore } from "react";
  3. import { supportsBackdropRefraction } from "./displacement";
  4. import { useLiquidGlass } from "./liquid-glass";
  5. type Mode = "none" | "lens" | "label";
  6. const LENS = 136;
  7. const FINE = "(hover: hover) and (pointer: fine)";
  8. const REDUCED = "(prefers-reduced-motion: reduce)";
  9. const subscribe = (onChange: () => void) => {
  10. const queries = [window.matchMedia(FINE), window.matchMedia(REDUCED)];
  11. queries.forEach((q) => q.addEventListener("change", onChange));
  12. return () =>
  13. queries.forEach((q) => q.removeEventListener("change", onChange));
  14. };
  15. const allowed = () =>
  16. window.matchMedia(FINE).matches && !window.matchMedia(REDUCED).matches;
  17. /**
  18. * A glass companion for the cursor (desktop only). Over images marked
  19. * `data-lens` it becomes a liquid-glass loupe; over anything with
  20. * `data-cursor="…"` it shows a small glass label describing the action.
  21. */
  22. export function CursorLens() {
  23. const enabled = useSyncExternalStore(subscribe, allowed, () => false);
  24. return enabled ? <LensLayer /> : null;
  25. }
  26. function LensLayer() {
  27. const lensRef = useRef<HTMLDivElement>(null);
  28. const lens = useLiquidGlass(lensRef, {
  29. magnify: 1.5,
  30. bezel: 18,
  31. thickness: 26,
  32. chroma: 0.05,
  33. blur: 0,
  34. saturate: 1.05,
  35. });
  36. const pillRef = useRef<HTMLDivElement>(null);
  37. const [mode, setMode] = useState<Mode>("none");
  38. const [label, setLabel] = useState("");
  39. useEffect(() => {
  40. const canLens = supportsBackdropRefraction();
  41. const target = { x: -200, y: -200 };
  42. const lensPos = { x: -200, y: -200 };
  43. const pillPos = { x: -200, y: -200 };
  44. let frame = 0;
  45. let currentMode: Mode = "none";
  46. let currentLabel = "";
  47. let last: PointerEvent | null = null;
  48. const tick = () => {
  49. frame = 0;
  50. lensPos.x += (target.x - lensPos.x) * 0.45;
  51. lensPos.y += (target.y - lensPos.y) * 0.45;
  52. pillPos.x += (target.x - pillPos.x) * 0.22;
  53. pillPos.y += (target.y - pillPos.y) * 0.22;
  54. if (lensRef.current) {
  55. lensRef.current.style.translate = `${lensPos.x - LENS / 2}px ${lensPos.y - LENS / 2}px`;
  56. }
  57. if (pillRef.current) {
  58. pillRef.current.style.translate = `${pillPos.x + 16}px ${pillPos.y + 18}px`;
  59. }
  60. if (
  61. Math.abs(target.x - pillPos.x) + Math.abs(target.y - pillPos.y) >
  62. 0.3
  63. ) {
  64. frame = requestAnimationFrame(tick);
  65. }
  66. };
  67. const onMove = (e: PointerEvent) => {
  68. if (e.pointerType !== "mouse") return;
  69. last = e;
  70. target.x = e.clientX;
  71. target.y = e.clientY;
  72. const el = (e.target as Element | null)?.closest?.(
  73. "[data-lens],[data-cursor]",
  74. );
  75. let next: Mode = "none";
  76. let text = "";
  77. if (el?.hasAttribute("data-lens") && canLens && !e.buttons) next = "lens";
  78. else if (el?.hasAttribute("data-cursor")) {
  79. next = "label";
  80. text = el.getAttribute("data-cursor") ?? "";
  81. }
  82. if (next !== currentMode || text !== currentLabel) {
  83. if (currentMode === "none") {
  84. lensPos.x = pillPos.x = target.x;
  85. lensPos.y = pillPos.y = target.y;
  86. }
  87. currentMode = next;
  88. currentLabel = text;
  89. setMode(next);
  90. if (text) setLabel(text);
  91. }
  92. if (!frame) frame = requestAnimationFrame(tick);
  93. };
  94. const onLeave = () => {
  95. if (currentMode === "none") return;
  96. currentMode = "none";
  97. currentLabel = "";
  98. setMode("none");
  99. };
  100. // A click can change what the thing under the cursor does (Meet Yilu →
  101. // Back to the owner), so read its label again once it has re-rendered.
  102. const onClick = () => {
  103. requestAnimationFrame(() => {
  104. if (last && currentMode !== "none") onMove(last);
  105. });
  106. };
  107. window.addEventListener("pointermove", onMove, { passive: true });
  108. window.addEventListener("click", onClick, { passive: true });
  109. window.addEventListener("scroll", onLeave, { passive: true });
  110. document.documentElement.addEventListener("pointerleave", onLeave);
  111. window.addEventListener("blur", onLeave);
  112. return () => {
  113. cancelAnimationFrame(frame);
  114. window.removeEventListener("pointermove", onMove);
  115. window.removeEventListener("click", onClick);
  116. window.removeEventListener("scroll", onLeave);
  117. document.documentElement.removeEventListener("pointerleave", onLeave);
  118. window.removeEventListener("blur", onLeave);
  119. };
  120. }, []);
  121. return (
  122. <>
  123. <div
  124. ref={lensRef}
  125. aria-hidden
  126. className="lg pointer-events-none fixed top-0 left-0 z-[70] rounded-full transition-[opacity,scale] duration-300 ease-out [--lg-blur:0px]"
  127. data-refract={lens.glassProps["data-refract"]}
  128. style={{
  129. ...lens.glassProps.style,
  130. width: LENS,
  131. height: LENS,
  132. opacity: mode === "lens" ? 1 : 0,
  133. scale: mode === "lens" ? "1" : "0.3",
  134. }}
  135. >
  136. {lens.filter}
  137. <span className="lg-caustic" />
  138. </div>
  139. <div
  140. ref={pillRef}
  141. aria-hidden
  142. className="lg pointer-events-none fixed top-0 left-0 z-[70] rounded-full px-3 py-1.5 text-[12px] font-medium whitespace-nowrap transition-[opacity,scale] duration-200 ease-out"
  143. style={{
  144. opacity: mode === "label" ? 1 : 0,
  145. scale: mode === "label" ? "1" : "0.6",
  146. transformOrigin: "0 0",
  147. }}
  148. >
  149. <span className="lg-caustic" />
  150. <span>{label}</span>
  151. </div>
  152. </>
  153. );
  154. }