Skip to content

Commit 23ec5a5

Browse files
committed
Add feature multiple toast container support
1 parent c7c3b9f commit 23ec5a5

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

src/SvelteToast.svelte

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@ import { toast } from './stores.js'
55
import ToastItem from './ToastItem.svelte'
66
77
export let options = {}
8-
const defaults = {
9-
duration: 4000,
10-
dismissable: true,
11-
initial: 1,
12-
progress: 0,
13-
reversed: false,
14-
intro: { x: 256 },
15-
theme: {}
16-
}
17-
toast._opts(defaults)
18-
$: toast._opts(options)
8+
export let target = 'default'
9+
10+
$: toast._init(target, options)
11+
12+
let items
13+
$: items = $toast.filter(i => i.target === target)
1914
2015
const getCss = theme => Object.keys(theme).reduce((a, c) => `${a}${c}:${theme[c]};`, '')
2116
</script>
@@ -36,7 +31,7 @@ ul {
3631
</style>
3732

3833
<ul>
39-
{#each $toast as item (item.id)}
34+
{#each items as item (item.id)}
4035
<li
4136
in:fly={item.intro}
4237
out:fade

src/stores.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,50 @@
11
import { writable } from 'svelte/store'
22

3+
const defaults = {
4+
duration: 4000,
5+
dismissable: true,
6+
initial: 1,
7+
progress: 0,
8+
reversed: false,
9+
intro: { x: 256 },
10+
theme: {}
11+
}
12+
313
const createToast = () => {
414
const { subscribe, update } = writable([])
515
let count = 0
6-
let defaults = {}
7-
const _obj = obj => typeof obj === 'object'
16+
const options = {}
17+
const _obj = obj => obj instanceof Object
818
const push = (msg, opts = {}) => {
9-
opts = _obj(msg) ? { ...msg } : { ...opts, msg }
10-
const entry = { ...defaults, ...opts, id: ++count, theme: { ...defaults.theme, ...opts.theme } }
19+
const param = { target: 'default', ..._obj(msg) ? msg : { ...opts, msg } }
20+
const conf = options[param.target] || {}
21+
const entry = { ...defaults, ...conf, ...param, theme: { ...conf.theme, ...param.theme }, id: ++count }
1122
update(n => entry.reversed ? [...n, entry] : [entry, ...n])
1223
return count
1324
}
1425
const pop = id => {
1526
update(n => {
16-
if (n.length === 0 || id === 0) return []
27+
if (!n.length || id === 0) return []
28+
if (_obj(id)) return n.filter(i => id(i))
1729
const target = id || Math.max(...n.map(i => i.id))
1830
return n.filter(i => i.id !== target)
1931
})
2032
}
2133
const set = (id, opts = {}) => {
22-
opts = _obj(id) ? { ...id } : { ...opts, id }
34+
const param = _obj(id) ? { ...id } : { ...opts, id }
2335
update(n => {
24-
const idx = n.findIndex(i => i.id === opts.id)
36+
const idx = n.findIndex(i => i.id === param.id)
2537
if (idx > -1) {
26-
n[idx] = { ...n[idx], ...opts }
38+
n[idx] = { ...n[idx], ...param }
2739
}
2840
return n
2941
})
3042
}
31-
const _opts = (obj = {}) => {
32-
defaults = { ...defaults, ...obj, theme: { ...defaults.theme, ...obj.theme } }
33-
return defaults
43+
const _init = (target = 'default', opts = {}) => {
44+
options[target] = opts
45+
return options
3446
}
35-
return { subscribe, push, pop, set, _opts }
47+
return { subscribe, push, pop, set, _init }
3648
}
3749

3850
export const toast = createToast()

0 commit comments

Comments
 (0)