iter(g141): 通知支持四角停靠
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
import { h } from '@ui-arch/frame';
|
||||
import type { NotificationProps } from './types.js';
|
||||
|
||||
let host: HTMLElement | null = null; // 右下角堆叠容器:首条通知出现时懒创建
|
||||
const MAX_STACK = 3; // 同屏最多条数,超出挤掉最早一条
|
||||
const hosts = new Map<string, HTMLElement>(); // 每个角落一个堆叠容器,首次用到该角落时懒创建
|
||||
const MAX_STACK = 3; // 同一角落最多条数,超出挤掉最早一条
|
||||
|
||||
/*** 确保堆叠容器存在:fixed 挂 body 末尾,与右上角 toast 分区不打架 ***/
|
||||
function ensureHost(): HTMLElement {
|
||||
if (host) return host;
|
||||
host = h('div', { class: 'ua-notice-host' });
|
||||
document.body.appendChild(host);
|
||||
return host;
|
||||
/*** 确保指定角落的堆叠容器存在:fixed 挂 body 末尾,与 toast 的右上角分区不打架 ***/
|
||||
function ensureHost(place: string): HTMLElement {
|
||||
const hit = hosts.get(place);
|
||||
if (hit) return hit;
|
||||
const el = h('div', { class: `ua-notice-host is-${place}` });
|
||||
document.body.appendChild(el);
|
||||
hosts.set(place, el);
|
||||
return el;
|
||||
}
|
||||
|
||||
/*** 退场并移除:摘 ua-open 触发过渡,transitionend 与 320ms 超时二选一兜底 ***/
|
||||
@@ -61,7 +63,7 @@ export function notification(props: NotificationProps): { el: HTMLElement; close
|
||||
el.addEventListener('focusin', pause); // 键盘聚焦到关闭/操作按钮同样暂停
|
||||
el.addEventListener('focusout', resume);
|
||||
|
||||
const stack = ensureHost();
|
||||
const stack = ensureHost(props.placement ?? 'bottom-right'); // 缺省右下,与右上角 toast 错开
|
||||
if (stack.childElementCount >= MAX_STACK) stack.firstElementChild?.remove(); // 超出上限挤掉最早一条
|
||||
stack.appendChild(el);
|
||||
requestAnimationFrame(() => el.classList.add('ua-open')); // 下一帧入场,保证过渡生效
|
||||
|
||||
@@ -486,6 +486,7 @@ export interface NotificationProps {
|
||||
duration?: number; // 自动关闭毫秒;缺省 0 = 常驻(通知通常要读完)
|
||||
closable?: boolean; // 是否显示关闭按钮;缺省 true
|
||||
action?: unknown; // 操作区内容(如按钮);缺省不渲染
|
||||
placement?: 'top-right' | 'bottom-right' | 'top-left' | 'bottom-left'; // 停靠角落;缺省 bottom-right(避开右上角 toast)
|
||||
}
|
||||
|
||||
/*** 文件选择入参 ***/
|
||||
|
||||
Reference in New Issue
Block a user