iter(g43): select/menu/popover/image 的 Esc 接入统一弹层栈

This commit is contained in:
HIsheR
2026-10-26 11:57:50 +08:00
parent f0b115db94
commit fc8a541fe6
5 changed files with 37 additions and 17 deletions
+5 -4
View File
@@ -1,4 +1,5 @@
import { h } from '@ui-arch/frame';
import { pushOverlay } from './overlay.js';
import type { ImageProps } from './types.js';
/*** 图片:缩略图展示,点击 / 回车打开遮罩大图预览(Esc 或点击遮罩关闭),加载失败自动落占位;样式只消费 --ua-* 令牌 ***/
@@ -24,6 +25,7 @@ export function image(props: ImageProps): HTMLElement {
/*** 打开全屏遮罩预览层;关闭时自清理、解绑键盘并归还焦点 ***/
function openViewer(src: string, alt?: string): void {
let releaseEsc: (() => void) | null = null; // Esc 栈注销函数:预览层占一层,关闭即释放
const prev = document.activeElement as HTMLElement | null; // 焦点归还目标
const mask = h(
'div',
@@ -31,12 +33,11 @@ function openViewer(src: string, alt?: string): void {
h('img', { class: 'ua-image-full', src, alt: alt ?? '' }),
);
const close = () => {
mask.remove(); // 卫语句式自清理:遮罩移除 + 键盘解绑 + 焦点归还
document.removeEventListener('keydown', onKey);
mask.remove(); // 自清理:遮罩移除 + 出 Esc 栈 + 焦点归还
releaseEsc?.();
prev?.focus?.();
};
const onKey = (e: KeyboardEvent) => { if (e.key !== 'Escape') return; e.stopPropagation(); close(); }; // 预览层 Esc 自关,不连带关外层
mask.addEventListener('click', close);
document.addEventListener('keydown', onKey);
releaseEsc = pushOverlay(close); // 入 Esc 栈:预览层压在最上面,Esc 先关它
document.body.appendChild(mask);
}