Skip to content

Message 消息提示 ​

新粗野主义风格的全局轻量消息提示系统(useMessage)。基于单例容器按需动态挂载与 TransitionGroup 堆叠动画,无需在模板中声明组件,调用即显示。如需模态对话框容器请参阅 Dialog 对话框,如需确认/输入反馈对话框请参阅 MessageBox 消息对话框。

预览 ​

Preview

四种消息类型

高级配置

安装 ​

pnpm dlx brutx-vue@latest add message

用法 ​

基础用法 ​

通过 info、success、warning、error 四个快捷方法显示不同类型的消息提示。

vue
<script setup>
import { useMessage } from 'brutx-ui-vue'

const { info, success, warning, error } = useMessage()
</script>

<template>
    <button @click="info('提示', '这是一条信息提示')">信息</button>
    <button @click="success('成功', '操作已成功完成')">成功</button>
    <button @click="warning('警告', '请注意检查输入内容')">警告</button>
    <button @click="error('错误', '操作失败,请重试')">错误</button>
</template>

自定义配置 ​

使用 show 方法传入完整配置项,自定义持续时间、是否显示关闭按钮等。

vue
<script setup>
import { useMessage } from 'brutx-ui-vue'

const { show } = useMessage()

function showCustom() {
    show({
        type: 'warning',
        title: '会话即将过期',
        description: '您的登录会话将在 5 分钟后失效,请及时保存工作。',
        duration: 8000,
        closable: true,
    })
}

function showPersistent() {
    show({
        type: 'error',
        title: '网络连接已断开',
        description: '请检查您的网络设置后重试。',
        duration: 0,
    })
}
</script>

<template>
    <button @click="showCustom">自定义时长(8 秒)</button>
    <button @click="showPersistent">不自动关闭</button>
</template>

手动关闭 ​

每个方法均返回一个 close 函数,调用即可手动关闭对应消息。

vue
<script setup>
import { useMessage } from 'brutx-ui-vue'
import { ref } from 'vue'

const { info } = useMessage()
const closeFn = ref(null)

function open() {
    closeFn.value = info('加载中', '正在处理请求,请稍候...')
}

function close() {
    closeFn.value?.()
}
</script>

<template>
    <button @click="open">打开消息</button>
    <button @click="close">手动关闭</button>
</template>

长文本换行 ​

标题和描述会在消息卡片的可用宽度内自动换行,包括没有空格的长词。完整文本始终保留,关闭按钮仍可操作。

API 参考 ​

MessageContainer 是公开导出的消息渲染容器,没有可配置的组件属性。消息操作入口是 useMessage(),其快捷方法、show(options) 和完整 MessageOptions 定义见下文。

数据类型 ​

MessageType ​

ts
type MessageType = 'info' | 'success' | 'warning' | 'error'

MessageOptions ​

ts
interface MessageOptions {
    type?: 'info' | 'success' | 'warning' | 'error'
    title?: string
    description?: string
    duration?: number
    closable?: boolean
}
字段类型默认值说明
type'info' | 'success' | 'warning' | 'error''info'消息类型
titlestring''标题文本
descriptionstring—描述文本
durationnumber3000自动关闭时长(毫秒),设为 0 不自动关闭
closablebooleantrue是否显示关闭按钮

MessageItem ​

ts
interface MessageItem {
    id: string
    type: MessageType
    title: string
    description?: string
    duration: number
    closable: boolean
}

ShowDialogOptions ​

ShowDialogOptions<T> 从 brutx-ui-vue/dialog 导出。参数与示例见 showDialog。

命令式浮层的应用上下文 ​

自定义非模态浮层时,从 brutx-ui-vue 导入 mountOverlay,调用 mountOverlay(Notice, props, { modal: false })。

mountOverlay()、showDialog() 和 showMessageBox() 优先使用显式传入的 appContext,其次使用安装了 BrutxUIPlugin 的最内层 app.runWithContext() 的应用。组件调用可继承所在应用与祖先提供的语言配置。useDialog() 和 useMessageBox() 保留创建时的上下文,供离开该上下文后调用。没有调用方上下文时,使用最近安装插件且仍存活的应用。应用卸载会清理归属该应用的应用级浮层。

对于已安装插件的应用,mountOverlay() 在 runWithContext() 的同步回调中立即返回句柄。回调中调用 close() 或 destroy() 可以结束尚待挂载的浮层。最外层回调结束后,Vue 退出运行上下文,浮层在 runWithContext() 返回前同步挂载,props 工厂此时首次读取状态。

首次挂载中的直抛异常会清理失败浮层,待结算句柄的 promise 以首个异常拒绝。若异常在 runWithContext() 返回前发生,调用也会同步传播异常,回调与挂载均抛错时同步传播回调异常。若 Vue 将首次挂载钩子排到调用返回之后,错误通过失败浮层的句柄传递。应用自定义错误处理器继续遵循 Vue 的错误处理流程。

