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.
Animate with CSS classes
animate.enteradds its classes as the element is inserted, and takes them off when the element's longest animation or transition ends.animate.leaveadds 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 200msand let the leave class setopacity: 0. - They work in
@for, so list items can animate out withoutLynxTransitionGroup.
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 withinheritor another CSS-wide keyword - inside
@media,@supportsor 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():
$event.targetis a Lynx element, so itsanimate()is Lynx's own.- If you never call
animationComplete(), Angular removes the element after 4 seconds (MAX_ANIMATION_TIMEOUT).
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:
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.
Related
- Enter and leave with
LynxTransition - Animations: choose between transitions, keyframes and code
- Animate from code: the element's
animate()method provideAnimateEnterLeaveDisabled()API- Animations example, with
animate.enterandanimate.leave - Animations on angular.dev
