Skip to content

🎥 Make videos programmatically with React

Converted the Loop component to follow the same design principle as the Freeze component

bpedersen92b87f52756d
diff --git a/TESTING.md b/TESTING.md
index 4c93ebecf5..77c30aa464 100644
--- a/TESTING.md
+++ b/TESTING.md
@@ -4,10 +4,10 @@ For general information concerning contribution, please consult [CONTRIBUTION.md
### Core Coverage
-![statements](https://img.shields.io/badge/Coverage_statements-77.2%25-yellow.svg)
-![branches](https://img.shields.io/badge/Coverage_branches-71.17%25-yellow.svg)
-![functions](https://img.shields.io/badge/Coverage_functions-61.14%25-red.svg)
-![lines](https://img.shields.io/badge/Coverage_lines-76.09%25-yellow.svg)
+![statements](https://img.shields.io/badge/Coverage_statements-77.19%25-yellow.svg)
+![branches](https://img.shields.io/badge/Coverage_branches-71.05%25-yellow.svg)
+![functions](https://img.shields.io/badge/Coverage_functions-60.72%25-red.svg)
+![lines](https://img.shields.io/badge/Coverage_lines-76.11%25-yellow.svg)
### Running tests
diff --git a/packages/core/src/loop/index.tsx b/packages/core/src/loop/index.tsx
index e0c38542c2..cd94e70954 100644
--- a/packages/core/src/loop/index.tsx
+++ b/packages/core/src/loop/index.tsx
@@ -1,5 +1,8 @@
-import React from 'react';
-import {Sequence, useVideoConfig} from '..';
+import React, {useContext, useMemo} from 'react';
+import {
+ TimelineContext,
+ TimelineContextValue,
+} from '../timeline-position-state';
export type LoopProps = {
children: React.ReactNode;
@@ -11,10 +14,11 @@ export type LoopProps = {
export const Loop: React.FC<LoopProps> = ({
durationInFrames,
- times,
+ times = Infinity,
children,
}) => {
- const video = useVideoConfig();
+ const context = useContext(TimelineContext);
+ const {frame} = context;
if (typeof durationInFrames !== 'number') {
throw new TypeError(
@@ -41,27 +45,27 @@ export const Loop: React.FC<LoopProps> = ({
);
}
- if (times && times % 1 !== 0) {
+ if (times !== Infinity && times % 1 !== 0) {
throw new TypeError(
`The "times" prop of a loop must be an integer, but got ${times}.`
);
}
- const loopCount = times
- ? times
- : Math.floor(video.durationInFrames / durationInFrames);
+ const value: TimelineContextValue = useMemo(() => {
+ // Number of loops of the durationInFrames to this frame
+ const loopsToThisFrame = Math.floor(frame / durationInFrames);
+ const currentFrame =
+ loopsToThisFrame < times ? frame % durationInFrames : frame;
+
+ return {
+ ...context,
+ frame: currentFrame,
+ };
+ }, [context, frame, durationInFrames, times]);
+
return (
- <>
- {new Array(loopCount).fill('').map((_, index) => (
- <Sequence
- // eslint-disable-next-line react/no-array-index-key
- key={index}
- from={durationInFrames * index}
- durationInFrames={durationInFrames}
- >
- {children}
- </Sequence>
- ))}
- </>
+ <TimelineContext.Provider value={value}>
+ {children}
+ </TimelineContext.Provider>
);
};
diff --git a/packages/core/src/test/loop-validation.test.tsx b/packages/core/src/test/loop-validation.test.tsx
index 83750bad2c..e99f2c98b1 100644
--- a/packages/core/src/test/loop-validation.test.tsx
+++ b/packages/core/src/test/loop-validation.test.tsx
@@ -102,24 +102,24 @@ describe('Loop-validation render should throw with invalid props', () => {
);
});
});
- describe('Should NOT throw with valid props', () => {
- test('It should allow null as children', () => {
- expect(() =>
- render(
- <Wrapper>
- <Loop durationInFrames={50}>{null}</Loop>
- </Wrapper>
- )
- ).not.toThrow();
- });
- test('It should allow undefined as children', () => {
- expect(() =>
- render(
- <Wrapper>
- <Loop durationInFrames={50}>{undefined}</Loop>
- </Wrapper>
- )
- ).not.toThrow();
- });
+});
+describe('Should NOT throw with valid props', () => {
+ test('It should allow null as children', () => {
+ expect(() =>
+ render(
+ <Wrapper>
+ <Loop durationInFrames={50}>{null}</Loop>
+ </Wrapper>
+ )
+ ).not.toThrow();
+ });
+ test('It should allow undefined as children', () => {
+ expect(() =>
+ render(
+ <Wrapper>
+ <Loop durationInFrames={50}>{undefined}</Loop>
+ </Wrapper>
+ )
+ ).not.toThrow();
});
});