Searching Puzzle

Type to search Puzzle.

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 to keyframe must equal the element's natural resting style; the enter animation's inline styles are released once it finishes.
  • Height can't animate to auto under the Web Animations API. Animate between explicit px values (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: reduce zeroes 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, after mounted().
  • viewWillHide() / viewDidHide() around the leave animation, before destroyed().

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
    },
  };
}
  • triggerOffset is 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.
  • triggerAnchor takes 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's delay providing the choreography.
  • The reveal plays once per mount; scrolling away and back doesn't replay it. viewWillShow() and viewDidShow() 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: { /* … */ } };
}
Experimental. Overlapping transitions work and are tested, but their interaction with other opt-in features (nested reused layouts, hash and base-path routing, anchors) has had less real-world mileage than the sequential default. They're safe to try; prefer sequential where stability matters most.

Shared-element morph transitions (experimental)

A morph transition flies one element into another across a navigation: a card on a list view opening into the header of a detail view, or a card morphing into a route-mounted dialog. Mark both ends with the same data-puzzle-morph value and enable it with one line in app.js:

import { enableMorph } from '@magic-spells/puzzle/morph';

// ... after creating the app
enableMorph(app);

This requires the optional peer package @magic-spells/morph-engine; apps that never import @magic-spells/puzzle/morph bundle none of it.

<!-- list view -->
<a href="{ link('/posts/42') }" data-puzzle-morph="post-42">…card…</a>

<!-- detail view -->
<header data-puzzle-morph="post-42">…</header>

Navigating forward morphs the card into the target; navigating away (a close button, the backdrop, or browser back) reverses it. It works whether the source stays mounted (a nested-route dialog) or is destroyed before the target mounts (a sibling view swap). Notes:

  • The initial navigation never morphs, so deep links render plainly.
  • Params-only navigations don't trigger it, and prefers-reduced-motion disables it.
  • Don't position a morph element with transform, don't give a target stylesheet opacity, and don't bind a changing style={ … } on either end: the engine drives inline transform and opacity during the flight.
  • Morphing views shouldn't also declare animations.in / out; the target view's entrance, if any, should be opacity-only.

For a directional (forward-only) morph, use the role spellings instead: data-puzzle-morph-trigger="id" on the launching element (it never receives) and data-puzzle-morph-target="id" on the receiving element (it never launches). Going back then renders the list plainly. Plain data-puzzle-morph on both ends stays the symmetric, round-tripping spelling for dialogs.

Experimental. Like overlapping transitions, morph works and is tested on its own, but its interaction with other opt-in features is less proven. Pick one transition mechanism per app: combining a morph with transitionMode: 'overlap' is best-effort.

Next

  • Routing: the navigation lifecycle transitions ride on.
  • Components: the lifecycle hooks around animations.