11[ ![ 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 )
33[ ![ Lines of code] ( https://img.shields.io/tokei/lines/github/zerodevx/svelte-toast?label=lines%20of%20code )] ( https://github.com/XAMPPRocky/tokei )
44[ ![ Size minified] ( https://img.shields.io/bundlephobia/min/@zerodevx/svelte-toast?label=minified )] ( https://bundlephobia.com/ )
55[ ![ 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
1313[ little] ( https://github.com/zerodevx/svelte-toast/blob/master/src/ToastItem.svelte )
1414[ lines] ( https://github.com/zerodevx/svelte-toast/blob/master/src/store.js )
1515[ 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.
1918
2019Because a demo helps better than a thousand API docs: https://zerodevx.github.io/svelte-toast/
2120
@@ -29,8 +28,8 @@ $ npm i -D @zerodevx/svelte-toast
2928
3029The following are exported:
3130
32- * ` SvelteToast ` as the toast container;
33- * ` toast ` as the toast emitter.
31+ - ` SvelteToast ` as the toast container;
32+ - ` toast ` as the toast emitter.
3433
3534### Svelte
3635
@@ -40,17 +39,16 @@ If you're using this in a Svelte app, import the toast container and place it in
4039
4140``` html
4241<script >
43- import { SvelteToast } from ' @zerodevx/svelte-toast'
42+ import { SvelteToast } from ' @zerodevx/svelte-toast'
4443
45- // Optionally set default options here
46- const options = {
47- ...
48- }
44+ // Optionally set default options here
45+ const options = {
46+ ...
47+ }
4948 </script >
5049
5150...
5251<SvelteToast {options} />
53-
5452```
5553
5654Use 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
146144 height : var (--toastHeight , auto );
147145 min-height : var (--toastMinHeight , 3.5rem );
148146 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 );
152152 border-radius : var (--toastBorderRadius , 0.125rem );
153153}
154154
@@ -157,20 +157,26 @@ In general, use CSS variables - the following (self-explanatory) vars are expose
157157}
158158
159159._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% );
161167}
162168```
163169
164170So to apply your custom theme globally, do something like:
165171
166172``` html
167173<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+ }
174180 </style >
175181```
176182
@@ -190,8 +196,8 @@ where `theme` is an object containing one or more CSS var key/value pairs.
190196
191197### Create Your Own Toast Actions
192198
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:
195201
196202` my-theme.js `
197203
@@ -202,7 +208,7 @@ export const success = m => toast.push(m, {
202208 theme: {
203209 ' --toastBackground' : ' green' ,
204210 ' --toastColor' : ' white' ,
205- ' --toastProgressBackground ' : ' darkgreen '
211+ ' --toastBarBackground ' : ' olive '
206212 }
207213})
208214
@@ -216,7 +222,7 @@ Then simply import these stubs in your consuming component:
216222``` js
217223import { success , warning , failure } from ' ./my-theme'
218224
219- // do something, then
225+ // Do something, then
220226success (' It works!' )
221227```
222228
@@ -232,20 +238,20 @@ toast.push(`<strong>You won the jackpot!</strong><br>
232238
233239### Custom Fonts
234240
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:
237243
238244``` html
239245<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+ }
249255 </style >
250256
251257<div class =" wrap" >
@@ -258,7 +264,7 @@ In Vanilla JS, simply apply your styles to the `._toastMsg` class:
258264``` css
259265._toastMsg {
260266 font-family : Roboto, sans-serif ;
261- ...
267+ ...;
262268}
263269```
264270
@@ -272,25 +278,25 @@ It's now easy to send toasts to different container targets within your app. For
272278
273279``` html
274280<script >
275- import { SvelteToast , toast } from ' @zerodevx/svelte-toast'
281+ import { SvelteToast , toast } from ' @zerodevx/svelte-toast'
276282
277- // Sends a toast to default toast container
278- toast .push (' Yo!' )
283+ // Sends a toast to default toast container
284+ toast .push (' Yo!' )
279285
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' })
282288 </script >
283289
284290<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+ }
294300 </style >
295301
296302<!-- Default toast container -->
@@ -309,7 +315,7 @@ toast.push('Hey!', { target: 'new' })
309315
310316``` js
311317// Remove all toasts from "new" container
312- toast .pop (i => i .target !== ' new' )
318+ toast .pop (( i ) => i .target !== ' new' )
313319
314320// Or remove ALL active toasts from ALL containers
315321toast .pop (0 )
@@ -321,47 +327,61 @@ toast.pop(0)
321327
322328``` js
323329let id = toast .push (' Yo!' , { duration: 2000 })
324- // is semantically equivalent to
330+ // ... is semantically equivalent to
325331id = toast .push ({ msg: ' Yo!' , duration: 2000 })
326332
327333toast .set (id, { msg: ' Waddup!' })
328- // is semantically equivalent to
334+ // ... is semantically equivalent to
329335toast .set ({ id, msg: ' Waddup!' })
330336```
331337
332338### New from ` v0.5 `
333339
334340#### Pausable Toasts
335341
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 ` .
338344
339345``` js
340346toast .push (' Hover me!' , { pausable: true })
341347```
342348
343349#### Send Svelte Component as a Message
344350
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:
347353
348354``` js
349355import MyCustomSvelteComponent from ' ./MyCustomSvelteComponent.svelte'
350356
351357toast .push ({
352358 component: {
353359 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` )
358377 },
359378 ... // any other toast options
360379})
361380```
362381
363382## Toast Options
364383
384+ <!-- prettier-ignore-start -->
365385``` js
366386// Default options
367387const options = {
@@ -375,6 +395,7 @@ const options = {
375395 theme: {} // css var overrides
376396}
377397```
398+ <!-- prettier-ignore-end -->
378399
379400## Toast Methods
380401
0 commit comments