38 lines
669 B
TypeScript
38 lines
669 B
TypeScript
import { ref } from "vue";
|
|
|
|
interface Toast {
|
|
id: number;
|
|
message: string;
|
|
type: "success" | "error" | "info";
|
|
}
|
|
|
|
const toasts = ref<Toast[]>([]);
|
|
let nextId = 0;
|
|
|
|
export function useToast() {
|
|
function show(
|
|
message: string,
|
|
type: Toast["type"] = "success",
|
|
duration = 2500,
|
|
): void {
|
|
const id = nextId++;
|
|
toasts.value.push({ id, message, type });
|
|
setTimeout(() => {
|
|
dismiss(id);
|
|
}, duration);
|
|
}
|
|
|
|
function dismiss(id: number): void {
|
|
const index = toasts.value.findIndex((t) => t.id === id);
|
|
if (index !== -1) {
|
|
toasts.value.splice(index, 1);
|
|
}
|
|
}
|
|
|
|
return {
|
|
toasts,
|
|
show,
|
|
dismiss,
|
|
};
|
|
}
|