1
0
Fork 0
mirror of synced 2024-07-06 23:10:57 +12:00

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

View file

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