Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply animations to multiple components with a base component in Angular2?

In my app when a component loads from menu using routing, I wanted to use some animations. I was able to do it with an angular animation successfully. I had to apply the animation in the individual component to achieve this. I need to know whether there is a better way to apply animations to a group of components or components inheriting from a specific base class?

Currently I have applied my animation like,

import { Component, OnInit } from '@angular/core';
import { routerTransition } from "../animations/animations";

@Component({
  selector: 'app-my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.css'],
  animations: [routerTransition()],
  host: {'[@routerTransition]': ''}
})
export class BaseComponentComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

animation code,

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

export function routerTransition() {
  return slideToLeft();
}

function slideToLeft() {
  return trigger('routerTransition', [
    state('void', style({position:'fixed', width:'100%'}) ),
    state('*', style({position:'fixed', width:'100%'}) ),
    transition(':enter', [  // before 2.1: transition('void => *', [
      style({transform: 'translateX(100%)'}),
      animate('0.5s ease-in-out', style({transform: 'translateX(0%)'}))
    ]),
    transition(':leave', [  // before 2.1: transition('* => void', [
      style({transform: 'translateX(0%)'}),
      animate('0.5s ease-in-out', style({transform: 'translateX(-100%)'}))
    ])
  ]);
}

What I tried:

I tried to create a base-component, applied animation properties there, and extended the base-component in the other components needed. Animation stopped working after that.

Please let me know if we can reuse the animations without applying it on individual components.

like image 875
Sanish Joseph Avatar asked Nov 07 '22 23:11

Sanish Joseph


1 Answers

Since version 4.2, Angular supports animating multiple elements via the newer query() and stagger() features in the animation DSL.

This article explains the new features: https://www.yearofmoo.com/2017/06/new-wave-of-animation-features.html

like image 150
fr0 Avatar answered Nov 14 '22 22:11

fr0