29 lines
1.3 KiB
TypeScript
29 lines
1.3 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
/** One shared tooltip for hover explanations, positioned near the pointer. */
|
|
import { el, clear, add } from './dom.js';
|
|
|
|
class Tooltip {
|
|
private node: HTMLElement | null = null;
|
|
private ensure(): HTMLElement {
|
|
if (!this.node) { this.node = el('div', { class: 'tip', role: 'tooltip', style: { display: 'none' } }); document.body.appendChild(this.node); }
|
|
return this.node;
|
|
}
|
|
show(x: number, y: number, info: { title: string; body: string; where?: string }): void {
|
|
const n = this.ensure();
|
|
clear(n);
|
|
add(n, el('div', { class: 'tip-title' }, el('code', {}, info.title), info.where ? el('span', { class: 'tip-where' }, info.where) : null), el('div', { class: 'tip-body' }, info.body));
|
|
n.style.display = '';
|
|
const w = Math.min(380, window.innerWidth - 24);
|
|
n.style.maxWidth = `${w}px`;
|
|
const left = Math.min(x + 14, window.innerWidth - w - 12);
|
|
n.style.left = `${Math.max(8, left)}px`;
|
|
n.style.top = `${y + 18}px`;
|
|
const r = n.getBoundingClientRect();
|
|
if (r.bottom > window.innerHeight - 8) n.style.top = `${Math.max(8, y - r.height - 10)}px`;
|
|
}
|
|
hide(): void { if (this.node) this.node.style.display = 'none'; }
|
|
}
|
|
|
|
export const tooltip = new Tooltip();
|