FxMsgBox 消息确认框
命令式消息确认框,提供 confirm / alert / prompt 三种用法,返回 Promise。不依赖任何 UI 框架,基于 fxOverlay + fxFocusTrap,样式走 @fx/styles token,图标走 @fx-core/icon(ep:xxx)。
基础用法(confirm)
以「确认 / 取消」为目的的二次确认。点击确定 resolve 'confirm',点击取消 / 关闭 reject 'cancel'(distinguishCancelAndClose 时区分 'close')。
提示(alert)
仅展示信息、单个确定按钮。alert 默认 closeOnClickModal / closeOnPressEscape 为 false。也可改用 callback 替代 Promise 接收结果。
输入(prompt)
带输入框的确认,resolve { value, action }。可用 inputPattern 或 inputValidator 校验。
不同状态
通过 type 显示 success / warning / info / error 图标与配色。
内容居中
center 让标题、图标、按钮区域居中。
自定义图标
icon 直接传图标名(图标集前缀,如 ep:delete),无需 markRaw。
使用 HTML 字符串
dangerouslyUseHTMLString: true 时把 message 作为 HTML 渲染。注意:动态拼接 HTML 存在 XSS 风险,仅用于可信内容。
自定义内容(VNode)
message 支持 VNode 或渲染函数,可渲染任意内容(含表单、动态 props)。
自定义按钮 + action 处理器
message 作为函数时,参数会注入 { confirm, cancel, close } 三个处理器,可在自定义内容里触发对应动作。
关闭前处理(beforeClose)
beforeClose(action, instance, done) 拦截关闭,常用于「确定后异步提交、完成再关闭」,可结合 instance.confirmButtonLoading 显示加载态。
区分取消与关闭
distinguishCancelAndClose: true 后,点击遮罩 / ESC / 关闭按钮 reject 'close',点击取消按钮 reject 'cancel'。
可拖拽
draggable 开启拖拽,overflow 允许拖出视口;可配合 customClass 自定义拖拽态样式。
局部引入
ts
import { fxMsgBox } from "@fx/components"
// 三种快捷方式
fxMsgBox.alert(message, title?, options?)
fxMsgBox.confirm(message, title?, options?)
fxMsgBox.prompt(message, title?, options?)
// 通用入口
fxMsgBox(options)
// 关闭所有
fxMsgBox.close()Options
| 名称 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| autofocus | 打开时是否自动聚焦(确定按钮 / 输入框) | boolean | true |
| title | 标题,传 null/undefined 不渲染标题栏 | string | — |
| message | 内容 | string | VNode | ((h: ActionHandlers) => VNode) | — |
| type | 状态类型(决定图标与配色) | 'primary' | 'success' | 'warning' | 'info' | 'error' | — |
| icon | 自定义状态图标。字符串为 IconString 类型(即 FxIcon 图标名,如 ep:edit、ant-design:home、svg:xxx),亦可传组件 | string | Component | — |
| closeIcon | 自定义关闭图标。字符串为 IconString 类型(即 FxIcon 图标名,如 ep:edit、ant-design:home、svg:xxx),亦可传组件 | string | Component | ep:close |
| dangerouslyUseHTMLString | message 作为 HTML 字符串渲染 | boolean | false |
| showClose | 是否显示关闭按钮 | boolean | true |
| showConfirmButton / showCancelButton | 显示确定 / 取消按钮 | boolean | true / false |
| confirmButtonText / cancelButtonText | 按钮文字 | string | 确定 / 取消 |
| confirmButtonType / cancelButtonType | 按钮类型,传 'text' 渲染为文本按钮 | ButtonType | primary / — |
| confirmButtonClass / cancelButtonClass | 按钮自定义类名 | string | — |
| confirmButtonDisabled | 禁用确定按钮 | boolean | false |
| confirmButtonLoading / cancelButtonLoading | 按钮加载态 | boolean | false |
| confirmButtonLoadingIcon / cancelButtonLoadingIcon | 按钮加载图标 | string | Component | ep:loading |
| buttonSize | 按钮尺寸 | 'small' | 'default' | 'large' | — |
| roundButton | 圆角按钮 | boolean | false |
| center | 内容居中 | boolean | false |
| draggable / overflow | 可拖拽 / 允许超出视口 | boolean | false |
| modal / modalClass / lockScroll | 遮罩 / 遮罩类名 / 锁定滚动 | boolean | string | true / — / true |
| closeOnClickModal / closeOnPressEscape / closeOnHashChange | 点击遮罩 / ESC / hash 变化时关闭 | boolean | true / true / true |
| beforeClose | 关闭前回调,调 done() 才真正关闭 | (action, instance, done) => void | — |
| callback | 不用 Promise 时的回调 | (value, action) => any | — |
| distinguishCancelAndClose | 区分取消与关闭 | boolean | false |
| customClass / customStyle | 自定义类名 / 行内样式 | string | CSSProperties | — |
| appendTo | 挂载容器 | string | HTMLElement | body |
prompt 专属
| 名称 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| showInput | 是否显示输入框 | boolean | false |
| inputPlaceholder | 输入框占位 | string | — |
| inputType | 输入框类型,textarea 渲染为多行 | string | text |
| inputValue | 输入框初始值 | string | — |
| inputPattern | 输入正则校验 | RegExp | — |
| inputValidator | 输入校验函数,返回 false 用默认错误,返回字符串作为错误信息 | (value) => boolean | string | — |
| inputErrorMessage | 校验失败提示 | string | 输入不合法 |
Slots
| 名称 | 说明 | 参数 |
|---|---|---|
| default | 内容区默认插槽,覆盖 message | — |
命令式调用时通过
message传 VNode / 渲染函数等价于使用默认插槽。
Events
| 名称 | 说明 | 回调参数 |
|---|---|---|
| action | 触发某个动作时(confirm / cancel / close) | (action: Action) |
| vanish | 关闭动画结束后(实例销毁) | — |
Methods
| 名称 | 说明 |
|---|---|
| fxMsgBox.close() | 关闭所有命令式实例 |
返回值
confirm/alert:resolveAction('confirm');取消 / 关闭 reject'cancel'(distinguishCancelAndClose时关闭为'close')prompt:resolve{ value: string, action: 'confirm' };取消 / 关闭同上
全局属性
安装插件后可用:$fxMsgBox / $fxAlert / $fxConfirm / $fxPrompt。
实现说明
- UI 框架无关:遮罩用内置
fxOverlay、焦点陷阱用fxFocusTrap,输入框使用原生input/textarea(不依赖第三方表单组件),按钮使用FxBtn,确保整组件可跨 UI 框架复用。 - 图标体系:状态图标与关闭图标统一走
@fx-core/icon。type映射success → ep:success-filled、warning → ep:warning-filled、info → ep:info-filled、error → ep:circle-close-filled,并按--color-success/--color-warning/--color-info/--color-danger着色。 - 图标参数双形态(fx 增强):
icon/closeIcon/loadingIcon既可传图标名字符串(推荐,无需markRaw),也可传组件对象;命令式入口对组件对象自动markRaw,模板内字符串图标走FxIcon、组件对象原样透传。 - 命令式实例管理:
messageInstance维护当前所有活跃实例,close()遍历doClose()并清空;onVanish(动画after-leave)负责卸载 vnode 与释放引用,避免内存泄漏。 - prompt 校验链:先
inputPattern正则,再inputValidator函数;inputValidator返回字符串时作为错误信息。inputValue变化时自动重新校验。 - 关闭前拦截:
beforeClose(action, instance, done)不调done()则不关闭,配合instance.confirmButtonLoading = true可实现「异步提交完成后再关闭」。 - action 区分:
distinguishCancelAndClose决定遮罩 / ESC / 关闭按钮触发的是'cancel'还是'close',影响 Promise reject 的值。 - 无障碍:根节点
role="dialog"、aria-modal,按showInput切换aria-describedby;prompt 下 message 渲染为<label for=inputId>;输入框带aria-invalid。focus trap 在release-requested(ESC)时按closeOnPressEscape决定是否关闭。 - 样式 token:所有取值走
@fx/stylestoken(--color-*/--radius-*/--shadow-popup/--duration-*),盒体规格(宽 420px、圆角 4px、内边距 12px、标题 18px、内容 14px、错误 12px、行高 24px)与设计基线一致;遮罩层以伪元素垂直居中,进出动画msgbox-fade。 - fx 增强(保留):按钮区用
gap排布;错误提示区预留min-height防抖动;输入框textarea支持resize: vertical与最小高度。
