Skip to content

Commit 89a4b1e

Browse files
authored
Invoke onpop() cb in onDestroy hook instead (#29)
1 parent eee28d8 commit 89a4b1e

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

cypress/integration/test.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,18 @@ describe('Integration Tests', () => {
261261
.get('._toastBtn')
262262
.click()
263263
})
264+
265+
it('Runs callback when toast is popped programatically', () => {
266+
cy.get('[data-btn=runCallbackOnToastRemoval]')
267+
.click()
268+
.get('._toastItem')
269+
.contains('Wait for it')
270+
.window()
271+
.invoke('toast.pop')
272+
.wait(500)
273+
.get('._toastItem')
274+
.contains('callback has been executed')
275+
.get('._toastBtn')
276+
.click()
277+
})
264278
})

docs/App.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ toast.pop(0)`,
269269
target: 'new',
270270
dismissable: false,
271271
initial: 0,
272+
intro: { y: -192 },
272273
theme: {
273274
'--toastPadding': '0',
274275
'--toastMsgPadding': '0',

src/ToastItem.svelte

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
<script>
2+
import { onDestroy } from 'svelte'
23
import { tweened } from 'svelte/motion'
34
import { linear } from 'svelte/easing'
45
import { toast } from './stores.js'
56
67
export let item
78
89
const progress = tweened(item.initial, { duration: item.duration, easing: linear })
9-
const close = () => {
10-
const { id, onpop } = item
11-
toast.pop(id)
12-
if (typeof onpop === 'function') {
13-
onpop(id)
14-
}
15-
}
10+
const close = () => toast.pop(item.id)
1611
const autoclose = () => {
1712
if ($progress === 1 || $progress === 0) {
1813
close()
@@ -57,6 +52,12 @@ const getProps = () => {
5752
$: if (typeof item.progress !== 'undefined') {
5853
item.next = item.progress
5954
}
55+
56+
onDestroy(() => {
57+
if (typeof item.onpop === 'function') {
58+
item.onpop(item.id)
59+
}
60+
})
6061
</script>
6162

6263
<style>

0 commit comments

Comments
 (0)