Layout Animations
Svelte Motion supports two kinds of layout animation:
layout— animate a single element when its own size or position changes (FLIP).layoutId— animate between two different elements that share the same identifier. When one unmounts and another with the samelayoutIdmounts, the new element FLIP-animates from the old one’s position.
Single-element layout animation
Add layout to any motion element to have it automatically animate when its position or size changes in the DOM.
<motion.div layout>
<!-- This element will FLIP-animate whenever its bounding rect changes -->
</motion.div><motion.div layout>
<!-- This element will FLIP-animate whenever its bounding rect changes -->
</motion.div>Use layout="position" to only animate translation (no scale):
<motion.div layout="position">
<!-- Translates smoothly, does not scale -->
</motion.div><motion.div layout="position">
<!-- Translates smoothly, does not scale -->
</motion.div>Shared layout with layoutId
The layoutId prop enables shared layout animations between completely different elements. When element A with layoutId="foo" unmounts and element B with the same layoutId="foo" mounts, B automatically animates from A’s last known position.
Tab underline example
The most common use case is a tab indicator that slides between tabs:
<script lang="ts">
import { AnimatePresence, motion } from '@humanspeak/svelte-motion'
let selectedTab = $state(0)
const tabs = ['Home', 'About', 'Contact']
</script>
{#each tabs as tab, i (tab)}
<button onclick={() => (selectedTab = i)}>
{tab}
<AnimatePresence>
{#if selectedTab === i}
<motion.div
key="underline"
layoutId="underline"
style="position: absolute; bottom: 0; left: 0; right: 0; height: 2px; background: royalblue;"
transition={{ type: 'spring', stiffness: 500, damping: 30 }}
/>
{/if}
</AnimatePresence>
</button>
{/each}<script lang="ts">
import { AnimatePresence, motion } from '@humanspeak/svelte-motion'
let selectedTab = $state(0)
const tabs = ['Home', 'About', 'Contact']
</script>
{#each tabs as tab, i (tab)}
<button onclick={() => (selectedTab = i)}>
{tab}
<AnimatePresence>
{#if selectedTab === i}
<motion.div
key="underline"
layoutId="underline"
style="position: absolute; bottom: 0; left: 0; right: 0; height: 2px; background: royalblue;"
transition={{ type: 'spring', stiffness: 500, damping: 30 }}
/>
{/if}
</AnimatePresence>
</button>
{/each}How it works
- One element leads each
layoutIdat a time. AlayoutIdcan change after mount: the element leaves its old id and joins the new one, as if it had unmounted and mounted again. Moving an id between mounted elements (layoutId={selected ? 'highlight' : undefined}) animates the new holder from the old one. An element that takes an id from one that is still on screen takes over instantly, and the other is hidden. - When the old element unmounts, its bounding rect is captured. Inside
AnimatePresenceit is held instead, and crossfades into the new one. - When the new element mounts, it reads the stored rect and performs a FLIP animation from the old position to its own natural position.
- The animation uses the element’s
transitionprop, or falls back to the previous element’s transition.
Crossfading with AnimatePresence
Inside AnimatePresence, the element that leaves a layoutId is held until the shared animation ends: it follows the new element and fades out while the new one fades in, as in Motion, even without an exit prop. See AnimatePresence: Shared layout.
Tabs without conditional rendering
Every tab can render its own highlight. Give layoutId only to the selected one, and the highlight slides to the new tab when the selection changes (Motion 14.7.1, #1411):
<script>
import { motion } from '@humanspeak/svelte-motion'
const tabs = ['Home', 'Search', 'Profile']
let selected = $state('Home')
</script>
{#each tabs as tab (tab)}
<button onclick={() => (selected = tab)} style="position: relative;">
{tab}
<motion.div
layoutId={selected === tab ? 'highlight' : undefined}
style="position: absolute; bottom: 0; left: 0; right: 0; height: 2px; background: {selected ===
tab
? 'royalblue'
: 'transparent'};"
/>
</button>
{/each}<script>
import { motion } from '@humanspeak/svelte-motion'
const tabs = ['Home', 'Search', 'Profile']
let selected = $state('Home')
</script>
{#each tabs as tab (tab)}
<button onclick={() => (selected = tab)} style="position: relative;">
{tab}
<motion.div
layoutId={selected === tab ? 'highlight' : undefined}
style="position: absolute; bottom: 0; left: 0; right: 0; height: 2px; background: {selected ===
tab
? 'royalblue'
: 'transparent'};"
/>
</button>
{/each}Layout animations can travel along a curve instead of a straight FLIP interpolation. See arc() for transition.layout.path and shared layoutId examples.
Full interactive example
Spring velocity
With a spring layout transition, an element that is interrupted mid-animation starts the next layout animation at the speed it was already moving, instead of stopping and starting again. Added in Motion 14.13.
<motion.div layout transition={{ type: 'spring', visualDuration: 0.4, bounce: 0.2 }} /><motion.div layout transition={{ type: 'spring', visualDuration: 0.4, bounce: 0.2 }} />Only some transitions carry velocity:
| Layout transition | When interrupted |
|---|---|
type: 'spring' with visualDuration / bounce, or stiffness / damping | Carries its velocity into the next animation |
type: 'spring' with duration | Starts from rest, so it ends on time |
| A tween, including the default layout transition | Starts from rest |
Any transition with repeat | Starts from rest |
Each edge of the box has its own spring, so an edge that does not move in either layout stays still while the others keep their speed. A child that animates inside an animating parent keeps its place in the parent and carries only its own velocity relative to it.
With shared layout, an element with the same layoutId continues the motion of the one it replaces.
mass: "auto"
Set mass: 'auto' to size the spring’s mass from the element:
<motion.div layout transition={{ type: 'spring', visualDuration: 0.45, bounce: 0, mass: 'auto' }} /><motion.div layout transition={{ type: 'spring', visualDuration: 0.45, bounce: 0, mass: 'auto' }} />A 300 px square has mass 1. Mass follows the element’s size (the square root of width × height) between 0.25 and 2. Damping is scaled with it, so the bounce stays the same and bigger elements settle later. "auto" only has an effect in layout animations; anywhere else it behaves like the default mass of 1.
See the layout velocity example.
Scrolling and layout animations
Layout measurements are taken in page space: the element’s position in the document, independent of how far the window is scrolled. Scrolling the page between two layout changes — or while a layout animation is playing — can never masquerade as a layout delta, so layout animations run identically whether or not the user has scrolled. This also applies to elements that change layout while scrolled offscreen: they animate to their new position like any other element instead of snapping.
Inside scroll containers — layoutScroll
Page-space measurement accounts for window scrolling automatically, but an overflow: scroll container’s internal scroll offset is invisible to it. If a motion.div with layout lives inside such a container and the user scrolls it during the animation, the container’s scroll offset shows up as an unwanted translate — the box visibly drifts.
The fix is to mark the scroll container with layoutScroll. Descendant layout animations then measure rects in that container’s coordinate space, so a mid-animation container scroll cancels out instead of leaking into the FLIP delta.
<motion.div layoutScroll style="overflow: auto; height: 320px;">
<motion.div layout>Resize me — scroll the container mid-animation</motion.div>
</motion.div><motion.div layoutScroll style="overflow: auto; height: 320px;">
<motion.div layout>Resize me — scroll the container mid-animation</motion.div>
</motion.div>Apply layoutScroll on the same motion.* element that owns the scrolling — the same element you’d set overflow: scroll (or auto) on. Nested layoutScroll containers stack: a layout descendant accounts for the scroll offset of every layoutScroll ancestor in its path, so you can wrap a scrollable list inside a scrollable panel and both contribute to the FLIP measurement.
| Prop | Type | Description |
|---|---|---|
layoutScroll | boolean | Mark this element as a scroll container so descendant layout measurements account for its scroll offset. Nested layoutScroll ancestors stack. |
Scoping shared animations — <LayoutGroup>
If two regions of your UI reuse the same layoutId values (think two tab strips both using layoutId="underline", or several Kanban columns each with a layoutId="indicator"), they’d cross-animate by default — the global registry doesn’t know which instance is which, and an unmount in one region can be picked up by a mount in another.
Wrap each region in <LayoutGroup id="…"> to scope the registry to that subtree:
<script>
import { AnimatePresence, LayoutGroup, motion } from '@humanspeak/svelte-motion'
</script>
<LayoutGroup id="strip-a">
<Tabs />
</LayoutGroup>
<LayoutGroup id="strip-b">
<Tabs />
<!-- same layoutId values inside — but independent of strip-a -->
</LayoutGroup><script>
import { AnimatePresence, LayoutGroup, motion } from '@humanspeak/svelte-motion'
</script>
<LayoutGroup id="strip-a">
<Tabs />
</LayoutGroup>
<LayoutGroup id="strip-b">
<Tabs />
<!-- same layoutId values inside — but independent of strip-a -->
</LayoutGroup>A <LayoutGroup> also groups layout animations, like Motion’s: every layout / layoutId element inside it is re-measured whenever any member changes layout or unmounts, so siblings animate together. Remove an item from a list and the remaining items glide into the freed space, even though only the removed item changed.
Nested groups chain by default (inherit={true}), so a <LayoutGroup id="inner"> inside a <LayoutGroup id="outer"> yields an effective id of "outer-inner" and joins the outer group’s animations. inherit="id" chains the id but starts a separate group: the outer group’s updates don’t re-measure those elements, which ride along with their parent instead of animating on their own. Pass inherit={false} to start a fresh scope and a separate group that ignore any surrounding LayoutGroup — useful for embedded widgets that should not be affected by ambient grouping.
| Prop | Type | Description |
|---|---|---|
id | string? | Stable identifier for this group. Combined with any surrounding LayoutGroup’s id when inherit is true. |
inherit | boolean \| 'id' (default true) | true: chain onto the parent group’s id and join its layout-animation group. 'id': chain onto the parent’s id but start a separate group, so the outer group’s updates don’t re-measure these elements (they follow their parent instead). false: ignore any outer group; fresh id scope and separate group. |
API reference
layout
| Value | Behavior |
|---|---|
true | Animate translate + scale on layout change |
"position" | Animate translate only (no scale) |
false / omitted | No layout animation |
layoutId
| Prop | Type | Description |
|---|---|---|
layoutId | string | Shared identifier. Elements with matching layoutId animate between each other’s positions. It can change after mount: the element moves to the new id’s shared layout, animating from the previous holder, or taking over instantly from one still on screen. |
layoutId works best inside AnimatePresence, which provides the registry that coordinates the snapshot/consume handoff.
Layout animation callbacks
| Prop | Type | Description |
|---|---|---|
onLayoutAnimationStart | () => void | Called when this element’s layout animation starts, as in Motion. |
onLayoutAnimationComplete | () => void | Called when this element’s layout animation completes, as in Motion. |
Layout transition
| Prop | Type | Description |
|---|---|---|
transition (unset) | — | With no transition on the element or a MotionConfig, a layout animation uses Motion’s default: a 0.45 s tween eased with [0.4, 0, 0.1, 1]. |
transition.type | 'spring' | A spring layout transition carries the element’s velocity through an interrupt. |
transition.mass | 'auto' | Sets the spring’s mass from the element’s size. Layout animations only. |
layoutRoot and layoutAnchor
| Prop | Type | Description |
|---|---|---|
layoutRoot | boolean | Make this element a layout root: descendants’ layout animations, including shared layoutId ones whose previous element was inside it too, resolve relative to it. Its own layout change is instant. Read at mount. See layoutRoot & layoutAnchor. |
layoutAnchor | { x: number; y: number } \| false | The point on the parent that relative layout projection is anchored to (default top-left; { x: 0.5, y: 0.5 } for a centred child), or false to turn relative projection off. |
Related
- AnimatePresence — Coordinate enter/exit animations
- Shared Layout Animation example — Full interactive demo
- LayoutGroup example — Two sibling tab strips reusing the same
layoutIdwithout crossing over - layoutRoot & layoutAnchor — Animate shared layout relative to a layout root, and anchor relative projection