Enter and leave transitions
LynxTransition plays one CSS animation when content appears and another when it disappears. It keeps the content on screen until the leave animation ends. LynxTransitionGroup does the same for each item in a list.
Animate content in and out
src/app/fade-panel.ts
[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 can't detect when a CSS animation ends, so 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
Angular's @for removes an item's view straight away, which leaves no time for a leave animation. LynxTransitionGroup renders the list itself, so each item can animate in and out:
src/app/animated-list.ts
[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
- Animations: choose between transitions, keyframes and code
LynxTransitionandLynxTransitionGroupAPI- Transitions example
