Gathering detailed insights and metrics for ngx-toastr
Gathering detailed insights and metrics for ngx-toastr
Gathering detailed insights and metrics for ngx-toastr
Gathering detailed insights and metrics for ngx-toastr
npm install ngx-toastr
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2,521 Stars
975 Commits
358 Forks
30 Watching
4 Branches
53 Contributors
Updated on 25 Nov 2024
Minified
Minified + Gzipped
TypeScript (52.26%)
HTML (19.21%)
SCSS (14.78%)
CSS (12.84%)
JavaScript (0.92%)
Cumulative downloads
Total Downloads
Last day
-2.8%
80,554
Compared to previous day
Last week
3%
438,144
Compared to previous week
Last month
5%
1,830,980
Compared to previous month
Last year
17.1%
19,777,850
Compared to previous year
1
3
DEMO: https://ngx-toastr.vercel.app
ViewContainerRef
*ngFor
. Fewer dirty checks and higher performance.Latest version available for each version of Angular
ngx-toastr | Angular |
---|---|
13.2.1 | 10.x 11.x |
14.3.0 | 12.x 13.x |
15.2.2 | 14.x. |
16.2.0 | 15.x |
17.0.2 | 16.x |
current | >= 17.x |
1npm install ngx-toastr --save
@angular/animations
package is a required dependency for the default toast
1npm install @angular/animations --save
Don't want to use @angular/animations
? See
Setup Without Animations.
step 1: add css
1// regular style toast 2@import 'ngx-toastr/toastr'; 3 4// bootstrap style toast 5// or import a bootstrap 4 alert styled design (SASS ONLY) 6// should be after your bootstrap imports, it uses bs4 variables, mixins, functions 7@import 'ngx-toastr/toastr-bs4-alert'; 8 9// if you'd like to use it without importing all of bootstrap it requires 10@import 'bootstrap/scss/functions'; 11@import 'bootstrap/scss/variables'; 12@import 'bootstrap/scss/mixins'; 13// bootstrap 4 14@import 'ngx-toastr/toastr-bs4-alert'; 15// boostrap 5 16@import 'ngx-toastr/toastr-bs5-alert';
1"styles": [ 2 "styles.scss", 3 "node_modules/ngx-toastr/toastr.css" // try adding '../' if you're using angular cli before 6 4]
step 2: add ToastrModule
to app NgModule
, or provideToastr
to providers, make sure you have BrowserAnimationsModule
(or provideAnimations
) as well.
1import { CommonModule } from '@angular/common'; 2import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; 3 4import { ToastrModule } from 'ngx-toastr'; 5 6@NgModule({ 7 imports: [ 8 CommonModule, 9 BrowserAnimationsModule, // required animations module 10 ToastrModule.forRoot(), // ToastrModule added 11 ], 12 bootstrap: [App], 13 declarations: [App], 14}) 15class MainModule {}
1import { AppComponent } from './src/app.component'; 2import { provideAnimations } from '@angular/platform-browser/animations'; 3 4import { provideToastr } from 'ngx-toastr'; 5 6bootstrapApplication(AppComponent, { 7 providers: [ 8 provideAnimations(), // required animations providers 9 provideToastr(), // Toastr providers 10 ] 11});
1import { ToastrService } from 'ngx-toastr'; 2 3@Component({...}) 4export class YourComponent { 5 constructor(private toastr: ToastrService) {} 6 7 showSuccess() { 8 this.toastr.success('Hello world!', 'Toastr fun!'); 9 } 10}
There are individual options and global options.
Passed to ToastrService.success/error/warning/info/show()
Option | Type | Default | Description |
---|---|---|---|
toastComponent | Component | Toast | Angular component that will be used |
closeButton | boolean | false | Show close button |
timeOut | number | 5000 | Time to live in milliseconds |
extendedTimeOut | number | 1000 | Time to close after a user hovers over toast |
disableTimeOut | boolean | 'timeOut' | 'extendedTimeOut' | false | Disable both timeOut and extendedTimeOut when set to true . Allows specifying which timeOut to disable, either: timeOut or extendedTimeOut |
easing | string | 'ease-in' | Toast component easing |
easeTime | string | number | 300 | Time spent easing |
enableHtml | boolean | false | Allow html in message |
newestOnTop | boolean | true | New toast placement |
progressBar | boolean | false | Show progress bar |
progressAnimation | 'decreasing' | 'increasing' | 'decreasing' | Changes the animation of the progress bar. |
toastClass | string | 'ngx-toastr' | CSS class(es) for toast |
positionClass | string | 'toast-top-right' | CSS class(es) for toast container |
titleClass | string | 'toast-title' | CSS class(es) for inside toast on title |
messageClass | string | 'toast-message' | CSS class(es) for inside toast on message |
tapToDismiss | boolean | true | Close on click |
onActivateTick | boolean | false | Fires changeDetectorRef.detectChanges() when activated. Helps show toast from asynchronous events outside of Angular's change detection |
success, error, info, warning take (message, title, ToastConfig)
pass an
options object to replace any default option.
1this.toastrService.error('everything is broken', 'Major Error', { 2 timeOut: 3000, 3});
All individual options can be overridden in the global options to affect all toasts. In addition, global options include the following options:
Option | Type | Default | Description |
---|---|---|---|
maxOpened | number | 0 | Max toasts opened. Toasts will be queued. 0 is unlimited |
autoDismiss | boolean | false | Dismiss current toast when max is reached |
iconClasses | object | see below | Classes used on toastr service methods |
preventDuplicates | boolean | false | Block duplicate messages |
countDuplicates | boolean | false | Displays a duplicates counter (preventDuplicates must be true). Toast must have a title and duplicate message |
resetTimeoutOnDuplicate | boolean | false | Reset toast timeout on duplicate (preventDuplicates must be true) |
includeTitleDuplicates | boolean | false | Include the title of a toast when checking for duplicates (by default only message is compared) |
1iconClasses = { 2 error: 'toast-error', 3 info: 'toast-info', 4 success: 'toast-success', 5 warning: 'toast-warning', 6};
Pass values to ToastrModule.forRoot()
or provideToastr()
to set global options.
1// root app NgModule 2imports: [ 3 ToastrModule.forRoot({ 4 timeOut: 10000, 5 positionClass: 'toast-bottom-right', 6 preventDuplicates: true, 7 }), 8],
1import { AppComponent } from './src/app.component'; 2import { provideAnimations } from '@angular/platform-browser/animations'; 3 4import { provideToastr } from 'ngx-toastr'; 5 6bootstrapApplication(AppComponent, { 7 providers: [ 8 provideToastr({ 9 timeOut: 10000, 10 positionClass: 'toast-bottom-right', 11 preventDuplicates: true, 12 }), 13 ] 14});
1export interface ActiveToast { 2 /** Your Toast ID. Use this to close it individually */ 3 toastId: number; 4 /** the title of your toast. Stored to prevent duplicates if includeTitleDuplicates set */ 5 title: string; 6 /** the message of your toast. Stored to prevent duplicates */ 7 message: string; 8 /** a reference to the component see portal.ts */ 9 portal: ComponentRef<any>; 10 /** a reference to your toast */ 11 toastRef: ToastRef<any>; 12 /** triggered when toast is active */ 13 onShown: Observable<any>; 14 /** triggered when toast is destroyed */ 15 onHidden: Observable<any>; 16 /** triggered on toast click */ 17 onTap: Observable<any>; 18 /** available for your use in custom toast */ 19 onAction: Observable<any>; 20}
Put toasts in a specific div inside your application. This should probably be
somewhere that doesn't get deleted. Add ToastContainerModule
to the ngModule
where you need the directive available. Make sure that your container has
an aria-live="polite"
attribute, so that any time a toast is injected into
the container it is announced by screen readers.
1import { BrowserModule } from '@angular/platform-browser'; 2import { NgModule } from '@angular/core'; 3import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; 4 5import { ToastrModule, ToastContainerModule } from 'ngx-toastr'; 6 7import { AppComponent } from './app.component'; 8 9@NgModule({ 10 declarations: [AppComponent], 11 imports: [ 12 BrowserModule, 13 BrowserAnimationsModule, 14 15 ToastrModule.forRoot({ positionClass: 'inline' }), 16 ToastContainerModule, 17 ], 18 providers: [], 19 bootstrap: [AppComponent], 20}) 21export class AppModule {}
Add a div with toastContainer
directive on it.
1import { Component, OnInit, ViewChild } from '@angular/core'; 2 3import { ToastContainerDirective, ToastrService } from 'ngx-toastr'; 4 5@Component({ 6 selector: 'app-root', 7 template: ` 8 <h1><a (click)="onClick()">Click</a></h1> 9 <div aria-live="polite" toastContainer></div> 10 `, 11}) 12export class AppComponent implements OnInit { 13 @ViewChild(ToastContainerDirective, { static: true }) 14 toastContainer: ToastContainerDirective; 15 16 constructor(private toastrService: ToastrService) {} 17 ngOnInit() { 18 this.toastrService.overlayContainer = this.toastContainer; 19 } 20 onClick() { 21 this.toastrService.success('in div'); 22 } 23}
Remove all or a single toast by optional id
1toastrService.clear(toastId?: number);
Remove and destroy a single toast by id
toastrService.remove(toastId: number);
If you are using SystemJS, you should also adjust your configuration to point to the UMD bundle.
In your SystemJS config file, map
needs to tell the System loader where to
look for ngx-toastr
:
1map: { 2 'ngx-toastr': 'node_modules/ngx-toastr/bundles/ngx-toastr.umd.min.js', 3}
If you do not want to include @angular/animations
in your project you can
override the default toast component in the global config to use
ToastNoAnimation
instead of the default one.
In your main module (ex: app.module.ts
)
1import { ToastrModule, ToastNoAnimation, ToastNoAnimationModule } from 'ngx-toastr'; 2 3@NgModule({ 4 imports: [ 5 // ... 6 7 // BrowserAnimationsModule no longer required 8 ToastNoAnimationModule.forRoot(), 9 ], 10 // ... 11}) 12class AppModule {}
That's it! Animations are no longer required.
Create your toast component extending Toast see the demo's pink toast for an example https://github.com/scttcper/ngx-toastr/blob/master/src/app/pink.toast.ts
1import { ToastrModule } from 'ngx-toastr'; 2 3@NgModule({ 4 imports: [ 5 ToastrModule.forRoot({ 6 toastComponent: YourToastComponent, // added custom toast! 7 }), 8 ], 9 bootstrap: [App], 10 declarations: [App, YourToastComponent], // add! 11}) 12class AppModule {}
1ngOnInit() { 2 setTimeout(() => this.toastr.success('sup')) 3}
1showToaster() { 2 this.toastr.success('Hello world!', 'Toastr fun!') 3 .onTap 4 .pipe(take(1)) 5 .subscribe(() => this.toasterClickedHandler()); 6} 7 8toasterClickedHandler() { 9 console.log('Toastr clicked'); 10}
See: https://github.com/scttcper/ngx-toastr/issues/594.1toastClass: 'yourclass ngx-toastr'
toastr original toastr
angular-toastr AngularJS toastr
notyf notyf (css)
MIT
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
packaging workflow detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 5
Details
Reason
Found 14/30 approved changesets -- score normalized to 4
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
1 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
16 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More