I'm playing with the Animation API, and I'd like to create a reusable animation like say sliding in content for top level router views. I managed to get through the funky meta data syntax (which is actually pretty cool once get past the crazy idea of using metadata) to get the animations mostly working.
@Component({
//moduleId: module.id,
selector: 'album-display',
templateUrl: './albumDisplay.html',
animations: [
trigger('slideIn', [
state('*', style({
transform: 'translateX(100%)',
})),
state('in', style({
transform: 'translateX(0)',
})),
state('out', style({
transform: 'translateX(-100%)',
})),
transition('* => in', animate('600ms ease-in')),
transition('in => out', animate('600ms ease-in'))
])
]
})
export class AlbumDisplay implements OnInit {
slideInState = 'in';
...
}
And then assigning that to my top level element in the component:
<div class="container" [@slideIn]="slideInState">
So this works, but how can I make this reusable? I don't want to stick this meta data onto every view. Since this is metadata I'm not sure how you could make this reusable.
One possible way is to put animation trigger code in separate file and export it as
const
variable and use it in component as below.animations.ts
album-display.component.ts
With a class and you can extended,
For use
I hope that way can help you
Proper solution would be to have animations supported in Directives.
It is not the case, but there is a issue for that on Angular's Github: https://github.com/angular/angular/issues/9947
Hope it will be solved soon.
Router Animation Example in Angular 4
I just finished tackling router animations myself using Angular 4, here's a couple of example animations I came up with for a fade in transition and slide in/out transition.
Check out this post for more details and a live demo.
Angular 4 Slide in/out animation
Angular 4 Fade in animation
Component with transition attached
Maybe it's a bit late, but I'd still like to put an answer for a more dynamic version of the trigger.
Put animation trigger code in separate file and export it as
function
.translate.ts
so, in the Component app.component.ts
and in template app.component.html
You can customize the trigger with more input data, for example separating the transition times, and so on.