For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /guide/animations/enter-leave.md.

Enter and leave transitions

Animate an element as it appears and disappears with Angular's animate.enter and animate.leave. Write them on an element in an @if, @for or @switch. This is the way angular.dev teaches, and the code is the same as in a web app.

You want toUse
Animate an element in an @if, @for or @switchanimate.enter and animate.leave
Run your own animate() keyframes as an element enters or leaves(animate.enter) and (animate.leave)
Set the timing in the template rather than read it from the CSSLynxTransition
Run code after each item of a list has animatedLynxTransitionGroup

Animate with CSS classes

src/app/card.ts
import { Component, signal } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-card',
  imports: [LYNX_ELEMENTS],
  template: `
    <view (bindtap)="toggle()"><text>Toggle</text></view>
    @if (shown()) {
      <view class="card" animate.enter="card-enter" animate.leave="card-leave">
        <text>Hello!</text>
      </view>
    }
  `,
  styles: `
    @keyframes card-enter {
      from {
        opacity: 0;
        transform: translateY(24px);
      }
      to {
        opacity: 1;
        transform: translateY(0px);
      }
    }
    @keyframes card-leave {
      from {
        opacity: 1;
      }
      to {
        opacity: 0;
      }
    }
    .card-enter {
      animation: card-enter 300ms ease-out;
    }
    .card-leave {
      animation: card-leave 250ms ease-in forwards;
    }
  `,
})
export class Card {
  readonly shown = signal(false);

  toggle(): void {
    this.shown.update((v) => !v);
  }
}
  • animate.enter adds its classes as the element is inserted, and takes them off when the element's longest animation or transition ends.
  • animate.leave adds its classes when the element is removed, and keeps the element on screen until its longest animation or transition ends.
  • A transition works too: give the element transition: opacity 200ms and let the leave class set opacity: 0.
  • They work in @for, so list items can animate out without LynxTransitionGroup.

How AngularLynx knows how long an animation runs

In a browser, Angular reads the element's animation and transition timings with getComputedStyle(). Lynx has no computed styles on the main thread, where Angular runs.

So the build writes down each rule's animation-* and transition-* values. The runtime matches them to the element's classes and ancestors, as it does for text styles.

That covers a rule with plain values, whose selector is made of classes, an id, Lynx tags, and > or descendant combinators. It can't follow a timing that's:

  • set from a var(), or with inherit or another CSS-wide keyword
  • inside @media, @supports or another at-rule, or in a nested rule
  • in a selector with an attribute, :not() or another pseudo-class, or a sibling combinator
  • set with an inline style

For those, it answers that there's no animation. The enter class comes off a frame after it's added, and a leaving element is removed at once. Move the timing into a plain rule for the class, or run your own animation.

The build warns about each class written in an animate.enter="…" or animate.leave="…" in your app's templates, when:

  • a rule like those above sets a timing for it, or
  • two stylesheets give it different timings.

It can't see the classes of a bound [animate.enter]="classes()", or of a library's templates. So a build without a warning doesn't vouch for those.

Run your own animation

Bind a function instead of a class. Angular calls it with the element as $event.target. For (animate.leave), it keeps the element until you call $event.animationComplete():

src/app/list.ts
import { type AnimationCallbackEvent, Component, signal } from '@angular/core';
import { type BaseLynxElement, LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-list',
  imports: [LYNX_ELEMENTS],
  template: `
    @for (item of items(); track item) {
      <view
        (animate.enter)="slideIn($event)"
        (animate.leave)="slideOut($event)"
        (bindtap)="remove(item)"
      >
        <text>Item {{ item }}</text>
      </view>
    }
  `,
})
export class List {
  readonly items = signal([1, 2, 3]);

  remove(item: number): void {
    this.items.update((items) => items.filter((other) => other !== item));
  }

  slideIn(event: AnimationCallbackEvent): void {
    const element = event.target as unknown as BaseLynxElement;
    element.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 250 });
    event.animationComplete();
  }

  slideOut(event: AnimationCallbackEvent): void {
    const element = event.target as unknown as BaseLynxElement;
    element.animate([{ opacity: 1 }, { opacity: 0 }], {
      duration: 250,
      fill: 'forwards',
    });
    setTimeout(() => event.animationComplete(), 250);
  }
}
  • $event.target is a Lynx element, so its animate() is Lynx's own.
  • If you never call animationComplete(), Angular removes the element after 4 seconds (MAX_ANIMATION_TIMEOUT).
The leave function runs twice on iOS and Android

It runs twice for an element in a block that's removed. Each call gets an animationComplete() of its own:

  • Call animationComplete() in each.
  • Keep the function free of side effects that mustn't happen twice.

Angular runs a view's leave animations when it's detached, and again when it's destroyed. It does that for a renderer that has destroyNode(), as AngularLynx's does and Angular's DOM renderer doesn't.

Turn them off

Add provideAnimateEnterLeaveDisabled() to keep animate.enter and animate.leave doing nothing on a phone, as before AngularLynx supported them. You might want that for templates shared with a web app:

src/app/app.config.ts
import { type ApplicationConfig } from '@angular/core';
import {
  provideAnimateEnterLeaveDisabled,
  provideRenderer,
} from '@blotch/angular-lynx';

export const appConfig: ApplicationConfig = {
  providers: [
    provideRenderer(),
    provideAnimateEnterLeaveDisabled(),
  ],
};

No class is added, no function is called, and a leaving element is removed without waiting.

Unit tests run with them off already: TestBed turns them off unless you pass animationsEnabled: true.

Limitations

  • No move animations. When items change order, they jump to their new places.
  • Only some properties animate, as with any Lynx animation.