question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular 2 RC 4 New Animations not supported in Directives

See original GitHub issue

Hi,

It seems that in current release we can add animations to components only and cannot define them as directives. I mean:

the below works

@Component({
    selector: "animate-demo",
    animations: [
        trigger('openClose', [
            state('closed, void',
                style({ height: "0px" })),
            state('open',
                style({ height: "*" })),
            transition("* => *", animate(200))
        ])
    ]
})

Whereas the below doesn’t work:

@Directive({
    selector: "animate-demo",
    animations: [
        trigger('openClose', [
            state('closed, void',
                style({ height: "0px" })),
            state('open',
                style({ height: "*" })),
            transition("* => *", animate(200))
        ])
    ]
})

And gives compilation error in visual studio:

"Argument of type ‘{ selector: string; animations: AnimationEntryMetadata[]; template: string; }’ is not assignable to parameter of type ‘{ selector?: string; inputs?: string[]; outputs?: string[]; properties?: string[]; events?: strin…’.
"

Usually we might want to have the commonly used animations such as slideDown, sliddeUp, fadeIn, fadeOut defined at one place as a directive and then in components where we want to use them we would just want to import the directive and use the animation instead of redefining animation in each component.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:67
  • Comments:33 (12 by maintainers)

github_iconTop GitHub Comments

39reactions
matskocommented, Jul 26, 2016

Even though directives are not attached to a view, they should still be able to place animations on the host element that they are attached to.

Say for example you wanted to create a directive to manipulate animate.css animations:

@Directive({
   styleUrls: ['animate.css'],
   selector: '[animateCss]',
   host: {
      '[@currentAnimation]': 'animationState'
   },
   animations: [
      trigger('currentAnimation', [
         transition('* => rotate3d', animate(1000, keyframes('rotate3d'))),
         transition('* => hinge', animate(1000, keyframes('hinge'))),
         transition('* => flipX', animate(1000, keyframes('flipX')))
      ])
   ]
})
class AnimatorMan {
   @Input("animateCss")
   public animationState;
}

(The use of using CSS classes/keyframes within animate will be possible once the CSS parser is integrated. The example above is just to show how useful it can be to make a directive that manages animations.)

16reactions
molcikcommented, Dec 9, 2016

As a workaround you can create animations.ts file like:

export class Animations: [
    public static heroState = trigger('heroState', [
      state('inactive', style({
        backgroundColor: '#eee',
        transform: 'scale(1)'
      })),
      state('active',   style({
        backgroundColor: '#cfd8dc',
        transform: 'scale(1.1)'
      })),
      transition('inactive => active', animate('100ms ease-in')),
      transition('active => inactive', animate('100ms ease-out'))
    ])
  ]
}

And then use it in components like:

import { Animations }                          from './animations';

@Component({
  selector: 'myapp-hero',
  styleUrls: ['./hero.component.css'],
  templateUrl: './heor.component.html',
  animations: [Animations.heroState]
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 2 RC 4 New Animations not supported in Directive
Animation works on a View, directives does not have a view. You need for reusing animations has a point but it will be...
Read more >
Introduction to Angular animations
Animation provides the illusion of motion: HTML elements change styling over time. Well-designed animations can make your application more fun and ...
Read more >
Animation transitions and triggers - Angular
To turn off all animations for an Angular application, place the @.disabled host binding on the topmost Angular component. src/app/app.component.ts content_copy
Read more >
angular/animations
An injectable service that produces an animation sequence programmatically within an Angular component or directive. Provided by the BrowserAnimationsModule or ...
Read more >
Browser support - Angular
The Angular CLI provides support for polyfills. If you are not using the CLI to create your projects, see Polyfill instructions for non-CLI...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found