/** * 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 }; }