Example Custom Toasts:
+ diff --git a/src/app/app.component.spec.ts b/src/app/app.component.spec.ts index 11ded8d3..6db3f07b 100644 --- a/src/app/app.component.spec.ts +++ b/src/app/app.component.spec.ts @@ -1,18 +1,18 @@ /* tslint:disable:no-use-before-declare */ -import { TestBed, async } from '@angular/core/testing'; +import { CommonModule } from '@angular/common'; import { NgModule } from '@angular/core'; +import { async, TestBed } from '@angular/core/testing'; import { FormsModule } from '@angular/forms'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; -import { CommonModule } from '@angular/common'; -import { ToastrModule, ActiveToast } from '../lib/public_api'; +import { ActiveToast, ToastrModule } from '../lib/public_api'; import { AppComponent } from './app.component'; -import { PinkToast } from './pink.toast'; -import { NotyfToast } from './notyf.toast'; import { FooterComponent } from './footer/footer.component'; -import { HeaderComponent } from './header/header.component'; import { GithubLinkComponent } from './github-link/github-link.component'; +import { HeaderComponent } from './header/header.component'; +import { NotyfToast } from './notyf.toast'; +import { PinkToast } from './pink.toast'; describe('AppComponent', () => { beforeEach(() => { @@ -47,7 +47,7 @@ describe('AppComponent', () => { const app = fixture.debugElement.componentInstance; const opened: ActiveToast = app.openToast(); expect(opened).toBeDefined(); - opened.onShown!.toPromise().then(() => { + opened.onShown.toPromise().then(() => { done(); }); }); @@ -56,7 +56,7 @@ describe('AppComponent', () => { const app = fixture.debugElement.componentInstance; const opened: ActiveToast = app.openToast(); expect(opened.portal).toBeDefined(); - opened.onHidden!.toPromise().then(() => { + opened.onHidden.toPromise().then(() => { done(); }); }); @@ -65,19 +65,19 @@ describe('AppComponent', () => { const app = fixture.debugElement.componentInstance; const opened: ActiveToast = app.openToast(); expect(opened.portal).toBeDefined(); - opened.onTap!.toPromise() + opened.onTap.toPromise() .then(() => { done(); }); - opened.portal!.instance.tapToast(); + opened.portal.instance.tapToast(); }); it('should extend life on mouseover and exit', (done) => { const fixture = TestBed.createComponent(AppComponent); const app = fixture.debugElement.componentInstance; const opened: ActiveToast = app.openToast(); - opened.portal!.instance.stickAround(); - opened.portal!.instance.delayedHideToast(); - expect(opened.portal!.instance.options.timeOut).toBe(1000); + opened.portal.instance.stickAround(); + opened.portal.instance.delayedHideToast(); + expect(opened.portal.instance.options.timeOut).toBe(1000); done(); }); it('should keep on mouse exit with extended timeout 0', (done) => { @@ -85,16 +85,16 @@ describe('AppComponent', () => { const app = fixture.debugElement.componentInstance; app.options.extendedTimeOut = 0; const opened: ActiveToast = app.openToast(); - opened.portal!.instance.stickAround(); - opened.portal!.instance.delayedHideToast(); - expect(opened.portal!.instance.options.timeOut).toBe(0); + opened.portal.instance.stickAround(); + opened.portal.instance.delayedHideToast(); + expect(opened.portal.instance.options.timeOut).toBe(0); done(); }); it('should trigger onShown for openPinkToast', (done) => { const fixture = TestBed.createComponent(AppComponent); const app = fixture.debugElement.componentInstance; const opened: ActiveToast = app.openPinkToast(); - opened.onShown!.toPromise().then(() => { + opened.onShown.toPromise().then(() => { done(); }); }); @@ -102,7 +102,7 @@ describe('AppComponent', () => { const fixture = TestBed.createComponent(AppComponent); const app = fixture.debugElement.componentInstance; const opened: ActiveToast = app.openPinkToast(); - opened.onHidden!.toPromise().then(() => { + opened.onHidden.toPromise().then(() => { done(); }); }); @@ -110,7 +110,7 @@ describe('AppComponent', () => { const fixture = TestBed.createComponent(AppComponent); const app = fixture.debugElement.componentInstance; const opened: ActiveToast = app.openNotyf(); - opened.onShown!.toPromise().then(() => { + opened.onShown.toPromise().then(() => { done(); }); }); @@ -118,7 +118,7 @@ describe('AppComponent', () => { const fixture = TestBed.createComponent(AppComponent); const app = fixture.debugElement.componentInstance; const opened: ActiveToast = app.openNotyf(); - opened.onHidden!.toPromise().then(() => { + opened.onHidden.toPromise().then(() => { done(); }); }); diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 5d1a96fd..4d2cd812 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -2,11 +2,15 @@ import { Component, VERSION } from '@angular/core'; import { Title } from '@angular/platform-browser'; import { cloneDeep, random } from 'lodash-es'; -import { GlobalConfig, ToastrService } from '../lib/public_api'; import json from '../lib/package.json'; +import { + GlobalConfig, + ToastrService, + ToastNoAnimation, +} from '../lib/public_api'; -import { PinkToast } from './pink.toast'; import { NotyfToast } from './notyf.toast'; +import { PinkToast } from './pink.toast'; interface Quote { title?: string; @@ -16,17 +20,17 @@ interface Quote { const quotes: Quote[] = [ { title: 'Title', - message: 'Message' + message: 'Message', }, { title: '😃', - message: 'Supports Emoji' + message: 'Supports Emoji', }, { message: 'My name is Inigo Montoya. You killed my father. Prepare to die!', }, { - message: 'Titles are not always needed' + message: 'Titles are not always needed', }, { title: 'Title only 👊', @@ -50,21 +54,17 @@ export class AppComponent { version = VERSION; private lastInserted: number[] = []; - constructor( - public toastr: ToastrService, - private t: Title - ) { + constructor(public toastr: ToastrService, title: Title) { // sync options to toastrservice // this sets the options in the demo this.options = this.toastr.toastrConfig; - const current = t.getTitle(); + const current = title.getTitle(); // fix for tests if (json) { - t.setTitle(`${current} ${json.version}`); + title.setTitle(`${current}: v${json.version}`); } } - openToast() { - // Clone current config so it doesn't change when ngModel updates + getMessage() { let m: string | undefined = this.message; let t: string | undefined = this.title; if (!this.title.length && !this.message.length) { @@ -72,8 +72,36 @@ export class AppComponent { m = randomMessage.message; t = randomMessage.title; } + return { + message: m, + title: t, + }; + } + openToast() { + const { message, title } = this.getMessage(); + // Clone current config so it doesn't change when ngModel updates + const opt = cloneDeep(this.options); + const inserted = this.toastr.show( + message, + title, + opt, + this.options.iconClasses[this.type], + ); + if (inserted) { + this.lastInserted.push(inserted.toastId); + } + return inserted; + } + openToastNoAnimation() { + const { message, title } = this.getMessage(); const opt = cloneDeep(this.options); - const inserted = this.toastr[this.type](m, t, opt); + opt.toastComponent = ToastNoAnimation; + const inserted = this.toastr.show( + message, + title, + opt, + this.options.iconClasses[this.type], + ); if (inserted) { this.lastInserted.push(inserted.toastId); } @@ -83,14 +111,8 @@ export class AppComponent { const opt = cloneDeep(this.options); opt.toastComponent = PinkToast; opt.toastClass = 'pinktoast'; - let m: string | undefined = this.message; - let t: string | undefined = this.title; - if (!this.title.length && !this.message.length) { - const randomMessage = quotes[random(0, quotes.length - 1)]; - m = randomMessage.message || ''; - t = randomMessage.title; - } - const inserted = this.toastr.show(m, t, opt); + const { message, title } = this.getMessage(); + const inserted = this.toastr.show(message, title, opt); if (inserted && inserted.toastId) { this.lastInserted.push(inserted.toastId); } @@ -102,15 +124,8 @@ export class AppComponent { opt.toastClass = 'notyf confirm'; opt.positionClass = 'notyf-container'; this.options.newestOnTop = false; - let m: string | undefined = this.message; - let t: string | undefined = this.title; - if (!this.title.length && !this.message.length) { - const randomMessage = quotes[random(0, quotes.length - 1)]; - m = randomMessage.message; - t = randomMessage.title; - } - m = m || 'Success'; - const inserted = this.toastr.show(m, t, opt); + const { message, title } = this.getMessage(); + const inserted = this.toastr.show(message || 'Success', title, opt); if (inserted && inserted.toastId) { this.lastInserted.push(inserted.toastId); } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 453f598d..c832cb3b 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -1,17 +1,17 @@ -import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; +import { BrowserModule } from '@angular/platform-browser'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; -import { ToastrModule } from '../lib/public_api'; +import { ToastrModule, ToastNoAnimationModule } from '../lib/public_api'; import { AppComponent } from './app.component'; -import { PinkToast } from './pink.toast'; -import { NotyfToast } from './notyf.toast'; import { FooterComponent } from './footer/footer.component'; -import { HeaderComponent } from './header/header.component'; import { GithubLinkComponent } from './github-link/github-link.component'; -// import { ToastContainerModule } from '../lib/toast-directive'; +import { HeaderComponent } from './header/header.component'; +import { NotyfToast } from './notyf.toast'; +import { PinkToast } from './pink.toast'; +// import { ToastContainerModule } from '../lib/toast.directive'; @NgModule({ declarations: [ @@ -26,6 +26,7 @@ import { GithubLinkComponent } from './github-link/github-link.component'; BrowserModule, FormsModule, BrowserAnimationsModule, + ToastNoAnimationModule, ToastrModule.forRoot(), // ToastContainerModule.forRoot(), ], diff --git a/src/app/footer/footer.component.ts b/src/app/footer/footer.component.ts index 1c46e8db..84aa6921 100644 --- a/src/app/footer/footer.component.ts +++ b/src/app/footer/footer.component.ts @@ -4,7 +4,7 @@ import { Component, OnInit, VERSION } from '@angular/core'; selector: 'app-footer', template: `Easy Toasts for Angular
`, + changeDetection: ChangeDetectionStrategy.OnPush, }) -export class HeaderComponent implements OnInit { - - constructor() { } - - ngOnInit() { - } - +export class HeaderComponent { } diff --git a/src/app/notyf.toast.ts b/src/app/notyf.toast.ts index ac658f97..822ee688 100644 --- a/src/app/notyf.toast.ts +++ b/src/app/notyf.toast.ts @@ -1,14 +1,13 @@ -/* tslint:disable:no-access-missing-member */ -import { Component, ApplicationRef } from '@angular/core'; import { - trigger, - state, - transition, animate, + keyframes, + state, style, - keyframes + transition, + trigger } from '@angular/animations'; -import { Toast, ToastPackage, ToastrService, ToastRef } from '../lib/public_api'; +import { Component } from '@angular/core'; +import { Toast, ToastrService, ToastPackage } from '../lib/public_api'; @Component({ selector: '[notyf-toast-component]', @@ -18,7 +17,7 @@ import { Toast, ToastPackage, ToastrService, ToastRef } from '../lib/public_api' - +