Router animation
Strategies for stateful router animation
A common scenario in a single-page application is page transitions. When a page loads or unloads, an animation or transition effect might be used to make it feel more interactive and app-like.
By leveraging lifecycle hooks, we can perform animations and transition effects in code.
At first glance, this might look like a lot of code, but we do the animation inside of the attaching
and detaching
hooks. Using the Anime.js animation library, we create two animation functions for sliding our views in and out.
We use the created
lifecycle callback to access the host element (the outer element of our custom element) which we will animate. Most of the other callbacks determine the direction we are heading in.
We inject out AnimationHooks
class into our main component, but we also inject it into the sub-components we want to animate. We avoid setting our hook globally, or it would run for all components (which you might want).
As you can see, besides some router-specific lifecycle methods, animating with the router isn't router-specific and leverages Aurelia lifecycles.
A link to a demo of slide in and out animations based on routing can be seen below:
Last updated