File size: 4,875 Bytes
0d811c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { useEffect, useRef, useState } from 'react';
import './cursor.css';

/* Mouse effects after Shery.js (Sheryians): a follower ring that trails the pointer with an expo ease, grows into a ring

   over links and buttons and into an "Open" bubble over rows that open something; primary buttons lean towards the

   pointer (magnet). The system cursor always stays. Only for a mouse (never touch or pen), never when the computer asks

   for reduced motion, and each person can switch it off (account menu); the choice stays in this browser. */

const KEY = 'pw.cursor';
const EVT = 'pw-cursor';

export const cursorEffectsOn = () => { try { return window.localStorage.getItem(KEY) !== 'off'; } catch { return true; } };
export function setCursorEffects(on: boolean) {
  try { window.localStorage.setItem(KEY, on ? 'on' : 'off'); } catch { /* storage blocked: the choice lasts this page */ }
  window.dispatchEvent(new Event(EVT));
}
export function useCursorEffects() {
  const [on, setOn] = useState(cursorEffectsOn);
  useEffect(() => {
    const f = () => setOn(cursorEffectsOn());
    window.addEventListener(EVT, f);
    return () => window.removeEventListener(EVT, f);
  }, []);
  return on;
}

const TEXT = 'input, textarea, select, [contenteditable="true"], .mantine-Input-input';
const OPENS = '[data-cursor="open"]';
const PRIMARY = '.mantine-Button-root[data-variant="filled"]';
const CLICKS = 'a, button, [role="button"], [role="tab"], [role="menuitem"], [role="option"], label, summary';

export function CursorFx() {
  const on = useCursorEffects();
  const [able, setAble] = useState(false);
  const ring = useRef<HTMLDivElement>(null);
  const label = useRef<HTMLSpanElement>(null);

  useEffect(() => {
    const fine = window.matchMedia?.('(pointer: fine)');
    const still = window.matchMedia?.('(prefers-reduced-motion: reduce)');
    const update = () => setAble(!!fine?.matches && !still?.matches);
    update();
    fine?.addEventListener?.('change', update);
    still?.addEventListener?.('change', update);
    return () => { fine?.removeEventListener?.('change', update); still?.removeEventListener?.('change', update); };
  }, []);

  useEffect(() => {
    const el = ring.current;
    if (!on || !able || !el) return;
    let magnet: HTMLElement | null = null;
    const release = () => { if (magnet) { magnet.style.translate = ''; magnet = null; } };
    const move = (e: PointerEvent) => {
      if (e.pointerType !== 'mouse') return;
      el.style.transform = `translate(${e.clientX}px, ${e.clientY}px)`;
      const t = e.target instanceof Element ? e.target : null;
      let mode = 'idle';
      let words = '';
      const primary = t?.closest<HTMLElement>(PRIMARY) ?? null;
      if (t?.closest(TEXT)) mode = 'text';
      else if (primary) mode = 'button';
      else {
        const row = t?.closest(OPENS) ?? null;
        const ctl = t?.closest(CLICKS) ?? null;
        // a row that opens something (it may itself be the link); a button or link inside the row is its own target
        if (row && (!ctl || ctl === row || ctl.contains(row))) { mode = 'row'; words = 'Open'; }
        else if (ctl) mode = 'link';
      }
      // the magnet: a primary button leans a little towards the pointer while it is over it
      if (primary && !primary.hasAttribute('disabled') && primary.dataset.disabled === undefined) {
        if (magnet && magnet !== primary) release();
        magnet = primary;
        const r = primary.getBoundingClientRect();
        primary.style.translate = `${((e.clientX - (r.left + r.width / 2)) * 0.22).toFixed(1)}px ${((e.clientY - (r.top + r.height / 2)) * 0.3).toFixed(1)}px`;
      } else release();
      el.dataset.mode = mode;
      el.dataset.show = 'true';
      if (label.current && label.current.textContent !== words) label.current.textContent = words;
    };
    const hide = () => { el.dataset.show = ''; release(); };
    const down = () => { el.dataset.down = 'true'; };
    const up = () => { el.dataset.down = ''; };
    window.addEventListener('pointermove', move, { passive: true });
    document.documentElement.addEventListener('pointerleave', hide);
    window.addEventListener('blur', hide);
    window.addEventListener('pointerdown', down);
    window.addEventListener('pointerup', up);
    return () => {
      window.removeEventListener('pointermove', move);
      document.documentElement.removeEventListener('pointerleave', hide);
      window.removeEventListener('blur', hide);
      window.removeEventListener('pointerdown', down);
      window.removeEventListener('pointerup', up);
      release();
    };
  }, [on, able]);

  if (!on || !able) return null;
  return <div ref={ring} className="pw-cursor" aria-hidden><span ref={label} className="pw-cursor-shape" /></div>;
}