feat: add toast notification system

This commit is contained in:
Your Name
2026-02-17 21:25:02 +02:00
parent 552e8c0607
commit 520846511b
3 changed files with 79 additions and 28 deletions

43
src/stores/toast.ts Normal file
View File

@@ -0,0 +1,43 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
export interface Toast {
id: number
message: string
type: 'success' | 'error' | 'info'
exiting?: boolean
}
export const useToastStore = defineStore('toast', () => {
const toasts = ref<Toast[]>([])
let nextId = 0
function addToast(message: string, type: Toast['type'] = 'info') {
const id = nextId++
toasts.value.push({ id, message, type })
// Max 3 visible
if (toasts.value.length > 3) {
toasts.value.shift()
}
// Auto-dismiss after 3s
setTimeout(() => removeToast(id), 3000)
}
function removeToast(id: number) {
const toast = toasts.value.find(t => t.id === id)
if (toast) {
toast.exiting = true
setTimeout(() => {
toasts.value = toasts.value.filter(t => t.id !== id)
}, 150)
}
}
function success(message: string) { addToast(message, 'success') }
function error(message: string) { addToast(message, 'error') }
function info(message: string) { addToast(message, 'info') }
return { toasts, addToast, removeToast, success, error, info }
})