You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
885 B
32 lines
885 B
export type ToastType = 'success' | 'error' | 'info' | 'warning'
|
|
|
|
export interface Toast {
|
|
id: number
|
|
type: ToastType
|
|
msg: string
|
|
}
|
|
|
|
let _counter = 0
|
|
|
|
export function useToast() {
|
|
const toasts = useState<Toast[]>('app:toasts', () => [])
|
|
|
|
function toast(msg: string, type: ToastType = 'info', duration = 3000) {
|
|
const id = ++_counter
|
|
toasts.value.push({ id, type, msg })
|
|
if (duration > 0) {
|
|
setTimeout(() => {
|
|
toasts.value = toasts.value.filter((t) => t.id !== id)
|
|
}, duration)
|
|
}
|
|
}
|
|
|
|
return {
|
|
toasts,
|
|
toast,
|
|
success: (msg: string, duration?: number) => toast(msg, 'success', duration),
|
|
error: (msg: string, duration?: number) => toast(msg, 'error', duration),
|
|
info: (msg: string, duration?: number) => toast(msg, 'info', duration),
|
|
warn: (msg: string, duration?: number) => toast(msg, 'warning', duration),
|
|
}
|
|
}
|
|
|