Skip to content

Layout animations

Layout properties are the ones you cannot animate cheaply: change justify-content or a width and the browser reflows, it does not tween. FLIP sidesteps that - measure where the element was (First), let the real layout change happen (Last), Invert the difference with a transform, then Play it back to zero. The result animates on the compositor while the DOM is already in its final state. Layout="true" is the whole opt-in; Bmotion does the measuring.

Animating a reflow

Add Layout="true" to any Bmotion element and every re-render that moves or resizes it plays a FLIP. Nothing below animates a property directly: the buttons change the list order and the container's justify-content, and the boxes work out the rest for themselves.

A
B
C
D
E

What moves: position, size or both

A layout change can move an element, resize it, or both, and Layout chooses which of those animate. Layout="true" (BmotionLayout.Full) animates both. BmotionLayout.Position animates only the move: the element takes its new size at once and slides there, with no scaling at all - the cheapest mode, and the one for anything holding text. BmotionLayout.Size is the mirror image: the element jumps to its new place and only the resize animates - for a box that grows or shrinks where it stands, like an accordion panel. Each box below moves across its track and doubles in size on the same click.

Layout="true" · moves and grows
Aa
BmotionLayout.Position · moves, size jumps
Aa
BmotionLayout.Size · grows, place jumps
Aa

Shared elements

LayoutId connects elements across mounts. When one unmounts and another mounts carrying the same id, the new one FLIPs from where the old one was - so a single underline appears to slide between tabs even though each tab renders its own. Layout="BmotionLayout.Position" animates position without scaling, which is what you want on anything containing text.

A LayoutId is global by default, so two components that happen to pick the same id would hand the element to each other. BmotionLayoutGroup namespaces the ids beneath it: the two strips below both name their underline "tab-underline", but each sits in a group of its own, so each underline only ever travels inside its own strip.

BmotionLayoutGroup Name="site"
BmotionLayoutGroup Name="settings"

Crossfade

When the old holder of a LayoutId is still on screen as the new one mounts - it is playing its exit under a presence in Sync mode - the two crossfade: the new one grows out of the old one's box as it fades in, and the old one travels onto the new one's box as it fades out. Click the card. The fade multiplies with the elements' own opacity, so their own enter and exit fades play on underneath; LayoutCrossfade="false" leaves just the FLIP - untick it and compare.

Size correction

Animating a box's size by scaling it is fast but wrong: the text inside stretches and the corners go oval. With Layout="true" Bmotion still scales - that is what keeps it on the compositor - but counter-scales the children and corrects the border-radius every frame, so the label and the inner dot stay the size and shape they were. Grow the card and watch what does not distort.

Layout

Anchoring and measurement

A FLIP is a comparison between two measurements, and four parameters decide what is being compared. LayoutAnchor picks the point of the box the animation is projected from - the point that appears to stay still while it resizes. It shows when the move and the resize are animated apart, as with BmotionLayout.Size: the box jumps to its new place and grows out of its anchor there. Both boxes below stay centred in their stages, so BmotionLayoutAnchor.Center grows them evenly about the middle, while the default TopLeft pins the new top-left corner - the box starts up and to the left of where it was and grows down and right, which is what an accordion panel whose top edge stays put should do. new BmotionLayoutAnchor(1, 0) would pin the top-right corner. (With Layout="true" the move plays along with the resize, so every anchor traces the same path.)

BmotionLayout.Size · anchor TopLeft (default)
BmotionLayout.Size · anchor Center
left stage: OnLayoutAnimationStart 0 · OnLayoutAnimationComplete 0

The other three are about the coordinate space. LayoutScroll is for an element inside a scrolling container: without it, scrolling that container between the two measurements looks exactly like the element having moved, and it jumps. LayoutRoot does the same job for a position: fixed element, which stays put while the page scrolls under it. LayoutDependency is the performance one - measuring forces a reflow, so pointing it at the state the layout actually depends on stops unrelated re-renders paying for one (the stages above measure only when _anchored changes). OnLayoutAnimationStart and OnLayoutAnimationComplete bracket each layout animation, and a superseded one still reports its completion, so every start is matched by exactly one end.

Bmotion 0 el · 0 active ▸
An unhandled error has occurred. Reload 🗙