Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/lib/ToastItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ let unlisten

const progress = tweened(item.initial, { duration: item.duration, easing: linear })

function close() {
function close(details) {
item.details = details
toast.pop(item.id)
}

function autoclose() {
if ($progress === 1 || $progress === 0) close()
if ($progress === 1 || $progress === 0)
close({
autoClose: true,
originalEvent: null
})
}

function pause() {
Expand Down Expand Up @@ -77,7 +82,7 @@ onMount(listen)
onDestroy(() => {
if (check(item.onpop, 'function')) {
// @ts-ignore
item.onpop(item.id)
item.onpop(item.id, item.details)
}
unlisten && unlisten()
})
Expand All @@ -104,9 +109,13 @@ onDestroy(() => {
class="_toastBtn pe"
role="button"
tabindex="0"
on:click={close}
on:click={(ev) =>
close({
autoClose: false,
originalEvent: ev
})}
on:keydown={(e) => {
if (e instanceof KeyboardEvent && ['Enter', ' '].includes(e.key)) close()
if (e instanceof KeyboardEvent && ['Enter', ' '].includes(e.key)) close(e)
}}
/>
{/if}
Expand Down
20 changes: 15 additions & 5 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,26 @@ toast.pop(0)`,
{
name: 'RUN CALLBACK ON TOAST REMOVAL',
code: `toast.push('Wait for it...', {
onpop: () => {
toast.push('onpop() callback has been executed.', { target: 'new' })
}
})`,
onpop: (id, details) => {
toast.push('onpop() callback has been executed.', { target: 'new' })
if (details.autoClose) {
console.log('closed automatically')
} else {
console.log('closed by user', details.originalEvent)
}
}
})`,
run: () =>
toast.push('Wait for it...', {
onpop: () => {
onpop: (id, details) => {
toast.push(`<strong><tt>onpop()</tt></strong> callback has been executed.`, {
target: 'new'
})
if (details.autoClose) {
console.log('closed automatically')
} else {
console.log('closed by user', details.originalEvent)
}
}
})
},
Expand Down