feat: add toasts

This commit is contained in:
Sam 2023-03-14 23:59:02 +01:00
parent 1a7186061a
commit 87e6e2809e
No known key found for this signature in database
GPG key ID: B4EF20DDE721CAA1
3 changed files with 55 additions and 0 deletions

View file

@ -0,0 +1,13 @@
<script lang="ts">
import { Toast } from "sveltestrap";
export let header: string | undefined = undefined;
export let body: string;
let isOpen = true;
const toggle = () => (isOpen = !isOpen);
</script>
<Toast {isOpen} {header} body class="m-3" {toggle}>
{body}
</Toast>

35
frontend/src/lib/toast.ts Normal file
View file

@ -0,0 +1,35 @@
import { writable } from "svelte/store";
export interface ToastData {
header?: string;
body: string;
duration?: number;
}
interface IdToastData extends ToastData {
id: number;
}
export const toastStore = writable<IdToastData[]>([]);
let maxId = 0;
export const addToast = (data: ToastData) => {
const id = maxId++;
console.log(id);
toastStore.update((toasts) => (toasts = [...toasts, { ...data, id }]));
if (data.duration !== -1) {
setTimeout(() => {
toastStore.update((toasts) => (toasts = toasts.filter((toast) => toast.id !== id)));
}, data.duration ?? 5000);
}
return id;
};
export const delToast = (id: number) => {
toastStore.update((toasts) => (toasts = toasts.filter((toast) => toast.id !== id)));
};

View file

@ -5,6 +5,8 @@
import Navigation from "./nav/Navigation.svelte";
import type { LayoutData } from "./$types";
import { version } from "$app/environment";
import { toastStore } from "$lib/toast";
import Toast from "$lib/components/Toast.svelte";
export let data: LayoutData;
@ -24,6 +26,11 @@
<Navigation />
<div class="container">
<slot />
<div class="position-absolute top-0 end-0">
{#each $toastStore as toast}
<Toast header={toast.header} body={toast.body} />
{/each}
</div>
<footer>
<hr />
<p>