Skip to content

Svelte transitions, animate: and motion (Svelte) ​

Svelte's template directives for animation: transition:/in:/out: attach a function that returns a css(t, u) or tick(t, u) program, which the runtime samples into Web Animations keyframes when a block enters or leaves; a leaving block is paused rather than destroyed, so its DOM stays until every outro finishes; animate: runs FLIP on keyed {#each} reorders, measuring each item immediately before reconciliation and again in a microtask after it; svelte/motion supplies rune-backed Tween and Spring values.

FieldValue
LanguageSvelte components compiled to JavaScript; runtime in JavaScript with JSDoc types
LicenseMIT (LICENSE.md)
Repositorysveltejs/svelte: src/transition, src/animate, src/motion, runtime dom/elements/transitions.js, dom/blocks/each.js
Documentationsvelte.dev/docs: transition:, in: and out:, animate:, svelte/motion (sources under documentation/docs in the repository)
Categorycompiler-attached, runtime-managed directive animations over the DOM; WAAPI playback of sampled keyframes; signal-backed tween/spring values
Versions surveyedSvelte 5.59.2 (packages/svelte/package.json) at commit f076077 (2026-11-25)
Animation state lives inper-block transition managers (effect.nodes.t) and per-item animation managers (effect.nodes.a) attached to the runtime's effect tree, holding the running WAAPI Animation; the leaving block's effect itself (flagged INERT/PAUSED); Tween/Spring state signals

NOTE

Read for sparkles:ui's two open questions: where FLIP takes its before and after geometry (UI-Q9, PR #775) and how a block removed from the description keeps painting through its exit (UI-Q10). The Web Animations objects Svelte creates are described on Web Animations; the React libraries that solve the same problems from a virtual DOM are on Motion and React Spring. Svelte 5's signals ("runes") are close kin to Solid's; vocabulary is on Concepts. Every source link is pinned to f076077; paths below are relative to packages/svelte/src unless stated.


Overview ​

What it solves ​

