Skip to content

FxMsgBox 消息确认框

命令式消息确认框,提供 confirm / alert / prompt 三种用法,返回 Promise。不依赖任何 UI 框架,基于 fxOverlay + fxFocusTrap,样式走 @fx/styles token,图标走 @fx-core/iconep:xxx)。

基础用法(confirm)

以「确认 / 取消」为目的的二次确认。点击确定 resolve 'confirm',点击取消 / 关闭 reject 'cancel'distinguishCancelAndClose 时区分 'close')。

提示(alert)

仅展示信息、单个确定按钮。alert 默认 closeOnClickModal / closeOnPressEscapefalse。也可改用 callback 替代 Promise 接收结果。

输入(prompt)

带输入框的确认,resolve { value, action }。可用 inputPatterninputValidator 校验。

不同状态

通过 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打开时是否自动聚焦(确定按钮 / 输入框)booleantrue
title标题,传 null/undefined 不渲染标题栏string
message内容string | VNode | ((h: ActionHandlers) => VNode)
type状态类型(决定图标与配色)'primary' | 'success' | 'warning' | 'info' | 'error'
icon自定义状态图标。字符串为 IconString 类型(即 FxIcon 图标名,如 ep:editant-design:homesvg:xxx),亦可传组件string | Component
closeIcon自定义关闭图标。字符串为 IconString 类型(即 FxIcon 图标名,如 ep:editant-design:homesvg:xxx),亦可传组件string | Componentep:close
dangerouslyUseHTMLStringmessage 作为 HTML 字符串渲染booleanfalse
showClose是否显示关闭按钮booleantrue
showConfirmButton / showCancelButton显示确定 / 取消按钮booleantrue / false
confirmButtonText / cancelButtonText按钮文字string确定 / 取消
confirmButtonType / cancelButtonType按钮类型,传 'text' 渲染为文本按钮ButtonTypeprimary / —
confirmButtonClass / cancelButtonClass按钮自定义类名string
confirmButtonDisabled禁用确定按钮booleanfalse
confirmButtonLoading / cancelButtonLoading按钮加载态booleanfalse
confirmButtonLoadingIcon / cancelButtonLoadingIcon按钮加载图标string | Componentep:loading
buttonSize按钮尺寸'small' | 'default' | 'large'
roundButton圆角按钮booleanfalse
center内容居中booleanfalse
draggable / overflow可拖拽 / 允许超出视口booleanfalse
modal / modalClass / lockScroll遮罩 / 遮罩类名 / 锁定滚动boolean | stringtrue / — / true
closeOnClickModal / closeOnPressEscape / closeOnHashChange点击遮罩 / ESC / hash 变化时关闭booleantrue / true / true
beforeClose关闭前回调,调 done() 才真正关闭(action, instance, done) => void
callback不用 Promise 时的回调(value, action) => any
distinguishCancelAndClose区分取消与关闭booleanfalse
customClass / customStyle自定义类名 / 行内样式string | CSSProperties
appendTo挂载容器string | HTMLElementbody

prompt 专属

名称说明类型默认值
showInput是否显示输入框booleanfalse
inputPlaceholder输入框占位string
inputType输入框类型,textarea 渲染为多行stringtext
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:resolve Action'confirm');取消 / 关闭 reject 'cancel'distinguishCancelAndClose 时关闭为 'close'
  • prompt:resolve { value: string, action: 'confirm' };取消 / 关闭同上

全局属性

安装插件后可用:$fxMsgBox / $fxAlert / $fxConfirm / $fxPrompt

实现说明

  • UI 框架无关:遮罩用内置 fxOverlay、焦点陷阱用 fxFocusTrap,输入框使用原生 input / textarea(不依赖第三方表单组件),按钮使用 FxBtn,确保整组件可跨 UI 框架复用。
  • 图标体系:状态图标与关闭图标统一走 @fx-core/icontype 映射 success → ep:success-filledwarning → ep:warning-filledinfo → ep:info-fillederror → 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/styles token(--color-* / --radius-* / --shadow-popup / --duration-*),盒体规格(宽 420px、圆角 4px、内边距 12px、标题 18px、内容 14px、错误 12px、行高 24px)与设计基线一致;遮罩层以伪元素垂直居中,进出动画 msgbox-fade
  • fx 增强(保留):按钮区用 gap 排布;错误提示区预留 min-height 防抖动;输入框 textarea 支持 resize: vertical 与最小高度。