61 lines
1.4 KiB
TypeScript
61 lines
1.4 KiB
TypeScript
/**
|
|
* Bootstrap Toast Composable
|
|
* 전역 토스트 메시지 관리
|
|
*/
|
|
interface ToastMessage {
|
|
id: number
|
|
type: 'success' | 'error' | 'warning' | 'info'
|
|
title?: string
|
|
message: string
|
|
duration?: number
|
|
}
|
|
|
|
const toasts = ref<ToastMessage[]>([])
|
|
let toastId = 0
|
|
|
|
export function useToast() {
|
|
function show(message: string, type: ToastMessage['type'] = 'info', title?: string, duration = 3000) {
|
|
const id = ++toastId
|
|
toasts.value.push({ id, type, title, message, duration })
|
|
|
|
if (duration > 0) {
|
|
setTimeout(() => {
|
|
remove(id)
|
|
}, duration)
|
|
}
|
|
}
|
|
|
|
function success(message: string, title?: string, duration = 3000) {
|
|
show(message, 'success', title || '성공', duration)
|
|
}
|
|
|
|
function error(message: string, title?: string, duration = 5000) {
|
|
show(message, 'error', title || '오류', duration)
|
|
}
|
|
|
|
function warning(message: string, title?: string, duration = 4000) {
|
|
show(message, 'warning', title || '경고', duration)
|
|
}
|
|
|
|
function info(message: string, title?: string, duration = 3000) {
|
|
show(message, 'info', title || '알림', duration)
|
|
}
|
|
|
|
function remove(id: number) {
|
|
const idx = toasts.value.findIndex(t => t.id === id)
|
|
if (idx > -1) {
|
|
toasts.value.splice(idx, 1)
|
|
}
|
|
}
|
|
|
|
return {
|
|
toasts,
|
|
show,
|
|
success,
|
|
error,
|
|
warning,
|
|
info,
|
|
remove
|
|
}
|
|
}
|