A compiled UI framework that updates the DOM in place has no virtual-DOM diff in which to keep a removed node, and no hook for "where was this element before the update". Svelte puts both into the runtime's block machinery: an {#if} or {#each} block that is about to remove a branch asks that branch's transitions to play first, and a keyed {#each} that is about to move items asks each item to measure itself first. The docs state the user-facing contract: "A transition is triggered by an element entering or leaving the DOM as a result of a state change. When a block (such as an {#if ...} block) is transitioning out, all elements inside it, including those that do not have their own transitions, are kept in the DOM until every transition in the block has been completed" (14-transition.md).

Design philosophy ​

  • A transition is a pure function of progress. A transition function returns { delay, duration, easing, css, tick }; "The t argument passed to css is a value between 0 and 1 after the easing function has been applied. In transitions run from 0 to 1, out transitions run from 1 to 0 — in other words, 1 is the element's natural state", and "The function is called repeatedly before the transition begins, with different t and u arguments" (14-transition.md).
  • Prefer declarative output the browser can run off-thread. "If it's possible to use css instead of tick, do so — web animations can run off the main thread, preventing jank on slower devices" (source).
  • Transitions are local by default. "Local transitions only play when the block they belong to is created or destroyed, not when parent blocks are created or destroyed"; |global opts in (source).
  • Leaving is pausing. The runtime comment: "When a block effect is removed, we don't immediately destroy it or yank it out of the DOM, because it might have transitions. Instead, we 'pause' it. It stays around (in memory, and in the DOM) until outro transitions have completed, and if the state change is reversed then we resume it" (pause_effect).

How it works ​

Compilation: directives become runtime calls ​

The compiler turns transition:fade={params} into $.transition(flags, node, () => fade, () => params), where flags packs TRANSITION_IN, TRANSITION_OUT (both for transition:, one for in:/out:) and TRANSITION_GLOBAL for |global; the call is emitted into the element's after_update list, "to ensure it always happens after bind:this" (TransitionDirective.js). animate:flip becomes $.animation(node, () => flip, params) (AnimateDirective.js) and sets EACH_IS_ANIMATED on the enclosing each block (EachBlock.js). Nothing about the animation itself is compiled: the transition function runs at runtime, against the live element.

svelte
<script>
    import { fade, fly } from 'svelte/transition';
    import { flip } from 'svelte/animate';
    let visible = $state(true);
    let items = $state([1, 2, 3]);
</script>

{#if visible}
    <div in:fly={{ y: 200 }} out:fade>flies in, fades out</div>
{/if}

{#each items as item (item)}
    <li animate:flip transition:fade>{item}</li>
{/each}

Transition managers on the effect tree ​

Svelte 5's runtime is a tree of effects: block effects ({#if}, {#each}, {#key}) own branch effects, which own the DOM nodes and template effects of one branch. $.transition(...) creates a TransitionManager — { is_global, in(), out(fn), stop() } — and pushes it onto the current effect's nodes.t list (transition). An intro runs on creation only if the transition is |global or the parent block effect has already run once (REACTION_RAN), which is how "local" is decided: a block created as part of its parent's first render does not intro (source).

When a block decides a branch must go, it calls pause_effect(branch, callback) instead of destroying it (pause_effect). pause_children walks the subtree, flips each effect to INERT (an inert effect no longer updates), and collects transition managers — every manager of the branch's own elements, but in nested blocks only |global ones, since local is carried down only through transparent effects (pause_children). Each collected manager's out(check) runs; when the last calls check, the branch is destroyed and the callback fires. If the state flips back before that, resume_effect clears INERT, schedules any effect whose dependencies changed while paused, and calls in() on the managers (resume_children). A manager's out also sets element.inert = true, so an outroing element cannot be focused or clicked (source).

animate(): sampling css into Web Animations keyframes ​

Both intros and outros end in the internal animate(element, options, counterpart, t2, …) (source). It takes the target t2 (1 for intro, 0 for outro) and the start t1 from the opposite-direction animation still running, if any (counterpart?.t()), so a transition: reversed mid-flight continues from its current progress and lasts duration * |t2 − t1|. With a css function it samples the program into one keyframe per 60 Hz frame and hands them to element.animate (source):

js
// packages/svelte/src/internal/client/dom/elements/transitions.js — animate()
if (css) {
  var n = Math.ceil(d / (1000 / 60)); // `n` must be an integer, or we risk missing the `t2` value
  for (var i = 0; i <= n; i += 1) {
    var t = t1 + delta * easing(i / n);
    var styles = css_to_keyframe(css(t, 1 - t));
    keyframes.push(styles);
  }
}
// …
animation = element.animate(keyframes, { duration: d, fill: 'forwards' });

Easing is baked into the keyframe values, and the WAAPI animation is linear in time. Progress is read back from animation.currentTime (get_t), which is what a reversing counterpart uses. A tick function is driven from Svelte's own requestAnimationFrame task loop, reading the same get_t (loop.js). The docs state the platform consequence: "Transitions are driven by the Web Animations API rather than by CSS. A global @media (prefers-reduced-motion: reduce) rule that zeroes transition-duration and animation-duration therefore has no effect on them" (source). Svelte 4 sampled the same css function into a CSS @keyframes rule inserted into a generated stylesheet and set node.style.animation to it (create_rule in style_manager.js, tag svelte@4.2.19); Svelte 5 plays the frames through element.animate, and since 5.0.0-next.240 also times tick-only transitions with WAAPI (CHANGELOG.md).

Any delay is a dummy WAAPI animation of that length that holds the intro's first keyframe (fill: 'forwards'), created in a microtask "so that transitions created in the same batch (e.g. on nested elements) all measure the DOM first … this still happens before the next paint" (source). A transition function may instead return a function, called in the next microtask — after the DOM update — which is how deferred transitions coordinate (source).

Built-in transitions and crossfade ​

svelte/transition provides fade, blur, fly, slide, scale, draw (SVG stroke) and crossfade; each reads the element's computed style once and returns a css template (e.g. fade reads the element's opacity o and returns a css that writes opacity: t * o, transition/index.js). crossfade({ fallback }) returns a [send, receive] pair sharing two maps keyed by a user key. Each side registers its node and returns a deferred transition; in the microtask, if the counterpart map has the key, it measures both nodes with getBoundingClientRect and returns a css that translates and scales the incoming node from the outgoing node's rect, otherwise it uses fallback (crossfade). This is a shared-element transition between two elements that exist simultaneously in the DOM — the outgoing one kept by its outro.

animate: and FLIP in keyed each blocks ​

$.animation(...) attaches an animation manager { element, measure, apply, fix, unfix } to the item's effect as nodes.a (animation). Keyed {#each} reconciliation drives it in four places (reconcile):

StepCodeEffect
1. start of reconcile, before any DOM moveeffect.nodes?.a?.measure() for every surviving item (source)from = element.getBoundingClientRect() — the old geometry, read from the live DOM (items coming in for the first time are skipped)
2. items being removed, after their own measurea.fix() (source)an outroing item is switched to position: absolute with its computed width/height and translated back to from, so it leaves the flow (survivors close the gap and animate) while it keeps painting where it was
3. DOM movesthe reconciliation itselfitems are inserted/moved; removed items are pause_effects-ed
4. microtask after reconcile, before paintqueue_micro_task(() => … a.apply()) (source)to = element.getBoundingClientRect() — the new geometry (forced layout); if any edge differs, call the user's animation function with { from, to } and play its css through the same animate(), aborting any previous move animation first

The built-in flip computes the translate and scale that map to back onto from (correcting for transform-origin, parent transforms and CSS zoom) and returns css: (t, u) => transform: … translate(u*dx, u*dy) scale(…) with a distance-dependent default duration Math.sqrt(d) * 120 (animate/index.js). The docs: "Animations do not run when an element is added or removed, only when the index of an existing data item within the each block changes", and to is "the DOMRect of the element in its final position after the list has been reordered and the DOM updated" (16-animate.md).

The whole sequence runs inside Svelte's effect flush: a state change schedules the each block's effect; when the batch flushes (a microtask after the change, or synchronously under flushSync), reconcile measures, moves, and queues apply in a microtask — all before the browser's next rendering opportunity.

svelte/motion: Tween, Spring, prefersReducedMotion ​

Tween and Spring (since 5.8.0) are classes whose current and target are rune state signals; reading .current in a template subscribes to it. tween.target = v / tween.set(v, opts) starts a task on Svelte's requestAnimationFrame loop that interpolates from the current value and writes current each frame (Tween.set); Tween.of(() => expr) creates one and retargets it from a render_effect whenever expr changes (Tween.of). Spring integrates a semi-implicit step per frame from the last two values (tick_spring):

js
// packages/svelte/src/motion/spring.js — tick_spring
const delta = target_value - current_value;
const velocity = (current_value - last_value) / (ctx.dt || 1 / 60); // guard div by 0
const spring = ctx.opts.stiffness * delta;
const damper = ctx.opts.damping * velocity;
const acceleration = (spring - damper) * ctx.inv_mass;
const d = (velocity + acceleration) * ctx.dt;

stiffness and damping are clamped to [0, 1] (defaults 0.15 and 0.8) and precision (0.01) is the settle threshold; dt is in 60 Hz frame units with elapsed time clamped to 1/30 s, "so that longer pauses (blocked thread or inactive tab) don't cause the spring to go haywire" (Spring.#update). set(v, { instant: true }) jumps; set(v, { preserveMomentum: ms }) zeroes the inverse mass and lets it recover over ms, so the spring coasts on its current trajectory — for flings (Spring.set). The Svelte 4 stores tweened and spring remain, marked @deprecated in favour of the classes. prefersReducedMotion is a MediaQuery on (prefers-reduced-motion: reduce) whose .current is reactive (motion/index.js).


Analysis ​

Where animation state lives ​

On the runtime's effect tree, not in components. A block's elements carry TransitionManagers in effect.nodes.t, each holding the running intro/outro Animation handles and the cached current_options (kept while a transition is in flight "so that reversible transitions reverse smoothly", source); a keyed item carries one animation manager in effect.nodes.a holding from, to and the running move animation. The visual progress of a CSS transition lives in the browser's Animation object (currentTime), and Svelte reads it back on reversal. The fact that a branch is leaving is the branch effect's PAUSED/INERT flags. Value animations (Tween, Spring) are plain objects holding state signals plus a task handle; their identity is whatever the component holds.

Declaring an animation ​

Transitions and animate: are declarative and explicit, attached to presence: the directive names a function, and the runtime decides when to run it — on block creation (intro), removal (outro) or reorder (animate). Nothing animates implicitly when an attribute or style changes; a changing value is animated only by routing it through Tween/Spring, which is implicit in the sense that setting target (or a Tween.of dependency changing) animates. The animation program itself is a pure function t → css string, sampled ahead of time; tick is the imperative escape hatch.

Time and the clock ​

Two clocks. CSS transitions and animate: moves play on the document timeline as WAAPI animations: duration is fixed when the keyframes are sampled and the browser advances them, off the main thread when the properties allow. tick callbacks, Tween and Spring run on Svelte's own task loop, a single requestAnimationFrame callback that calls each task with raf.now() (performance.now() in the browser) — deliberately not the rAF timestamp argument (loop.js, timing.js). raf is an internal mutable object (tick, now, tasks), which is how tests replace it.

Interruption and retargeting ​

Transitions reverse, they do not retarget: a bidirectional transition: interrupted mid-outro starts its intro from the outro's current t and runs for the remaining fraction of the duration, using the same cached options; velocity is not considered (the progress curve simply turns around). in:/out: are not reversible — "an in transition will continue to 'play' alongside the out transition, rather than reversing … If an out transition is aborted, transitions will restart from scratch" (15-in-and-out.md). animate: aborts the running move and starts a fresh one from the freshly measured from — which includes the old animation's current transform, since getBoundingClientRect reports the visual box — again without velocity. Tween.set restarts interpolation from current with the full duration, no velocity. Spring keeps velocity implicitly: velocity is (current − last) / dt, and a new target changes only delta, so the motion continues smoothly. Nothing is additive.

Curves and springs ​

Transitions and animate: are duration + easing (svelte/easing supplies the Penner set plus linear); easing is applied while sampling, so arbitrary JS easing functions work on WAAPI. Durations may be functions of distance (flip's Math.sqrt(d) * 120, crossfade's Math.sqrt(d) * 30). Spring is the only physics: unitless stiffness/damping in [0, 1] and a precision threshold, integrated per frame (frame-rate-normalised to 60 Hz units), plus preserveMomentum. There is no spring-driven transition or animate:.

Layout animation ​

animate: is FLIP on keyed each items only. First is read from the live DOM with getBoundingClientRect at the start of reconcile, in the each block's effect, before items are moved; Last is read in a microtask after reconcile, after the DOM mutation and before paint, forcing layout; Invert/Play is a WAAPI transform animation sampled from the user's css and run by the browser — on the compositor when only transform/opacity change (table above). It needs the previous geometry and obtains it by measuring before mutating, not from retained layout state. Items that leave are taken out of flow (fix: position: absolute + a compensating translate) so their outro keeps them visible at their old place while survivors FLIP into the gap. Elements outside a keyed each, or size changes that are not reorders, have no layout animation; crossfade provides element-to-element transitions by measuring both elements in the deferred-transition microtask.

Enter and exit ​

Exit keeps the real DOM subtree and its effects alive: pause_effect marks the branch INERT (no further updates — the content is frozen as of the removal), plays every local transition's outro and destroys the branch when the last completes; elements without transitions in that block stay too. An outroing element is inert. A reversal before completion resumes the same effect and DOM. For keyed each items, a leaving item is additionally fixed out of flow. Enter runs in() on creation, skipped for local transitions whose parent block is itself being created (and on hydration/initial mount unless intro: true is passed to mount). Elements report introstart/introend/outrostart/outroend events.

Scheduling, reduced motion and testing ​

WAAPI animations need no frames from Svelte; the task loop books requestAnimationFrame only while raf.tasks is non-empty and stops when every task returns false (loop.js); Spring drops its task once settled. Reduced motion is the author's job: Svelte does not consult the preference, and because transitions are WAAPI, a CSS prefers-reduced-motion reset does not affect them — the docs direct authors to prefersReducedMotion.current to shrink or skip the transition (source). Testing: Svelte's own suite swaps raf.tick and raf.now for a manual clock and replaces Element.prototype.animate with a fake Animation advanced by raf.tick(time), which calls flushSync() first (tests/animation-helpers.js) — a deterministic clock for both the task loop and WAAPI; flushSync itself is public.

Lessons for sparkles:ui ​

  • Svelte's FLIP shape matches a frame pipeline cleanly: measure before mutation, measure after, animate a transform. In sparkles:ui neither measurement needs a DOM read: First is last frame's committed rect in the element store, Last is this frame's layout result, and the inversion is applied after layout, before paint (UI-Q9). Svelte's two forced layouts per reorder are the cost sparkles avoids by construction.
  • "Pause, don't destroy" is the UI-Q10 answer in a retained runtime: the exiting branch stays in the tree, frozen (INERT), with its transitions playing, and is resurrected on reversal. In an immediate-mode builder the equivalent is a store entry for an identity absent from this frame's set that keeps a retained snapshot of its last subtree (Svelte's choice: content frozen at removal) — distinct from React Spring's re-build-with-leave-phase alternative on Motion and React Spring.
  • fix() is a necessary detail: a leaving item must be taken out of layout yet painted at its old rect, or survivors cannot animate into the gap. In sparkles that means the store can paint a closing element at its last rect without it participating in this frame's layout.
  • A transition as t → style is purity-friendly and precomputable. Svelte samples it into keyframes once; sparkles can evaluate the same pure function per frame from the store's phase, with no allocation and no callback into components.
  • Reversal versus velocity. Svelte's reversible transitions keep progress but not velocity; only Spring, whose velocity is the difference of its last two values, retargets smoothly — the minimal state an element-store spring needs is (current, previous, target).

Strengths ​

  • Exit animations are a property of the block machinery: any removed subtree, including untransitioned children, stays until its outros finish, and reversal resumes the same DOM.
  • Transition programs are small pure functions; css output plays as WAAPI, often off the main thread; arbitrary easing still works because it is sampled.
  • animate:flip gives keyed-list FLIP with one directive, including leaving-item out-of-flow handling and crossfade shared elements.
  • Local-by-default transitions avoid cascades of animations when an ancestor block appears or disappears.
  • Spring/Tween are rune-native and need no subscription plumbing; preserveMomentum supports flings.

Weaknesses ​

  • Layout animation is limited to reorders in keyed each blocks; resizes and moves caused by other changes do not animate.
  • FLIP and crossfade force synchronous layout (getBoundingClientRect) before and after each reconcile.
  • Transitions and moves are duration-based; interruption keeps progress but drops velocity; animate: restarts on every reorder.
  • Keyframes are sampled at 60 Hz from the start state; a css function that reads changing state during playback cannot be expressed, and tick is main-thread.
  • Reduced motion is entirely opt-in per transition, and the WAAPI choice defeats the common CSS-reset approach.

Key design decisions and trade-offs ​

DecisionRationaleTrade-off
Transitions are functions t → css sampled into WAAPI keyframesdeclarative output the browser can play off-thread; any JS easing worksfixed at start; 60 Hz sampling; bypasses CSS prefers-reduced-motion resets
Leaving blocks are paused (INERT) until every outro finishesthe DOM and effects survive the exit and can be resumed on reversalthe leaving content is frozen at removal; outro bookkeeping on the effect tree
Transitions are local by default, |global opt-inprevents a whole subtree animating when an ancestor block togglesauthors must know which block owns a transition
animate: measures at the start of reconcile and in a microtask afterthe DOM is the geometry store; both reads land before painttwo forced layouts per reorder; only keyed each items animate
Leaving each items are fixed out of flowsurvivors can FLIP into the gap while the leaver outros in placeinline style rewriting (position, size, transform) restored by unfix
Bidirectional transitions reverse from current tsmooth reversal without velocity bookkeepingno momentum; in:/out: cannot reverse at all
Tween/Spring are rune-backed classes on a rAF task loopcomposes with the reactivity system; reads subscribemain-thread, per-frame signal writes; a separate clock from the WAAPI transitions

Sources ​