iter(g139): 提示气泡支持左右方位

This commit is contained in:
HIsheR
2026-10-26 15:32:47 +08:00
parent a3db5e66fc
commit 08ceb8f127
9 changed files with 62 additions and 18 deletions
+13
View File
@@ -9,3 +9,16 @@ export function flip(trigger: HTMLElement, panel: HTMLElement, preferred: 'top'
if (preferred === 'top') return up >= h + GAP || up >= down ? 'top' : 'bottom'; // 首选上方:放得下、或不比下方更局促就保持
return down >= h + GAP || down >= up ? 'bottom' : 'top'; // 首选下方:对称判定
}
/*** 浮层方位判定(四向):水平方向按宽度判左右空间,垂直方向复用 flip 的上下一套逻辑 ***/
export function flipSide(trigger: HTMLElement, panel: HTMLElement, preferred: 'top' | 'bottom' | 'left' | 'right'): 'top' | 'bottom' | 'left' | 'right' {
const GAP = 8; // 与样式表预留的间距一致
const MIN = 24; // 兜底:面板尚未量出宽度时按最小可读宽度估算
if (preferred === 'top' || preferred === 'bottom') return flip(trigger, panel, preferred); // 垂直交回原判定
const t = trigger.getBoundingClientRect();
const w = panel.offsetWidth || MIN;
const left = t.left; // 触发器左侧可用空间
const right = window.innerWidth - t.right; // 触发器右侧可用空间
if (preferred === 'left') return left >= w + GAP || left >= right ? 'left' : 'right'; // 对称判定
return right >= w + GAP || right >= left ? 'right' : 'left';
}
+3 -3
View File
@@ -1,6 +1,6 @@
import { effect, h, signal } from '@ui-arch/frame';
import type { TooltipProps } from './types.js';
import { flip } from './position.js';
import { flipSide } from './position.js';
let seq = 0; // 全局自增序号:保证每个气泡 id 唯一,供 aria-describedby 关联
@@ -8,7 +8,7 @@ let seq = 0; // 全局自增序号:保证每个气泡 id 唯一,供 aria-des
export function tooltip(props: TooltipProps, trigger: HTMLElement): HTMLElement {
// —— 状态集中声明 ——
const open = signal(false); // 气泡显隐
const place = signal(props.placement ?? 'top'); // 实际生效方位:出场时按视口空间自动翻转
const place = signal<'top' | 'bottom' | 'left' | 'right'>(props.placement ?? 'top'); // 实际生效方位:出场时按视口空间自动翻转
const id = `ua-tip-${++seq}`; // 气泡唯一 id
const delay = props.delay ?? 150; // 出场延迟毫秒;<=0 视为立即显示
let timer: number | null = null; // 出场延迟计时器句柄
@@ -64,7 +64,7 @@ export function tooltip(props: TooltipProps, trigger: HTMLElement): HTMLElement
// —— 触摸屏:没有 hover,改点按。按下即出(不等 150ms),抬手留 1.2s 再收,否则手指正挡着气泡就没了 ——
root.addEventListener('pointerdown', (e: PointerEvent) => { if (e.pointerType === 'touch') show(); });
root.addEventListener('pointerup', (e: PointerEvent) => { if (e.pointerType === 'touch') hideLater(); });
effect(() => { if (open()) place(flip(trigger, bubble, props.placement ?? 'top')); }); // 每次出场重算方位:空间不足自动翻转
effect(() => { if (open()) place(flipSide(trigger, bubble, props.placement ?? 'top')); }); // 每次出场重算方位:空间不足自动翻转
root.addEventListener('keydown', (e: KeyboardEvent) => {
if (e.key === 'Escape') { e.stopPropagation(); hide(); } // Esc 立即收起且不影响外层弹层
});
+1 -1
View File
@@ -4,7 +4,7 @@ import type { Signal } from '@ui-arch/frame';
/*** 悬浮提示入参 ***/
export interface TooltipProps {
content: string; // 提示文案(纯文本)
placement?: 'top' | 'bottom'; // 气泡方位;缺省 top(触发器上方)
placement?: 'top' | 'bottom' | 'left' | 'right'; // 气泡方位;缺省 top,空间不足自动翻到对侧
delay?: number; // 出场延迟毫秒;缺省 150,传 0 立即显示
}