1
1
[ ![ npm (scoped)] ( https://img.shields.io/npm/v/@zerodevx/svelte-toast )] ( https://www.npmjs.com/package/@zerodevx/svelte-toast )
2
2
[ ![ JavaScript Style Guide] ( https://img.shields.io/badge/code_style-standard-brightgreen.svg )] ( https://standardjs.com )
3
+ [ ![ Lines of code] ( https://img.shields.io/tokei/lines/github/zerodevx/svelte-toast?label=lines%20of%20code )] ( https://github.com/XAMPPRocky/tokei )
4
+ [ ![ Size minified] ( https://img.shields.io/bundlephobia/min/@zerodevx/svelte-toast?label=minified )] ( https://bundlephobia.com/ )
5
+ [ ![ Size gzipped] ( https://img.shields.io/bundlephobia/minzip/@zerodevx/svelte-toast?label=gzipped )] ( https://bundlephobia.com/ )
3
6
4
7
# svelte-toast
5
8
6
9
> Simple elegant toast notifications.
7
10
8
- A lightweight, unopinionated and performant toast notification component for modern web frontends in very little
9
- lines of code. Compiled, it's only ** 15kb** minified (** 6kb** gzipped) and can be used in Vanilla JS, or as a
10
- Svelte component.
11
+ A lightweight, unopinionated and performant toast notification component for modern web frontends in
12
+ [ very] ( https://github.com/zerodevx/svelte-toast/blob/master/src/SvelteToast.svelte )
13
+ [ little] ( https://github.com/zerodevx/svelte-toast/blob/master/src/ToastItem.svelte )
14
+ [ lines] ( https://github.com/zerodevx/svelte-toast/blob/master/src/store.js )
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
+ as a Svelte component.
11
19
12
20
Because a demo helps better than a thousand API docs: https://zerodevx.github.io/svelte-toast/
13
21
@@ -101,7 +109,7 @@ Or if you prefer to go old-school javascript and a CDN:
101
109
})
102
110
103
111
// Now you can `toast` anywhere!
104
- toast .push (' Hello world!' );
112
+ toast .push (' Hello world!' )
105
113
</script >
106
114
</head >
107
115
```
@@ -214,14 +222,15 @@ then apply styles on the wrapper:
214
222
215
223
``` html
216
224
<style >
217
- .wrap {
218
- font-family : Roboto, sans-serif ;
219
- font-size : 0.875rem ;
220
- ...
221
- }
222
- .wrap :global(strong ) {
223
- font-weight : 600 ;
224
- }
225
+ .wrap {
226
+ display : contents ;
227
+ font-family : Roboto, sans-serif ;
228
+ font-size : 0.875rem ;
229
+ ...
230
+ }
231
+ .wrap :global(strong ) {
232
+ font-weight : 600 ;
233
+ }
225
234
</style >
226
235
227
236
<div class =" wrap" >
@@ -238,6 +247,8 @@ In Vanilla JS, simply apply your styles to the `._toastMsg` class:
238
247
}
239
248
```
240
249
250
+ ## Features
251
+
241
252
### New from ` v0.4 `
242
253
243
254
#### Supporting Multiple Toast Containers
@@ -289,43 +300,74 @@ toast.pop(i => i.target !== 'new')
289
300
toast .pop (0 )
290
301
```
291
302
292
- #### Accepts Object as First Param
303
+ #### Accepting Object as First Param
293
304
294
305
` push() ` and ` set() ` can also take an object as its first parameter.
295
306
296
307
``` js
297
308
let id = toast .push (' Yo!' , { duration: 2000 })
298
-
299
309
// is semantically equivalent to
300
310
id = toast .push ({ msg: ' Yo!' , duration: 2000 })
301
311
302
312
toast .set (id, { msg: ' Waddup!' })
303
-
304
313
// is semantically equivalent to
305
314
toast .set ({ id, msg: ' Waddup!' })
306
315
```
307
316
308
- ## Options
317
+ ### New from ` v0.5 `
318
+
319
+ #### Pausable Toasts
320
+
321
+ Progress bar tweens can now be paused when the mouse cursor (on desktop) is hovered on the toast item.
322
+ This behaviour by default is ** disabled** . To enable, set option ` pausable: true ` :
323
+
324
+ ``` js
325
+ toast .push (' Hover me!' , { pausable: true })
326
+ ```
327
+
328
+ #### Send Svelte Component as a Message
329
+
330
+ To allow for complex UI or workflows, a Svelte component can be pushed instead of a standard message.
331
+ Obviously, this works only if you're consuming ` svelte-toast ` as part of a larger Svelte project.
332
+ To do so, push options with ` component ` property defined:
333
+
334
+ ``` js
335
+ import MyCustomSvelteComponent from ' ./MyCustomSvelteComponent.svelte'
336
+
337
+ toast .push ({
338
+ component: {
339
+ src: MyCustomSvelteComponent, // set the `src` to the component itself (required)
340
+ props: { // optionally pass in `props` as key/value pairs
341
+ ...
342
+ },
343
+ sendIdTo: ' toastId' // optionally forward the toast id to `toastId` prop
344
+ },
345
+ ... // any other toast options
346
+ })
347
+ ```
348
+
349
+ ## Toast Options
309
350
310
351
``` js
311
352
// Default options
312
353
const options = {
313
- duration: 4000 , // duration of progress bar tween
314
- dismissable: true , // allow dismiss with close button
354
+ duration: 4000 , // duration of progress bar tween to the `next` value
315
355
initial: 1 , // initial progress bar value
316
- progress: 0 , // current progress
356
+ next: 0 , // next progress value
357
+ dismissable: true , // allow dismiss with close button
358
+ pausable: false , // allow pause on mouse hover
317
359
reversed: false , // insert new toast to bottom of stack
318
360
intro: { x: 256 }, // toast intro fly animation settings
319
361
theme: {} // css var overrides
320
362
}
321
363
```
322
364
323
- ## Toast API
365
+ ## Toast Methods
324
366
325
367
``` js
326
- const id = toast .push (message, { options })
327
- toast .pop (id)
328
- toast .set (id, { options })
368
+ const id = toast .push (message, { ... options })
369
+ toast .pop (id) // accepts a filter function too
370
+ toast .set (id, { ... options })
329
371
```
330
372
331
373
## Development
0 commit comments