UNPKG

13.8 kBMarkdownView Raw
1<div align="center">
2 <img src="https://raw.githubusercontent.com/scttcper/ngx-toastr/master/misc/documentation-assets/ngx-toastr-example.png" width="300" alt="Angular Toastr">
3 <br>
4 <h1>ngx-toastr</h1>
5 <br>
6 <a href="https://www.npmjs.org/package/ngx-toastr">
7 <img src="https://badge.fury.io/js/ngx-toastr.svg" alt="npm">
8 </a>
9 <a href="https://circleci.com/gh/scttcper/ngx-toastr">
10 <img src="https://circleci.com/gh/scttcper/ngx-toastr.svg?style=svg" alt="circleci">
11 </a>
12 <a href="https://codecov.io/github/scttcper/ngx-toastr">
13 <img src="https://img.shields.io/codecov/c/github/scttcper/ngx-toastr.svg" alt="codecov">
14 </a>
15 <br>
16 <br>
17</div>
18
19DEMO: https://ngx-toastr.vercel.app
20
21## Features
22
23- Toast Component Injection without being passed `ViewContainerRef`
24- No use of `*ngFor`. Fewer dirty checks and higher performance.
25- AoT compilation and lazy loading compatible
26- Component inheritance for custom toasts
27- SystemJS/UMD rollup bundle
28- Animations using Angular's
29 [Web Animations API](https://angular.io/docs/ts/latest/guide/animations.html)
30- Output toasts to an optional target directive
31
32## Dependencies
33
34Latest version available for each version of Angular
35
36| ngx-toastr | Angular |
37| ---------- | ----------- |
38| 6.5.0 | 4.x |
39| 8.10.2 | 5.x |
40| 10.1.0 | 8.x 7.x 6.x |
41| 11.3.3 | 8.x |
42| 12.1.0 | 9.x |
43| 13.2.1 | 10.x 11.x |
44| current | >= 12.x |
45
46## Install
47
48```bash
49npm install ngx-toastr --save
50```
51
52`@angular/animations` package is a required dependency for the default toast
53
54```bash
55npm install @angular/animations --save
56```
57
58Don't want to use `@angular/animations`? See
59[Setup Without Animations](#setup-without-animations).
60
61## Setup
62
63**step 1:** add css
64
65- copy
66 [toast css](/src/lib/toastr.css)
67 to your project.
68- If you are using sass you can import the css.
69
70```scss
71// regular style toast
72@import '~ngx-toastr/toastr';
73
74// bootstrap style toast
75// or import a bootstrap 4 alert styled design (SASS ONLY)
76// should be after your bootstrap imports, it uses bs4 variables, mixins, functions
77@import '~ngx-toastr/toastr-bs4-alert';
78
79// if you'd like to use it without importing all of bootstrap it requires
80@import '~bootstrap/scss/functions';
81@import '~bootstrap/scss/variables';
82@import '~bootstrap/scss/mixins';
83@import '~ngx-toastr/toastr-bs4-alert';
84```
85
86- If you are using angular-cli you can add it to your angular.json
87
88```ts
89"styles": [
90 "styles.scss",
91 "node_modules/ngx-toastr/toastr.css" // try adding '../' if you're using angular cli before 6
92]
93```
94
95**step 2:** add ToastrModule to app NgModule, make sure you have BrowserAnimationsModule as well
96
97```typescript
98import { CommonModule } from '@angular/common';
99import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
100
101import { ToastrModule } from 'ngx-toastr';
102
103@NgModule({
104 imports: [
105 CommonModule,
106 BrowserAnimationsModule, // required animations module
107 ToastrModule.forRoot(), // ToastrModule added
108 ],
109 bootstrap: [App],
110 declarations: [App],
111})
112class MainModule {}
113```
114
115## Use
116
117```typescript
118import { ToastrService } from 'ngx-toastr';
119
120@Component({...})
121export class YourComponent {
122 constructor(private toastr: ToastrService) {}
123
124 showSuccess() {
125 this.toastr.success('Hello world!', 'Toastr fun!');
126 }
127}
128```
129
130## Options
131
132There are **individual options** and **global options**.
133
134### Individual Options
135
136Passed to `ToastrService.success/error/warning/info/show()`
137
138| Option | Type | Default | Description
139| ----------------- | ------------------------------ | ------------------------------ | ------------------------------------------------- |
140| toastComponent | Component | Toast | Angular component that will be used |
141| closeButton | boolean | false | Show close button |
142| timeOut | number | 5000 | Time to live in milliseconds |
143| extendedTimeOut | number | 1000 | Time to close after a user hovers over toast |
144| disableTimeOut | `boolean \| 'timeOut' \| 'extendedTimeOut'` | false | Disable both timeOut and extendedTimeOut when set to `true`. Allows specifying which timeOut to disable, either: `timeOut` or `extendedTimeOut` |
145| easing | string | 'ease-in' | Toast component easing |
146| easeTime | string \| number | 300 | Time spent easing |
147| enableHtml | boolean | false | Allow html in message |
148| progressBar | boolean | false | Show progress bar |
149| progressAnimation | `'decreasing' \| 'increasing'` | 'decreasing' | Changes the animation of the progress bar. |
150| toastClass | string | 'ngx-toastr' | Class on toast |
151| positionClass | string | 'toast-top-right' | Class on toast container |
152| titleClass | string | 'toast-title' | Class inside toast on title |
153| messageClass | string | 'toast-message' | Class inside toast on message |
154| tapToDismiss | boolean | true | Close on click |
155| onActivateTick | boolean | false | Fires `changeDetectorRef.detectChanges()` when activated. Helps show toast from asynchronous events outside of Angular's change detection |
156
157#### Setting Individual Options
158
159success, error, info, warning take `(message, title, ToastConfig)` pass an
160options object to replace any default option.
161
162```typescript
163this.toastrService.error('everything is broken', 'Major Error', {
164 timeOut: 3000,
165});
166```
167
168### Global Options
169
170All [individual options](#individual-options) can be overridden in the global
171options to affect all toasts. In addition, global options include the following
172options:
173
174| Option | Type | Default | Description |
175| ----------------------- | ------- | ---------------------------------- | ------------------------------------------------------------------ |
176| maxOpened | number | 0 | Max toasts opened. Toasts will be queued. 0 is unlimited |
177| autoDismiss | boolean | false | Dismiss current toast when max is reached |
178| iconClasses | object | [see below](#iconclasses-defaults) | Classes used on toastr service methods |
179| newestOnTop | boolean | true | New toast placement |
180| preventDuplicates | boolean | false | Block duplicate messages |
181| countDuplicates | boolean | false | Displays a duplicates counter (preventDuplicates must be true). Toast must have a title and duplicate message |
182| resetTimeoutOnDuplicate | boolean | false | Reset toast timeout on duplicate (preventDuplicates must be true) |
183| includeTitleDuplicates | boolean | false | Include the title of a toast when checking for duplicates (by default only message is compared) |
184
185##### iconClasses defaults
186
187```typescript
188iconClasses = {
189 error: 'toast-error',
190 info: 'toast-info',
191 success: 'toast-success',
192 warning: 'toast-warning',
193};
194```
195
196#### Setting Global Options
197
198Pass values to `ToastrModule.forRoot()`
199
200```typescript
201// root app NgModule
202imports: [
203 ToastrModule.forRoot({
204 timeOut: 10000,
205 positionClass: 'toast-bottom-right',
206 preventDuplicates: true,
207 }),
208],
209```
210
211### Toastr Service methods return:
212
213```typescript
214export interface ActiveToast {
215 /** Your Toast ID. Use this to close it individually */
216 toastId: number;
217 /** the title of your toast. Stored to prevent duplicates if includeTitleDuplicates set */
218 title: string;
219 /** the message of your toast. Stored to prevent duplicates */
220 message: string;
221 /** a reference to the component see portal.ts */
222 portal: ComponentRef<any>;
223 /** a reference to your toast */
224 toastRef: ToastRef<any>;
225 /** triggered when toast is active */
226 onShown: Observable<any>;
227 /** triggered when toast is destroyed */
228 onHidden: Observable<any>;
229 /** triggered on toast click */
230 onTap: Observable<any>;
231 /** available for your use in custom toast */
232 onAction: Observable<any>;
233}
234```
235
236### Put toasts in your own container
237
238Put toasts in a specific div inside your application. This should probably be
239somewhere that doesn't get deleted. Add `ToastContainerModule` to the ngModule
240where you need the directive available.
241
242```typescript
243import { BrowserModule } from '@angular/platform-browser';
244import { NgModule } from '@angular/core';
245import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
246
247import { ToastrModule, ToastContainerModule } from 'ngx-toastr';
248
249import { AppComponent } from './app.component';
250
251@NgModule({
252 declarations: [AppComponent],
253 imports: [
254 BrowserModule,
255 BrowserAnimationsModule,
256
257 ToastrModule.forRoot({ positionClass: 'inline' }),
258 ToastContainerModule,
259 ],
260 providers: [],
261 bootstrap: [AppComponent],
262})
263export class AppModule {}
264```
265
266Add a div with `toastContainer` directive on it.
267
268```typescript
269import { Component, OnInit, ViewChild } from '@angular/core';
270
271import { ToastContainerDirective, ToastrService } from 'ngx-toastr';
272
273@Component({
274 selector: 'app-root',
275 template: `
276 <h1><a (click)="onClick()">Click</a></h1>
277 <div toastContainer></div>
278 `,
279})
280export class AppComponent implements OnInit {
281 @ViewChild(ToastContainerDirective, { static: true })
282 toastContainer: ToastContainerDirective;
283
284 constructor(private toastrService: ToastrService) {}
285 ngOnInit() {
286 this.toastrService.overlayContainer = this.toastContainer;
287 }
288 onClick() {
289 this.toastrService.success('in div');
290 }
291}
292```
293
294## Functions
295
296##### Clear
297
298Remove all or a single toast by optional id
299
300```ts
301toastrService.clear(toastId?: number);
302```
303
304##### Remove
305
306Remove and destroy a single toast by id
307
308```
309toastrService.remove(toastId: number);
310```
311
312## SystemJS
313
314If you are using SystemJS, you should also adjust your configuration to point to
315the UMD bundle.
316
317In your SystemJS config file, `map` needs to tell the System loader where to
318look for `ngx-toastr`:
319
320```js
321map: {
322 'ngx-toastr': 'node_modules/ngx-toastr/bundles/ngx-toastr.umd.min.js',
323}
324```
325
326## Setup Without Animations
327
328If you do not want to include `@angular/animations` in your project you can
329override the default toast component in the global config to use
330`ToastNoAnimation` instead of the default one.
331
332In your main module (ex: `app.module.ts`)
333
334```typescript
335import { ToastrModule, ToastNoAnimation, ToastNoAnimationModule } from 'ngx-toastr';
336
337@NgModule({
338 imports: [
339 // ...
340
341 // BrowserAnimationsModule no longer required
342 ToastNoAnimationModule.forRoot(),
343 ],
344 // ...
345})
346class AppModule {}
347```
348
349That's it! Animations are no longer required.
350
351## Using A Custom Toast
352
353Create your toast component extending Toast see the demo's pink toast for an example
354https://github.com/scttcper/ngx-toastr/blob/master/src/app/pink.toast.ts
355
356```typescript
357import { ToastrModule } from 'ngx-toastr';
358
359@NgModule({
360 imports: [
361 ToastrModule.forRoot({
362 toastComponent: YourToastComponent, // added custom toast!
363 }),
364 ],
365 entryComponents: [YourToastComponent], // add!
366 bootstrap: [App],
367 declarations: [App, YourToastComponent], // add!
368})
369class AppModule {}
370```
371
372## FAQ
373
3741. ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it
375 was checked\
376 When opening a toast inside an angular lifecycle wrap it in setTimeout
377
378```typescript
379ngOnInit() {
380 setTimeout(() => this.toastr.success('sup'))
381}
382```
383
3842. Change default icons (check, warning sign, etc)\
385 Overwrite the css background-image https://github.com/scttcper/ngx-toastr/blob/master/src/lib/toastr.css
3863. How do I use this in an ErrorHandler? See:
387 https://github.com/scttcper/ngx-toastr/issues/179
3884. How can I translate messages See:
389 https://github.com/scttcper/ngx-toastr/issues/201
3905. How to handle toastr click/tap action?
391
392```ts
393showToaster() {
394 this.toastr.success('Hello world!', 'Toastr fun!')
395 .onTap
396 .pipe(take(1))
397 .subscribe(() => this.toasterClickedHandler());
398}
399
400toasterClickedHandler() {
401 console.log('Toastr clicked');
402}
403```
404
405## Previous Works
406
407[toastr](https://github.com/CodeSeven/toastr) original toastr\
408[angular-toastr](https://github.com/Foxandxss/angular-toastr) AngularJS toastr\
409[notyf](https://github.com/caroso1222/notyf) notyf (css)
410
411## License
412
413MIT
414
415---
416
417> GitHub [@scttcper](https://github.com/scttcper) &nbsp;&middot;&nbsp;
418> Twitter [@scttcper](https://twitter.com/scttcper)