better-motion
Decides what should animate and builds it, with exact values for duration, easing, scale and distance and recipes for enters, exits, icon swaps, shared layout animation and drag.
Install
npx skills add https://github.com/jakubkrehel/skills --skill better-motionLayout animation
Recipes for shared layout animation, AnimatePresence modes and animated height with Motion. Enter and exit values live in enter-exit.md [blocked].
Shared layout animation
Each state renders its own element, and the elements share a layoutId. Motion measures both and animates from one to the other, so nothing is moved by hand.
Several elements can move at once when each pair has its own id, as in circle-1, circle-2 and circle-3. Unrelated elements that share an id animate into each other, so build ids in a list from the item's key.
Values that differ between states
Position and size come from each state's classes. A value Motion has to interpolate through the move, such as the radius, goes through animate:
With AnimatePresence
Content that fades while the shared element moves sits inside AnimatePresence. A shared element that swaps one state for the other sits outside it:
A layoutId element runs its initial and exit during the move, and an opacity exit makes it fade mid-flight. Give it neither.
Tab indicator
Render the indicator only inside the active tab and let the shared id carry it across. The label sits above it:
Two tab groups on one page need two different ids.
Card to dialog
The card and the dialog popup are separate components that share a layoutId, and the card stays mounted behind the dialog. Motion cross-fades two mounted elements that share an id. AnimatePresence keeps the popup in the DOM on close until it has animated back to the card:
The popup takes no initial or exit props. Keep the project's dialog primitive for focus and dismissal, and render the popup as a motion element.
Presence modes
In sync both children take up space while they overlap, which is why two children in the same place need popLayout.
A cross-fade between steps of one dialog uses popLayout, opacity and a small scale:
popLayout positions the exiting child absolutely, so the parent needs a position other than static. A custom component used as the direct child has to pass its ref to the DOM node.
Animated height
The outer element animates to a measured number and the inner wrapper is what gets measured:
bounds.height is 0 until the content mounts and is measured. A dialog that opens on a measured height therefore grows from nothing, which is why the first state above takes a fixed number.
Where the project has no measuring hook, this one is enough:
offsetSize reads the layout size. getBoundingClientRect reads the painted size, which is wrong while an ancestor is scaling in.