iter(g27): 抽出弹层 Esc 栈,多弹层只关最上层
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { effect, h } from '@ui-arch/frame';
|
||||
import type { DrawerProps } from './types.js';
|
||||
import { pushOverlay } from './overlay.js';
|
||||
|
||||
/*** 抽屉:open 信号驱动侧滑面板,点遮罩 / × / Esc 关闭;Tab 焦点陷阱 + 关闭归还焦点,与 modal 的分工是不打断主视图、可从侧边保留上下文 ***/
|
||||
export function drawer(props: DrawerProps, ...children: unknown[]): HTMLElement {
|
||||
@@ -29,15 +30,19 @@ export function drawer(props: DrawerProps, ...children: unknown[]): HTMLElement
|
||||
overlay.append(panel);
|
||||
|
||||
let lastFocus: HTMLElement | null = null; // 打开前焦点宿主,关闭时归还
|
||||
let release: (() => void) | null = null; // Esc 栈注销函数,关闭时释放
|
||||
const focusables = (): HTMLElement[] =>
|
||||
Array.from(panel.querySelectorAll<HTMLElement>('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'))
|
||||
.filter((el) => !el.hasAttribute('disabled')); // 面板内可聚焦元素(排除禁用)
|
||||
effect(() => {
|
||||
if (props.open()) {
|
||||
lastFocus = document.activeElement as HTMLElement | null; // 记录打开前焦点
|
||||
release ??= pushOverlay(close); // 入 Esc 栈:多弹层时只关最上层
|
||||
(panel.querySelector('button, input, textarea') as HTMLElement | null)?.focus(); // 打开时聚焦首个可交互元素
|
||||
} else if (lastFocus) {
|
||||
lastFocus.focus(); // 关闭时归还焦点
|
||||
} else {
|
||||
release?.(); // 出 Esc 栈
|
||||
release = null;
|
||||
lastFocus?.focus(); // 关闭时归还焦点
|
||||
lastFocus = null;
|
||||
}
|
||||
});
|
||||
@@ -53,8 +58,5 @@ export function drawer(props: DrawerProps, ...children: unknown[]): HTMLElement
|
||||
e.preventDefault(); first.focus(); // Tab 到末位时回绕到开头
|
||||
}
|
||||
});
|
||||
document.addEventListener('keydown', (e: KeyboardEvent) => {
|
||||
if (e.key === 'Escape' && props.open()) close();
|
||||
});
|
||||
return overlay;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { effect, h } from '@ui-arch/frame';
|
||||
import type { ModalProps } from './types.js';
|
||||
import { pushOverlay } from './overlay.js';
|
||||
|
||||
/*** 弹窗:open 信号驱动显隐,点遮罩、× 或 Esc 触发 onClose;Tab 焦点陷阱 + 关闭归还焦点 ***/
|
||||
export function modal(props: ModalProps, ...children: unknown[]): HTMLElement {
|
||||
@@ -23,6 +24,7 @@ export function modal(props: ModalProps, ...children: unknown[]): HTMLElement {
|
||||
);
|
||||
overlay.append(box);
|
||||
let lastFocus: HTMLElement | null = null; // 打开前焦点宿主,关闭时归还
|
||||
let release: (() => void) | null = null; // Esc 栈注销函数,关闭时释放
|
||||
const focusables = (): HTMLElement[] =>
|
||||
Array.from(box.querySelectorAll<HTMLElement>('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'))
|
||||
.filter((el) => !el.hasAttribute('disabled')); // 弹窗内可聚焦元素(排除禁用)
|
||||
@@ -31,9 +33,12 @@ export function modal(props: ModalProps, ...children: unknown[]): HTMLElement {
|
||||
overlay.classList.toggle('ua-open', open); // 显隐跟随信号
|
||||
if (open) {
|
||||
lastFocus = document.activeElement as HTMLElement | null; // 记录打开前焦点
|
||||
release ??= pushOverlay(close); // 入 Esc 栈:多弹层时只关最上层
|
||||
(box.querySelector('button, input, textarea') as HTMLElement | null)?.focus(); // 打开时聚焦首个可交互元素
|
||||
} else if (lastFocus) {
|
||||
lastFocus.focus(); // 关闭时归还焦点
|
||||
} else {
|
||||
release?.(); // 出 Esc 栈
|
||||
release = null;
|
||||
lastFocus?.focus(); // 关闭时归还焦点
|
||||
lastFocus = null;
|
||||
}
|
||||
});
|
||||
@@ -49,8 +54,5 @@ export function modal(props: ModalProps, ...children: unknown[]): HTMLElement {
|
||||
e.preventDefault(); first.focus(); // Tab 到末位时回绕到开头
|
||||
}
|
||||
});
|
||||
document.addEventListener('keydown', (e: KeyboardEvent) => {
|
||||
if (e.key === 'Escape' && props.open()) close(); // Esc 关闭当前打开的弹窗
|
||||
});
|
||||
return overlay;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/*** 弹层键盘栈:维护打开顺序,Esc 只关最上层,避免多个弹层(含嵌套)被一次全关 ***/
|
||||
|
||||
// —— 状态集中声明 ——
|
||||
const stack: { close: () => void }[] = []; // 打开中的弹层,后进先出
|
||||
let bound = false; // 全局 keydown 监听只挂一次,栈空时摘除
|
||||
|
||||
/*** 全局 Esc 分发:只把事件交给栈顶弹层,其余保持不动 ***/
|
||||
const onKeydown = (e: KeyboardEvent) => {
|
||||
if (e.key !== 'Escape') return; // 卫语句:只关心 Esc
|
||||
const top = stack[stack.length - 1];
|
||||
if (!top) return; // 卫语句:栈空无事可做
|
||||
e.preventDefault();
|
||||
top.close();
|
||||
};
|
||||
|
||||
/*** 注册一个打开的弹层,返回注销函数(弹层关闭时务必调用,否则栈不清理) ***/
|
||||
export function pushOverlay(close: () => void): () => void {
|
||||
if (!bound) {
|
||||
document.addEventListener('keydown', onKeydown); // 首个弹层才挂全局监听
|
||||
bound = true;
|
||||
}
|
||||
const entry = { close };
|
||||
stack.push(entry);
|
||||
return () => {
|
||||
const i = stack.indexOf(entry);
|
||||
if (i >= 0) stack.splice(i, 1); // 出栈:允许非栈顶关闭
|
||||
if (!stack.length && bound) {
|
||||
document.removeEventListener('keydown', onKeydown); // 栈空摘监听,避免泄漏
|
||||
bound = false;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user