1
1
[ ![ npm (scoped)] ( https://img.shields.io/npm/v/@zerodevx/svelte-toast/latest )] ( https://www.npmjs.com/package/@zerodevx/svelte-toast )
2
- [ ![ JavaScript Style Guide ] ( https://img.shields.io/badge/code_style-standard-brightgreen .svg )] ( https://standardjs .com )
2
+ [ ![ code style: prettier ] ( https://img.shields.io/badge/code_style-prettier-ff69b4 .svg )] ( https://github .com/prettier/prettier )
3
3
[ ![ Lines of code] ( https://img.shields.io/tokei/lines/github/zerodevx/svelte-toast?label=lines%20of%20code )] ( https://github.com/XAMPPRocky/tokei )
4
4
[ ![ Size minified] ( https://img.shields.io/bundlephobia/min/@zerodevx/svelte-toast?label=minified )] ( https://bundlephobia.com/ )
5
5
[ ![ Size gzipped] ( https://img.shields.io/bundlephobia/minzip/@zerodevx/svelte-toast?label=gzipped )] ( https://bundlephobia.com/ )
@@ -13,9 +13,8 @@ A lightweight, unopinionated and performant toast notification component for mod
13
13
[ little] ( https://github.com/zerodevx/svelte-toast/blob/master/src/ToastItem.svelte )
14
14
[ lines] ( https://github.com/zerodevx/svelte-toast/blob/master/src/store.js )
15
15
[ of] ( https://github.com/zerodevx/svelte-toast/blob/master/src/index.js )
16
- [ code] ( https://github.com/zerodevx/svelte-toast/blob/master/src/index.d.ts ) .
17
- Compiled (into UMD), it's only ** 17kb** minified (** 7kb** gzipped) and can be used in Vanilla JS,
18
- or as a Svelte component.
16
+ [ code] ( https://github.com/zerodevx/svelte-toast/blob/master/src/index.d.ts ) . Compiled (into UMD), it's only ** 18kb**
17
+ minified (** 7kb** gzipped) and can be used in Vanilla JS, or as a Svelte component.
19
18
20
19
Because a demo helps better than a thousand API docs: https://zerodevx.github.io/svelte-toast/
21
20
@@ -29,8 +28,8 @@ $ npm i -D @zerodevx/svelte-toast
29
28
30
29
The following are exported:
31
30
32
- * ` SvelteToast ` as the toast container;
33
- * ` toast ` as the toast emitter.
31
+ - ` SvelteToast ` as the toast container;
32
+ - ` toast ` as the toast emitter.
34
33
35
34
### Svelte
36
35
@@ -40,17 +39,16 @@ If you're using this in a Svelte app, import the toast container and place it in
40
39
41
40
``` html
42
41
<script >
43
- import { SvelteToast } from ' @zerodevx/svelte-toast'
42
+ import { SvelteToast } from ' @zerodevx/svelte-toast'
44
43
45
- // Optionally set default options here
46
- const options = {
47
- ...
48
- }
44
+ // Optionally set default options here
45
+ const options = {
46
+ ...
47
+ }
49
48
</script >
50
49
51
50
...
52
51
<SvelteToast {options} />
53
-
54
52
```
55
53
56
54
Use anywhere in your app - just import the toast emitter.
@@ -146,9 +144,11 @@ In general, use CSS variables - the following (self-explanatory) vars are expose
146
144
height : var (--toastHeight , auto );
147
145
min-height : var (--toastMinHeight , 3.5rem );
148
146
margin : var (--toastMargin , 0 0 0.5rem 0 );
149
- background : var (--toastBackground , rgba (66 ,66 ,66 ,0.9 ));
150
- color : var (--toastColor , #FFF );
151
- box-shadow : var (--toastBoxShadow , 0 4px 6px -1px rgba (0 ,0 ,0 ,0.1 ),0 2px 4px -1px rgba (0 ,0 ,0 ,0.06 ));
147
+ padding : var (--toastPadding , 0 );
148
+ background : var (--toastBackground , rgba (66 , 66 , 66 , 0.9 ));
149
+ color : var (--toastColor , #fff );
150
+ box-shadow : var (--toastBoxShadow , 0 4px 6px -1px rgba (0 , 0 , 0 , 0.1 ), 0 2px 4px -1px rgba (0 , 0 , 0 , 0.06 ));
151
+ border : var (--toastBorder , none );
152
152
border-radius : var (--toastBorderRadius , 0.125rem );
153
153
}
154
154
@@ -157,20 +157,26 @@ In general, use CSS variables - the following (self-explanatory) vars are expose
157
157
}
158
158
159
159
._toastBar {
160
- background : var (--toastProgressBackground , rgba (33 ,150 ,243 ,0.75 ));
160
+ background : var (--toastBarBackground , rgba (33 , 150 , 243 , 0.75 ));
161
+ top : var (--toastBarTop , auto );
162
+ right : var (--toastBarRight , auto );
163
+ bottom : var (--toastBarBottom , 0 );
164
+ left : var (--toastBarLeft , 0 );
165
+ height : var (--toastBarHeight , 6px );
166
+ width : var (--toastBarWidth , 100% );
161
167
}
162
168
```
163
169
164
170
So to apply your custom theme globally, do something like:
165
171
166
172
``` html
167
173
<style >
168
- :root {
169
- --toastBackground : #ABCDEF ;
170
- --toastColor : #123456 ;
171
- --toastHeight : 300px ;
172
- ...
173
- }
174
+ :root {
175
+ --toastBackground : #abcdef ;
176
+ --toastColor : #123456 ;
177
+ --toastHeight : 300px ;
178
+ ...;
179
+ }
174
180
</style >
175
181
```
176
182
@@ -190,8 +196,8 @@ where `theme` is an object containing one or more CSS var key/value pairs.
190
196
191
197
### Create Your Own Toast Actions
192
198
193
- For convenient composing, the recommended way is to create your own common toast actions by stubbing them out.
194
- For example:
199
+ For convenient composing, the recommended way is to create your own common toast actions by stubbing them out. For
200
+ example:
195
201
196
202
` my-theme.js `
197
203
@@ -202,7 +208,7 @@ export const success = m => toast.push(m, {
202
208
theme: {
203
209
' --toastBackground' : ' green' ,
204
210
' --toastColor' : ' white' ,
205
- ' --toastProgressBackground ' : ' darkgreen '
211
+ ' --toastBarBackground ' : ' olive '
206
212
}
207
213
})
208
214
@@ -216,7 +222,7 @@ Then simply import these stubs in your consuming component:
216
222
``` js
217
223
import { success , warning , failure } from ' ./my-theme'
218
224
219
- // do something, then
225
+ // Do something, then
220
226
success (' It works!' )
221
227
```
222
228
@@ -232,20 +238,20 @@ toast.push(`<strong>You won the jackpot!</strong><br>
232
238
233
239
### Custom Fonts
234
240
235
- In a Svelte app, the easiest way to apply custom font styles is to wrap the toast container
236
- then apply styles on the wrapper:
241
+ In a Svelte app, the easiest way to apply custom font styles is to wrap the toast container then apply styles on the
242
+ wrapper:
237
243
238
244
``` html
239
245
<style >
240
- .wrap {
241
- display : contents ;
242
- font-family : Roboto, sans-serif ;
243
- font-size : 0.875rem ;
244
- ...
245
- }
246
- .wrap :global(strong ) {
247
- font-weight : 600 ;
248
- }
246
+ .wrap {
247
+ display : contents ;
248
+ font-family : Roboto, sans-serif ;
249
+ font-size : 0.875rem ;
250
+ ...;
251
+ }
252
+ .wrap :global(strong ) {
253
+ font-weight : 600 ;
254
+ }
249
255
</style >
250
256
251
257
<div class =" wrap" >
@@ -258,7 +264,7 @@ In Vanilla JS, simply apply your styles to the `._toastMsg` class:
258
264
``` css
259
265
._toastMsg {
260
266
font-family : Roboto, sans-serif ;
261
- ...
267
+ ...;
262
268
}
263
269
```
264
270
@@ -272,25 +278,25 @@ It's now easy to send toasts to different container targets within your app. For
272
278
273
279
``` html
274
280
<script >
275
- import { SvelteToast , toast } from ' @zerodevx/svelte-toast'
281
+ import { SvelteToast , toast } from ' @zerodevx/svelte-toast'
276
282
277
- // Sends a toast to default toast container
278
- toast .push (' Yo!' )
283
+ // Sends a toast to default toast container
284
+ toast .push (' Yo!' )
279
285
280
- // Sends a toast to "new" toast container
281
- toast .push (' Hey!' , { target: ' new' })
286
+ // Sends a toast to "new" toast container
287
+ toast .push (' Hey!' , { target: ' new' })
282
288
</script >
283
289
284
290
<style >
285
- .wrap {
286
- --toastContainerTop : 0.5rem ;
287
- --toastContainerRight : 2rem ;
288
- --toastContainerBottom : auto ;
289
- --toastContainerLeft : 2rem ;
290
- --toastWidth : 100% ;
291
- font-size : 0.875rem ;
292
- ...
293
- }
291
+ .wrap {
292
+ --toastContainerTop : 0.5rem ;
293
+ --toastContainerRight : 2rem ;
294
+ --toastContainerBottom : auto ;
295
+ --toastContainerLeft : 2rem ;
296
+ --toastWidth : 100% ;
297
+ font-size : 0.875rem ;
298
+ ...;
299
+ }
294
300
</style >
295
301
296
302
<!-- Default toast container -->
@@ -309,7 +315,7 @@ toast.push('Hey!', { target: 'new' })
309
315
310
316
``` js
311
317
// Remove all toasts from "new" container
312
- toast .pop (i => i .target !== ' new' )
318
+ toast .pop (( i ) => i .target !== ' new' )
313
319
314
320
// Or remove ALL active toasts from ALL containers
315
321
toast .pop (0 )
@@ -321,47 +327,61 @@ toast.pop(0)
321
327
322
328
``` js
323
329
let id = toast .push (' Yo!' , { duration: 2000 })
324
- // is semantically equivalent to
330
+ // ... is semantically equivalent to
325
331
id = toast .push ({ msg: ' Yo!' , duration: 2000 })
326
332
327
333
toast .set (id, { msg: ' Waddup!' })
328
- // is semantically equivalent to
334
+ // ... is semantically equivalent to
329
335
toast .set ({ id, msg: ' Waddup!' })
330
336
```
331
337
332
338
### New from ` v0.5 `
333
339
334
340
#### Pausable Toasts
335
341
336
- Progress bar tweens can now be paused when the mouse cursor (on desktop) is hovered over the toast item.
337
- This behaviour is ** disabled** by default. To enable, set option ` pausable: true ` .
342
+ Progress bar tweens can now be paused when the mouse cursor (on desktop) is hovered over the toast item. This behaviour
343
+ is ** disabled** by default. To enable, set option ` pausable: true ` .
338
344
339
345
``` js
340
346
toast .push (' Hover me!' , { pausable: true })
341
347
```
342
348
343
349
#### Send Svelte Component as a Message
344
350
345
- To support complex UI or workflows, a Svelte component can be pushed instead of a standard message if
346
- you're using this package in a Svelte project. To do so, push options with ` component ` defined:
351
+ To support complex UIs or workflows, a Svelte component can be pushed instead of a standard message if you're using this
352
+ in a Svelte project. To do so, push options with ` component ` defined:
347
353
348
354
``` js
349
355
import MyCustomSvelteComponent from ' ./MyCustomSvelteComponent.svelte'
350
356
351
357
toast .push ({
352
358
component: {
353
359
src: MyCustomSvelteComponent, // set `src` to the component itself (required)
354
- props: { // optionally pass in `props` as key/value pairs
355
- ...
356
- },
357
- sendIdTo: ' toastId' // optionally forward toast id to `toastId` prop
360
+ props: { ... }, // pass in `props` as key/value pairs (optional)
361
+ sendIdTo: ' toastId' // forward toast id to `toastId` prop (optional)
362
+ },
363
+ ... // any other toast options
364
+ })
365
+ ```
366
+
367
+ ### New from ` v0.6 `
368
+
369
+ #### ` onpop() ` Callback Function
370
+
371
+ A callback function can be run when a toast is closed. To do so, pass the function to the ` onpop ` toast option:
372
+
373
+ ``` js
374
+ toast .push (' Hello world' , {
375
+ onpop : (id ) => { // optionally get the toast id if needed
376
+ console .log (` ${ id} removed` )
358
377
},
359
378
... // any other toast options
360
379
})
361
380
```
362
381
363
382
## Toast Options
364
383
384
+ <!-- prettier-ignore-start -->
365
385
``` js
366
386
// Default options
367
387
const options = {
@@ -375,6 +395,7 @@ const options = {
375
395
theme: {} // css var overrides
376
396
}
377
397
```
398
+ <!-- prettier-ignore-end -->
378
399
379
400
## Toast Methods
380
401
0 commit comments