From acd85f6373dbf0edbc2cce5c9256464e5986abf0 Mon Sep 17 00:00:00 2001 From: Mel Date: Mon, 21 Feb 2022 01:56:43 +0100 Subject: Add toasts --- assets/src/state/toast.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 assets/src/state/toast.ts (limited to 'assets/src/state') diff --git a/assets/src/state/toast.ts b/assets/src/state/toast.ts new file mode 100644 index 0000000..8eba306 --- /dev/null +++ b/assets/src/state/toast.ts @@ -0,0 +1,42 @@ +import { writable } from "svelte/store"; +import generateId from "../utils/generateId"; + +export enum ToastType { + INFO, + ERROR, +} + +export type ToastData = { + type: ToastType; + message: string; + title?: string; +}; + +export type Toast = { + id: string; +} & ToastData; + +export const toasts = writable([]); + +export function toast(toast: ToastData): Toast { + const toastWithId: Toast = { + ...toast, + id: generateId(8), + }; + + toasts.update(toasts => [...toasts, toastWithId]); + + // Dissmiss toast after 5 seconds. + setTimeout(() => { + dismissToast(toastWithId); + }, 5000); + + return toastWithId; +} + +// @ts-ignore +window.toast = toast; + +export function dismissToast(toast: Toast) { + toasts.update(toasts => toasts.filter(t => t.id !== toast.id)); +} -- cgit 1.4.1