|
| 1 | +<script> |
| 2 | +/* eslint no-useless-escape: 0 */ |
| 3 | +
|
| 4 | +import { tick } from 'svelte' |
| 5 | +import { SvelteToast, toast } from '../src' |
| 6 | +
|
| 7 | +// Hoist to `window` for debug |
| 8 | +window.toast = toast |
| 9 | +
|
| 10 | +let selected |
| 11 | +let code = '' |
| 12 | +
|
| 13 | +const handleDefault = () => { |
| 14 | + selected = 'default' |
| 15 | + code = 'toast.push(\'Hello world!\')' |
| 16 | + toast.push('Hello world!') |
| 17 | +} |
| 18 | +
|
| 19 | +const handleGreen = () => { |
| 20 | + selected = 'green' |
| 21 | + code = `toast.push('Success!', { |
| 22 | + theme: { |
| 23 | + '--toastBackground': '#48BB78', |
| 24 | + '--toastProgressBackground': '#2F855A' |
| 25 | + } |
| 26 | +)` |
| 27 | + toast.push('Success!', { theme: { '--toastBackground': '#48BB78', '--toastProgressBackground': '#2F855A' } }) |
| 28 | +} |
| 29 | +
|
| 30 | +const handleYellow = () => { |
| 31 | + selected = 'yellow' |
| 32 | + code = `toast.push('Warning!', { |
| 33 | + theme: { |
| 34 | + '--toastBackground': '#ECC94B', |
| 35 | + '--toastProgressBackground': '#B7791F' |
| 36 | + } |
| 37 | +)` |
| 38 | + toast.push('Warning!', { theme: { '--toastBackground': '#ECC94B', '--toastProgressBackground': '#B7791F' } }) |
| 39 | +} |
| 40 | +
|
| 41 | +const handleRed = () => { |
| 42 | + selected = 'red' |
| 43 | + code = `toast.push('Error!', { |
| 44 | + theme: { |
| 45 | + '--toastBackground': '#F56565', |
| 46 | + '--toastProgressBackground': '#C53030' |
| 47 | + } |
| 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?!?', { |
| 61 | + initial: 0, |
| 62 | + progress: 0, |
| 63 | + 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 |
| 71 | +toast.pop() |
| 72 | +
|
| 73 | +// Or remove a particular one |
| 74 | +const 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', { |
| 82 | + initial: 0, |
| 83 | + progress: 1 |
| 84 | +})` |
| 85 | + toast.push('Progress bar is flipped', { initial: 0, progress: 1 }) |
| 86 | +} |
| 87 | +
|
| 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)) |
| 92 | +const id = toast.push('Loading, please wait...', { |
| 93 | + duration: 300, |
| 94 | + initial: 0, |
| 95 | + progress: 0, |
| 96 | + dismissable: false |
| 97 | +}) |
| 98 | +await sleep(500) |
| 99 | +toast.set(id, { progress: 0.1 }) |
| 100 | +await sleep(3000) |
| 101 | +toast.set(id, { progress: 0.7 }) |
| 102 | +await sleep(1000) |
| 103 | +toast.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 | +} |
| 117 | +
|
| 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 | +} |
| 126 | +</style> |
| 127 | +<script> |
| 128 | + 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> |
| 139 | +:root { |
| 140 | + --toastContainerTop: auto; |
| 141 | + --toastContainerRight: auto; |
| 142 | + --toastContainerBottom: 8rem; |
| 143 | + --toastContainerLeft: calc(50vw - 8rem); |
| 144 | +} |
| 145 | +</style> |
| 146 | +
|
| 147 | +<SvelteToast options={{ reversed: true, intro: { y: 192 } }} /> |
| 148 | +
|
| 149 | +<script> |
| 150 | + 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 | +} |
| 167 | +
|
| 168 | +</script> |
| 169 | + |
| 170 | +<style> |
| 171 | +.colors { |
| 172 | + --toastBackground: rgba(255,255,255,0.98); |
| 173 | + --toastColor: #2D3748; |
| 174 | +} |
| 175 | +.bottom { |
| 176 | + --toastContainerTop: auto; |
| 177 | + --toastContainerRight: auto; |
| 178 | + --toastContainerBottom: 8rem; |
| 179 | + --toastContainerLeft: calc(50vw - 8rem); |
| 180 | +} |
| 181 | +</style> |
| 182 | + |
| 183 | +<div class="container"> |
| 184 | + |
| 185 | + <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> |
| 187 | + {code} |
| 188 | + </code></pre> |
| 189 | + </div> |
| 190 | + |
| 191 | + <div class="flex flex-row flex-wrap items-center justify-center"> |
| 192 | + |
| 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> |
| 205 | + |
| 206 | + </div> |
| 207 | +</div> |
| 208 | + |
| 209 | +<div class:colors class:bottom> |
| 210 | + <SvelteToast {options} /> |
| 211 | +</div> |
0 commit comments