Skip to content

🎥 Make videos programmatically with React

feat(lottie): use union type for direction prop

Arthur Denner5951bbc2c20f
diff --git a/packages/docs/docs/lottie/index.md b/packages/docs/docs/lottie/index.md
index e5c7415f59..1bdb8a436e 100644
--- a/packages/docs/docs/lottie/index.md
+++ b/packages/docs/docs/lottie/index.md
@@ -52,7 +52,7 @@ You can now start using the [`<Lottie>`](/docs/lottie/lottie) component in your
- Playing Lottie animations using `lottie-web`
- Change the speed of the animation
-- Playing animation in reverse
+- Playing animation forwards and backwards
- Playing remote files
- Determining dimensions and duration of a Lottie animation
diff --git a/packages/docs/docs/lottie/lottie-comp.md b/packages/docs/docs/lottie/lottie-comp.md
index f2767e1e57..fd582b78b2 100644
--- a/packages/docs/docs/lottie/lottie-comp.md
+++ b/packages/docs/docs/lottie/lottie-comp.md
@@ -31,22 +31,28 @@ export const MyAnimation: React.FC = () => {
A JSON object that adheres to the Lottie schema.
+### `className`
+
+A CSS class name to be given to the `<div>` that contains the animation.
+
+### `direction`
+
+_"forward"_ or _"backward"_
+
+The direction of the animation. Default: `forward`.
+
### `loop`
_boolean_
-Whether the animation should loop or not. Default false.
+Whether the animation should loop or not. Default: `false`.
### `playbackRate`
_number_
-The speed of the animation. A higher number is faster. Default 1.
+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.
-
-### `className`
-
-A CSS class name to be given to the `<div>` that contains the animation.
diff --git a/packages/example/src/Lottie/Loader/index.tsx b/packages/example/src/Lottie/Loader/index.tsx
index 15c7da1c9c..8237898988 100644
--- a/packages/example/src/Lottie/Loader/index.tsx
+++ b/packages/example/src/Lottie/Loader/index.tsx
@@ -33,7 +33,7 @@ const LottieLoader = () => {
const {durationInFrames: df} = getLottieMetadata(animationData) || {};
const nLoop = df && frame > 0 ? Math.ceil(frame / df) : 1;
- const direction = nLoop % 2 === 0 ? -1 : 1;
+ const direction = nLoop % 2 === 0 ? 'backward' : 'forward';
return <Lottie loop animationData={animationData} direction={direction} />;
};
diff --git a/packages/lottie/src/Lottie.tsx b/packages/lottie/src/Lottie.tsx
index cf32de0ffb..1bd1f11643 100644
--- a/packages/lottie/src/Lottie.tsx
+++ b/packages/lottie/src/Lottie.tsx
@@ -63,7 +63,7 @@ export const Lottie = ({
useEffect(() => {
if (animationRef.current && direction) {
- animationRef.current.setDirection(direction);
+ animationRef.current.setDirection(direction === 'backward' ? -1 : 1);
}
}, [direction]);
diff --git a/packages/lottie/src/test/utils.test.ts b/packages/lottie/src/test/utils.test.ts
index d369d16a28..3684361825 100644
--- a/packages/lottie/src/test/utils.test.ts
+++ b/packages/lottie/src/test/utils.test.ts
@@ -29,13 +29,13 @@ describe('getNextFrame', () => {
describe('when direction is reverse and loop is falsy', () => {
it('returns the correct frame if current frame is smaller than total frames', () => {
expect(
- getNextFrame({currentFrame: 15, totalFrames: 20, direction: -1})
+ getNextFrame({currentFrame: 15, totalFrames: 20, direction: 'backward'})
).toBe(5);
});
it('returns frame zero if current frame is bigger than total frames', () => {
expect(
- getNextFrame({currentFrame: 23, totalFrames: 20, direction: -1})
+ getNextFrame({currentFrame: 23, totalFrames: 20, direction: 'backward'})
).toBe(0);
});
});
@@ -46,7 +46,7 @@ describe('getNextFrame', () => {
getNextFrame({
currentFrame: 15,
totalFrames: 20,
- direction: -1,
+ direction: 'backward',
loop: true,
})
).toBe(5);
@@ -57,7 +57,7 @@ describe('getNextFrame', () => {
getNextFrame({
currentFrame: 23,
totalFrames: 20,
- direction: -1,
+ direction: 'backward',
loop: true,
})
).toBe(17);
diff --git a/packages/lottie/src/types.ts b/packages/lottie/src/types.ts
index a080edb8ee..99f06811cb 100644
--- a/packages/lottie/src/types.ts
+++ b/packages/lottie/src/types.ts
@@ -13,9 +13,13 @@ export type LottieProps = {
* */
animationData: LottieAnimationData;
/**
- * The direction of the animation. 1 is forward, -1 is reverse.
+ * CSS classes to apply on the container of the animation.
+ */
+ className?: string;
+ /**
+ * The direction of the animation. Defaults to forward.
*/
- direction?: 1 | -1;
+ direction?: 'forward' | 'backward';
/**
* If the animation should loop after its end.
*/
@@ -25,11 +29,7 @@ export type LottieProps = {
*/
playbackRate?: number;
/**
- * CSS classes to apply on the container of the animation.
- */
- className?: string;
- /**
- * CSS properties to apply on the container of the animation.
+ * CSS properties to apply to the container of the animation.
*/
style?: CSSProperties;
};
diff --git a/packages/lottie/src/utils.ts b/packages/lottie/src/utils.ts
index f622caef74..4de4e36fd6 100644
--- a/packages/lottie/src/utils.ts
+++ b/packages/lottie/src/utils.ts
@@ -1,9 +1,9 @@
-interface Params {
+import type {LottieProps} from './types';
+
+type Params = Pick<LottieProps, 'direction' | 'loop'> & {
currentFrame: number;
- direction?: number;
- loop?: boolean;
totalFrames: number;
-}
+};
export const getNextFrame = ({
currentFrame,
@@ -15,7 +15,7 @@ export const getNextFrame = ({
? currentFrame % totalFrames
: Math.min(currentFrame, totalFrames);
- if (direction === -1) {
+ if (direction === 'backward') {
return totalFrames - nextFrame;
}