Performance: Enter and leave animations
This affects the smoothness of UI transitions and the responsiveness of user interactions during element appearance and disappearance.
Jump into concepts and practice - no test required
import { Component } from '@angular/core'; import { trigger, transition, style, animate } from '@angular/animations'; @Component({ selector: 'app-good-animation', template: `<div *ngIf="show" @fade>Content</div>`, animations: [ trigger('fade', [ transition(':enter', [style({ opacity: 0 }), animate('500ms ease-in', style({ opacity: 1 }))]), transition(':leave', [animate('500ms ease-out', style({ opacity: 0 }))]) ]) ] }) export class GoodAnimationComponent { show = true; toggle() { this.show = !this.show; } }
import { Component } from '@angular/core'; @Component({ selector: 'app-bad-animation', template: `<div *ngIf="show" class="fade">Content</div>`, styles: [`.fade { transition: opacity 0.5s; opacity: 1; }`] }) export class BadAnimationComponent { show = true; toggle() { this.show = !this.show; } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| CSS transition with *ngIf immediate removal | Removes element immediately | Multiple reflows due to layout shifts | High paint cost due to abrupt changes | [X] Bad |
| Angular animation trigger with :enter and :leave | Keeps element until animation ends | Single reflow per animation | Lower paint cost with smooth opacity changes | [OK] Good |
:enter and :leave states represent in Angular animations?:enter state triggers when an element is added to the DOM, and :leave triggers when it is removed.transition(':enter', [...]) inside a trigger with defined styles and animate calls.trigger('fadeIn', [transition(':enter', [style({opacity: 0}), animate('500ms', style({opacity: 1}))])]) correctly uses transition(':enter', [style(...), animate(...)]). The distractors misuse state, animate directly, or wrong transition name.trigger('slideInOut', [
transition(':enter', [style({transform: 'translateX(-100%)'}), animate('300ms ease-out', style({transform: 'translateX(0%)'}))]),
transition(':leave', [animate('300ms ease-in', style({transform: 'translateX(100%)'}))])
])animate('300ms ease-in', style({transform: 'translateX(100%)'})), moving it to the right (100%).trigger('fade', [
transition(':enter', [animate('500ms', style({opacity: 1}))]),
transition(':leave', [style({opacity: 1}), animate('500ms', style({opacity: 0}))])
])transition(':enter', [style({opacity: 0}), animate('400ms', style({opacity: 1}))]) correctly fades in from opacity 0 to 1.transition(':leave', [animate('400ms', style({transform: 'translateX(-100%)'}))]) slides the element to the left.animate call.