• Stars
    star
    2,503
  • Rank 18,340 (Top 0.4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🍞 Angular Toastr
Angular Toastr

ngx-toastr


npm codecov

DEMO: https://ngx-toastr.vercel.app

Features

  • Toast Component Injection without being passed ViewContainerRef
  • No use of *ngFor. Fewer dirty checks and higher performance.
  • AoT compilation and lazy loading compatible
  • Component inheritance for custom toasts
  • SystemJS/UMD rollup bundle
  • Animations using Angular's Web Animations API
  • Output toasts to an optional target directive

Dependencies

Latest version available for each version of Angular

ngx-toastr Angular
11.3.3 8.x
12.1.0 9.x
13.2.1 10.x 11.x
14.3.0 12.x 13.x
15.2.2 14.x.
16.2.0 15.x
current >= 16.x

Install

npm install ngx-toastr --save

@angular/animations package is a required dependency for the default toast

npm install @angular/animations --save

Don't want to use @angular/animations? See Setup Without Animations.

Setup

step 1: add css

  • copy toast css to your project.
  • If you are using sass you can import the css.
// regular style toast
@import 'ngx-toastr/toastr';

// bootstrap style toast
// or import a bootstrap 4 alert styled design (SASS ONLY)
// should be after your bootstrap imports, it uses bs4 variables, mixins, functions
@import 'ngx-toastr/toastr-bs4-alert';

// if you'd like to use it without importing all of bootstrap it requires
@import 'bootstrap/scss/functions';
@import 'bootstrap/scss/variables';
@import 'bootstrap/scss/mixins';
// bootstrap 4
@import 'ngx-toastr/toastr-bs4-alert';
// boostrap 5
@import 'ngx-toastr/toastr-bs5-alert';
  • If you are using angular-cli you can add it to your angular.json
"styles": [
  "styles.scss",
  "node_modules/ngx-toastr/toastr.css" // try adding '../' if you're using angular cli before 6
]

step 2: add ToastrModule to app NgModule, or provideToastr to providers, make sure you have BrowserAnimationsModule (or provideAnimations) as well.

  • Module based
import { CommonModule } from '@angular/common';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { ToastrModule } from 'ngx-toastr';

@NgModule({
  imports: [
    CommonModule,
    BrowserAnimationsModule, // required animations module
    ToastrModule.forRoot(), // ToastrModule added
  ],
  bootstrap: [App],
  declarations: [App],
})
class MainModule {}
  • Standalone
import { AppComponent } from './src/app.component';
import { provideAnimations } from '@angular/platform-browser/animations';

import { provideToastr } from 'ngx-toastr';

bootstrapApplication(AppComponent, {
  providers: [
    provideAnimations(), // required animations providers
    provideToastr(), // Toastr providers
  ]
});

Use

import { ToastrService } from 'ngx-toastr';

@Component({...})
export class YourComponent {
  constructor(private toastr: ToastrService) {}

  showSuccess() {
    this.toastr.success('Hello world!', 'Toastr fun!');
  }
}

Options

There are individual options and global options.

Individual 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

Setting Individual Options

success, error, info, warning take (message, title, ToastConfig) pass an options object to replace any default option.

this.toastrService.error('everything is broken', 'Major Error', {
  timeOut: 3000,
});

Global Options

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)
iconClasses defaults
iconClasses = {
  error: 'toast-error',
  info: 'toast-info',
  success: 'toast-success',
  warning: 'toast-warning',
};

Setting Global Options

Pass values to ToastrModule.forRoot() or provideToastr() to set global options.

  • Module based
// root app NgModule
imports: [
  ToastrModule.forRoot({
    timeOut: 10000,
    positionClass: 'toast-bottom-right',
    preventDuplicates: true,
  }),
],
  • Standalone
import { AppComponent } from './src/app.component';
import { provideAnimations } from '@angular/platform-browser/animations';

import { provideToastr } from 'ngx-toastr';

