Files
vigilcare-records/vigilcare-records-web/src/composables/useToast.ts
T

32 lines
802 B
TypeScript

import { ref } from 'vue'
export type ToastType = 'success' | 'error' | 'warning' | 'info'
export interface Toast {
id: number
message: string
type: ToastType
duration: number
}
let nextId = 0
export const toasts = ref<Toast[]>([])
function addToast(message: string, type: ToastType, duration = 4000) {
const id = nextId++
toasts.value.push({ id, message, type, duration })
setTimeout(() => {
toasts.value = toasts.value.filter(t => t.id !== id)
}, duration)
}
export function useToast() {
return {
success: (message: string) => addToast(message, 'success'),
error: (message: string) => addToast(message, 'error', 6000),
warning: (message: string) => addToast(message, 'warning', 5000),
info: (message: string) => addToast(message, 'info'),
toasts,
}
}