diff --git a/app/composables/useToast.ts b/app/composables/useToast.ts new file mode 100644 index 0000000..bc03ff8 --- /dev/null +++ b/app/composables/useToast.ts @@ -0,0 +1,30 @@ +type Toast = { + id: number + message: string + type?: "success" | "error" | "info" +} + +const toasts = ref([]) + +export const useToast = () => { + const show = ( + message: string, + type: Toast["type"] = "info", + duration = 5 * 1000 + ): number => { + const id = toasts.value.length + 1 + toasts.value.push({ id, message, type }) + + setTimeout(() => { + remove(id) + }, duration) + + return id + } + + const remove = (id: number) => { + toasts.value = toasts.value.filter((t) => t.id != id) + } + + return { toasts, show, remove } +} diff --git a/app/ui/ToastContainer.vue b/app/ui/ToastContainer.vue new file mode 100644 index 0000000..aad626d --- /dev/null +++ b/app/ui/ToastContainer.vue @@ -0,0 +1,77 @@ + + + + +