Files
kokkoja-rel/client/js/tooltip.js
T
ackandClaude Fable 5.1 bc3c9bb886 Everyone has the Notebooks chooser
From the development repository at 1ccf212.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-11 17:34:24 +02:00

99 lines
3.2 KiB
JavaScript

// @ts-check
/**
* Tooltips: a full explanation of a button, shown below it after the
* pointer (or a pen hovering above the screen) has rested on it for a
* moment. The buttons themselves carry terse labels; this is where the
* explanation lives.
*
* Any element with a `data-help` attribute gets one. Elements added later
* are covered too, since the listeners are on the document.
*/
import { trace } from './log.js';
const DELAY_MS = 2000;
const GAP_PX = 6;
/** @type {HTMLElement | null} */
let tooltip = null;
/** @type {number | null} */
let timer = null;
/** @type {Element | null} */
let shownFor = null;
/**
* @param {EventTarget | null} target
* @returns {HTMLElement | null} The element with help under the pointer.
*/
function helpElement(target) {
return target instanceof Element ? target.closest('[data-help]') : null;
}
/** @param {HTMLElement} element */
function show(element) {
if (!tooltip) {
tooltip = document.createElement('div');
tooltip.className = 'tooltip';
tooltip.setAttribute('role', 'tooltip');
document.body.append(tooltip);
}
tooltip.textContent = element.getAttribute('data-help');
tooltip.hidden = false;
shownFor = element;
// Below the element, aligned with its left edge, kept within the window.
const box = element.getBoundingClientRect();
const width = tooltip.offsetWidth;
const left = Math.max(GAP_PX, Math.min(box.left, window.innerWidth - width - GAP_PX));
tooltip.style.left = `${left}px`;
tooltip.style.top = `${box.bottom + GAP_PX}px`;
}
function hide() {
if (timer !== null) clearTimeout(timer);
timer = null;
shownFor = null;
if (tooltip) tooltip.hidden = true;
}
export function enableTooltips() {
// Traced into the Session window's log (2026-10-11: Andy's Wacom pen
// hovering over a button on Hollis showed no tooltip, and the log is
// the way to see what the browser delivers for a hovering pen).
document.addEventListener('pointerover', (event) => {
const element = helpElement(event.target);
if (!element) {
hide();
return;
}
if (element === shownFor) return;
hide();
trace(`Hover: ${event.pointerType} over ${element.textContent?.trim() || element.id}`);
timer = setTimeout(() => {
timer = null;
trace(`Tooltip: ${element.textContent?.trim() || element.id}`);
show(element);
}, DELAY_MS);
});
document.addEventListener('pointerleave', (event) => {
if (timer !== null || shownFor) trace(`Hover: ${event.pointerType} left the document`);
hide();
});
// Focus that comes from a click brings no tooltip: the user is already
// acting on the element, and a list it opens would be covered (Andy,
// 2026-10-07, the notebook list). Focus from the keyboard does.
let pressedAt = -Infinity;
document.addEventListener('pointerdown', () => {
pressedAt = performance.now();
hide();
});
document.addEventListener('focusin', (event) => {
if (performance.now() - pressedAt < 500) return;
const element = helpElement(event.target);
if (element) show(element);
});
document.addEventListener('focusout', hide);
// Typing means the user's attention is on the keys, not the tooltip.
document.addEventListener('keydown', hide);
}