|
| 1 | +/* Cursor-following tooltip. |
| 2 | + * |
| 3 | + * Replaces the old CSS `::after` tooltip, which was pinned to the horizontal |
| 4 | + * centre of the hovered element (so it could sit far from the pointer and run |
| 5 | + * off-screen near a window edge). This implementation: |
| 6 | + * - shows the tooltip next to the pointer on hover/move, |
| 7 | + * - anchors it just below the element for keyboard focus, and |
| 8 | + * - clamps the position so it never leaves the viewport. |
| 9 | + * |
| 10 | + * A single shared element (`#cursor-tooltip`) is reused for every target; the |
| 11 | + * text comes from each element's `data-tooltip` attribute (set both in markup |
| 12 | + * and dynamically by other scripts, e.g. the window controls). |
| 13 | + */ |
| 14 | +(function () { |
| 15 | + const tooltip = document.getElementById("cursor-tooltip"); |
| 16 | + if (!tooltip) return; |
| 17 | + |
| 18 | + let activeTarget = null; |
| 19 | + |
| 20 | + const GAP = 12; // distance from the pointer / element edge |
| 21 | + const MARGIN = 8; // minimum distance kept from the viewport edge |
| 22 | + |
| 23 | + function positionTooltip(preferredLeft, preferredTop) { |
| 24 | + const rect = tooltip.getBoundingClientRect(); |
| 25 | + const viewportWidth = window.innerWidth; |
| 26 | + const viewportHeight = window.innerHeight; |
| 27 | + |
| 28 | + let left = preferredLeft; |
| 29 | + let top = preferredTop; |
| 30 | + |
| 31 | + if (left + rect.width > viewportWidth - MARGIN) { |
| 32 | + left = viewportWidth - MARGIN - rect.width; |
| 33 | + } |
| 34 | + if (left < MARGIN) left = MARGIN; |
| 35 | + |
| 36 | + if (top + rect.height > viewportHeight - MARGIN) { |
| 37 | + top = viewportHeight - MARGIN - rect.height; |
| 38 | + } |
| 39 | + if (top < MARGIN) top = MARGIN; |
| 40 | + |
| 41 | + tooltip.style.left = `${left}px`; |
| 42 | + tooltip.style.top = `${top}px`; |
| 43 | + } |
| 44 | + |
| 45 | + function showForElement(element, clientX, clientY) { |
| 46 | + const text = element.dataset.tooltip; |
| 47 | + if (!text) return; |
| 48 | + activeTarget = element; |
| 49 | + tooltip.textContent = text; |
| 50 | + if (typeof clientY === "number") { |
| 51 | + // Pointer hover: place down-right of the cursor, then clamp on-screen. |
| 52 | + positionTooltip(clientX + GAP, clientY + GAP); |
| 53 | + } else { |
| 54 | + // Keyboard focus: anchor just below the element's left edge, then clamp. |
| 55 | + const box = element.getBoundingClientRect(); |
| 56 | + positionTooltip(box.left, box.bottom + GAP); |
| 57 | + } |
| 58 | + tooltip.classList.add("is-visible"); |
| 59 | + } |
| 60 | + |
| 61 | + function hide() { |
| 62 | + activeTarget = null; |
| 63 | + tooltip.classList.remove("is-visible"); |
| 64 | + } |
| 65 | + |
| 66 | + document.addEventListener("pointerover", (event) => { |
| 67 | + const element = event.target.closest("[data-tooltip]"); |
| 68 | + if (!element || !element.dataset.tooltip) return; |
| 69 | + showForElement(element, event.clientX, event.clientY); |
| 70 | + }); |
| 71 | + |
| 72 | + document.addEventListener("pointermove", (event) => { |
| 73 | + if (!activeTarget) return; |
| 74 | + const element = event.target.closest("[data-tooltip]"); |
| 75 | + if (element !== activeTarget) return; |
| 76 | + positionTooltip(event.clientX + GAP, event.clientY + GAP); |
| 77 | + }); |
| 78 | + |
| 79 | + document.addEventListener("pointerout", (event) => { |
| 80 | + const element = event.target.closest("[data-tooltip]"); |
| 81 | + if (!element) return; |
| 82 | + // Ignore moves between the element and its own descendants. |
| 83 | + if (event.relatedTarget && element.contains(event.relatedTarget)) return; |
| 84 | + // Moving straight to another tooltip target is handled by its pointerover, |
| 85 | + // so don't hide here (prevents a flicker between adjacent targets). |
| 86 | + if ( |
| 87 | + event.relatedTarget && |
| 88 | + typeof event.relatedTarget.closest === "function" && |
| 89 | + event.relatedTarget.closest("[data-tooltip]") |
| 90 | + ) { |
| 91 | + return; |
| 92 | + } |
| 93 | + hide(); |
| 94 | + }); |
| 95 | + |
| 96 | + document.addEventListener("focusin", (event) => { |
| 97 | + const element = event.target.closest("[data-tooltip]"); |
| 98 | + if (!element || !element.dataset.tooltip) return; |
| 99 | + showForElement(element); |
| 100 | + }); |
| 101 | + |
| 102 | + document.addEventListener("focusout", (event) => { |
| 103 | + const element = event.target.closest("[data-tooltip]"); |
| 104 | + if (!element) return; |
| 105 | + hide(); |
| 106 | + }); |
| 107 | +})(); |
0 commit comments