Toast 轻提示
新粗野主义风格通知提示系统,提供 useToast 组合式函数、5 种变体和 ToastContainer 用于渲染。
预览
变体
尺寸
悬停暂停(pauseOnHover)
将鼠标悬停在提示上可暂停倒计时与进度条,移出后从剩余时间继续。点击下方按钮显示一条 10 秒提示进行体验,可切换开关对比行为。
安装
pnpm dlx brutx-vue@latest add toast用法
<script setup>
import { ToastContainer } from 'brutx-ui-vue'
</script>
<template>
<router-view />
<ToastContainer />
</template>使用 useToast
<script setup>
import { useToast } from 'brutx-ui-vue'
const { success, error, warning, info, addToast } = useToast()
function handleSave() {
success('Saved!', 'Your changes have been saved.')
}
function handleError() {
error('Error', 'Something went wrong. Please try again.')
}
</script>
<template>
<button @click="handleSave">Save</button>
<button @click="handleError">Trigger Error</button>
</template>自定义提示
<script setup>
import { useToast } from 'brutx-ui-vue'
const { addToast } = useToast()
function showCustomToast() {
addToast({
variant: 'warning',
title: 'Warning',
description: 'Your session will expire in 5 minutes.',
duration: 10000,
})
}
</script>悬停暂停
Toast 默认启用 pauseOnHover(默认值 true):鼠标移入提示时暂停倒计时与顶部进度条动画,移出后从剩余时间继续,便于用户阅读较长内容。设置 :pause-on-hover="false" 可禁用该行为。
注意:
pauseOnHover是<Toast>组件自身的 prop,不通过useToast的addToast/ToastItem传递。在自定义ToastContainer渲染<Toast>时直接绑定即可。
默认插槽提供可渲染的提示列表。每项包含 toast 数据、稳定的渲染 key、当前展示的 revision 与 close 回调。把 key 放在每项的最外层节点上,并将 revision 绑定到 Toast 的 presentation-revision。同类提示合并时保留 DOM,取消正在进行的离场并重启倒计时。如果焦点仍停留在离场时转移到的位置,合并后的提示会恢复原焦点。将 close 绑定到离场完成事件。新一轮展示开始后,上一轮回调失效。
<script setup>
import { useToast, ToastContainer, Toast } from 'brutx-ui-vue'
const { addToast } = useToast()
</script>
<template>
<ToastContainer v-slot="{ toasts }" position="bottom-right">
<Toast
v-for="{ toast, key, revision, close } in toasts"
:key="key"
:title="toast.title"
:description="toast.description"
:duration="toast.duration"
:count="toast.count"
:presentation-revision="revision"
:pause-on-hover="true"
@close="close"
/>
</ToastContainer>
<button @click="addToast({ title: '悬停我', description: '将鼠标移到提示上可暂停倒计时。', duration: 10000 })">
显示可暂停提示
</button>
</template>变体
| 变体 | 背景 | 图标 |
|---|---|---|
default | bg-brutal-bg | Zap |
success | bg-brutal-success | CheckCircle |
error | bg-brutal-destructive | AlertCircle |
warning | bg-brutal-accent | AlertTriangle |
info | bg-brutal-secondary | Info |
所有变体统一使用 text-brutal-fg 文字颜色和 shadow-brutal-lg 阴影。
尺寸
| 尺寸 | 宽度 |
|---|---|
sm | w-72 (288px) |
default | w-80 (320px) |
lg | w-96 (384px) |
数据类型
ToastItem
interface ToastItem {
id: string
variant?: 'default' | 'success' | 'error' | 'warning' | 'info'
size?: 'sm' | 'default' | 'lg'
title?: string
description?: string
duration?: number
}PromiseToastOptions
interface PromiseToastOptions<T> {
loading: string // loading 状态文本
success: string | ((data: T) => string) // 成功文本或格式化函数
error: string | ((error: Error) => string) // 错误文本或格式化函数
duration?: number // 成功/错误后显示时长
loadingVariant?: 'default' | 'success' | 'error' | 'warning' | 'info' // loading 状态变体
successVariant?: 'default' | 'success' | 'error' | 'warning' | 'info' // 成功状态变体
errorVariant?: 'default' | 'success' | 'error' | 'warning' | 'info' // 错误状态变体
}组合式函数
useToast
import { useToast } from 'brutx-ui-vue'
const {
toasts, // DeepReadonly<Ref<readonly ToastItem[]>> - 响应式提示列表
addToast, // (toast: Omit<ToastItem, 'id'>) => string
removeToast, // (id: string) => void
clearToasts, // () => void
success, // (title: string, description?: string) => string
error, // (title: string, description?: string) => string
warning, // (title: string, description?: string) => string
info, // (title: string, description?: string) => string
promise, // <T>(promise: Promise<T> | (() => Promise<T>), options: PromiseToastOptions<T>) => Promise<T>
} = useToast()返回值
| 属性 | 类型 | 说明 |
|---|---|---|
toasts | DeepReadonly<Ref<readonly ToastItem[]>> | 响应式提示列表 |
addToast | (toast: Omit<ToastItem, 'id'>) => string | 添加自定义提示,返回提示 ID |
removeToast | (id: string) => void | 按队列 ID 直接移除指定提示 |
clearToasts | () => void | 清除所有提示 |
success | (title: string, description?: string) => string | 显示成功提示 |
error | (title: string, description?: string) => string | 显示错误提示 |
warning | (title: string, description?: string) => string | 显示警告提示 |
info | (title: string, description?: string) => string | 显示信息提示 |
promise | <T>(promise: Promise<T> | (() => Promise<T>), options: PromiseToastOptions<T>) => Promise<T> | 自动追踪 Promise 状态 |
provideToast
在应用根组件调用 provideToast() 提供 toast 实例,子组件中 useToast() 会自动注入该实例。
<!-- App.vue -->
<script setup>
import { provideToast } from 'brutx-ui-vue'
provideToast()
</script>未提供实例时,
useToast()在客户端输出提示,并在当前应用内共享后备实例。浏览器中脱离 Vue 组件上下文调用时,共用浏览器后备实例。可以安装BrutxUIPlugin或调用provideToast()提供实例。测试隔离或热更新时,可调用destroyBrutxUI()清理后备实例和命令式浮层。
createToast
使用 createToast(options?) 创建独立队列,适用于显式提供实例或脱离 Vue 组件使用的场景。传入 { grouping: true } 为队列启用分组。若在 Vue 响应式作用域内创建,作用域停止时会清空队列。
Promise Toast
自动追踪 Promise 状态,显示 loading → success/error 流程:
<script setup>
import { useToast } from 'brutx-ui-vue'
const { promise } = useToast()
async function handleSave() {
await promise(saveData(), {
loading: '保存中...',
success: '保存成功!',
error: '保存失败,请重试',
})
}
</script>API 参考
Toast
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
variant | "default" | "error" | "info" | "success" | "warning" | undefined | "default" | 设置提示的语义和视觉类型,如成功、警告、信息或错误。 |
详情声明类型NonNullable<"default" | "success" | "warning" | "info" | "error" | null | undefined> | undefined 字面量
默认值详情
来源packages/ui/src/components/toast/Toast.vue | |||
size | "default" | "lg" | "sm" | undefined | "default" | 设置提示容器的尺寸变体。 |
详情声明类型NonNullable<"default" | "lg" | "sm" | null | undefined> | undefined 字面量
默认值详情
来源packages/ui/src/components/toast/Toast.vue | |||
title | string | undefined | undefined | 提示标题文字;省略时不渲染标题区域。 |
详情声明类型string | undefined 默认值详情
来源packages/ui/src/components/toast/Toast.vue | |||
description | string | undefined | undefined | 提示正文说明文字;省略时不渲染描述区域。 |
详情声明类型string | undefined 默认值详情
来源packages/ui/src/components/toast/Toast.vue | |||
duration | number | undefined | 5000 | 提示自动关闭前的显示时长,单位为毫秒;设为 0 时不自动关闭。 |
详情声明类型number | undefined 默认值详情
来源packages/ui/src/components/toast/Toast.vue | |||
pauseOnHover | boolean | undefined | true | 鼠标悬停时暂停自动关闭计时和进度动画,离开后继续剩余计时。 |
详情声明类型boolean | undefined 字面量
默认值详情
来源packages/ui/src/components/toast/Toast.vue | |||
class | string | undefined | undefined | 追加到提示根容器的 CSS 类。 |
详情声明类型string | undefined 默认值详情
来源packages/ui/src/components/toast/Toast.vue | |||
iconSize | IconSize | undefined | "xl" | 设置类型图标的尺寸。 |
详情声明类型IconSize | undefined 字面量
关联类型 IconSize 类型定义 — IconSizeexport type IconSize = NonNullable<VariantProps<typeof iconSizeVariants>['size']> 默认值详情
来源packages/ui/src/components/toast/Toast.vue | |||
count | number | undefined | 1 | 显示同类提示聚合数量;大于 1 时呈现计数标记。 |
详情声明类型number | undefined 默认值详情
来源packages/ui/src/components/toast/Toast.vue | |||
presentationRevision | symbol | undefined | undefined | 自定义列表中绑定 ToastContainer 提供的 revision。轮次变化时取消离场并重启倒计时。 |
详情声明类型symbol | undefined 默认值详情
来源packages/ui/src/components/toast/Toast.vue | |||
| 名称 | 签名或类型 | 说明 |
|---|---|---|
@close | [] | 提示离场结算后发出,每轮展示至多触发一次。 |
详情声明类型[] 来源packages/ui/src/components/toast/Toast.vue | ||
| 名称 | 签名或类型 | 说明 |
|---|---|---|
#default | {} | 显示在标题和描述之后的自定义补充内容。 |
详情声明类型{}来源packages/ui/src/components/toast/Toast.vue | ||
ToastContainer
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
position | ToastPosition | undefined | "bottom-right" | 设置提示容器在视口中的预设位置,或通过 x、y 和 anchor 指定偏移位置。 |
详情声明类型ToastPosition | undefined 字面量
关联类型 ToastPosition 类型定义 — ToastPositionexport type ToastPosition =
| 'top-left'
| 'top-center'
| 'top-right'
| 'bottom-left'
| 'bottom-center'
| 'bottom-right'
| { x: number; y: number; anchor?: 'top-left' | 'bottom-left' | 'top-right' | 'bottom-right' }默认值详情
来源packages/ui/src/components/toast/ToastContainer.vue | |||
stack | ToastStackOptions | undefined | { "expandDirection": "down", "gap": 12, "maxVisible": 5 }按实例调用工厂函数 | 设置可见提示数量上限、提示间距以及容器展开方向。 |
详情声明类型ToastStackOptions | undefined 关联类型 ToastStackOptions 类型定义 — ToastStackOptionsexport interface ToastStackOptions {
maxVisible?: number
gap?: number
expandDirection?: 'down' | 'up'
}默认值详情
来源packages/ui/src/components/toast/ToastContainer.vue | |||
class | string | undefined | undefined | 追加到固定定位提示容器的 CSS 类。 |
详情声明类型string | undefined 默认值详情
来源packages/ui/src/components/toast/ToastContainer.vue | |||
| 名称 | 签名或类型 | 说明 |
|---|---|---|
#default | { toasts: readonly RenderedToast[]; } | 自定义渲染提示列表。每项提供 toast 数据、稳定的 key、当前展示的 revision 和 close 回调。省略插槽时由容器自动渲染。 |
详情声明类型{ toasts: readonly RenderedToast[]; }关联类型 RenderedToast 类型定义 — RenderedToastinterface RenderedToast {
readonly toast: Readonly<ToastItem>
readonly key: symbol
readonly revision: symbol
readonly close: () => void
}来源packages/ui/src/components/toast/ToastContainer.vue | ||
可访问性
- 键盘操作:支持
Escape关闭当前提示 - ARIA 属性:Toast 容器使用
role="status"和aria-live="polite"通知屏幕阅读器 - 焦点管理:若焦点位于离场提示内,依次尝试聚焦后续提示的关闭按钮、在前提示的关闭按钮、原页面焦点及主内容区域。离场提示退出 Tab 顺序和指针命中。
- 动效降级:尊重
prefers-reduced-motion系统设置,自动禁用或简化动画
样式定制
通过 --brutal-toast-enter-duration 与 --brutal-toast-exit-duration 调整进出场时长,默认均为 150ms。提示根节点上有限的离场动效结算后触发 close,包括切换到离场状态时改变时序的 CSS 动画。动效结束、取消或暂停时视为结算,禁用动效时直接关闭。