Files
UI/packages/skin/ui/src/position.ts
T
2026-10-26 10:51:35 +08:00

12 lines
1.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*** 浮层上下翻转判定:按触发器与面板的视口几何判断首选方位是否放得下,放不下且另一侧更宽敞则翻转;返回实际生效方位 'top' | 'bottom' ***/
export function flip(trigger: HTMLElement, panel: HTMLElement, preferred: 'top' | 'bottom'): 'top' | 'bottom' {
const GAP = 8; // 与 CSS 预留的浮层间距一致(popover 为 8px)
const MIN = 24; // 兜底:面板尚未量出高度时按最小可读高度估算
const t = trigger.getBoundingClientRect(); // 触发器视口矩形
const h = panel.offsetHeight || MIN; // 面板高度(visibility:hidden 仍占布局,可直接量)
const up = t.top; // 触发器上方可用空间
const down = window.innerHeight - t.bottom; // 触发器下方可用空间
if (preferred === 'top') return up >= h + GAP || up >= down ? 'top' : 'bottom'; // 首选上方:放得下、或不比下方更局促就保持
return down >= h + GAP || down >= up ? 'bottom' : 'top'; // 首选下方:对称判定
}