Skip to content

Toast

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

预览

安装

pnpm dlx brutx@latest add toast

用法

1. 在应用中添加 ToastContainer

ToastContainer 放置在根 App.vue 中:

vue
<script setup>
import ToastContainer from '@/components/ui/ToastContainer.vue'
</script>

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

2. 使用 useToast 组合式函数

vue
<script setup>
import { useToast } from '@/composables/useToast'

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>

变体

变体背景文字
defaultbg-brutal-bgtext-brutal-fg
successbg-brutal-successtext-black
errorbg-brutal-destructivetext-white
warningbg-brutal-accenttext-black
infobg-brutal-secondarytext-black

尺寸

尺寸最大宽度
smmax-w-xs
defaultmax-w-sm
lgmax-w-md

useToast API

ts
const {
    toasts,       // Ref<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
} = useToast()

自定义提示

vue
<script setup>
import { useToast } from '@/composables/useToast'

const { addToast } = useToast()

function showCustomToast() {
    addToast({
        variant: 'warning',
        title: 'Warning',
        description: 'Your session will expire in 5 minutes.',
        duration: 10000,
    })
}
</script>

ToastItem 类型

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

属性

Toast

属性类型默认值
variant'default' | 'success' | 'error' | 'warning' | 'info''default'
size'sm' | 'default' | 'lg''default'
classstring

ToastContainer

属性类型默认值
classstring