iter(g11): 浮层空间不足时自动翻转

This commit is contained in:
HIsheR
2026-10-26 10:51:35 +08:00
parent 62784b1f2d
commit 00a7b90f44
12 changed files with 65 additions and 27 deletions
+5 -2
View File
@@ -1,5 +1,6 @@
import { h, signal } from '@ui-arch/frame';
import { effect, h, signal } from '@ui-arch/frame';
import type { TooltipProps } from './types.js';
import { flip } from './position.js';
let seq = 0; // 全局自增序号:保证每个气泡 id 唯一,供 aria-describedby 关联
@@ -7,6 +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 id = `ua-tip-${++seq}`; // 气泡唯一 id
const delay = props.delay ?? 150; // 出场延迟毫秒;<=0 视为立即显示
let timer: number | null = null; // 出场延迟计时器句柄
@@ -33,7 +35,7 @@ export function tooltip(props: TooltipProps, trigger: HTMLElement): HTMLElement
'span',
{
id,
class: () => `ua-tip ua-tip-${props.placement ?? 'top'}${open() ? ' ua-open' : ''}`,
class: () => `ua-tip ua-tip-${place()}${open() ? ' ua-open' : ''}`,
role: 'tooltip',
'aria-hidden': () => String(!open()),
},
@@ -45,6 +47,7 @@ export function tooltip(props: TooltipProps, trigger: HTMLElement): HTMLElement
root.addEventListener('pointerleave', hide); // 鼠标离开
root.addEventListener('focusin', show); // 键盘聚焦(focusin 会冒泡)
root.addEventListener('focusout', hide); // 失焦
effect(() => { if (open()) place(flip(trigger, bubble, props.placement ?? 'top')); }); // 每次出场重算方位:空间不足自动翻转
root.addEventListener('keydown', (e: KeyboardEvent) => {
if (e.key === 'Escape') hide(); // Esc 立即收起,不必等延迟
});