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