You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import { writable } from 'svelte/store';
|
|
|
|
export type ToastType = 'info' | 'success' | 'error';
|
|
|
|
export type ToastItem = {
|
|
id: string;
|
|
type: ToastType;
|
|
message: string;
|
|
timeout?: number;
|
|
};
|
|
|
|
export const toasts = writable<ToastItem[]>([]);
|
|
|
|
function uid(): string {
|
|
return Math.random().toString(36).slice(2) + Date.now().toString(36);
|
|
}
|
|
|
|
export function show(message: string, type: ToastType = 'info', timeout = 2500): string {
|
|
const id = uid();
|
|
toasts.update((list) => [...list, { id, type, message, timeout }]);
|
|
if (timeout > 0) {
|
|
setTimeout(() => dismiss(id), timeout);
|
|
}
|
|
return id;
|
|
}
|
|
|
|
export function success(message: string, timeout = 2500): string {
|
|
return show(message, 'success', timeout);
|
|
}
|
|
|
|
export function error(message: string, timeout = 3500): string {
|
|
return show(message, 'error', timeout);
|
|
}
|
|
|
|
export function info(message: string, timeout = 2500): string {
|
|
return show(message, 'info', timeout);
|
|
}
|
|
|
|
export function dismiss(id: string): void {
|
|
toasts.update((list) => list.filter((t) => t.id !== id));
|
|
}
|