cursor-lens.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. const tick = () => {
  48. frame = 0;
  49. lensPos.x += (target.x - lensPos.x) * 0.45;
  50. lensPos.y += (target.y - lensPos.y) * 0.45;
  51. pillPos.x += (target.x - pillPos.x) * 0.22;
  52. pillPos.y += (target.y - pillPos.y) * 0.22;
  53. if (lensRef.current) {
  54. lensRef.current.style.translate = `${lensPos.x - LENS / 2}px ${lensPos.y - LENS / 2}px`;
  55. }
  56. if (pillRef.current) {
  57. pillRef.current.style.translate = `${pillPos.x + 16}px ${pillPos.y + 18}px`;
  58. }
  59. if (
  60. Math.abs(target.x - pillPos.x) + Math.abs(target.y - pillPos.y) >
  61. 0.3
  62. ) {
  63. frame = requestAnimationFrame(tick);
  64. }
  65. };
  66. const onMove = (e: PointerEvent) => {
  67. if (e.pointerType !== "mouse") return;
  68. target.x = e.clientX;
  69. target.y = e.clientY;
  70. const el = (e.target as Element | null)?.closest?.(
  71. "[data-lens],[data-cursor]",
  72. );
  73. let next: Mode = "none";
  74. let text = "";
  75. if (el?.hasAttribute("data-lens") && canLens && !e.buttons) next = "lens";
  76. else if (el?.hasAttribute("data-cursor")) {
  77. next = "label";
  78. text = el.getAttribute("data-cursor") ?? "";
  79. }
  80. if (next !== currentMode || text !== currentLabel) {
  81. if (currentMode === "none") {
  82. lensPos.x = pillPos.x = target.x;
  83. lensPos.y = pillPos.y = target.y;
  84. }
  85. currentMode = next;
  86. currentLabel = text;
  87. setMode(next);
  88. if (text) setLabel(text);
  89. }
  90. if (!frame) frame = requestAnimationFrame(tick);
  91. };
  92. const onLeave = () => {
  93. if (currentMode === "none") return;
  94. currentMode = "none";
  95. currentLabel = "";
  96. setMode("none");
  97. };
  98. window.addEventListener("pointermove", onMove, { passive: true });
  99. window.addEventListener("scroll", onLeave, { passive: true });
  100. document.documentElement.addEventListener("pointerleave", onLeave);
  101. window.addEventListener("blur", onLeave);
  102. return () => {
  103. cancelAnimationFrame(frame);
  104. window.removeEventListener("pointermove", onMove);
  105. window.removeEventListener("scroll", onLeave);
  106. document.documentElement.removeEventListener("pointerleave", onLeave);
  107. window.removeEventListener("blur", onLeave);
  108. };
  109. }, []);
  110. return (
  111. <>
  112. <div
  113. ref={lensRef}
  114. aria-hidden
  115. className="lg pointer-events-none fixed top-0 left-0 z-[70] rounded-full transition-[opacity,scale] duration-300 ease-out [--lg-blur:0px]"
  116. data-refract={lens.glassProps["data-refract"]}
  117. style={{
  118. ...lens.glassProps.style,
  119. width: LENS,
  120. height: LENS,
  121. opacity: mode === "lens" ? 1 : 0,
  122. scale: mode === "lens" ? "1" : "0.3",
  123. }}
  124. >
  125. {lens.filter}
  126. <span className="lg-caustic" />
  127. </div>
  128. <div
  129. ref={pillRef}
  130. aria-hidden
  131. 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"
  132. style={{
  133. opacity: mode === "label" ? 1 : 0,
  134. scale: mode === "label" ? "1" : "0.6",
  135. transformOrigin: "0 0",
  136. }}
  137. >
  138. <span className="lg-caustic" />
  139. <span>{label}</span>
  140. </div>
  141. </>
  142. );
  143. }