iter(g102): 焦点陷阱下沉为共享助手

This commit is contained in:
HIsheR
2026-10-26 13:51:40 +08:00
parent f373b8cf31
commit abfbb821b7
5 changed files with 37 additions and 34 deletions
+4 -2
View File
@@ -1,5 +1,5 @@
import { h } from '@ui-arch/frame';
import { lockScroll, pushOverlay } from './overlay.js';
import { lockScroll, pushOverlay, trapFocus } from './overlay.js';
import type { ImageProps } from './types.js';
/*** 图片:缩略图展示,点击 / 回车打开遮罩大图预览(Esc 或点击遮罩关闭),加载失败自动落占位;样式只消费 --ua-* 令牌 ***/
@@ -30,7 +30,7 @@ function openViewer(src: string, alt?: string): void {
const prev = document.activeElement as HTMLElement | null; // 焦点归还目标
const mask = h(
'div',
{ class: 'ua-image-mask', role: 'dialog', 'aria-modal': 'true', 'aria-label': '图片预览' },
{ class: 'ua-image-mask', role: 'dialog', 'aria-modal': 'true', 'aria-label': '图片预览', tabindex: '-1' },
h('img', { class: 'ua-image-full', src, alt: alt ?? '' }),
);
const close = () => {
@@ -41,5 +41,7 @@ function openViewer(src: string, alt?: string): void {
};
mask.addEventListener('click', close);
releaseEsc = pushOverlay(close); // 入 Esc 栈:预览层压在最上面,Esc 先关它
trapFocus(mask, () => true); // 预览层常开:Tab 全程锁在遮罩内(层内无控件,等于整体拦截)
document.body.appendChild(mask);
mask.focus(); // 焦点移入预览层,读屏与键盘都不再落在背后的页面
}