Skip to content

Toast 轻提示

新粗野主义风格通知提示系统,提供 useToast 组合式函数、5 种变体和 ToastContainer 用于渲染。

预览

Preview

变体

尺寸

悬停暂停(pauseOnHover)

将鼠标悬停在提示上可暂停倒计时与进度条,移出后从剩余时间继续。点击下方按钮显示一条 10 秒提示进行体验,可切换开关对比行为。

安装

pnpm dlx brutx-vue@latest add toast

用法

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

<template>
    <router-view />
    <ToastContainer />
</template>

使用 useToast

vue
<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>

自定义提示

vue
<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,不通过 useToastaddToast / ToastItem 传递。在自定义 ToastContainer 渲染 <Toast> 时直接绑定即可。

vue
<script setup>
import { useToast, ToastContainer, Toast } from 'brutx-ui-vue'

const { toasts, addToast, removeToast } = useToast()
</script>

<template>
    <ToastContainer position="bottom-right">
        <Toast
            v-for="toast in toasts"
            :key="toast.id"
            :duration="toast.duration"
            :pause-on-hover="true"
            @close="removeToast(toast.id)"
        />
    </ToastContainer>

    <button @click="addToast({ title: '悬停我', description: '将鼠标移到提示上可暂停倒计时。', duration: 10000 })">
        显示可暂停提示
    </button>
</template>

变体

变体背景图标
defaultbg-brutal-bgZap
successbg-brutal-successCheckCircle
errorbg-brutal-destructiveAlertCircle
warningbg-brutal-accentAlertTriangle
infobg-brutal-secondaryInfo

所有变体统一使用 text-brutal-fg 文字颜色和 shadow-brutal-lg 阴影。

尺寸

尺寸宽度
smw-72 (288px)
defaultw-80 (320px)
lgw-96 (384px)

数据类型

ToastItem

ts
interface ToastItem {
    id: string
    variant?: 'default' | 'success' | 'error' | 'warning' | 'info'
    size?: 'sm' | 'default' | 'lg'
    title?: string
    description?: string
    duration?: number
}

PromiseToastOptions

ts
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

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

const {
    toasts,          // Ref<ToastItem[]> - 响应式提示列表
    addToast,        // (toast: Omit<ToastItem, 'id'>) => string
    removeToast,     // (id: string) => void
    clearToasts,     // () => void
    clearAllTimers,  // () => 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()

返回值

属性类型说明
toastsRef<ToastItem[]>响应式提示列表
addToast(toast: Omit<ToastItem, 'id'>) => string添加自定义提示,返回提示 ID
removeToast(id: string) => void移除指定提示
clearToasts() => void清除所有提示
clearAllTimers() => 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() 会自动注入该实例。

vue
<!-- App.vue -->
<script setup>
import { provideToast } from 'brutx-ui-vue'

provideToast()
</script>

注意:若未调用 provideToast()useToast() 会回退到共享单例并输出警告。生产应用推荐在根组件显式调用 provideToast();共享单例仅作为兼容兜底。测试、多应用同页或热更新场景可调用 destroyBrutxFallbacks() 集中清理 toast/theme/message 的 fallback 状态。

Promise Toast

自动追踪 Promise 状态,显示 loading → success/error 流程:

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

const { promise } = useToast()

async function handleSave() {
    await promise(saveData(), {
        loading: '保存中...',
        success: '保存成功!',
        error: '保存失败,请重试',
    })
}
</script>

Props

Toast

属性类型默认值说明
variant'default' | 'success' | 'error' | 'warning' | 'info''default'提示类型
size'sm' | 'default' | 'lg''default'尺寸
titlestring标题文本
descriptionstring描述文本
durationnumber5000显示时长(毫秒),设为 0 则不自动关闭
pauseOnHoverbooleantrue鼠标悬停时暂停倒计时与进度条动画,移出后从剩余时间继续
iconSize'xs' | 'sm' | 'default' | 'lg' | 'xl' | '2xl''xl'主图标尺寸
classstring自定义样式类

ToastContainer

属性类型默认值说明
position'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right' | { x: number; y: number; anchor?: string }'bottom-right'显示位置
stack{ maxVisible?: number; gap?: number; expandDirection?: 'up' | 'down' }{ maxVisible: 5, gap: 12, expandDirection: 'down' }堆叠配置
classstring自定义样式类

事件

事件参数说明
close提示关闭时触发(动画完成后)

插槽

插槽作用域说明
default自定义内容,渲染在标题和描述下方

可访问性

  • 键盘操作:支持 Escape 关闭当前提示
  • ARIA 属性:Toast 容器使用 role="status"aria-live="polite" 通知屏幕阅读器
  • 动效降级:尊重 prefers-reduced-motion 系统设置,自动禁用或简化动画

蛮力铸就。