Enter and leave with LynxTransition
LynxTransition plays one CSS animation when content appears and another when it disappears. It keeps the content on screen for the duration you give it. LynxTransitionGroup does the same for each item in a list.
Use them when you want to:
- Set the timing in the template, rather than have it read from the CSS.
- Run code after each item of a list has animated.
Otherwise, Angular's own animate.enter and animate.leave work too: see Enter and leave transitions.
Animate content in and out
[show]controls whether the content is rendered.- When
showturnstrue, AngularLynx renders the content and adds thefade-enterclass. - When
showturnsfalse, it swapsfade-enterforfade-leave, waitsdurationmilliseconds, then removes the content. - The classes go on the
<lynx-transition>element, which wraps your content. Write the styles in the component that uses it. - No animation on the first render. If
showstarts astrue, the content appears without playingfade-enter.
The class names
name sets the prefix of the two classes. With name="fade":
- The default
nameisv, which givesv-enterandv-leave. - Give each class a
@keyframesanimation, not atransition. - Use
bothin theanimationshorthand (animation-fill-mode: both). The element then holds the first frame before the animation starts and the last frame after it ends.fade-enterstays on while the content is shown, so its last frame is the resting state.
Match duration to your CSS
duration is the animation length in milliseconds, and defaults to 300. LynxTransition doesn't read the CSS animation's length. It uses duration to know when to remove leaving content.
- If
durationis shorter than the CSS animation, the content disappears before the leave animation finishes. - If it's longer, the content stays on its last frame until the time is up.
Run code after an animation
(afterEnter) and (afterLeave) fire when duration has passed:
(afterLeave) fires as the content is removed.
Animate list items with LynxTransitionGroup
LynxTransitionGroup renders the list itself. Each item can then animate in and out with the timing you give it, and the group tells you which item finished.
For a plain @for, animate.enter and animate.leave work on each item too.
[each]is the list, and the<ng-template>renders one item.let-itemnames the item.[trackBy]returns a unique key per item, liketrackin@for. The default uses the item itself, which only works for strings, numbers and other values you don't recreate.- The classes go on each item's first element, here the
<view>inside the template. - Items in the first render don't animate. Only items added or removed later do.
- A removed item stays in place while it animates out. If it comes back before the leave animation ends, it stops leaving.
(afterEnter)and(afterLeave)emit the item that finished animating.
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 transitions: Angular's
animate.enterandanimate.leave - Animations: choose between transitions, keyframes and code
LynxTransitionandLynxTransitionGroupAPI- Transitions example
