2
2
import { onMount , onDestroy } from ' svelte'
3
3
import { tweened } from ' svelte/motion'
4
4
import { linear } from ' svelte/easing'
5
- import { toast } from ' ./stores.js '
5
+ import { toast } from ' ./stores'
6
6
7
+ /** @type {import('./stores').SvelteToastOptions} */
7
8
export let item
8
9
9
- const progress = tweened (item .initial , { duration: item .duration , easing: linear })
10
- const close = () => toast .pop (item .id )
11
- const autoclose = () => {
12
- if ($progress === 1 || $progress === 0 ) {
13
- close ()
14
- }
15
- }
16
10
let next = item .initial
17
11
let prev = next
18
12
let paused = false
13
+ let cprops = {}
14
+ let unlisten
19
15
20
- $: if (next !== item .next ) {
21
- next = item .next
22
- prev = $progress
23
- paused = false
24
- progress .set (next).then (autoclose)
16
+ const progress = tweened (item .initial , { duration: item .duration , easing: linear })
17
+
18
+ function close () {
19
+ toast .pop (item .id )
20
+ }
21
+
22
+ function autoclose () {
23
+ if ($progress === 1 || $progress === 0 ) close ()
25
24
}
26
25
27
- const pause = () => {
26
+ function pause () {
28
27
if (! paused && $progress !== next) {
29
28
progress .set ($progress, { duration: 0 })
30
29
paused = true
31
30
}
32
31
}
33
32
34
- const resume = () => {
33
+ function resume () {
35
34
if (paused) {
36
35
const d = item .duration
37
36
const duration = d - d * (($progress - prev) / (next - prev))
@@ -40,20 +39,11 @@ const resume = () => {
40
39
}
41
40
}
42
41
43
- let componentProps = {}
44
- $: if (item .component ) {
45
- const { props = {}, sendIdTo } = item .component
46
- componentProps = { ... props, ... (sendIdTo && { [sendIdTo]: item .id }) }
47
- }
48
-
49
- const check = (prop , kind = ' undefined' ) => typeof prop === kind
50
- // `progress` has been renamed to `next`; shim included for backward compatibility, to remove in next major
51
- $: if (! check (item .progress )) {
52
- item .next = item .progress
42
+ function check (prop , kind = ' undefined' ) {
43
+ return typeof prop === kind
53
44
}
54
45
55
- let unlisten
56
- const listen = (d = document ) => {
46
+ function listen (d = document ) {
57
47
if (check (d .hidden )) return
58
48
const handler = () => (d .hidden ? pause () : resume ())
59
49
const name = ' visibilitychange'
@@ -62,7 +52,25 @@ const listen = (d = document) => {
62
52
handler ()
63
53
}
64
54
55
+ $: if (next !== item .next ) {
56
+ next = item .next
57
+ prev = $progress
58
+ paused = false
59
+ progress .set (next).then (autoclose)
60
+ }
61
+
62
+ $: if (item .component ) {
63
+ const { props = {}, sendIdTo } = item .component
64
+ cprops = { ... props, ... (sendIdTo && { [sendIdTo]: item .id }) }
65
+ }
66
+
67
+ // `progress` has been renamed to `next`; shim included for backward compatibility, to remove in next major
68
+ $: if (! check (item .progress )) {
69
+ item .next = item .progress
70
+ }
71
+
65
72
onMount (listen)
73
+
66
74
onDestroy (() => {
67
75
if (check (item .onpop , ' function' )) {
68
76
item .onpop (item .id )
@@ -81,7 +89,7 @@ onDestroy(() => {
81
89
>
82
90
<div role ="status" class ="_toastMsg" class:pe ={item .component }>
83
91
{#if item .component }
84
- <svelte:component this ={item .component .src } {...componentProps } />
92
+ <svelte:component this ={item .component .src } {...cprops } />
85
93
{:else }
86
94
{@html item .msg }
87
95
{/if }
0 commit comments