fix(sidebar): keep tooltips within the viewport

This commit is contained in:
zipg authored and GitHub committed 2026-07-30 18:15:41 +08:00
1 parent 661575227f
commit 7d1a72e074
3 files changed
+76 -3

No files matched your search

@@ -0,0 +1,29 @@
export interface FloatingRect {
left: number;
right: number;
top: number;
bottom: number;
}
export interface FloatingViewport {
width: number;
height: number;
}
export function floatingViewportShift(rect: FloatingRect, viewport: FloatingViewport, margin = 8): { x: number; y: number } {
let x = 0;
let y = 0;
if (rect.left < margin) x = margin - rect.left;
else if (rect.right > viewport.width - margin) x = viewport.width - margin - rect.right;
if (rect.top < margin) y = margin - rect.top;
else if (rect.bottom > viewport.height - margin) y = viewport.height - margin - rect.bottom;
return { x, y };
}
export function floatingArrowOffset(length: number, shift: number, padding = 8): number {
const maximum = Math.max(padding, length - padding);
return Math.min(Math.max(padding, length / 2 - shift), maximum);
}