Animations & Transitions
Add enter/leave animations to views and components, reveal content on scroll, and animate route changes.
Puzzle animates through the Web Animations API. You declare enter and leave animations on a component, and the framework plays them when the component appears and disappears: a list row being added or removed, or a view swapping on navigation.
Enter/leave animations
Add an animations class field to any view, layout, or component. Each of in and out is { from, to, duration, easing?, delay? }: keyframes plus timing:
// components/TodoItem.pzl
export default class TodoItem extends PuzzleView {
animations = {
in: {
from: { height: '0px', opacity: 0, transform: 'scale(0.96)' },
to: { height: '44px', opacity: 1, transform: 'scale(1)' },
duration: 200,
easing: 'ease-out',
},
out: {
from: { opacity: 1 },
to: { opacity: 0 },
duration: 150,
},
};
}Things to know:
- The animation target is the instance's root element, with no wrapper added. So the
tokeyframe must equal the element's natural resting style; the enter animation's inline styles are released once it finishes. - Height can't animate to
autounder the Web Animations API. Animate between explicitpxvalues (here the row wraps its content in a fixed 44px inner element). - Components animate when added to or removed from a list. Key the list so leaving rows animate out cleanly; development warns when an unkeyed list removes an out-animated component. Views animate on route transitions (below).
prefers-reduced-motion: reducezeroes all durations automatically. A malformed spec warns once and is skipped.
Lifecycle hooks
Four optional hooks bracket the phases and fire even when no animations field is declared:
viewWillShow()/viewDidShow()around the enter animation, aftermounted().viewWillHide()/viewDidHide()around the leave animation, beforedestroyed().
Reveal on scroll
By default the in animation plays the moment the component mounts, so a section far down a long page animates before anyone sees it. Add trigger: 'visible' and the element mounts held at its from keyframe, then plays once the first time it scrolls into view:
// components/FeatureSection.pzl
export default class FeatureSection extends PuzzleView {
animations = {
in: {
from: { opacity: 0, transform: 'translateY(24px)' },
to: { opacity: 1, transform: 'translateY(0)' },
duration: 500,
easing: 'ease-out',
trigger: 'visible',
triggerOffset: '15%', // optional: fire 15% above the viewport bottom
},
};
}triggerOffsetis a pixel number or a'%'string: how far above the bottom of the viewport the trigger line sits. Omit it to fire as soon as any part of the element is visible.triggerAnchortakes a CSS selector matched against the component's ancestors, and the component reveals when that ancestor crosses the line. Anchor a heading and three cards to the same section and they fire together, with each one'sdelayproviding the choreography.- The reveal plays once per mount; scrolling away and back doesn't replay it.
viewWillShow()andviewDidShow()fire around the actual reveal. - Content is never stranded hidden: without
IntersectionObserver, it plays the mount animation instead; with reduced motion, it shows immediately. Keep the default mount trigger for content that's already on screen.
Animating reorders: flip
Put a bare flip attribute on the root of a keyed {#for} row, and retained rows slide to their new position when the list is sorted, filtered, or reordered. New rows still play their enter animation and leaving rows their exit:
{#for track in sortedTracks}
<li key={ track.id } flip>{ track.title }</li>
{/for}The DOM order is final immediately; only the paint catches up. The default duration is 250ms. To change it, pass options built in data() (a template expression can't start with an object literal): flip={ flipOpts } with flipOpts: { duration: 400, easing: 'ease-in-out' }. It respects reduced motion, and a flip on an unkeyed row warns, since positional lists have no identity to animate.
Route transitions
When a view declares an animations field, navigation plays it: the old view runs its out animation and is destroyed, then the new view mounts and runs its in animation. By default these run sequentially, out and then in, not overlapping.
There's one animator per transition: a view swapped inside a reused layout animates alone, and a layout swap animates the layout as the unit. In a nested section, the topmost swapped view animates and everything below rides along, so a sibling-pane swap animates only the pane, not the reused shell.
The URL and title commit together with the new view, after the out animation. A navigation that's superseded or fails during the out phase commits nothing.
Overlapping transitions (experimental)
Opt in with transitionMode: 'overlap' so the old view's out and the new view's in play concurrently, for cross-fades and shared-axis slides:
const app = new PuzzleApp({
target: '#app',
routes,
transitionMode: 'overlap', // default is 'sequential'
});The router pins the leaving view in place, mounts the newcomer immediately, and tears the leaver down when its out settles. A constraint to know: ancestors of the mount container must not carry transform, filter, or contain, which would re-root the pin.
transitionMode can also be set per route in routes.js (a parent route's value covers its children) or as a class field on a view or layout. The destination decides: for a navigation from A to B, only B's route, B's view, or the app default is consulted, most specific first.
// routes.js: every page under /gallery cross-fades in
{ path: '/gallery', name: 'gallery', view: GalleryShell, layout: DefaultLayout, transitionMode: 'overlap', children: [ /* … */ ] },
// or on the view class itself
export default class GalleryView extends PuzzleView {
transitionMode = 'overlap';
animations = { in: { /* … */ }, out: { /* … */ } };
}Next
- Routing: the navigation lifecycle transitions ride on.
- Components: the lifecycle hooks around animations.