Files
UI/packages/skin/ui/src/alert.ts
T
2026-10-23 17:04:28 +08:00

33 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { h } from '@ui-arch/frame';
import type { AlertProps } from './types.js';
/*** 提示条:variant 决定语义色,可选标题与关闭按钮;关闭即退场并从 DOM 移除(内容由调用方持有) ***/
export function alert(props: AlertProps): HTMLElement {
const body = h(
'div',
{ class: 'ua-alert-body' },
props.title ? h('div', { class: 'ua-alert-title' }, props.title) : null,
h('div', { class: 'ua-alert-message' }, props.message),
);
const el = h('div', { class: `ua-alert ua-alert-${props.variant ?? 'info'}`, role: 'alert' }, body);
if (!props.closable) return el; // 纯展示形态
el.append(
h(
'button',
{
type: 'button',
class: 'ua-alert-close',
'aria-label': '关闭提示',
onClick: () => {
el.classList.add('ua-alert-out'); // 先退场
window.setTimeout(() => el.isConnected && el.remove(), 200); // 过渡结束再摘除
props.onClose?.();
},
},
'×',
),
);
return el;
}