iter(g109): 提示气泡支持触摸点按
This commit is contained in:
@@ -12,6 +12,7 @@ export function tooltip(props: TooltipProps, trigger: HTMLElement): HTMLElement
|
||||
const id = `ua-tip-${++seq}`; // 气泡唯一 id
|
||||
const delay = props.delay ?? 150; // 出场延迟毫秒;<=0 视为立即显示
|
||||
let timer: number | null = null; // 出场延迟计时器句柄
|
||||
let holdTimer: number | null = null; // 触摸抬手后的延迟收起计时器
|
||||
|
||||
const show = () => {
|
||||
if (timer != null) window.clearTimeout(timer); // 重复进入先清旧计时
|
||||
@@ -26,9 +27,22 @@ export function tooltip(props: TooltipProps, trigger: HTMLElement): HTMLElement
|
||||
window.clearTimeout(timer);
|
||||
timer = null;
|
||||
}
|
||||
if (holdTimer != null) { // 触摸遗留的延迟收起一并取消,避免刚点开又被上一轮收掉
|
||||
window.clearTimeout(holdTimer);
|
||||
holdTimer = null;
|
||||
}
|
||||
open(false);
|
||||
};
|
||||
|
||||
/*** 触摸抬手后延迟收起:手指离开屏幕时气泡正被人挡着,留一会儿再收 ***/
|
||||
const hideLater = (ms = 1200): void => {
|
||||
if (holdTimer != null) window.clearTimeout(holdTimer);
|
||||
holdTimer = window.setTimeout(() => {
|
||||
holdTimer = null;
|
||||
open(false);
|
||||
}, ms);
|
||||
};
|
||||
|
||||
// —— 触发器与气泡 ——
|
||||
trigger.setAttribute('aria-describedby', id); // 无障碍:读屏把提示文案关联到触发器
|
||||
const bubble = h(
|
||||
@@ -43,10 +57,13 @@ export function tooltip(props: TooltipProps, trigger: HTMLElement): HTMLElement
|
||||
);
|
||||
const root = h('span', { class: 'ua-tip-wrap' }, trigger, bubble);
|
||||
|
||||
root.addEventListener('pointerenter', show); // 鼠标进入
|
||||
root.addEventListener('pointerleave', hide); // 鼠标离开
|
||||
root.addEventListener('pointerenter', (e: PointerEvent) => { if (e.pointerType !== 'touch') show(); }); // 鼠标进入(触摸交给下面的点按)
|
||||
root.addEventListener('pointerleave', (e: PointerEvent) => { if (e.pointerType !== 'touch') hide(); }); // 鼠标离开
|
||||
root.addEventListener('focusin', show); // 键盘聚焦(focusin 会冒泡)
|
||||
root.addEventListener('focusout', hide); // 失焦
|
||||
// —— 触摸屏:没有 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')); }); // 每次出场重算方位:空间不足自动翻转
|
||||
root.addEventListener('keydown', (e: KeyboardEvent) => {
|
||||
if (e.key === 'Escape') { e.stopPropagation(); hide(); } // Esc 立即收起且不影响外层弹层
|
||||
|
||||
Reference in New Issue
Block a user