-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #184 from dejanvasic85/dev
Adding toaster notifications
- Loading branch information
Showing
11 changed files
with
130 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<script lang="ts"> | ||
import { slide } from 'svelte/transition'; | ||
import { createToaster, melt } from '@melt-ui/svelte'; | ||
import { getToastMessages } from '$lib/state/toastMessages.svelte'; | ||
import Icon from '$components/Icon.svelte'; | ||
import type { ToastMessage } from '$lib/types'; | ||
const toastMessages = getToastMessages(); | ||
const { | ||
elements: { content, description, close }, | ||
helpers, | ||
states: { toasts }, | ||
actions: { portal } | ||
} = createToaster<ToastMessage>({}); | ||
$effect(() => { | ||
toastMessages.messages | ||
.filter((m) => m.isShown === false) | ||
.forEach((message) => { | ||
helpers.addToast({ type: 'background', data: message }); | ||
message.isShown = true; | ||
}); | ||
}); | ||
</script> | ||
|
||
<div use:portal class="fixed left-1/2 top-2 z-toaster w-80 -translate-x-1/2 transform"> | ||
{#each $toasts as { id, data } (id)} | ||
<div use:melt={$content(id)}> | ||
<div | ||
in:slide={{ duration: 200 }} | ||
out:slide={{ duration: 200 }} | ||
class="relative mb-2 flex flex-col items-start justify-between rounded-lg border border-gray-200 bg-white px-8 py-4 text-sm shadow-lg" | ||
class:bg-red-300={data.type === 'error'} | ||
class:bg-green-300={data.type === 'success'} | ||
> | ||
<div use:melt={$description(id)}> | ||
{data.message} | ||
</div> | ||
<button | ||
use:melt={$close(id)} | ||
aria-label="close notification" | ||
class="absolute right-0 top-0 p-2" | ||
> | ||
<Icon icon="x-mark" /> | ||
</button> | ||
</div> | ||
</div> | ||
{/each} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { getContext, setContext } from 'svelte'; | ||
import { nanoid } from 'nanoid'; | ||
|
||
import { type ToastMessage } from '$lib/types'; | ||
|
||
type ToastMessageWithId = ToastMessage & { id: string; isShown: boolean }; | ||
|
||
export class ToastMessages { | ||
messages: ToastMessageWithId[] = $state([]); | ||
|
||
constructor() {} | ||
|
||
addMessage(message: ToastMessage) { | ||
const id = nanoid(); | ||
|
||
this.messages.push({ | ||
...message, | ||
id, | ||
isShown: false | ||
}); | ||
} | ||
} | ||
|
||
const ToastMessagesKey = Symbol('ToastMessages'); | ||
|
||
export function setToastMessages() { | ||
return setContext(ToastMessagesKey, new ToastMessages()); | ||
} | ||
|
||
export function getToastMessages() { | ||
return getContext<ReturnType<typeof setToastMessages>>(ToastMessagesKey); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
<script lang="ts"> | ||
import './app.css'; | ||
import Toaster from '$components/Toaster.svelte'; | ||
import { setBoardState } from '$lib/state/boardState.svelte'; | ||
import { setToastMessages } from '$lib/state/toastMessages.svelte'; | ||
let { children } = $props(); | ||
setBoardState(); | ||
setToastMessages(); | ||
</script> | ||
|
||
<Toaster /> | ||
{@render children()} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,8 @@ export default { | |
menu: 10, | ||
overlay: 20, | ||
dialog: 25, | ||
dropdown: 30 | ||
dropdown: 30, | ||
toaster: 40 | ||
} | ||
} | ||
}, | ||
|