Spaces:
Paused

File size: 5,511 Bytes
bc9f3e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<script>
  import { onMount } from "svelte";
  import "./Tooltip.css";

  /** Singleton styled tooltip — the app's ONLY tooltip mechanism. Mounted
   * once in App.svelte; it listens (delegated) for hover/focus over any
   * element carrying a `data-tooltip` attribute (the bilingual strings the
   * components bind, e.g. `data-tooltip={t("web_tt_send")}`) and shows a
   * warm floating bubble positioned against the anchor: flips above/below
   * when space runs out, clamps to the viewport, and aims its arrow at the
   * anchor. Native `title` attributes are intentionally not used anywhere
   * in the app — the components' `title={...}` bindings were migrated to
   * `data-tooltip={...}` so every tooltip renders through this component.
   * The bubble is pointer-events: none (read-only), fades in after a short
   * delay to avoid flicker while moving across controls, and dismisses on
   * scroll / resize / click / Escape / blur. Keyboard users get the same
   * tooltip via focusin/focusout; the anchored elements keep their
   * `aria-label` for screen readers. */

  let text = $state("");
  let open = $state(false);
  let pos = $state({ x: 0, y: 0, above: true, arrow: 50 });
  let bubble;
  let anchor = null;
  let openTimer = null;

  const GAP = 8;
  const OPEN_DELAY = 120;

  /** While an in-page dropdown menu is open (MiniSelect's listbox — mounted
   * only while open — or its trigger reporting aria-expanded + aria-haspopup
   * "listbox"), hover tooltips are noise: the bubble would cover the very
   * options the user is trying to read/pick. Suppress the tooltip inside
   * such a subtree (and hide any open one) until the menu closes. The
   * sidebar's aria-expanded toggle is NOT a popup, so it is deliberately
   * excluded (it has no aria-haspopup). */
  function isInOpenPopup(target) {
    if (!target || !target.closest) return false;
    if (target.closest("[role='listbox']")) return true;
    const trig = target.closest("[aria-expanded='true']");
    return !!(trig && trig.getAttribute && trig.getAttribute("aria-haspopup") === "listbox");
  }

  function onOver(e) {
    const target = e.target && e.target.closest ? e.target.closest("[data-tooltip]") : null;
    if (!target) {
      hide();
      return;
    }
    if (isInOpenPopup(target)) {
      hide();
      return;
    }
    const value = target.getAttribute("data-tooltip") || "";
    if (!value.trim()) return;
    anchor = target;
    clearTimeout(openTimer);
    openTimer = setTimeout(() => {
      text = value;
      open = true;
    }, OPEN_DELAY);
  }

  function onOut(e) {
    const target = e.target && e.target.closest ? e.target.closest("[data-tooltip]") : null;
    if (target) {
      const to = e.relatedTarget;
      if (to && target.contains(to)) return; // still inside the same anchor
    }
    // Moving toward the OPEN bubble keeps it (it is pointer-events: none,
    // so the cursor passes through it — do not close mid-approach).
    if (open && bubble && typeof e.clientX === "number") {
      const b = bubble.getBoundingClientRect();
      if (
        e.clientX >= b.left - 4 && e.clientX <= b.right + 4 &&
        e.clientY >= b.top - 4 && e.clientY <= b.bottom + 4
      ) {
        return;
      }
    }
    hide();
  }

  function hide() {
    clearTimeout(openTimer);
    open = false;
    anchor = null;
  }

  // Place the bubble once it is mounted and opened: above the anchor when
  // there is room, otherwise below; clamped to the viewport; arrow aimed at
  // the anchor's horizontal centre.
  $effect(() => {
    if (!open || !bubble || !anchor) return;
    const r = anchor.getBoundingClientRect();
    const b = bubble.getBoundingClientRect();
    const above = r.top - b.height - GAP >= 8;
    const y = above ? r.top - b.height - GAP : r.bottom + GAP;
    const anchorCx = r.left + r.width / 2;
    let x = anchorCx - b.width / 2;
    x = Math.max(GAP, Math.min(x, window.innerWidth - b.width - GAP));
    const arrow = Math.max(10, Math.min(90, ((anchorCx - x) / b.width) * 100));
    pos = { x: Math.round(x), y: Math.round(y), above, arrow: Math.round(arrow) };
  });

  onMount(() => {
    const onScrollHide = () => hide();
    const onKey = (e) => {
      if (e.key === "Escape") hide();
    };
    const onClick = () => hide();
    document.addEventListener("mouseover", onOver, true);
    document.addEventListener("mouseout", onOut, true);
    document.addEventListener("focusin", onOver, true);
    document.addEventListener("focusout", onOut, true);
    document.addEventListener("scroll", onScrollHide, true);
    window.addEventListener("resize", onScrollHide);
    document.addEventListener("click", onClick);
    document.addEventListener("keydown", onKey);
    return () => {
      document.removeEventListener("mouseover", onOver, true);
      document.removeEventListener("mouseout", onOut, true);
      document.removeEventListener("focusin", onOver, true);
      document.removeEventListener("focusout", onOut, true);
      document.removeEventListener("scroll", onScrollHide, true);
      window.removeEventListener("resize", onScrollHide);
      document.removeEventListener("click", onClick);
      document.removeEventListener("keydown", onKey);
    };
  });
</script>

<div
  bind:this={bubble}
  class="lal-tooltip"
  class:open={open}
  class:above={pos.above}
  class:below={!pos.above}
  style:left="{pos.x}px"
  style:top="{pos.y}px"
  style:--ax="{pos.arrow}%"
  role="tooltip"
  aria-hidden={!open}
>
  {text}
</div>