bootstrapApplication(AppComponent, {
  providers: [
    provideToastr({
      timeOut: 10000,
      positionClass: 'toast-bottom-right',
      preventDuplicates: true,
    }), 
  ]
});

Toastr Service methods return:

export interface ActiveToast {
  /** Your Toast ID. Use this to close it individually */
  toastId: number;
  /** the title of your toast. Stored to prevent duplicates if includeTitleDuplicates set */
  title: string;
  /** the message of your toast. Stored to prevent duplicates */
  message: string;
  /** a reference to the component see portal.ts */
  portal: ComponentRef<any>;
  /** a reference to your toast */
  toastRef: ToastRef<any>;
  /** triggered when toast is active */
  onShown: Observable<any>;
  /** triggered when toast is destroyed */
  onHidden: Observable<any>;
  /** triggered on toast click */
  onTap: Observable<any>;
  /** available for your use in custom toast */
  onAction: Observable<any>;
}

Put toasts in your own container

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.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { ToastrModule, ToastContainerModule } from 'ngx-toastr';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,

    ToastrModule.forRoot({ positionClass: 'inline' }),
    ToastContainerModule,
  ],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}

Add a div with toastContainer directive on it.

import { Component, OnInit, ViewChild } from '@angular/core';

import { ToastContainerDirective, ToastrService } from 'ngx-toastr';

@Component({
  selector: 'app-root',
  template: `
    <h1><a (click)="onClick()">Click</a></h1>
    <div aria-live="polite" toastContainer></div>
  `,
})
export class AppComponent implements OnInit {
  @ViewChild(ToastContainerDirective, { static: true })
  toastContainer: ToastContainerDirective;

  constructor(private toastrService: ToastrService) {}
  ngOnInit() {
    this.toastrService.overlayContainer = this.toastContainer;
  }
  onClick() {
    this.toastrService.success('in div');
  }
}

Functions

Clear

Remove all or a single toast by optional id

toastrService.clear(toastId?: number);
Remove

Remove and destroy a single toast by id

toastrService.remove(toastId: number);

SystemJS

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:

map: {
  'ngx-toastr': 'node_modules/ngx-toastr/bundles/ngx-toastr.umd.min.js',
}

Setup Without Animations

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)

import { ToastrModule, ToastNoAnimation, ToastNoAnimationModule } from 'ngx-toastr';

@NgModule({
  imports: [
    // ...

    // BrowserAnimationsModule no longer required
    ToastNoAnimationModule.forRoot(),
  ],
  // ...
})
class AppModule {}

That's it! Animations are no longer required.

Using A Custom Toast

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

import { ToastrModule } from 'ngx-toastr';

@NgModule({
  imports: [
    ToastrModule.forRoot({
      toastComponent: YourToastComponent, // added custom toast!
    }),
  ],
  bootstrap: [App],
  declarations: [App, YourToastComponent], // add!
})
class AppModule {}

FAQ

  1. ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked
    When opening a toast inside an angular lifecycle wrap it in setTimeout
ngOnInit() {
    setTimeout(() => this.toastr.success('sup'))
}
  1. Change default icons (check, warning sign, etc)
    Overwrite the css background-image: https://github.com/scttcper/ngx-toastr/blob/master/src/lib/toastr.css.
  2. How do I use this in an ErrorHandler?
    See: #179.
  3. How can I translate messages?
    See: #201.
  4. How to handle toastr click/tap action?
    showToaster() {
      this.toastr.success('Hello world!', 'Toastr fun!')
        .onTap
        .pipe(take(1))
        .subscribe(() => this.toasterClickedHandler());
    }
    
    toasterClickedHandler() {
      console.log('Toastr clicked');
    }
  5. How to customize styling without overridding defaults?
    Add multiple CSS classes separated by a space:
    toastClass: 'yourclass ngx-toastr'
    See: #594.

Previous Works

toastr original toastr
angular-toastr AngularJS toastr
notyf notyf (css)

