feat: add toast notification system
This commit is contained in:
43
src/stores/toast.ts
Normal file
43
src/stores/toast.ts
Normal 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 }
|
||||
})
|
||||
Reference in New Issue
Block a user