// Tooltips: every control with a title (or a data-tip) gets one drawn by the // page rather than the browser's, so it comes up at once, reads the same // everywhere, and shows the shortcut as keys. A title like "Save (⌘S)" is // the name and the shortcut; the parenthesis with keys in it is shown as // , the rest as text. The native tooltip is kept from showing by moving // the title into data-tip the first time the control is hovered; icon-only // controls keep their accessible name through aria-label. const KEYS = /[⌘⌃⌥⇧⏎↑↓←→⌫⇥]|\bEsc(?:ape)?\b|\bEnter\b|\bTab\b|\bCtrl\b|\bAlt\b|\bShift\b/u; const PAREN = /\s*\(([^()]*)\)/gu; const DELAY = 350; const GAP = 6; export type Tip = { name: string; keys: string[]; rest: string }; /** "Save (⌘S) — autosaving" → name "Save", keys ["⌘S"], rest "— autosaving". */ export function parseTip(title: string): Tip { const keys: string[] = []; let name = title.replace(PAREN, (whole, inner: string) => { if (!KEYS.test(inner)) return whole; for (const k of inner.split(/,\s*/)) if (k.trim()) keys.push(k.trim()); return ''; }); let rest = ''; const dash = name.search(/\s[—–-]\s/u); if (dash > 0) { rest = name.slice(dash).trim(); name = name.slice(0, dash); } const colon = name.indexOf(': '); if (!rest && colon > 0 && name.length - colon > 40) { rest = name.slice(colon + 2).trim(); name = name.slice(0, colon); } return { name: name.trim(), keys, rest }; } let tip: HTMLElement | null = null; let timer: ReturnType | null = null; let shownFor: Element | null = null; let lastHidden = 0; function tipOf(el: Element): string | null { const title = el.getAttribute('title'); if (title) { // ours from now on: the browser's would come up too el.setAttribute('data-tip', title); el.removeAttribute('title'); if (!el.getAttribute('aria-label') && !el.textContent?.trim() && !el.querySelector('[aria-label], .visually-hidden')) el.setAttribute('aria-label', parseTip(title).name); return title; } return el.getAttribute('data-tip'); } function render(text: string): HTMLElement { if (!tip) { tip = document.createElement('div'); tip.className = 'draft-tip'; tip.setAttribute('role', 'tooltip'); document.body.append(tip); } const t = parseTip(text); tip.replaceChildren(); const name = document.createElement('span'); name.className = 'tip-name'; name.textContent = t.name; tip.append(name); for (const k of t.keys) { const kbd = document.createElement('kbd'); kbd.textContent = k; tip.append(kbd); } if (t.rest) { const rest = document.createElement('span'); rest.className = 'tip-rest'; rest.textContent = t.rest; tip.append(rest); } return tip; } function place(el: Element): void { if (!tip) return; const r = el.getBoundingClientRect(); const w = tip.offsetWidth; const h = tip.offsetHeight; const vw = document.documentElement.clientWidth; const vh = document.documentElement.clientHeight; let x = r.left + r.width / 2 - w / 2; x = Math.max(4, Math.min(x, vw - w - 4)); let y = r.bottom + GAP; if (y + h > vh - 4) y = r.top - GAP - h; tip.style.left = `${Math.round(x)}px`; tip.style.top = `${Math.round(y)}px`; } function show(el: Element): void { const text = tipOf(el); if (!text) return; render(text); tip!.classList.add('on'); shownFor = el; place(el); } export function hideTooltip(): void { if (timer) clearTimeout(timer); timer = null; if (tip && shownFor) lastHidden = Date.now(); tip?.classList.remove('on'); shownFor = null; } function target(e: Event): Element | null { const t = e.target as Element | null; return t?.closest?.('[title], [data-tip]') ?? null; } let installed = false; /** Draw tooltips for everything on the page that has a title; call once. */ export function installTooltips(): void { if (installed || typeof document === 'undefined') return; installed = true; document.addEventListener('pointerover', (e) => { const el = target(e); if (!el || el === shownFor) return; if (timer) clearTimeout(timer); // moving from one control to the next: no second wait const soon = Date.now() - lastHidden < 400 || !!shownFor; if (shownFor) hideTooltip(); timer = setTimeout(() => show(el), soon ? 0 : DELAY); }); document.addEventListener('pointerout', (e) => { const el = target(e); if (!el) return; const to = (e as PointerEvent).relatedTarget as Element | null; if (to && el.contains(to)) return; hideTooltip(); }); document.addEventListener('focusin', (e) => { const el = target(e); if (el && (e.target as HTMLElement).matches?.(':focus-visible')) show(el); }); document.addEventListener('focusout', () => hideTooltip()); document.addEventListener('pointerdown', () => hideTooltip(), true); document.addEventListener('keydown', (e) => { if (e.key === 'Escape' || e.metaKey || e.ctrlKey) hideTooltip(); }, true); document.addEventListener('scroll', () => hideTooltip(), true); } /** The tooltip's text for a control (its title, or the data-tip it became). For tests and hosts. */ export function tooltipOf(el: Element): string | null { return el.getAttribute('title') ?? el.getAttribute('data-tip'); }