Allow client notifications to be stacked

This commit is contained in:
Mel O'Hagan 2022-07-20 13:41:18 +01:00
parent 1d15e4a999
commit 4e2dea375a
2 changed files with 22 additions and 14 deletions

View File

@ -6,7 +6,7 @@
<div class="notifications">
{#if $notificationStore}
{#key $notificationStore.id}
{#each $notificationStore as { type, icon, message, id, dismissable } (id)}
<div
in:fly={{
duration: 300,
@ -16,14 +16,14 @@
out:fly={{ y: -20, duration: 150 }}
>
<Notification
type={$notificationStore.type}
message={$notificationStore.message}
icon={$notificationStore.icon}
dismissable={$notificationStore.dismissable}
on:dismiss={notificationStore.actions.dismiss}
{type}
{message}
{icon}
{dismissable}
on:dismiss={() => notificationStore.actions.dismiss(id)}
/>
</div>
{/key}
{/each}
{/if}
</div>
@ -31,6 +31,8 @@
.notifications {
position: fixed;
top: 20px;
bottom: 40px;
width: 25%;
left: 0;
right: 0;
margin: 0 auto;
@ -39,7 +41,8 @@
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: center;
align-items: end;
pointer-events: none;
gap: 10px;
}
</style>

View File

@ -8,9 +8,10 @@ const createNotificationStore = () => {
let timeout
let block = false
const store = writable(null, () => {
const store = writable([], () => {
return () => {
clearTimeout(timeout)
store.set([])
}
})
@ -37,15 +38,17 @@ const createNotificationStore = () => {
})
return
}
store.set({
store.update(state => {
return [...state,
{
id: generate(),
type,
message,
icon,
dismissable: !autoDismiss,
delay: get(store) != null,
})
}
]})
clearTimeout(timeout)
if (autoDismiss) {
timeout = setTimeout(() => {
@ -54,9 +57,11 @@ const createNotificationStore = () => {
}
}
const dismiss = () => {
const dismiss = id => {
clearTimeout(timeout)
store.set(null)
store.update(state => {
return state.filter(n => n.id !== id)
})
}
return {