FxNotification 通知
悬浮出现在页面角落(默认右上角),用于主动推送较重要的通知。不依赖任何 UI 框架,样式统一走 @fx/styles token,图标走 @fx-core/icon。
基础用法
title 设置标题,message 设置内容(可为字符串 / VNode / 返回 VNode 的函数),duration 控制自动关闭时长(默认 4500ms,设 0 不自动关)。
不同类型
fxNotification.primary / .success / .warning / .info / .error 快捷方法,自动带对应类型图标与配色。
出现位置
position 控制:top-right(默认)/ top-left / bottom-right / bottom-left。
带偏移
offset 控制距边缘的额外偏移(同一方位的多条会自动堆叠,间距 16px)。
不自动关闭
duration 设为 0 时不自动关闭,需手动关闭。
隐藏关闭按钮
showClose 设为 false 隐藏右上角关闭按钮。
使用 HTML 内容
dangerouslyUseHTMLString 使 message 作为 HTML 渲染(注意 XSS 风险,message 内容应是可信的,切勿直接渲染用户输入)。
使用 VNode 内容
message 可直接传 VNode,或传一个返回 VNode 的函数(含动态 props 时必须用函数形式)。
局部引入
ts
import { fxNotification } from "@fx/components"
fxNotification(options)
fxNotification.primary(options)
fxNotification.success(options)
fxNotification.warning(options)
fxNotification.info(options)
fxNotification.error(options)
// 关闭所有
fxNotification.closeAll()
// 单个实例关闭
const handle = fxNotification({ message: "hi" })
handle.close()API
Options
| 名称 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| title | 标题 | string | — |
| message | 内容 | string | VNode | (() => VNode) | — |
| type | 类型 | 'primary' | 'success' | 'info' | 'warning' | 'error' | — |
| icon | 自定义图标名(被 type 覆盖) | string(IconString,如 ep:bell) | — |
| closeIcon | 自定义关闭图标名 | string(IconString) | ep:close |
| duration | 自动关闭时长 ms(0 不自动关) | number | 4500 |
| position | 出现位置 | 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left' | top-right |
| offset | 距边缘偏移 | number | 0 |
| showClose | 是否显示关闭按钮 | boolean | true |
| dangerouslyUseHTMLString | message 是否作为 HTML 渲染 | boolean | false |
| customClass | 自定义类名(追加到根节点) | string | — |
| customStyle | 自定义样式(合并到根节点 style) | CSSProperties | — |
| zIndex | 自定义层级(不传则自动递增) | number | — |
| onClick | 点击通知时的回调 | () => void | — |
| onClose | 关闭时的回调 | (vm: VNode) => void | — |
| appendTo | 挂载容器,传非法值回退到 document.body | string | HTMLElement | body |
fxNotification.success(...)等快捷方法等同于fxNotification({ ...options, type: 'success' }),也支持直接传字符串或 VNode 作为message。
Slots
| 名称 | 说明 | 参数 |
|---|---|---|
| default | 自定义内容区,覆盖 message | — |
Events
| 名称 | 说明 | 回调参数 |
|---|---|---|
| destroy | 通知完全离场(动画结束后)触发,内部用于清理 DOM | — |
Methods
| 名称 | 说明 |
|---|---|
| fxNotification.closeAll() | 关闭所有方位的全部通知 |
| fxNotification.updateOffsets(position?) | 重新计算指定方位(默认 top-right)的堆叠偏移 |
| 返回值.close() | 关闭单个通知(走完整离场动画生命周期) |
Expose(组件实例)
| 名称 | 说明 |
|---|---|
| visible | 是否可见(Ref<boolean>) |
| close | 关闭当前通知 |
全局属性
安装插件后可在模板中通过 $fxNotification / $fxNotify 调用。
实现说明
- 命令式挂载:
fxNotification通过createVNode+render在运行时创建实例并挂载到appendTo指定容器,不依赖任何全局组件注册。message为函数时作为 children render prop(含动态 props 的 VNode 必须用此形式),为 VNode 时包一层返回函数,否则按字符串 / HTML 渲染。 - 方位分桶堆叠:内部按 4 个方位各自维护实例队列,新实例的纵向偏移 = 已有实例高度累加 + 16px 间距;某条关闭时,其后方的实例自动回收高度并更新
offset,从而平滑上滑/下滑(top/bottom过渡)。 - 关闭生命周期:返回值的
close()仅把内部visible置为false,让<transition>走完整离场动画,@before-leave时回收堆叠偏移、调用用户onClose,@after-leave触发destroy清理 DOM,避免内存泄漏。 - 图标方案(fx):类型图标与关闭图标统一用
@fx-core/icon(FxIcon,图标名为 IconString,如ep:success-filled),类型配色通过--color-*token 注入;type与icon同时存在时type优先。 - 样式取值(fx):宽 / 内边距 / 圆角 / 阴影 / 边框 / 字号 / 字重等规格均与设计基准一致,圆角走
--radius-lg、阴影走--shadow-popup、边框走--color-border、字重走--fw-bold,不引用任何第三方 UI 库 CSS 变量。 - 层级管理(fx 增强):未传
zIndex时自动从全局nextZIndex()取值并递增,避免多实例层级错乱。 - 键盘交互:监听
delete/backspace暂停计时,esc关闭当前通知,其余按键恢复自动关闭计时。
