@@ -14,7 +14,7 @@ A lightweight, unopinionated and performant toast notification component for mod
14
14
[ lines] ( https://github.com/zerodevx/svelte-toast/blob/master/src/stores.js )
15
15
[ of] ( https://github.com/zerodevx/svelte-toast/blob/master/src/index.js )
16
16
[ code] ( https://github.com/zerodevx/svelte-toast/blob/master/src/index.d.ts ) . Compiled (into UMD),
17
- it's only ** 18kb ** minified (** 7kb ** gzipped) and can be used in Vanilla JS, or as a Svelte
17
+ it's only ** 19kb ** minified (** 8kb ** gzipped) and can be used in Vanilla JS, or as a Svelte
18
18
component.
19
19
20
20
Because a demo helps better than a thousand API docs: https://zerodevx.github.io/svelte-toast/
@@ -34,10 +34,12 @@ The following are exported:
34
34
35
35
### Svelte
36
36
37
- If you're using this in a Svelte app, import the toast container and place it in your app shell.
37
+ If you're using this in a Svelte app, import the toast container and place it in your app shell or
38
+ root layout.
38
39
39
- ` App .svelte` :
40
+ ` +layout .svelte` :
40
41
42
+ <!-- prettier-ignore -->
41
43
``` html
42
44
<script >
43
45
import { SvelteToast } from ' @zerodevx/svelte-toast'
@@ -58,7 +60,7 @@ Use anywhere in your app - just import the toast emitter.
58
60
59
61
``` html
60
62
<script >
61
- import { toast } from ' @zerodevx/svelte-toast'
63
+ import { toast } from ' @zerodevx/svelte-toast'
62
64
</script >
63
65
64
66
<button on:click ={() => toast.push('Hello world!')}>EMIT TOAST</button >
@@ -162,6 +164,16 @@ In general, use CSS variables - the following (self-explanatory) vars are expose
162
164
padding : var (--toastMsgPadding , 0.75rem 0.5rem );
163
165
}
164
166
167
+ ._toastBtn {
168
+ width : var (--toastBtnWidth , 2rem );
169
+ height : var (--toastBtnHeight , 100% );
170
+ font : var (--toastBtnFont , 1rem sans-serif );
171
+ }
172
+
173
+ ._toastBtn ::after {
174
+ content : var (--toastBtnContent , ' ✕' );
175
+ }
176
+
165
177
._toastBar {
166
178
background : var (--toastBarBackground , rgba (33 , 150 , 243 , 0.75 ));
167
179
top : var (--toastBarTop , auto );
@@ -175,13 +187,14 @@ In general, use CSS variables - the following (self-explanatory) vars are expose
175
187
176
188
So to apply your custom theme globally, do something like:
177
189
190
+ <!-- prettier-ignore -->
178
191
``` html
179
192
<style >
180
193
:root {
181
194
--toastBackground : #abcdef ;
182
195
--toastColor : #123456 ;
183
196
--toastHeight : 300px ;
184
- ...;
197
+ ...
185
198
}
186
199
</style >
187
200
```
@@ -244,18 +257,21 @@ toast.push(`<strong>You won the jackpot!</strong><br>
244
257
Click <a href="#" target="_blank">here</a> for details! 😛` )
245
258
```
246
259
247
- ### Custom Fonts
260
+ ### Custom Fonts and Styles
248
261
249
- In a Svelte app, the easiest way to apply custom font styles is to wrap the toast container then
250
- apply styles on the wrapper:
262
+ In a Svelte app, the quickest way to apply custom styles is to wrap the toast container then apply
263
+ styles on the wrapper:
251
264
265
+ <!-- prettier-ignore -->
252
266
``` html
253
267
<style >
254
268
.wrap {
255
269
display : contents ;
256
270
font-family : Roboto, sans-serif ;
257
271
font-size : 0.875rem ;
258
- ...;
272
+ /* You can set CSS vars here too */
273
+ --toastBackground : pink ;
274
+ ...
259
275
}
260
276
.wrap :global(strong ) {
261
277
font-weight : 600 ;
@@ -267,12 +283,14 @@ apply styles on the wrapper:
267
283
</div >
268
284
```
269
285
270
- In Vanilla JS, simply apply your styles to the ` ._toastMsg ` class:
286
+ In Vanilla JS, simply apply your styles to the ` ._toastContainer ` class:
271
287
288
+ <!-- prettier-ignore -->
272
289
``` css
273
- ._toastMsg {
290
+ ._toastContainer {
274
291
font-family : Roboto, sans-serif ;
275
- ...;
292
+ --toastBackground : yellow ;
293
+ ...
276
294
}
277
295
```
278
296
@@ -284,6 +302,7 @@ In Vanilla JS, simply apply your styles to the `._toastMsg` class:
284
302
285
303
It's now easy to send toasts to different container targets within your app. For example:
286
304
305
+ <!-- prettier-ignore -->
287
306
``` html
288
307
<script >
289
308
import { SvelteToast , toast } from ' @zerodevx/svelte-toast'
@@ -303,7 +322,7 @@ It's now easy to send toasts to different container targets within your app. For
303
322
--toastContainerLeft : 2rem ;
304
323
--toastWidth : 100% ;
305
324
font-size : 0.875rem ;
306
- ...;
325
+ ...
307
326
}
308
327
</style >
309
328
@@ -411,9 +430,42 @@ or implementing CSS-only dark modes.
411
430
</style >
412
431
```
413
432
433
+ ### New from ` v0.8 `
434
+
435
+ #### Auto-pause Toasts when Page Hidden
436
+
437
+ This feature uses the
438
+ [ Page Visibility API] ( https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API ) (if it's
439
+ supported) to pause/resume a toast whenever the browser tab visibility changes - allowing one to
440
+ emit notifications in the background without it being dismissed prematurely. This now happens
441
+ automatically and is default behaviour, since notifications should by nature ensure that they're
442
+ seen.
443
+
444
+ #### Customise Dismiss Button
445
+
446
+ Additional CSS vars are exposed - specifically, ` --toastBtnContent ` allows the '✕' default character
447
+ to be changed. As with CSS ` content ` keys for pseudo elements, ` url() ` can be used to load external
448
+ or inline icons.
449
+
450
+ ``` html
451
+ <script >
452
+ import { toast , SvelteToast } from ' @zerodevx/svelte-toast'
453
+
454
+ const options = {
455
+ theme: {
456
+ ' --toastBtnContent' : ` url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' ...")`
457
+ }
458
+ }
459
+ </script >
460
+
461
+ <button on:click ={() => toast.push('Ping!')}>PONG</button >
462
+
463
+ <SvelteToast {options} />
464
+ ```
465
+
414
466
## Toast Options
415
467
416
- <!-- prettier-ignore-start -->
468
+ <!-- prettier-ignore -->
417
469
``` js
418
470
// Default options
419
471
const options = {
@@ -428,7 +480,6 @@ const options = {
428
480
classes: [] // user-defined classes
429
481
}
430
482
```
431
- <!-- prettier-ignore-end -->
432
483
433
484
## Toast Methods
434
485
@@ -440,7 +491,8 @@ toast.set(id, { ...options })
440
491
441
492
## Development
442
493
443
- Standard Github [ contribution workflow] ( https://gist.github.com/Chaser324/ce0505fbed06b947d962 )
494
+ Standard Github
495
+ [ contribution workflow] ( https://docs.github.com/en/get-started/quickstart/contributing-to-projects )
444
496
applies.
445
497
446
498
### Tests
0 commit comments