🎥 Make videos programmatically with React
Series
Jonny Burger47c74a824b41
diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex 2ff77558e2..d0bab87dd4 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -27,8 +27,8 @@ export * from './random';export * from './ready-manager';export {registerRoot} from './register-root';export {Sequence} from './sequencing';+export {Series} from './series';export * from './spring';-export {Stagger, StaggerChild} from './stagger';export * from './use-frame';export * from './use-video-config';export * from './video';diff --git a/packages/core/src/series/flatten-children.tsx b/packages/core/src/series/flatten-children.tsxnew file mode 100644index 0000000000..00acc0de89--- /dev/null+++ b/packages/core/src/series/flatten-children.tsx@@ -0,0 +1,17 @@+import React from 'react';++type ReactChildArray = ReturnType<typeof React.Children.toArray>;++export const flattenChildren = (children: React.ReactNode): ReactChildArray => {+ const childrenArray = React.Children.toArray(children);+ return childrenArray.reduce((flatChildren: ReactChildArray, child) => {+ if ((child as React.ReactElement<unknown>).type === React.Fragment) {+ return flatChildren.concat(+ flattenChildren((child as React.ReactElement<any>).props.children)+ );+ }++ flatChildren.push(child);+ return flatChildren;+ }, []);+};diff --git a/packages/core/src/stagger/index.tsx b/packages/core/src/series/index.tsxsimilarity index 58%rename from packages/core/src/stagger/index.tsxrename to packages/core/src/series/index.tsxindex 3dea62062d..a8015900f4 100644--- a/packages/core/src/stagger/index.tsx+++ b/packages/core/src/series/index.tsx@@ -1,27 +1,30 @@import React, {Children, FC, PropsWithChildren, useMemo} from 'react';import {Sequence, SequenceProps} from '../sequencing';import {validateDurationInFrames} from '../validation/validate-duration-in-frames';+import {flattenChildren} from './flatten-children';-type StaggerChildProps = PropsWithChildren<+type SeriesSequenceProps = PropsWithChildren<{durationInFrames: number;offset?: number;} & Pick<SequenceProps, 'layout' | 'name'>>;-const StaggerChild = ({children}: StaggerChildProps) => {+const SeriesSequence = ({children}: SeriesSequenceProps) => {// eslint-disable-next-line react/jsx-no-useless-fragmentreturn <>{children}</>;};-const Stagger: FC = ({children}) => {+const Series: FC & {+ Sequence: typeof SeriesSequence;+} = ({children}) => {const childrenValue = useMemo(() => {let startFrame = 0;- return Children.map(children, (child, i) => {+ return Children.map(flattenChildren(children), (child, i) => {const castedChild = (child as unknown) as| {- props: StaggerChildProps;- type: typeof StaggerChild;+ props: SeriesSequenceProps;+ type: typeof SeriesSequence;}| string;if (typeof castedChild === 'string') {@@ -30,13 +33,13 @@ const Stagger: FC = ({children}) => {}throw new TypeError(- `The <Stagger> component only accepts a list of <StaggerChild /> components as it's children, but you passed a string "${castedChild}"`+ `The <Series /> component only accepts a list of <Series.Sequence /> components as it's children, but you passed a string "${castedChild}"`);}- if (castedChild.type !== StaggerChild) {+ if (castedChild.type !== SeriesSequence) {throw new TypeError(- "The <Stagger> component only accepts a list of <StaggerChild /> components as it's children"+ "The <Series /> component only accepts a list of <Series.Sequence /> components as it's children");}@@ -44,7 +47,7 @@ const Stagger: FC = ({children}) => {if (!castedChild || !castedChild.props.children) {throw new TypeError(- `A <StaggerChild /> component (${debugInfo}) doesn't have any children.`+ `A <Series.Sequence /> component (${debugInfo}) doesn't have any children.`);}@@ -54,23 +57,23 @@ const Stagger: FC = ({children}) => {children: _children,...passedProps} = castedChild.props;- validateDurationInFrames(durationInFramesProp, `<StaggerChild />`);+ validateDurationInFrames(durationInFramesProp, `<Series.Sequence />`);const offset = castedChild.props.offset ?? 0;if (Number.isNaN(offset)) {throw new TypeError(- `The "offset" property of a <StaggerChild /> must not be NaN, but got NaN (${debugInfo}).`+ `The "offset" property of a <Series.Sequence /> must not be NaN, but got NaN (${debugInfo}).`);}if (!Number.isFinite(offset)) {throw new TypeError(- `The "offset" property of a <StaggerChild /> must be finite, but got ${offset} (${debugInfo}).`+ `The "offset" property of a <Series.Sequence /> must be finite, but got ${offset} (${debugInfo}).`);}if (offset % 1 !== 0) {throw new TypeError(- `The "offset" property of a <StaggerChild /> must be finite, but got ${offset} (${debugInfo}).`+ `The "offset" property of a <Series.Sequence /> must be finite, but got ${offset} (${debugInfo}).`);}@@ -92,4 +95,6 @@ const Stagger: FC = ({children}) => {return <>{childrenValue}</>;};-export {Stagger, StaggerChild};+Series.Sequence = SeriesSequence;++export {Series};diff --git a/packages/core/src/test/series.test.tsx b/packages/core/src/test/series.test.tsxnew file mode 100644index 0000000000..2515008214--- /dev/null+++ b/packages/core/src/test/series.test.tsx@@ -0,0 +1,280 @@+/* eslint-disable react/jsx-no-constructed-context-values */+import {render} from '@testing-library/react';+import React from 'react';+import {Series} from '../index';+import {TimelineContext} from '../timeline-position-state';+import {useCurrentFrame} from '../use-frame';+import {expectToThrow} from './expect-to-throw';++const First = () => {+ const frame = useCurrentFrame();+ return <div>{'first ' + frame}</div>;+};++const Second = () => {+ const frame = useCurrentFrame();+ return <div>{'second ' + frame}</div>;+};++const Third = () => {+ const frame = useCurrentFrame();+ return <div>{'third ' + frame}</div>;+};++const renderForFrame = (frame: number, markup: React.ReactNode) => {+ return render(+ <TimelineContext.Provider+ value={{+ rootId: '',+ frame,+ playing: false,+ }}+ >+ {markup}+ </TimelineContext.Provider>+ );+};++test('Basic series test', () => {+ const {queryByText} = renderForFrame(+ 10,+ <Series>+ <Series.Sequence durationInFrames={5}>+ <First />+ </Series.Sequence>+ <Series.Sequence durationInFrames={5}>+ <Second />+ </Series.Sequence>+ <Series.Sequence durationInFrames={5}>+ <Third />+ </Series.Sequence>+ </Series>+ );+ expect(queryByText(/^third\s0$/g)).not.toBe(null);+});++test('Should support fragments', () => {+ const {queryByText} = renderForFrame(+ 10,+ <Series>+ <Series.Sequence durationInFrames={5}>+ <First />+ </Series.Sequence>+ <>+ <Series.Sequence key="0" durationInFrames={5}>+ <Second />+ </Series.Sequence>+ <Series.Sequence key="1" durationInFrames={5}>+ <Third />+ </Series.Sequence>+ </>+ </Series>+ );++ expect(queryByText(/^third\s0$/g)).not.toBe(null);+});+test('Should not allow foreign elements', () => {+ expectToThrow(() => {+ render(+ <Series>+ <First />+ </Series>+ );+ }, /only accepts a/);+});+test('Should allow layout prop', () => {+ const {container} = renderForFrame(+ 0,+ <Series>+ <Series.Sequence durationInFrames={1}>+ <First />+ </Series.Sequence>+ </Series>+ );+ expect(container.outerHTML).toBe(+ '<div><div style="position: absolute; display: flex; width: 100%; height: 100%; top: 0px; bottom: 0px; left: 0px; right: 0px;"><div>first 0</div></div></div>'+ );++ const {container: withoutLayoutContainer} = renderForFrame(+ 0,+ <Series>+ <Series.Sequence durationInFrames={1} layout="none">+ <First />+ </Series.Sequence>+ </Series>+ );+ expect(withoutLayoutContainer.outerHTML).toBe(+ '<div><div>first 0</div></div>'+ );+});+test('Should render nothing after the end', () => {+ const {container} = renderForFrame(+ 10,+ <Series>+ <Series.Sequence durationInFrames={1}>+ <First />+ </Series.Sequence>+ </Series>+ );+ expect(container.outerHTML).toBe('<div></div>');+});+test('Should throw if invalid or no duration provided', () => {+ expectToThrow(() => {+ renderForFrame(+ 10,+ <Series>+ <Series.Sequence durationInFrames={NaN}>+ <First />+ </Series.Sequence>+ </Series>+ );+ }, /The "durationInFrames" prop of a <Series.Sequence \/> component must be an integer, but got NaN./);+ expectToThrow(() => {+ renderForFrame(+ 10,+ <Series>+ {/**+ * @ts-expect-error */}+ <Series.Sequence>+ <First />+ </Series.Sequence>+ </Series>+ );+ }, /The "durationInFrames" prop of a <Series.Sequence \/> component must be a number, but you passed a value of type undefined/);+});+test('Should allow whitespace', () => {+ const {queryByText} = renderForFrame(+ 11,+ <Series>+ <Series.Sequence durationInFrames={10}>+ <First />+ </Series.Sequence>{' '}+ <Series.Sequence durationInFrames={10}>+ <Second />+ </Series.Sequence>+ </Series>+ );+ expect(queryByText(/^second\s1$/g)).not.toBe(null);+});+test('Handle empty Series.Sequence', () => {+ expectToThrow(+ () =>+ renderForFrame(+ 11,+ <Series>+ <Series.Sequence durationInFrames={10}>+ <First />+ </Series.Sequence>+ <Series.Sequence durationInFrames={10} />+ </Series>+ ),+ /A <Series.Sequence \/> component \(index = 1, duration = 10\) doesn't have any children\./+ );+});++test('Should allow negative overlap prop', () => {+ const {container} = renderForFrame(+ 4,+ <Series>+ <Series.Sequence durationInFrames={5} layout="none">+ <First />+ </Series.Sequence>+ <Series.Sequence offset={-1} layout="none" durationInFrames={5}>+ <Second />+ </Series.Sequence>+ </Series>+ );+ expect(container.outerHTML).toBe(+ '<div><div>first 4</div><div>second 0</div></div>'+ );+});++test('Should allow positive overlap prop', () => {+ const {container} = renderForFrame(+ 5,+ <Series>+ <Series.Sequence durationInFrames={5} layout="none">+ <First />+ </Series.Sequence>+ <Series.Sequence offset={1} layout="none" durationInFrames={5}>+ <Second />+ </Series.Sequence>+ </Series>+ );+ expect(container.outerHTML).toBe('<div></div>');+});++test('Should disallow NaN as offset prop', () => {+ expectToThrow(() => {+ renderForFrame(+ 9,+ <Series>+ <Series.Sequence offset={NaN} layout="none" durationInFrames={5}>+ <Second />+ </Series.Sequence>+ </Series>+ );+ }, /The "offset" property of a <Series.Sequence \/> must not be NaN, but got NaN \(index = 0, duration = 5\)\./);+});++test('Should disallow Infinity as offset prop', () => {+ expectToThrow(() => {+ renderForFrame(+ 9,+ <Series>+ <Series.Sequence offset={Infinity} layout="none" durationInFrames={5}>+ <Second />+ </Series.Sequence>+ </Series>+ );+ }, /The "offset" property of a <Series.Sequence \/> must be finite, but got Infinity \(index = 0, duration = 5\)\./);+});++test('Should disallow non-integer numbers as offset prop', () => {+ expectToThrow(() => {+ renderForFrame(+ 9,+ <Series>+ <Series.Sequence offset={Math.PI} layout="none" durationInFrames={5}>+ <Second />+ </Series.Sequence>+ </Series>+ );+ }, /The "offset" property of a <Series.Sequence \/> must be finite, but got 3.141592653589793 \(index = 0, duration = 5\)\./);+});++test('Should cascade negative offset props', () => {+ const {container} = renderForFrame(+ 9,+ <Series>+ <Series.Sequence durationInFrames={5} layout="none">+ <First />+ </Series.Sequence>+ <Series.Sequence offset={-1} layout="none" durationInFrames={5}>+ <Second />+ </Series.Sequence>+ <Series.Sequence layout="none" durationInFrames={5}>+ <Third />+ </Series.Sequence>+ </Series>+ );+ expect(container.outerHTML).toBe('<div><div>third 0</div></div>');+});++test('Should cascade positive offset props', () => {+ const {container} = renderForFrame(+ 11,+ <Series>+ <Series.Sequence durationInFrames={5} layout="none">+ <First />+ </Series.Sequence>+ <Series.Sequence offset={1} layout="none" durationInFrames={5}>+ <Second />+ </Series.Sequence>+ <Series.Sequence layout="none" durationInFrames={5}>+ <Third />+ </Series.Sequence>+ </Series>+ );+ expect(container.outerHTML).toBe('<div><div>third 0</div></div>');+});diff --git a/packages/core/src/test/stagger.test.tsx b/packages/core/src/test/stagger.test.tsxdeleted file mode 100644index 65142c67d6..0000000000--- a/packages/core/src/test/stagger.test.tsx+++ /dev/null@@ -1,260 +0,0 @@-/* eslint-disable react/jsx-no-constructed-context-values */-import {render} from '@testing-library/react';-import React from 'react';-import {Stagger, StaggerChild} from '../stagger';-import {TimelineContext} from '../timeline-position-state';-import {useCurrentFrame} from '../use-frame';-import {expectToThrow} from './expect-to-throw';--const First = () => {- const frame = useCurrentFrame();- return <div>{'first ' + frame}</div>;-};--const Second = () => {- const frame = useCurrentFrame();- return <div>{'second ' + frame}</div>;-};--const Third = () => {- const frame = useCurrentFrame();- return <div>{'third ' + frame}</div>;-};--const renderForFrame = (frame: number, markup: React.ReactNode) => {- return render(- <TimelineContext.Provider- value={{- rootId: '',- frame,- playing: false,- }}- >- {markup}- </TimelineContext.Provider>- );-};--test('Basic stagger test', () => {- const {queryByText} = renderForFrame(- 10,- <Stagger>- <StaggerChild durationInFrames={5}>- <First />- </StaggerChild>- <StaggerChild durationInFrames={5}>- <Second />- </StaggerChild>- <StaggerChild durationInFrames={5}>- <Third />- </StaggerChild>- </Stagger>- );-- expect(queryByText(/^third\s0$/g)).not.toBe(null);-});-test('Should not allow foreign elements', () => {- expectToThrow(() => {- render(- <Stagger>- <First />- </Stagger>- );- }, /only accepts a/);-});-test('Should allow layout prop', () => {- const {container} = renderForFrame(- 0,- <Stagger>- <StaggerChild durationInFrames={1}>- <First />- </StaggerChild>- </Stagger>- );- expect(container.outerHTML).toBe(- '<div><div style="position: absolute; display: flex; width: 100%; height: 100%; top: 0px; bottom: 0px; left: 0px; right: 0px;"><div>first 0</div></div></div>'- );-- const {container: withoutLayoutContainer} = renderForFrame(- 0,- <Stagger>- <StaggerChild durationInFrames={1} layout="none">- <First />- </StaggerChild>- </Stagger>- );- expect(withoutLayoutContainer.outerHTML).toBe(- '<div><div>first 0</div></div>'- );-});-test('Should render nothing after the end', () => {- const {container} = renderForFrame(- 10,- <Stagger>- <StaggerChild durationInFrames={1}>- <First />- </StaggerChild>- </Stagger>- );- expect(container.outerHTML).toBe('<div></div>');-});-test('Should throw if invalid or no duration provided', () => {- expectToThrow(() => {- renderForFrame(- 10,- <Stagger>- <StaggerChild durationInFrames={NaN}>- <First />- </StaggerChild>- </Stagger>- );- }, /The "durationInFrames" prop of a <StaggerChild \/> component must be an integer, but got NaN./);- expectToThrow(() => {- renderForFrame(- 10,- <Stagger>- {/**- * @ts-expect-error */}- <StaggerChild>- <First />- </StaggerChild>- </Stagger>- );- }, /The "durationInFrames" prop of a <StaggerChild \/> component must be a number, but you passed a value of type undefined/);-});-test('Should allow whitespace', () => {- const {queryByText} = renderForFrame(- 11,- <Stagger>- <StaggerChild durationInFrames={10}>- <First />- </StaggerChild>{' '}- <StaggerChild durationInFrames={10}>- <Second />- </StaggerChild>- </Stagger>- );- expect(queryByText(/^second\s1$/g)).not.toBe(null);-});-test('Handle empty StaggerChild', () => {- expectToThrow(- () =>- renderForFrame(- 11,- <Stagger>- <StaggerChild durationInFrames={10}>- <First />- </StaggerChild>- <StaggerChild durationInFrames={10} />- </Stagger>- ),- /A <StaggerChild \/> component \(index = 1, duration = 10\) doesn't have any children\./- );-});--test('Should allow negative overlap prop', () => {- const {container} = renderForFrame(- 4,- <Stagger>- <StaggerChild durationInFrames={5} layout="none">- <First />- </StaggerChild>- <StaggerChild offset={-1} layout="none" durationInFrames={5}>- <Second />- </StaggerChild>- </Stagger>- );- expect(container.outerHTML).toBe(- '<div><div>first 4</div><div>second 0</div></div>'- );-});--test('Should allow positive overlap prop', () => {- const {container} = renderForFrame(- 5,- <Stagger>- <StaggerChild durationInFrames={5} layout="none">- <First />- </StaggerChild>- <StaggerChild offset={1} layout="none" durationInFrames={5}>- <Second />- </StaggerChild>- </Stagger>- );- expect(container.outerHTML).toBe('<div></div>');-});--test('Should disallow NaN as offset prop', () => {- expectToThrow(() => {- renderForFrame(- 9,- <Stagger>- <StaggerChild offset={NaN} layout="none" durationInFrames={5}>- <Second />- </StaggerChild>- </Stagger>- );- }, /The "offset" property of a <StaggerChild \/> must not be NaN, but got NaN \(index = 0, duration = 5\)\./);-});--test('Should disallow Infinity as offset prop', () => {- expectToThrow(() => {- renderForFrame(- 9,- <Stagger>- <StaggerChild offset={Infinity} layout="none" durationInFrames={5}>- <Second />- </StaggerChild>- </Stagger>- );- }, /The "offset" property of a <StaggerChild \/> must be finite, but got Infinity \(index = 0, duration = 5\)\./);-});--test('Should disallow non-integer numbers as offset prop', () => {- expectToThrow(() => {- renderForFrame(- 9,- <Stagger>- <StaggerChild offset={Math.PI} layout="none" durationInFrames={5}>- <Second />- </StaggerChild>- </Stagger>- );- }, /The "offset" property of a <StaggerChild \/> must be finite, but got 3.141592653589793 \(index = 0, duration = 5\)\./);-});--test('Should cascade negative offset props', () => {- const {container} = renderForFrame(- 9,- <Stagger>- <StaggerChild durationInFrames={5} layout="none">- <First />- </StaggerChild>- <StaggerChild offset={-1} layout="none" durationInFrames={5}>- <Second />- </StaggerChild>- <StaggerChild layout="none" durationInFrames={5}>- <Third />- </StaggerChild>- </Stagger>- );- expect(container.outerHTML).toBe('<div><div>third 0</div></div>');-});--test('Should cascade positive offset props', () => {- const {container} = renderForFrame(- 11,- <Stagger>- <StaggerChild durationInFrames={5} layout="none">- <First />- </StaggerChild>- <StaggerChild offset={1} layout="none" durationInFrames={5}>- <Second />- </StaggerChild>- <StaggerChild layout="none" durationInFrames={5}>- <Third />- </StaggerChild>- </Stagger>- );- expect(container.outerHTML).toBe('<div><div>third 0</div></div>');-});diff --git a/packages/example/src/StaggerTesting/index.tsx b/packages/example/src/StaggerTesting/index.tsxindex f0a2ba65d7..fd9bb24a9d 100644--- a/packages/example/src/StaggerTesting/index.tsx+++ b/packages/example/src/StaggerTesting/index.tsx@@ -1,22 +1,22 @@import {FC} from 'react';-import {Stagger, StaggerChild} from 'remotion';+import {Series} from 'remotion';import ReactSvg from '../ReactSvg';import Tiles from '../Tiles';-const StaggerTesting: FC = () => {+const SeriesTesting: FC = () => {return (- <Stagger>- <StaggerChild durationInFrames={10}>+ <Series>+ <Series.Sequence durationInFrames={10}><Tiles />- </StaggerChild>- <StaggerChild durationInFrames={50}>+ </Series.Sequence>+ <Series.Sequence durationInFrames={50}><ReactSvg transparent={false} />- </StaggerChild>- <StaggerChild durationInFrames={80}>+ </Series.Sequence>+ <Series.Sequence durationInFrames={80}><Tiles />- </StaggerChild>- </Stagger>+ </Series.Sequence>+ </Series>);};-export {StaggerTesting};+export {SeriesTesting as SeriesTesting};diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsxindex cd64039d09..35cea7828a 100644--- a/packages/example/src/Video.tsx+++ b/packages/example/src/Video.tsx@@ -5,7 +5,7 @@ import {ColorInterpolation} from './ColorInterpolation';import {Framer} from './Framer';import {MissingImg} from './MissingImg';import RemoteVideo from './RemoteVideo';-import {StaggerTesting} from './StaggerTesting';+import {SeriesTesting} from './StaggerTesting';import {TenFrameTester} from './TenFrameTester';import ThreeBasic from './ThreeBasic';import {VideoSpeed} from './VideoSpeed';@@ -255,7 +255,7 @@ export const Index: React.FC = () => {/><Compositionid="stagger-test"- component={StaggerTesting}+ component={SeriesTesting}width={1280}height={720}fps={30}