← Back to Angular, page 4

angular-0076 · Medium mid

How do you animate elements entering and leaving the page in Angular, and what changed with animate.enter and animate.leave?

Since Angular 20.2 you animate entering and leaving elements with the animate.enter and animate.leave bindings, which apply CSS classes and wait for the animation to finish before removing the element. The older @angular/animations package (trigger, state, transition) is deprecated and planned for removal, so use it only to maintain existing code.

Full answer

Before Angular 20.2, animation in Angular meant the @angular/animations package: a trigger with state and transition definitions in the component metadata, [@trigger] bindings in the template, and provideAnimationsAsync() in the app config. It worked, but it added to the bundle and used its own timeline engine on top of the browser.

From Angular 20.2 the recommended way uses plain CSS:

  • animate.enter="fade-in" adds the class when the element is inserted, and removes it when the animation ends.
  • animate.leave="fade-out" keeps the element in the DOM, adds the class, and removes the element after the animation completes (Angular waits for the longest CSS animation or transition).
  • Both accept a string, an array of classes, or a function for libraries such as GSAP. A leave function must call the completion callback, or Angular removes the element after a timeout of four seconds.
  • No import and no provider are needed. They are compiler features that work with @if and @for.

Other ordinary changes (hover effects, state changes) use normal CSS transitions with class and style bindings. Reordering and layout animations can use the browser's View Transitions API through the router.

The @angular/animations package is deprecated as of Angular 20.2, with removal planned for a later major version. Do not mix both systems in the same component. Migrate gradually, and remove provideAnimationsAsync when the last trigger is gone, which also reduces the bundle.

Accessibility: respect prefers-reduced-motion in CSS, and do not hide meaning in motion alone.

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

// Modern: CSS classes, no package, no provider (Angular 20.2 and newer).
@Component({
  selector: 'app-notice',
  template: `
    <button (click)="show.set(!show())">Toggle notice</button>
    @if (show()) {
      <p class="notice" animate.enter="fade-in" animate.leave="fade-out">Exam timetable updated</p>
    }
  `,
  styles: `
    .fade-in { animation: fade-in 300ms ease-out; }
    .fade-out { animation: fade-out 200ms ease-in forwards; }
    @keyframes fade-in { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; } }
    @keyframes fade-out { to { opacity: 0; transform: translateY(-8px); } }
    @media (prefers-reduced-motion: reduce) {
      .fade-in, .fade-out { animation-duration: 1ms; }
    }
  `,
})
export class NoticeComponent {
  readonly show = signal(false);
}

// LEGACY (deprecated since 20.2): the @angular/animations trigger syntax.
@Component({
  selector: 'app-legacy-panel',
  animations: [
    trigger('panel', [
      state('open', style({ opacity: 1, height: '*' })),
      state('closed', style({ opacity: 0, height: '0px' })),
      transition('open <=> closed', animate('200ms ease-in-out')),
    ]),
  ],
  template: `
    <button (click)="open.set(!open())">Toggle</button>
    <div [@panel]="open() ? 'open' : 'closed'">Legacy animated content</div>
  `,
})
export class LegacyPanelComponent {
  readonly open = signal(true);
}

Angular 20.3, strict template check passes. The second component is the legacy style, kept to show what existing code looks like. It needs `provideAnimationsAsync()` from `@angular/platform-browser/animations/async`.

Key points

  • `animate.enter` and `animate.leave` bind CSS classes. Angular waits for leave animations before removal.
  • No provider or package is needed. They work with `@if` and `@for`.
  • `@angular/animations` is deprecated from 20.2. Keep it only for existing code. Do not mix both in one component.
  • Honour `prefers-reduced-motion`.

Common mistakes

  • Forgetting to call the completion function in a function-based `animate.leave`, which delays removal.
  • Mixing legacy triggers and `animate.*` in one component.
  • Animating layout properties such as `height` when `transform` and `opacity` would be smoother.

Follow-ups

  • How would you animate items of a list as they are removed?
  • How do you disable animations for users who prefer reduced motion?
  • How would you migrate a `:enter` and `:leave` transition to the new API?

Tips

  • State the version where it changed (20.2) and the status of the old package.
  • Say that CSS does the work, so you can use any CSS skills.
  • Mention reduced motion without being asked.

Evaluation rubric

  • Uses animate.enter and animate.leave correctly.
  • Describes the legacy package and its deprecation.
  • Considers accessibility and performance.