Performance: Transition between states
MEDIUM IMPACT
This affects how smoothly the UI updates when components change state, impacting user interaction responsiveness and visual stability.
import { Component } from '@angular/core'; import { trigger, state, style, animate, transition } from '@angular/animations'; @Component({ selector: 'app-good-transition', template: `<div [@fadeInOut]="isVisible ? 'visible' : 'hidden'" (click)="toggle()">Content</div>`, animations: [ trigger('fadeInOut', [ state('visible', style({ opacity: 1 })), state('hidden', style({ opacity: 0 })), transition('visible <=> hidden', animate('300ms ease-in-out')) ]) ] }) export class GoodTransitionComponent { isVisible = true; toggle() { this.isVisible = !this.isVisible; } }
import { Component } from '@angular/core'; @Component({ selector: 'app-bad-transition', template: `<div [style.opacity]="isVisible ? '1' : '0'" (click)="toggle()">Content</div>`, }) export class BadTransitionComponent { isVisible = true; toggle() { this.isVisible = !this.isVisible; } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct style changes on state toggle | Minimal DOM ops | Multiple reflows per toggle | High paint cost due to layout changes | [X] Bad |
| Angular animations with opacity transitions | Minimal DOM ops | No reflows, only composite | Low paint cost, GPU accelerated | [OK] Good |