🎥 Make videos programmatically with React
static file
Jonny Burger531aeeb86905
diff --git a/packages/docs/docs/lottie/lottie-comp.md b/packages/docs/docs/lottie/lottie-comp.mdindex cdf57d5e56..17cb4eb942 100644--- a/packages/docs/docs/lottie/lottie-comp.md+++ b/packages/docs/docs/lottie/lottie-comp.md@@ -56,3 +56,7 @@ CSS properties to be applied to the `<div>` that contains the animation.### `className`A CSS class name to be given to the `<div>` that contains the animation.++## See also++- [Loading Lottie animations using `staticFile()`](/docs/lottie/staticfile)diff --git a/packages/docs/docs/lottie/lottie-staticfile.md b/packages/docs/docs/lottie/lottie-staticfile.mdnew file mode 100644index 0000000000..c0b8cabf4a--- /dev/null+++ b/packages/docs/docs/lottie/lottie-staticfile.md@@ -0,0 +1,45 @@+---+id: lottie-staticfile+sidebar_label: "Loading from staticFile()"+title: "Loading Lottie animations from staticFile()"+slug: staticfile+---++In order to load a Lottie animation from a file that has been put into the `public/` folder, use [`staticFile()`](/docs/staticfile) in combination with [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) and Remotion's [`delayRender()`](/docs/delay-render) function.++Use the `LottieAnimationData` type to keep a state using React's `useState()` and only render the [`<Lottie>`](/docs/lottie/lottie) component once the data has been fetched.++```tsx twoslash title="Animation.tsx"+import { continueRender, delayRender, staticFile } from "remotion";+import { useEffect, useState } from "react";+import { Lottie, LottieAnimationData } from "@remotion/lottie";++const Balloons = () => {+ const [handle] = useState(() => delayRender("Loading Lottie animation"));++ const [animationData, setAnimationData] =+ useState<LottieAnimationData | null>(null);++ useEffect(() => {+ fetch(staticFile("animation.json"))+ .then((data) => data.json())+ .then((json) => {+ setAnimationData(json);+ continueRender(handle);+ })+ .catch((err) => {+ console.log("Animation failed to load", err);+ });+ }, [handle]);++ if (!animationData) {+ return null;+ }++ return <Lottie animationData={animationData} />;+};+```++## See also++- [`<Lottie>`](/docs/lottie/lottie)diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex 2df3c5b152..ff6b706a60 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -234,7 +234,7 @@ module.exports = {type: "doc",id: "lottie/lottie-index",},- items: ["lottie/lottie-comp"],+ items: ["lottie/lottie-comp", "lottie/lottie-staticfile"],},{type: "category",diff --git a/packages/lottie/src/Lottie.tsx b/packages/lottie/src/Lottie.tsxindex a1c836e69d..99c3ebbcbd 100644--- a/packages/lottie/src/Lottie.tsx+++ b/packages/lottie/src/Lottie.tsx@@ -5,11 +5,13 @@ import {useEffect, useRef, useState} from 'react';import {continueRender, delayRender, useCurrentFrame} from 'remotion';import {getNextFrame} from './utils';+export type LottieAnimationData = Record<string | number | symbol, unknown>;+export interface LottieProps {/*** JSON object with the animation data.* */- animationData: Record<string | number | symbol, unknown>;+ animationData: LottieAnimationData;/*** If the animation should loop after its end.*/diff --git a/packages/lottie/src/index.ts b/packages/lottie/src/index.tsindex e107ccb275..0984168d5b 100644--- a/packages/lottie/src/index.ts+++ b/packages/lottie/src/index.ts@@ -1 +1 @@-export {Lottie, LottieProps} from './Lottie';+export {Lottie, LottieAnimationData, LottieProps} from './Lottie';