iter(g43): select/menu/popover/image 的 Esc 接入统一弹层栈
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { effect, h, signal } from '@ui-arch/frame';
|
||||
import type { SelectProps } from './types.js';
|
||||
import { flip } from './position.js';
|
||||
import { pushOverlay } from './overlay.js';
|
||||
|
||||
/*** 下拉选择:自绘弹层替代原生 select,value 信号双向绑定;searchable 时弹层顶部带搜索框,支持 ↑↓/Home/End 导航与 Esc 关闭 ***/
|
||||
export function select(props: SelectProps): HTMLElement {
|
||||
@@ -10,6 +11,7 @@ export function select(props: SelectProps): HTMLElement {
|
||||
const query = signal(''); // 搜索关键字(仅 searchable 时使用)
|
||||
const value = props.value ?? signal(''); // 选中值;未传信号则内部持有
|
||||
|
||||
let releaseEsc: (() => void) | null = null; // Esc 栈注销函数:展开期间占一层,收起即释放
|
||||
const labels = props.options.map((o) => o.label); // 文案缓存,供搜索与键入快跳共用
|
||||
const labelOf = (v: string) => props.options.find((o) => o.value === v)?.label ?? '';
|
||||
const current = () => labelOf(value()); // 触发器显示文案(响应式)
|
||||
@@ -87,7 +89,6 @@ export function select(props: SelectProps): HTMLElement {
|
||||
};
|
||||
|
||||
root.addEventListener('keydown', (e: KeyboardEvent) => {
|
||||
if (e.key === 'Escape') { e.stopPropagation(); open(false); trigger.focus(); return; } // Esc 只关本层并回焦触发器
|
||||
const list = visibleIdx();
|
||||
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
|
||||
e.preventDefault(); // 阻止页面滚动
|
||||
@@ -122,6 +123,14 @@ export function select(props: SelectProps): HTMLElement {
|
||||
requestAnimationFrame(() => search.focus()); // 搜索模式:打开即聚焦输入框
|
||||
}
|
||||
});
|
||||
effect(() => {
|
||||
if (open()) {
|
||||
releaseEsc ??= pushOverlay(() => { open(false); trigger.focus(); }); // 入 Esc 栈:与弹窗/抽屉共用同一套逐层关闭
|
||||
} else {
|
||||
releaseEsc?.();
|
||||
releaseEsc = null;
|
||||
}
|
||||
});
|
||||
effect(() => { if (open()) place(flip(trigger, pop, 'bottom')); }); // 每次打开重算方位:空间不足自动翻转
|
||||
return root;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user