File size: 1,061 Bytes
4083225 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /**
* Place a tooltip near (clientX, clientY) without jumping left unless the
* default below-right placement would overflow the viewport.
*
* @param {number} clientX
* @param {number} clientY
* @param {{ estWidth?: number; estHeight?: number; gap?: number; pad?: number }} [opts]
* Pass measured width/height from the tooltip element when possible; defaults
* that are larger than the real box cause early horizontal clamping.
*/
export function viewportTooltipPosition(clientX, clientY, opts = {}) {
const vw = typeof window !== "undefined" ? window.innerWidth : 1200;
const vh = typeof window !== "undefined" ? window.innerHeight : 800;
const W = opts.estWidth ?? 236;
const H = opts.estHeight ?? 92;
const gap = opts.gap ?? 12;
const pad = opts.pad ?? 8;
let left = clientX + gap;
let top = clientY + gap;
if (left + W + pad > vw) {
left = vw - W - pad;
}
if (left < pad) {
left = pad;
}
if (top + H + pad > vh) {
top = vh - H - pad;
}
if (top < pad) {
top = pad;
}
return { left, top };
}
|