Performance: Enter and leave animations
MEDIUM IMPACT
This affects the smoothness of UI transitions and the responsiveness of user interactions during element appearance and disappearance.
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 |