54 lines
990 B
TypeScript
54 lines
990 B
TypeScript
import { writable } from 'svelte/store';
|
|
|
|
export type ToastColor =
|
|
| 'primary'
|
|
| 'secondary'
|
|
| 'success'
|
|
| 'danger'
|
|
| 'warning'
|
|
| 'info'
|
|
| 'light'
|
|
| 'dark';
|
|
|
|
export interface Toast {
|
|
id: string;
|
|
header: string;
|
|
body: string;
|
|
color: ToastColor;
|
|
persistent: boolean;
|
|
onRemove?: (id: string) => void;
|
|
}
|
|
|
|
export interface ToastInit {
|
|
header: string;
|
|
body: string;
|
|
color?: ToastColor;
|
|
persistent?: boolean;
|
|
onRemove?: (id: string) => void;
|
|
}
|
|
|
|
export const toasts = writable<Toast[]>([]);
|
|
|
|
export function addToast(init: ToastInit): string {
|
|
const id = crypto.randomUUID();
|
|
toasts.update((all) => [
|
|
...all,
|
|
{
|
|
id,
|
|
header: init.header,
|
|
body: init.body,
|
|
color: init.color ?? 'info',
|
|
persistent: init.persistent ?? false,
|
|
onRemove: init.onRemove,
|
|
},
|
|
]);
|
|
return id;
|
|
}
|
|
|
|
export function removeToast(id: string): void {
|
|
toasts.update((all) => {
|
|
const t = all.find((x) => x.id === id);
|
|
t?.onRemove?.(id);
|
|
return all.filter((x) => x.id !== id);
|
|
});
|
|
}
|