License

MIT


GitHub @scttcper Β Β·Β  Twitter @scttcper

More Repositories

1

tinycolor

🎨 Color manipulation and conversion
TypeScript
543
star
2

gatsby-casper

A Casper blog starter for Gatsby
TypeScript
478
star
3

ngx-emoji-mart

Customizable Slack-like emoji picker for Angular
TypeScript
448
star
4

ngx-color

🎨 Color Pickers from Sketch, Photoshop, Chrome, Github, Twitter & more
TypeScript
433
star
5

ngx-codemirror

Codemirror Wrapper for Angular
TypeScript
281
star
6

ng2-adsense

Angular Adsense Component
TypeScript
137
star
7

ngx-trend

πŸ“ˆ Simple, elegant spark lines for Angular
TypeScript
117
star
8

react-adsense

Adsense component for react
TypeScript
95
star
9

ngx-rightclick

Context Menu Service for Angular
TypeScript
81
star
10

ts-trueskill

TypeScript port of the python TrueSkill package
TypeScript
73
star
11

video-filename-parser

Scene release name parser
TypeScript
57
star
12

ngx-chartjs

Functional Chart.js wrapper for Angular
TypeScript
56
star
13

qbittorrent

qBittorrent api wrapper
TypeScript
42
star
14

ngx-csv

Angular directive to generate a CSV download in the browser
TypeScript
38
star
15

ngx-droppable

Give file dropping super-powers to any element or component
TypeScript
34
star
16

deluge

Deluge API wrapper
TypeScript
27
star
17

transmission

Transmission torrent client API wrapper
TypeScript
21
star
18

plex

Plex API client for node written in typescript
TypeScript
18
star
19

golang-template

basic golang template parsing in js
TypeScript
18
star
20

gatsby-theme-casper

TypeScript
17
star
21

koa-simple-ratelimit

Simple rate limiter for Koa.js v2 web framework
TypeScript
17
star
22

magnet-link

Parse a magnet URI into an object
TypeScript
15
star
23

ts-base32

Base32 encoder/decoder with support for multiple variants
TypeScript
13
star
24

ngx-github-buttons

GitHub Buttons in Angular
TypeScript
11
star
25

torrent-file

TypeScript torrent file parser
TypeScript
11
star
26

honbot-api

HoN 4 compatible API
TypeScript
8
star
27

react-orgchart

JavaScript
8
star
28

irc

Irc client library for node
TypeScript
7
star
29

utorrent

utorrent api wrapper
TypeScript
6
star
30

shared-torrent

shared types and interfaces between torrent clients
TypeScript
5
star
31

typescript-quickstart-lib

TypeScript
5
star
32

xm-channel-scrape

5
star
33

ts-gaussian

TypeScript model of the normal distribution
TypeScript
5
star
34

honbot-frontend

Angular frontend for HoN stats website
TypeScript
4
star
35

mac-address

Parse and manipulate MAC addresses
TypeScript
4
star
36

ngx-numbered-codeblock

Prism.js wrapper with built-in line numbers
TypeScript
3
star
37

hangry-py

python port of https://github.com/iancanderson/hangry
Python
2
star
38

url-join

TypeScript
2
star
39

eslint-config

JavaScript
1
star
40

shared-nzb

TypeScript
1
star
41

ts-wcwidth

Determine number of columns needed for a fixed-size wide-character string
TypeScript
1
star
42

gatsby-theme-casper-example

JavaScript
1
star
43

rtorrent

rtorrent api wrapper
TypeScript
1
star
44

csv-fns

TypeScript
1
star
45

version-adoption

NPM package download count by version over the last 7 days - grouped by major and minor version
TypeScript
1
star
46

eslint-config-react

JavaScript
1
star
47

eslint-config-biome

JavaScript
1
star
48

ng2-bs-dropdown

DEPRECATED: use ng-bootstrap
TypeScript
1
star
49

eslint-config-react-biome

JavaScript
1
star