这些应用识别与挂载时序适用于已安装插件的 runWithContext() 调用栈。未安装插件时,可在应用上下文回调以外显式传入 appContext 来指定归属。异步回调从第一个 await 恢复后已离开 Vue 的运行上下文。

MessageBoxOptions ​

MessageBoxOptions 从 brutx-ui-vue/message-box 导出。MessageBox 页面说明了组件属性和命令式方法。

组合式函数 ​

useMessage ​

ts
import { useMessage } from 'brutx-ui-vue'

const {
    info,      // (title: string, description?: string) => () => void
    success,   // (title: string, description?: string) => () => void
    warning,   // (title: string, description?: string) => () => void
    error,     // (title: string, description?: string) => () => void
    show,      // (options: MessageOptions) => () => void
} = useMessage()
方法参数返回值说明
info(title: string, description?: string)() => void显示信息提示,返回关闭函数
success(title: string, description?: string)() => void显示成功提示,返回关闭函数
warning(title: string, description?: string)() => void显示警告提示,返回关闭函数
error(title: string, description?: string)() => void显示错误提示,返回关闭函数
show(options: MessageOptions)() => void自定义提示,返回关闭函数

核心特性:

  • 单例模式:首次调用自动挂载 MessageContainer 到 body,无需手动声明
  • 堆叠动画:多条消息通过 TransitionGroup 垂直堆叠,自动排列
  • 自动关闭:默认 3 秒后自动关闭,可通过 duration 自定义,设为 0 禁用
  • 手动关闭:每个方法返回 close 函数,调用即可立即关闭该条消息
  • 活跃消息守卫与惰性 GC:具备组件引用计数与活跃消息双重守卫,组件销毁时展示中消息不被误杀;全部消息消费完毕且无活跃组件后 3000ms 触发惰性 GC 卸载容器
  • 显式清理。从 brutx-ui-vue 导入 destroyMessageFallback,或从 brutx-ui-vue/useMessage 导入 destroyFallback。要统一清理 BrutxUI 的后备实例和命令式浮层,可调用 destroyBrutxUI()。
  • SSR 安全:内部使用 isClient 守卫,服务端渲染环境不会报错

useDialog ​

ts
import { useDialog } from 'brutx-ui-vue'

const {
    show,      // (options?: ShowDialogOptions) => DialogInstance
} = useDialog()
方法参数返回值说明
show(options?: ShowDialogOptions)DialogInstance打开对话框

返回值说明:

属性类型说明
close() => void手动关闭对话框
promisePromise<DialogResult | undefined>兑现对话框的终态。SSR 或全局清理仍未结算的对话框时,结果为 undefined。

useMessageBox ​

ts
import { useMessageBox } from 'brutx-ui-vue'

const {
    show,      // (options?: MessageBoxOptions) => MessageBoxInstance
} = useMessageBox()
方法参数返回值说明
show(options?: MessageBoxOptions)MessageBoxInstance打开确认/输入框

返回值说明:

属性类型说明
close() => void手动关闭消息框
promisePromise<MessageBoxResult>恒兑现 { action, value? }:action='confirm' 确认(showInput 时 value 为输入值);action='cancel' 取消/关闭按钮/ESC/遮罩;action='destroy' 手动销毁

可访问性 ​

  • 键盘操作:对话框支持 Escape 关闭
  • ARIA 属性:消息提示使用 role="status" 和 aria-live="polite" 通知屏幕阅读器;关闭按钮包含国际化 aria-label,支持屏幕阅读器朗读
  • 焦点管理:useDialog 和 useMessageBox 基于 reka-ui 的 DialogRoot,打开时锁定焦点、关闭时恢复焦点
  • 动效降级:尊重 prefers-reduced-motion 系统设置,自动简化过渡动画

常见问题 ​

Q: useMessage 和 useToast 有什么区别?

A: useMessage 是轻量级的顶部消息提示,固定在页面顶部居中显示,适合简短的操作反馈;useToast 功能更丰富,支持多种位置、堆叠配置、Promise 追踪和悬停暂停等特性,适合复杂的通知场景。

Q: 消息提示在 SSR 环境下会报错吗?

A: 不会。useMessage 内部使用 isClient 守卫,在服务端渲染环境下调用不会产生副作用,也不会抛出错误。

Q: 如何在对话框关闭前进行异步校验?

A: 使用 beforeClose 钩子,支持回调模式和 Promise 模式:

vue
<script setup>
import { useDialog } from 'brutx-ui-vue'

const { show } = useDialog()

function openWithValidation() {
    show({
        title: '编辑资料',
        content: '表单内容...',
        beforeClose: async () => {
            const isValid = await validateForm()
            return isValid
        },
    })
}
</script>

Q: 如何读取 useMessageBox 的关闭结果?

A: 等待 useMessageBox().show(options).promise,通过 result.action === 'confirm' 判断是否确认。无参 close() 和 SSR 得到 cancel,无参 destroy() 和全局清理得到 destroy。详见 MessageBox 的 Promise 契约。

蛮力铸就。