Skip to content

Commit 5053c50

Browse files
authored
Return details with onpop() (#104)
1 parent 53f8d4a commit 5053c50

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/lib/ToastItem.svelte

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@ let unlisten
1717
1818
const progress = tweened(item.initial, { duration: item.duration, easing: linear })
1919
20-
function close() {
20+
function close(details) {
21+
item.details = details
2122
toast.pop(item.id)
2223
}
2324
2425
function autoclose() {
25-
if ($progress === 1 || $progress === 0) close()
26+
if ($progress === 1 || $progress === 0)
27+
close({
28+
autoClose: true,
29+
originalEvent: null
30+
})
2631
}
2732
2833
function pause() {
@@ -77,7 +82,7 @@ onMount(listen)
7782
onDestroy(() => {
7883
if (check(item.onpop, 'function')) {
7984
// @ts-ignore
80-
item.onpop(item.id)
85+
item.onpop(item.id, item.details)
8186
}
8287
unlisten && unlisten()
8388
})
@@ -104,9 +109,13 @@ onDestroy(() => {
104109
class="_toastBtn pe"
105110
role="button"
106111
tabindex="0"
107-
on:click={close}
112+
on:click={(ev) =>
113+
close({
114+
autoClose: false,
115+
originalEvent: ev
116+
})}
108117
on:keydown={(e) => {
109-
if (e instanceof KeyboardEvent && ['Enter', ' '].includes(e.key)) close()
118+
if (e instanceof KeyboardEvent && ['Enter', ' '].includes(e.key)) close(e)
110119
}}
111120
/>
112121
{/if}

src/routes/+page.svelte

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,26 @@ toast.pop(0)`,
298298
{
299299
name: 'RUN CALLBACK ON TOAST REMOVAL',
300300
code: `toast.push('Wait for it...', {
301-
onpop: () => {
302-
toast.push('onpop() callback has been executed.', { target: 'new' })
303-
}
304-
})`,
301+
onpop: (id, details) => {
302+
toast.push('onpop() callback has been executed.', { target: 'new' })
303+
if (details.autoClose) {
304+
console.log('closed automatically')
305+
} else {
306+
console.log('closed by user', details.originalEvent)
307+
}
308+
}
309+
})`,
305310
run: () =>
306311
toast.push('Wait for it...', {
307-
onpop: () => {
312+
onpop: (id, details) => {
308313
toast.push(`<strong><tt>onpop()</tt></strong> callback has been executed.`, {
309314
target: 'new'
310315
})
316+
if (details.autoClose) {
317+
console.log('closed automatically')
318+
} else {
319+
console.log('closed by user', details.originalEvent)
320+
}
311321
}
312322
})
313323
},

0 commit comments

Comments
 (0)