This commit is contained in:
nub31
2026-03-16 22:00:24 +01:00
parent d677582757
commit 3bf281c7a7
41 changed files with 1790 additions and 7 deletions

View File

@@ -0,0 +1,46 @@
import { writable } from 'svelte/store';
export const toasts = writable<Toast[]>([]);
type Toast = {
id: string;
type: 'success' | 'error' | 'info' | 'warning';
text: string;
};
function addToast(
text: string,
type: 'success' | 'error' | 'warning' | 'info' = 'info',
dismissible: boolean
) {
const id: string = `toast-${
crypto.randomUUID
? crypto.randomUUID()
: Math.floor(Date.now() * Math.random() * 100).toString()
}`;
toasts.update((all) => [
...all,
{
id: id,
text: text,
dismissible: dismissible,
type: type
}
]);
}
function dismiss(id: string) {
toasts.update((all) => all.filter((x) => x.id !== id));
}
export const toast = {
clear: () => toasts.set([]),
error: (text: string | null, dismissible = true) =>
addToast(text ?? 'En feil oppstod', 'error', dismissible),
info: (text: string, dismissible = true) => addToast(text, 'info', dismissible),
warning: (text: string, dismissible = true) => addToast(text, 'warning', dismissible),
success: (text: string | null, dismissible = true) =>
addToast(text ?? 'Suksess', 'success', dismissible),
dismiss: dismiss
};