1
1
import { writable } from 'svelte/store'
2
2
3
+ /**
4
+ * @typedef {import('svelte').ComponentType } SvelteComponent
5
+ */
6
+
7
+ /**
8
+ * @typedef {import('svelte/types/runtime/transition/index').FlyParams } FlyParams
9
+ */
10
+
11
+ /**
12
+ * @typedef {Object } SvelteToastCustomComponent
13
+ * @property {SvelteComponent } src - custom Svelte Component
14
+ * @property {Object.<string, *> } [props] - props to pass into custom component
15
+ * @property {string } [sendIdTo] - forward toast id to prop name
16
+ */
17
+
18
+ /**
19
+ * @callback SvelteToastOnPopCallback
20
+ * @param {number } [id] - optionally get the toast id if needed
21
+ */
22
+
23
+ /**
24
+ * @typedef {Object } SvelteToastOptions
25
+ * @property {number } [id] - unique id generated for every toast
26
+ * @property {string } [target] - container target name to send toast to
27
+ * @property {string } [msg] - toast message
28
+ * @property {number } [duration] - duration of progress bar tween from initial to next
29
+ * @property {number } [initial] - initial progress bar value
30
+ * @property {number } [next] - next progress bar value
31
+ * @property {boolean } [pausable] - pause progress bar tween on mouse hover
32
+ * @property {boolean } [dismissable] - allow dissmiss with close button
33
+ * @property {boolean } [reversed] - display toasts in reverse order
34
+ * @property {FlyParams } [intro] - toast intro fly animation settings
35
+ * @property {Object.<string, string|number> } [theme] - css var overrides
36
+ * @property {string[] } [classes] - user-defined classes
37
+ * @property {SvelteToastOnPopCallback } [onpop] - callback that runs on toast dismiss
38
+ * @property {SvelteToastCustomComponent } [component] - send custom Svelte Component as a message
39
+ * @property {number } [progress] - DEPRECATED
40
+ */
41
+
42
+ /** @type {SvelteToastOptions } */
3
43
const defaults = {
4
44
duration : 4000 ,
5
45
initial : 1 ,
@@ -12,11 +52,31 @@ const defaults = {
12
52
13
53
const createToast = ( ) => {
14
54
const { subscribe, update } = writable ( [ ] )
15
- let count = 0
55
+ /** @type { Object.<string, SvelteToastOptions> } */
16
56
const options = { }
17
- const _obj = ( obj ) => obj instanceof Object
18
- const push = ( msg , opts = { } ) => {
19
- const param = { target : 'default' , ...( _obj ( msg ) ? msg : { ...opts , msg } ) }
57
+ let count = 0
58
+
59
+ /** @param {* } obj */
60
+ function _obj ( obj ) {
61
+ return obj instanceof Object
62
+ }
63
+
64
+ function _init ( target = 'default' , opts = { } ) {
65
+ options [ target ] = opts
66
+ return options
67
+ }
68
+
69
+ /**
70
+ * Send a new toast
71
+ * @param {(string|SvelteToastOptions) } msg
72
+ * @param {SvelteToastOptions } [opts]
73
+ * @returns {number }
74
+ */
75
+ function push ( msg , opts ) {
76
+ const param = {
77
+ target : 'default' ,
78
+ ...( _obj ( msg ) ? /** @type {SvelteToastOptions } */ ( msg ) : { ...opts , msg } )
79
+ }
20
80
const conf = options [ param . target ] || { }
21
81
const entry = {
22
82
...defaults ,
@@ -29,16 +89,34 @@ const createToast = () => {
29
89
update ( ( n ) => ( entry . reversed ? [ ...n , entry ] : [ entry , ...n ] ) )
30
90
return count
31
91
}
32
- const pop = ( id ) => {
92
+
93
+ /**
94
+ * Remove toast(s)
95
+ * - toast.pop() // removes the last toast
96
+ * - toast.pop(0) // remove all toasts
97
+ * - toast.pop(id) // removes the toast with specified `id`
98
+ * - toast.pop({ target: 'foo' }) // remove all toasts from target `foo`
99
+ * @param {(number|Object.<'target', string>) } [id]
100
+ */
101
+ function pop ( id ) {
33
102
update ( ( n ) => {
34
103
if ( ! n . length || id === 0 ) return [ ]
35
- if ( _obj ( id ) ) return n . filter ( ( i ) => id ( i ) )
36
- const target = id || Math . max ( ...n . map ( ( i ) => i . id ) )
37
- return n . filter ( ( i ) => i . id !== target )
104
+ // Filter function is deprecated; shim added for backward compatibility
105
+ if ( typeof id === 'function' ) return n . filter ( ( i ) => id ( i ) )
106
+ if ( _obj ( id ) ) return n . filter ( ( i ) => i . target !== id . target )
107
+ const found = id || Math . max ( ...n . map ( ( i ) => i . id ) )
108
+ return n . filter ( ( i ) => i . id !== found )
38
109
} )
39
110
}
40
- const set = ( id , opts = { } ) => {
41
- const param = _obj ( id ) ? { ...id } : { ...opts , id }
111
+
112
+ /**
113
+ * Update an existing toast
114
+ * @param {(number|SvelteToastOptions) } id
115
+ * @param {SvelteToastOptions } [opts]
116
+ */
117
+ function set ( id , opts ) {
118
+ /** @type {object } */
119
+ const param = _obj ( id ) ? id : { ...opts , id }
42
120
update ( ( n ) => {
43
121
const idx = n . findIndex ( ( i ) => i . id === param . id )
44
122
if ( idx > - 1 ) {
@@ -47,10 +125,7 @@ const createToast = () => {
47
125
return n
48
126
} )
49
127
}
50
- const _init = ( target = 'default' , opts = { } ) => {
51
- options [ target ] = opts
52
- return options
53
- }
128
+
54
129
return { subscribe, push, pop, set, _init }
55
130
}
56
131
0 commit comments