11<script >
2- /* eslint no-useless-escape: 0 */
3-
42import { tick } from ' svelte'
53import { SvelteToast , toast } from ' ../src'
64
75// Hoist to `window` for debug
86window .toast = toast
97
108let selected
11- let code = ' '
12-
13- const handleDefault = () => {
14- selected = ' default'
15- code = ' toast.push(\' Hello world!\' )'
16- toast .push (' Hello world!' )
17- }
9+ let code = ' // Tap a button below'
10+ let colors = false
11+ let bottom = false
12+ let options = {}
1813
19- const handleGreen = () => {
20- selected = ' green'
21- code = ` toast.push('Success!', {
14+ const handleClick = btn => {
15+ selected = btn .name
16+ code = btn .code
17+ btn .run ()
18+ }
19+
20+ const buttons = [
21+ {
22+ name: ' DEFAULT' ,
23+ code: ` toast.push('Hello world!')` , // eslint-disable-line quotes
24+ run : () => {
25+ toast .push (' Hello world!' )
26+ }
27+ },
28+ {
29+ name: ' GREEN' ,
30+ code: ` toast.push('Success!', {
2231 theme: {
2332 '--toastBackground': '#48BB78',
2433 '--toastProgressBackground': '#2F855A'
2534 }
26- )`
27- toast .push (' Success!' , { theme: { ' --toastBackground' : ' #48BB78' , ' --toastProgressBackground' : ' #2F855A' } })
28- }
29-
30- const handleYellow = () => {
31- selected = ' yellow'
32- code = ` toast.push('Warning!', {
35+ )` ,
36+ run : () => {
37+ toast .push (' Success!' , { theme: { ' --toastBackground' : ' #48BB78' , ' --toastProgressBackground' : ' #2F855A' } })
38+ }
39+ },
40+ {
41+ name: ' YELLOW' ,
42+ code: ` toast.push('Warning!', {
3343 theme: {
3444 '--toastBackground': '#ECC94B',
3545 '--toastProgressBackground': '#B7791F'
3646 }
37- )`
38- toast .push (' Warning!' , { theme: { ' --toastBackground' : ' #ECC94B' , ' --toastProgressBackground' : ' #B7791F' } })
39- }
40-
41- const handleRed = () => {
42- selected = ' red'
43- code = ` toast.push('Error!', {
47+ )` ,
48+ run : () => {
49+ toast .push (' Warning!' , { theme: { ' --toastBackground' : ' #ECC94B' , ' --toastProgressBackground' : ' #B7791F' } })
50+ }
51+ },
52+ {
53+ name: ' RED' ,
54+ code: ` toast.push('Error!', {
4455 theme: {
4556 '--toastBackground': '#F56565',
4657 '--toastProgressBackground': '#C53030'
4758 }
48- )`
49- toast .push (' Error!' , { theme: { ' --toastBackground' : ' #F56565' , ' --toastProgressBackground' : ' #C53030' } })
50- }
51-
52- const handleLong = () => {
53- selected = ' long'
54- code = ' toast.push(\' Watching the paint dry...\' , { duration: 20000 })'
55- toast .push (' Watching the paint dry...' , { duration: 20000 })
56- }
57-
58- const handleNodismiss = () => {
59- selected = ' nodismiss'
60- code = ` toast.push('Where the close btn?!?', {
59+ )` ,
60+ run : () => {
61+ toast .push (' Error!' , { theme: { ' --toastBackground' : ' #F56565' , ' --toastProgressBackground' : ' #C53030' } })
62+ }
63+ },
64+ {
65+ name: ' LONG DURATION' ,
66+ code: ` toast.push('Watching the paint dry...', { duration: 20000 })` , // eslint-disable-line quotes
67+ run : () => {
68+ toast .push (' Watching the paint dry...' , { duration: 20000 })
69+ }
70+ },
71+ {
72+ name: ' NON-DISMISSABLE' ,
73+ code: ` toast.push('Where the close btn?!?', {
6174 initial: 0,
6275 progress: 0,
6376 dismissable: false
64- })`
65- toast .push (' Where the close btn?!?' , { initial: 0 , progress: 0 , dismissable: false })
66- }
67-
68- const handleRemove = () => {
69- selected = ' remove'
70- code = ` // Remove the latest toast
77+ })` ,
78+ run : () => {
79+ toast .push (' Where the close btn?!?' , { initial: 0 , progress: 0 , dismissable: false })
80+ }
81+ },
82+ {
83+ name: ' REMOVE LAST TOAST' ,
84+ code: ` // Remove the latest toast
7185toast.pop()
7286
7387// Or remove a particular one
7488const id = toast('Yo!')
75- toast.pop(id)`
76- toast .pop ()
77- }
78-
79- const handleFlip = () => {
80- selected = ' flip'
81- code = ` toast.push('Progress bar is flipped', {
89+ toast.pop(id)` ,
90+ run : () => {
91+ toast .pop ()
92+ }
93+ },
94+ {
95+ name: ' FLIP PROGRESS BAR' ,
96+ code: ` toast.push('Progress bar is flipped', {
8297 initial: 0,
8398 progress: 1
84- })`
85- toast .push (' Progress bar is flipped' , { initial: 0 , progress: 1 })
86- }
99+ })` ,
100+ run : () => {
101+ toast .push (' Progress bar is flipped' , { initial: 0 , progress: 1 })
102+ }
103+ },
104+ {
105+ name: ' USE AS LOADING INDICATOR' ,
106+ code: ` const sleep = t => new Promise(resolve => setTimeout(resolve, t))
87107
88- const sleep = t => new Promise (resolve => setTimeout (resolve, t))
89- const handleLoading = async () => {
90- selected = ' loading'
91- code = ` const sleep = t => new Promise(resolve => setTimeout(resolve, t))
92108const id = toast.push('Loading, please wait...', {
93109 duration: 300,
94110 initial: 0,
95111 progress: 0,
96112 dismissable: false
97113})
114+
98115await sleep(500)
99116toast.set(id, { progress: 0.1 })
117+
100118await sleep(3000)
101119toast.set(id, { progress: 0.7 })
120+
102121await sleep(1000)
103122toast.set(id, { msg: 'Just a bit more', progress: 0.8 })
104- await sleep(2000)
105- toast.set(id, { progress: 1 })
106- `
107- const id = toast .push (' Loading, please wait...' , { duration: 300 , initial: 0 , progress: 0 , dismissable: false })
108- await sleep (500 )
109- toast .set (id, { progress: 0.1 })
110- await sleep (3000 )
111- toast .set (id, { progress: 0.7 })
112- await sleep (1000 )
113- toast .set (id, { msg: ' Just a bit more' , progress: 0.8 })
114- await sleep (2000 )
115- toast .set (id, { progress: 1 })
116- }
117123
118- let colors = false
119- const handleColor = () => {
120- selected = ' color'
121- code = ` <style>
122- :root {
123- --toastBackground: rgba(255,255,255,0.98);
124- --toastColor: #2D3748;
125- }
124+ await sleep(2000)
125+ toast.set(id, { progress: 1 })` ,
126+ run: async () => {
127+ const sleep = t => new Promise (resolve => setTimeout (resolve, t))
128+ const id = toast .push (' Loading, please wait...' , { duration: 300 , initial: 0 , progress: 0 , dismissable: false })
129+ await sleep (500 )
130+ toast .set (id, { progress: 0.1 })
131+ await sleep (3000 )
132+ toast .set (id, { progress: 0.7 })
133+ await sleep (1000 )
134+ toast .set (id, { msg: ' Just a bit more' , progress: 0.8 })
135+ await sleep (2000 )
136+ toast .set (id, { progress: 1 })
137+ }
138+ },
139+ {
140+ name: ' CHANGE DEFAULT COLORS' ,
141+ code: ` <style>
142+ :root {
143+ --toastBackground: rgba(255,255,255,0.95);
144+ --toastColor: #424242;
145+ --toastProgressBackground: aquamarine;
146+ }
126147</style>
127148<script>
128149 toast.push('Changed some colors')
129- <\/ script>`
130- colors = true
131- toast .push (' Changed some colors' )
132- }
133-
134- let options = {}
135- let bottom = false
136- const handleBottom = async () => {
137- selected = ' bottom'
138- code = ` <style>
150+ <\/ script>` , // eslint-disable-line no-useless-escape
151+ run : () => {
152+ colors = true
153+ toast .push (' Changed some colors' )
154+ }
155+ },
156+ {
157+ name: ' POSITION TO BOTTOM' ,
158+ code: ` <style>
139159:root {
140160 --toastContainerTop: auto;
141161 --toastContainerRight: auto;
@@ -148,29 +168,34 @@ const handleBottom = async () => {
148168
149169<script>
150170 toast.push('Bottoms up!')
151- <\/ script>`
152- bottom = true
153- options = { reversed: true , intro: { y: 128 } }
154- await tick ()
155- toast .push (' Bottoms up!' )
156- }
157-
158- const handleRestore = async () => {
159- selected = ' restore'
160- code = ' // All default settings restored!'
161- colors = false
162- bottom = false
163- options = {}
164- await tick ()
165- toast .push (' All themes reset!' )
166- }
171+ <\/ script>` , // eslint-disable-line no-useless-escape
172+ run: async () => {
173+ bottom = true
174+ options = { reversed: true , intro: { y: 128 } }
175+ await tick ()
176+ toast .push (' Bottoms up!' )
177+ }
178+ },
179+ {
180+ name: ' RESTORE DEFAULTS' ,
181+ code: ' // All default settings restored!' ,
182+ run: async () => {
183+ colors = false
184+ bottom = false
185+ options = { reversed: false , intro: { x: 256 } }
186+ await tick ()
187+ toast .push (' All themes reset!' )
188+ }
189+ }
190+ ]
167191
168192 </script >
169193
170194<style >
171195.colors {
172- --toastBackground : rgba (255 ,255 ,255 ,0.98 );
173- --toastColor : #2D3748 ;
196+ --toastBackground : rgba (255 ,255 ,255 ,0.95 );
197+ --toastColor : #424242 ;
198+ --toastProgressBackground : aquamarine ;
174199}
175200.bottom {
176201 --toastContainerTop : auto ;
@@ -183,25 +208,19 @@ const handleRestore = async () => {
183208<div class =" container" >
184209
185210 <div class =" w-full h-64 px-2 mt-4 mb-8" >
186- <pre class =" w-full h-full bg-gray-700 text-gray-200 font-mono border border-gray-500 rounded-sm overflow-scroll p-4" ><code >
211+ <pre class =" w-full h-full bg-gray-700 text-gray-200 font-mono shadow rounded-sm overflow-scroll p-4" ><code >
187212 {code }
188213 </code ></pre >
189214 </div >
190215
191216 <div class =" flex flex-row flex-wrap items-center justify-center" >
192217
193- <button class ="btn" class:selected ={selected === ' default' } on:click ={handleDefault }>DEFAULT</button >
194- <button class ="btn" class:selected ={selected === ' green' } on:click ={handleGreen }>GREEN</button >
195- <button class ="btn" class:selected ={selected === ' yellow' } on:click ={handleYellow }>YELLOW</button >
196- <button class ="btn" class:selected ={selected === ' red' } on:click ={handleRed }>RED</button >
197- <button class ="btn" class:selected ={selected === ' long' } on:click ={handleLong }>LONG TIME</button >
198- <button class ="btn" class:selected ={selected === ' nodismiss' } on:click ={handleNodismiss }>NO DISMISS</button >
199- <button class ="btn" class:selected ={selected === ' remove' } on:click ={handleRemove }>REMOVE LAST</button >
200- <button class ="btn" class:selected ={selected === ' flip' } on:click ={handleFlip }>FLIP PROGRESS</button >
201- <button class ="btn" class:selected ={selected === ' loading' } on:click ={handleLoading }>USE AS LOADING INDICATOR</button >
202- <button class ="btn" class:selected ={selected === ' color' } on:click ={handleColor }>CHANGE DEFAULT COLORS</button >
203- <button class ="btn" class:selected ={selected === ' bottom' } on:click ={handleBottom }>POSITION TO BOTTOM</button >
204- <button class ="btn" class:selected ={selected === ' restore' } on:click ={handleRestore }>RESTORE DEFAULTS</button >
218+ {#each buttons as btn }
219+ <button
220+ class:selected ={selected === btn .name }
221+ on:click ={() => { handleClick (btn ) }}
222+ >{btn .name }</button >
223+ {/each }
205224
206225 </div >
207226</div >
0 commit comments