Skip to main content
Lottie to codeUpdated November 20265 min read

Lottie to Svelte: play a Lottie file in a Svelte app

The short answer

In Svelte you mount a Lottie player on an element in onMount and destroy it in onDestroy. If the animation needs to react to state, convert it into an .exode file and render it with the exodeui-svelte runtime, which passes inputs in and reports events back as normal Svelte props and callbacks.

What matters

  • A Lottie player is a class you instantiate against an element — it fits Svelte actions well.
  • Always destroy the player in onDestroy or you will leak animation frames on navigation.
  • There is an official Svelte runtime (exodeui-svelte) that takes inputs and events instead of playback only.
  • One .exode file works in Svelte, React, Vue, Flutter and native apps without redoing the motion.
  • Free Community plan; Professional is $5 per month billed yearly.

Playing a Lottie file in Svelte today

The standard approach is a player component that takes the exported JSON and renders it. It is small, it works, and if the animation is a decorative loader or a success tick, it is the right answer.

What it cannot do is change behaviour. If the design calls for the animation to react — pause on hover, spring back when dragged, switch to an error state when a request fails — that logic has to be written in your component around the player, and the animation itself stays fixed.

The import route

The other option is to move the animation into ExodeUI, where the timeline, the inputs and the logic live in one file. Drop your Lottie JSON on /import/lottie, check how it converts, then add states and physics to it on the canvas instead of in code.

Keep the .exode JSON with your other assets and pass it as artboard; the runtime plays it on a canvas, so nothing is code-generated into your Svelte components.

We make ExodeUI, so treat that suggestion as the vendor recommending its own runtime. If the animation is a fixed performance with nothing to interact with, a Lottie player is the simpler answer.

The Svelte code

The Svelte runtime exposes a canvas component plus createExodeUI() for inputs. Pass the .exode file as artboard, seed values once the engine is ready, and receive named events through ontrigger.

$npm install exodeui-svelte
Svelte
<script>
  import { ExodeUICanvas, createExodeUI } from 'exodeui-svelte';

  // loader.exode, imported or fetched once and held in a variable
  export let loaderArtboard;

  const exo = createExodeUI();

  function onReady(engine) {
    exo.setEngine(engine);
    exo.setInputNumber('progress', 0.4);
  }
</script>

<ExodeUICanvas
  artboard={loaderArtboard}
  fit="Layout"
  onready={onReady}
  ontrigger={(name) => {
    // named events fired from the file
  }}
  style="width: 100%; height: 160px"
/>

Inputs go in, events come out. The component you ship is a canvas that renders the file, not a pile of keyframes translated into framework code, so a designer can retune the spring without touching the component.

Which route to pick

Use the plain player when the animation is a one-way performance: a loading state, an empty-state illustration, a marketing flourish. There is nothing to interact with, so a timeline is the whole job.

Use the runtime when the thing on screen is a component people operate. A toggle, a drag-to-dismiss card, a form field with focus, loading, error and success states — those are not animations that play, they are motion bound to state, and putting the motion in the file keeps the component short.

Questions

How do I add a Lottie animation to Svelte?

Instantiate a Lottie player such as lottie-web against a bound element inside onMount, load the JSON, and call destroy in onDestroy. A Svelte action wrapping the player keeps the lifecycle in one place if you use it in several components.

Do I need a LottieFiles subscription to play a Lottie file?

No. Lottie is an open JSON format and the players that render it are open source. A LottieFiles subscription buys the asset library, hosting and team workflow, not permission to render the format.

Can a Lottie file hold state or logic?

A Lottie file is a timeline. Interactivity comes from the player API — you call play, pause or go to a frame from your code — so the states live in your component, not in the file.

Does the Lottie import upload my file?

No. The conversion runs in your browser and the file stays on your device. Only a usage count is recorded.

Is there a Svelte Lottie component?

Community wrappers exist, but lottie-web itself is a plain JavaScript class and works in Svelte without a wrapper. If you would rather have states and inputs than playback, ExodeUI publishes an official Svelte runtime (exodeui-svelte) that renders .exode files the same way.

Related

Open the editor and try it on your own file

Free Community plan. No credit card. One open .exode file runs in React, Vue, Flutter and more.

Open the web editor
ExodeUI

EXODEUI DESKTOP

A little space for good ideas.

Choose your platform to get the official ExodeUI installer.

Looking for Intel Mac or other builds? Explore the web studio or check all download options.

Prefer the browser? Try for free in your browser →