diff options
Diffstat (limited to 'src/lib/Notification')
| -rw-r--r-- | src/lib/Notification/Notification.svelte | 30 | ||||
| -rw-r--r-- | src/lib/Notification/NotificationsProvider.svelte | 12 | ||||
| -rw-r--r-- | src/lib/Notification/options.ts | 36 | ||||
| -rw-r--r-- | src/lib/Notification/store.ts | 38 |
4 files changed, 61 insertions, 55 deletions
diff --git a/src/lib/Notification/Notification.svelte b/src/lib/Notification/Notification.svelte index 864d6568..a3271d58 100644 --- a/src/lib/Notification/Notification.svelte +++ b/src/lib/Notification/Notification.svelte @@ -1,21 +1,25 @@ <script lang="ts"> - import settings from '$stores/settings'; - import { onMount } from 'svelte'; - import type { Notification } from './store'; +import settings from "$stores/settings"; +import { onMount } from "svelte"; +import type { Notification } from "./store"; - export let notification: Notification; - export let onRemove: () => void = () => { - return; - }; - export let removed = false; +export let notification: Notification; +export let onRemove: () => void = () => { + return; +}; +export let removed = false; - onMount(() => setTimeout(remove, notification.duration)); +onMount(() => { + const removeTimer = setTimeout(remove, notification.duration); - const remove = () => { - removed = true; + return () => clearTimeout(removeTimer); +}); - setTimeout(onRemove, 150); - }; +const remove = () => { + removed = true; + + setTimeout(onRemove, 150); +}; </script> {#if !$settings.displayDisableNotifications} diff --git a/src/lib/Notification/NotificationsProvider.svelte b/src/lib/Notification/NotificationsProvider.svelte index 964317e9..c346aceb 100644 --- a/src/lib/Notification/NotificationsProvider.svelte +++ b/src/lib/Notification/NotificationsProvider.svelte @@ -1,12 +1,12 @@ <script lang="ts"> - import { notifications } from './store'; - import EventNotification from './Notification.svelte'; +import { notifications } from "./store"; +import EventNotification from "./Notification.svelte"; - export let zIndex = 5000; +export let zIndex = 5000; - const handleRemove = (id: string) => { - notifications.remove(id); - }; +const handleRemove = (id: string) => { + notifications.remove(id); +}; </script> <div class="notifications-container" style="z-index: {zIndex};"> diff --git a/src/lib/Notification/options.ts b/src/lib/Notification/options.ts index be972a7e..28eeecb3 100644 --- a/src/lib/Notification/options.ts +++ b/src/lib/Notification/options.ts @@ -1,24 +1,26 @@ -type Position = 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left'; +type Position = "top-right" | "top-left" | "bottom-right" | "bottom-left"; export interface Options { - heading: string; - description?: string; - position: Position; - duration: number; - id: string; + heading: string; + description?: string; + position: Position; + duration: number; + id: string; } export const options = (preferences: { - heading: string; - description?: string; - position?: string; - duration?: number; + heading: string; + description?: string; + position?: string; + duration?: number; }): Options => { - return { - position: (preferences.position || 'top-right') as Position, - duration: preferences.duration || 3000, - heading: preferences.heading, - description: preferences.description, - id: Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15) - }; + return { + position: (preferences.position || "top-right") as Position, + duration: preferences.duration || 3000, + heading: preferences.heading, + description: preferences.description, + id: + Math.random().toString(36).substring(2, 15) + + Math.random().toString(36).substring(2, 15), + }; }; diff --git a/src/lib/Notification/store.ts b/src/lib/Notification/store.ts index 0cb4cf96..4eb163f9 100644 --- a/src/lib/Notification/store.ts +++ b/src/lib/Notification/store.ts @@ -1,32 +1,32 @@ -import { writable } from 'svelte/store'; +import { writable } from "svelte/store"; export interface Notification { - id: string; - heading: string; - description?: string; - duration?: number; + id: string; + heading: string; + description?: string; + duration?: number; } function createNotificationStore() { - const { subscribe, update } = writable<Notification[]>([]); + const { subscribe, update } = writable<Notification[]>([]); - return { - subscribe, - add: (notification: Omit<Notification, 'id'>) => { - const id = crypto.randomUUID(); + return { + subscribe, + add: (notification: Omit<Notification, "id">) => { + const id = crypto.randomUUID(); - update((notifications) => [...notifications, { ...notification, id }]); + update((notifications) => [...notifications, { ...notification, id }]); - return id; - }, - remove: (id: string) => { - update((notifications) => notifications.filter((n) => n.id !== id)); - } - }; + return id; + }, + remove: (id: string) => { + update((notifications) => notifications.filter((n) => n.id !== id)); + }, + }; } export const notifications = createNotificationStore(); -export function addNotification(notification: Omit<Notification, 'id'>) { - return notifications.add(notification); +export function addNotification(notification: Omit<Notification, "id">) { + return notifications.add(notification); } |