I ended up with following factory class as shown below being used for animations[]
:-
This is used as below:
@Component({
selector: 'my-app-header',
moduleId: module.id,
templateUrl: './app-header.component.html',
styleUrls: ['./app-header.component.scss'],
animations: [
MyToolbarAnimator.createTrigger('appHeaderState', '50px', '0')
]
})
Defined MyToolbarAnimator
with a static method createTrigger
which returns AnimationTriggerMetadata
as shown below
MyToolbarAnimator
import {trigger, state, style, animate, transition, AnimationTriggerMetadata} from '@angular/animations';
export const HEADER_SHRINK_TRANSITION = '250ms cubic-bezier(0.4,0.0,0.2,1)';
export class MyToolbarAnimator {
static createTrigger(triggerName: string, initialTop: string, upTop: string): AnimationTriggerMetadata {
return trigger(triggerName, [
state('initial', style({top: initialTop})),
state('up', style({top: upTop})),
transition('initial => up, up => initial',
animate(HEADER_SHRINK_TRANSITION))
]);
}
}
UPDATE:
Or if your animation parameters are very dynamic and changes based on the component behavior use https://angular.io/api/animations/AnimationBuilder#usage-notes
This
// import the service from BrowserAnimationsModule
import {AnimationBuilder} from '@angular/animations';
// require the service as a dependency
class MyCmp {
width = 100;
constructor(private _builder: AnimationBuilder) {}
changeWidth(aWidth:number) {
this.width = aWidth;
}
makeAnimation(element: any) {
// first define a reusable animation
const myAnimation = this._builder.build([
style({ width: 0 }),
animate(1000, style({ width: `${this.width}px` }))
]);
// use the returned factory object to create a player
const player = myAnimation.create(element);
player.play();
}
}