Skip to content

Commit

Permalink
Merge pull request #1384 from remotion-dev/lottie-on-animation-loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyBurger committed Oct 9, 2022
2 parents 00a3825 + 3be84fd commit bd379f0
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
40 changes: 40 additions & 0 deletions packages/docs/docs/lottie/lottie-comp.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export const MyAnimation: React.FC = () => {

A JSON object that adheres to the Lottie schema.

:::note
If the identity of the object changes, the animation will be re-initialized. Consider memoizing the animationData value.
:::

### `className`

A CSS class name to be given to the `<div>` that contains the animation.
Expand All @@ -56,3 +60,39 @@ The speed of the animation. A higher number is faster. Default: `1`.
### `style`

CSS properties to be applied to the `<div>` that contains the animation.

### `onAnimationLoaded`

_available from v3.2.29, optional_

Callback that gets invoked when the Lottie animation has been initialized. The Lottie [`AnimationItem`](https://airbnb.io/lottie/#/web?id=usage-1) gets passed as a parameter.

```tsx twoslash
import type { LottieAnimationData } from "@remotion/lottie";

const animationData: LottieAnimationData = {
fr: 0,
w: 0,
h: 0,
op: 0,
};

// ---cut---

import { Lottie } from "@remotion/lottie";
import { AnimationItem } from "lottie-web";
import { useCallback } from "react";

const Comp: React.FC = () => {
const onAnimationLoaded = useCallback((item: AnimationItem) => {
console.log(item.renderer);
}, []);

return (
<Lottie
animationData={animationData}
onAnimationLoaded={onAnimationLoaded}
/>
);
};
```
16 changes: 14 additions & 2 deletions packages/example/src/Lottie/Loader/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {getLottieMetadata, Lottie, LottieAnimationData} from '@remotion/lottie';
import {useEffect, useState} from 'react';
import {AnimationItem} from 'lottie-web';
import {useCallback, useEffect, useState} from 'react';
import {
continueRender,
delayRender,
Expand Down Expand Up @@ -27,6 +28,10 @@ const LottieLoader = () => {
});
}, [handle]);

const onAnimationLoaded = useCallback((item: AnimationItem) => {
console.log(item.renderer);
}, []);

if (!animationData) {
return null;
}
Expand All @@ -35,7 +40,14 @@ const LottieLoader = () => {
const nLoop = df && frame > 0 ? Math.ceil(frame / df) : 1;
const direction = nLoop % 2 === 0 ? 'backward' : 'forward';

return <Lottie loop animationData={animationData} direction={direction} />;
return (
<Lottie
loop
animationData={animationData}
direction={direction}
onAnimationLoaded={onAnimationLoaded}
/>
);
};

export default LottieLoader;
7 changes: 7 additions & 0 deletions packages/lottie/src/Lottie.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const Lottie = ({
loop,
playbackRate,
style,
onAnimationLoaded,
}: LottieProps) => {
if (typeof animationData !== 'object') {
throw new Error(
Expand All @@ -27,6 +28,10 @@ export const Lottie = ({
const animationRef = useRef<AnimationItem>();
const lastFrameRef = useRef<number | null>(null);
const containerRef = useRef<HTMLDivElement>(null);

const onAnimationLoadedRef = useRef<LottieProps['onAnimationLoaded']>();
onAnimationLoadedRef.current = onAnimationLoaded;

const [handle] = useState(() =>
delayRender('Waiting for Lottie animation to load')
);
Expand Down Expand Up @@ -54,6 +59,8 @@ export const Lottie = ({

animation.addEventListener('DOMLoaded', onComplete);

onAnimationLoadedRef.current?.(animation);

return () => {
lastFrameRef.current = animation.currentFrame;
animation.removeEventListener('DOMLoaded', onComplete);
Expand Down
5 changes: 5 additions & 0 deletions packages/lottie/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type {AnimationItem} from 'lottie-web';
import type {CSSProperties} from 'react';

export type LottieAnimationData = {
Expand Down Expand Up @@ -32,4 +33,8 @@ export type LottieProps = {
* CSS properties to apply to the container of the animation.
*/
style?: CSSProperties;
/**
* Callback that gets invoked when new animation data has been initialized
*/
onAnimationLoaded?: (animation: AnimationItem) => void;
};

1 comment on commit bd379f0

@vercel
Copy link

@vercel vercel bot commented on bd379f0 Oct 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.