Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(animations) #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions src/toast.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,69 @@
// This project is licensed under the terms of the MIT license.
// https://github.com/akserg/ng2-toasty

import { Component, Input, Output, EventEmitter } from '@angular/core';
import { Component, Input, Output, EventEmitter, trigger, state, style, transition, animate } from '@angular/core';

import { ToastData } from './toasty.service';

/**
* A Toast component shows message with title and close button.
*/
@Component({
selector: 'ng2-toast',
template: `
<div class="toast" [ngClass]="[toast.type, toast.theme]">
selector: 'ng2-toast',
animations: [
trigger('enterLeave', [

// Enter from right
state('fromRight', style({opacity: 1, transform: 'translateX(0)'})),
transition('* => fromRight', [
style({opacity: 0, transform: 'translateX(5%)'}),
animate('400ms ease-in-out')
]),
state('fromRightOut', style({opacity: 0, transform: 'translateX(-5%)'})),
transition('fromRight => fromRightOut', [
style({opacity: 1, transform: 'translateX(0)'}),
animate('300ms ease-in-out')
]),

// Enter from left
state('fromLeft', style({opacity: 1, transform: 'translateX(0)'})),
transition('* => fromLeft', [
style({opacity: 0, transform: 'translateX(-5%)'}),
animate('400ms ease-in-out')
]),
state('fromLeftOut', style({opacity: 0, transform: 'translateX(5%)'})),
transition('fromLeft => fromLeftOut', [
style({opacity: 1, transform: 'translateX(0)'}),
animate('300ms ease-in-out')
]),

// Rotate
state('scale', style({opacity: 1, transform: 'scale(1)'})),
transition('* => scale', [
style({opacity: 0, transform: 'scale(0)'}),
animate('400ms ease-in-out')
]),
state('scaleOut', style({opacity: 0, transform: 'scale(0)'})),
transition('scale => scaleOut', [
style({opacity: 1, transform: 'scale(1)'}),
animate('400ms ease-in-out')
]),

// Scale
state('rotate', style({opacity: 1, transform: 'rotate(0deg)'})),
transition('* => rotate', [
style({opacity: 0, transform: 'rotate(5deg)'}),
animate('400ms ease-in-out')
]),
state('rotateOut', style({opacity: 0, transform: 'rotate(-5deg)'})),
transition('rotate => rotateOut', [
style({opacity: 1, transform: 'rotate(0deg)'}),
animate('400ms ease-in-out')
])
])
],
template: `
<div class="toast" [ngClass]="[toast.type, toast.theme]" [@enterLeave]="toast.animate">
<div *ngIf="toast.showClose" class="close-button" (click)="close($event)"></div>
<div *ngIf="toast.title || toast.msg" class="toast-text">
<span *ngIf="toast.title" class="toast-title" [innerHTML]="toast.title | safeHtml"></span>
Expand Down
26 changes: 24 additions & 2 deletions src/toasty.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,18 @@ export class ToastyComponent implements OnInit {
if (value.onRemove && isFunction(value.onRemove)) {
value.onRemove.call(this, value);
}
this.toasts.splice(key, 1);
if (value.animate === 'none') {
this.toasts.splice(key, 1);
} else {
value.animate += 'Out';
setTimeout(() => {
// Finding index again because of async operation
let index = this.toasts.findIndex((toast) => toast.id === value.id);
if (index > -1) {
this.toasts.splice(index, 1);
}
}, 400);
}
}
});
} else {
Expand All @@ -136,12 +147,23 @@ export class ToastyComponent implements OnInit {
* Clear all toasts
*/
clearAll() {
let isContainAnimate = false;
this.toasts.forEach((value: any, key: number) => {
if (value.onRemove && isFunction(value.onRemove)) {
value.onRemove.call(this, value);
}
if (value.animate !== 'none') {
isContainAnimate = true;
value.animate += 'Out';
}
});
this.toasts = [];
if (!isContainAnimate) {
this.toasts = [];
} else {
setTimeout(() => {
this.toasts = [];
}, 400);
}
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/toasty.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class ToastOptions {
showClose?: boolean;
theme?: string;
timeout?: number;
animate?: string;
onAdd?: Function;
onRemove?: Function;
}
Expand All @@ -34,6 +35,7 @@ export class ToastData {
type: string;
theme: string;
timeout: number;
animate: string;
onAdd: Function;
onRemove: Function;
onClick: Function;
Expand All @@ -57,6 +59,9 @@ export class ToastyConfig {
// How long (in miliseconds) the toasty shows before it's removed. Set to null/0 to turn off.
timeout: number = 5000;

// Enter and leave animate of each toast
animate: 'fromRight' | 'fromLeft' | 'scale' | 'rotate' | 'none' = 'fromRight';

// What theme to use
theme: 'default' | 'material' | 'bootstrap' = 'default';
}
Expand All @@ -82,6 +87,8 @@ export function toastyServiceFactory(config: ToastyConfig): ToastyService {
export class ToastyService {
// Allowed THEMES
static THEMES: Array<string> = ['default', 'material', 'bootstrap'];
// Allowed ANIMATES
static ANIMATES: Array<string> = ['fromRight', 'fromLeft', 'scale', 'rotate', 'none'];
// Init the counter
uniqueCounter: number = 0;
// ToastData event emitter
Expand Down Expand Up @@ -185,13 +192,22 @@ export class ToastyService {
theme = this.config.theme;
}

// If we have a animate set, make sure it's a valid one
let animate: string;
if (toastyOptions.animate) {
animate = ToastyService.ANIMATES.indexOf(toastyOptions.animate) > -1 ? toastyOptions.animate : this.config.animate;
} else {
animate = this.config.animate;
}

let toast: ToastData = <ToastData>{
id : this.uniqueCounter,
title : toastyOptions.title,
msg : toastyOptions.msg,
showClose: showClose,
type : 'toasty-type-' + type,
theme : 'toasty-theme-' + theme,
animate : animate,
onAdd : toastyOptions.onAdd && isFunction(toastyOptions.onAdd) ? toastyOptions.onAdd : null,
onRemove : toastyOptions.onRemove && isFunction(toastyOptions.onRemove) ? toastyOptions.onRemove : null
};
Expand Down
1 change: 1 addition & 0 deletions tests/toast.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('ToastComponent', () => {
type: 'toasty-type-default',
theme:'toasty-theme-default',
timeout: null,
animate: 'none',
onAdd: null,
onRemove:null,
onClick:null
Expand Down
2 changes: 2 additions & 0 deletions tests/toasty.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('ToastyComponent', () => {
type: 'toasty-type-default',
theme:'toasty-theme-default',
timeout: null,
animate: 'none',
onAdd: null,
onRemove:null,
onClick:null
Expand All @@ -31,6 +32,7 @@ describe('ToastyComponent', () => {
type: 'toasty-type-default',
theme:'toasty-theme-default',
timeout: null,
animate: 'none',
onAdd: null,
onRemove:null,
onClick:null
Expand Down