Chapter 12.4โ˜• 15 min read

Animations

Angular built-in animation system provides CSS-like transition definitions in TypeScript. Trigger, state, transition, and keyframes for smooth, performant animations.

01Angular Animations Module

Angular has a powerful, built-in animation system that uses CSS-like syntax in TypeScript โ€” no external libraries needed.

"Angular animations = choreography in TypeScript โ€” CSS se powerful."

Setup:

// app.config.ts
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';

bootstrapApplication(AppComponent, {
  providers: [
    provideAnimationsAsync()
  ]
});

Why use Angular animations over CSS?

  • State-based โ€” define open and closed states, animate between them
  • Enter/leave โ€” animate elements entering and leaving the DOM
  • Keyframes โ€” multi-step animations with precise control
  • Animation callbacks โ€” detect when animations start/end
  • Works with Angular routing, @if, @for seamlessly
02Trigger โ€” Define Animations

trigger() defines an animation that you can use in templates. Each trigger has one or more transitions.

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

const fadeInOut = trigger('fadeInOut', [
  transition(':enter', [
    style({ opacity: 0 }),
    animate('300ms ease-out', style({ opacity: 1 }))
  ]),
  transition(':leave', [
    animate('200ms ease-in', style({ opacity: 0 }))
  ]),
]);

"Trigger = animation ka naam โ€” template mein use karo."

  • trigger('name', [...]) โ€” creates named animation
  • :enter โ€” element added to DOM (@if true)
  • :leave โ€” element removed from DOM (@if false)
  • style({...}) โ€” CSS styles at a point
  • animate('300ms', style({...})) โ€” transition between styles
03Using Animations in Template

Using animations in templates is as simple as adding an attribute with @triggerName.

@Component({
  animations: [fadeInOut],
  template: `
    @if (show) {
      <div @fadeInOut>Content</div>
    }
    <div [@openClose]="isOpen ? 'open' : 'close'">Panel</div>
  `
})

"Template mein @triggerName lagao โ€” Angular baaki sambhal lega."

Important: With Angular 17+ @if/@for, animations work perfectly. Using *ngIf may cause issues โ€” use @if instead.

04Keyframes โ€” Multi-Step Animations

keyframes() allows multi-step animations with intermediate style stops, like creating a bounce effect.

import { trigger, transition, style, animate, keyframes } from '@angular/animations';

const bounceIn = trigger('bounceIn', [
  transition(':enter', [
    animate('600ms ease-out', keyframes([
      style({ transform: 'scale(0)', opacity: 0, offset: 0 }),
      style({ transform: 'scale(1.15)', opacity: 1, offset: 0.5 }),
      style({ transform: 'scale(0.95)', opacity: 1, offset: 0.75 }),
      style({ transform: 'scale(1)', opacity: 1, offset: 1 }),
    ]))
  ]),
]);

"Keyframes = frame by frame โ€” ek ek step define karo. offset 0 = start, 1 = end."

offset is a number from 0 to 1 representing animation progress at each keyframe.

05Animation States and Transitions

State-based animations are for components that have multiple visual states.

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

const openClose = trigger('openClose', [
  state('open', style({ height: '200px', backgroundColor: '#dd0031' })),
  state('closed', style({ height: '0px', backgroundColor: 'transparent' })),
  transition('open => closed', [animate('300ms ease-in')]),
  transition('closed => open', [animate('400ms ease-out')]),
]);

"States = positions โ€” open ya closed. Transition = movement between them."

When to use states vs enter/leave:

  • Enter/Leave (:enter, :leave) โ€” element appears/disappears from DOM. Use with @if.
  • State-based โ€” element exists in DOM but changes appearance.
  • Both combined โ€” you can have :enter/:leave AND state transitions in the same trigger.

Key Takeaways

  • provideAnimationsAsync() in app.config.ts to enable animations
  • trigger() defines named animations with transitions and states
  • :enter / :leave animate element appearing/disappearing with @if/@for
  • keyframes() for multi-step animations with offset control
  • state() defines visual states, transition() moves between them
  • Use @if/@for (not *ngIf/*ngFor) for reliable animation triggers
Course Search
Search across all chapters & stages
๐Ÿ“–

Search the course

Type any topic โ€” branching, stash, rebase, hooks โ€” and jump straight to that chapter.

merge branchesgit stashundo commitrebase