Files
dawnline/web/src/admin/ui/tooltip.ts
T
Aemeath 2388603322
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
Dawnline 0.16.0
- The personal page loads faster and costs the router less: its script
  and styles come compressed, stay in the browser until the next update,
  and the icon comes with the page instead of a request of its own
- The page uses the system's own font (SF Pro on Apple devices, Segoe UI
  on Windows, Roboto on Android) instead of downloading one
- The top of the page shows the server's name alone, so it fits on a
  phone
- Text can be selected only where it is there to be copied (addresses,
  file names, the configuration), with the name field's own selection:
  a soft highlight with handles that glides after the pointer
- Sheets scroll without a scrollbar
- Red buttons (Remove) light up under the pointer, as the others do
- Demo: "Demo" and GitHub are one button; on a phone it floats at the
  bottom of the screen, and switching scenarios no longer makes the
  edge fades jump
2026-10-24 21:33:48 +03:00

55 lines
2.0 KiB
TypeScript

// One floating tooltip that follows the pointer (small charts, meters, round
// buttons). It hides when its anchor leaves the page or is hidden (a repaint
// replaced it, another section is shown), on scrolling, on a tap or click
// elsewhere (touch screens have no mouseleave) and when the page navigates
// away, so it never lingers on <body>.
import { E } from '../e';
import { onNavigate } from './nav';
let floating: HTMLElement | null = null;
let anchor: HTMLElement | null = null;
let watch = 0;
function hideTip() {
if (floating) floating.style.display = 'none';
anchor = null;
clearInterval(watch);
watch = 0;
}
export function attachTip(el: HTMLElement, content: () => (Node | string)[]) {
const show = (ev: MouseEvent) => {
floating ??= document.body.appendChild(E('div', { class: 'dl-tip dl-tip-float dl-root' }));
floating.replaceChildren(...content().map((c) => (typeof c === 'string' ? document.createTextNode(c) : c)));
floating.style.display = '';
floating.style.left = `${ev.clientX}px`;
floating.style.top = `${ev.clientY - 10}px`;
anchor = el;
// No mouseleave comes from an element that was removed or hidden under the pointer.
watch ||= window.setInterval(() => {
if (!anchor?.isConnected || !anchor.getClientRects().length) hideTip();
}, 400);
};
el.addEventListener('mouseenter', show);
el.addEventListener('mousemove', show);
el.addEventListener('mouseleave', hideTip);
// No hover on a touch screen: a tap on the anchor shows it, the next tap
// anywhere (the document listener below) hides it.
el.addEventListener('pointerup', (ev: PointerEvent) => {
if (ev.pointerType === 'mouse') return;
if (anchor === el) hideTip();
else show(ev as unknown as MouseEvent);
});
}
onNavigate(hideTip);
window.addEventListener('scroll', () => anchor && hideTip(), { capture: true, passive: true });
document.addEventListener(
'pointerdown',
(ev) => {
if (anchor && !anchor.contains(ev.target as Node)) hideTip();
},
true,
);