Skip to content

🎥 Make videos programmatically with React

revamp

Jonny Burgercdff8cc01692
diff --git a/packages/core/src/Composition.tsx b/packages/core/src/Composition.tsx
index 321a8ddeaa..e16963958c 100644
--- a/packages/core/src/Composition.tsx
+++ b/packages/core/src/Composition.tsx
@@ -59,7 +59,7 @@ export const Composition = <T,>({
validateDimension(width, 'width');
validateDimension(height, 'height');
- validateDurationInFrames(durationInFrames);
+ validateDurationInFrames(durationInFrames, `<Composition />`);
validateFps(fps);
registerComposition<T>({
durationInFrames,
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index bef8cfae68..2ff77558e2 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -28,7 +28,7 @@ export * from './ready-manager';
export {registerRoot} from './register-root';
export {Sequence} from './sequencing';
export * from './spring';
-export {Stagger} from './stagger';
+export {Stagger, StaggerChild} from './stagger';
export * from './use-frame';
export * from './use-video-config';
export * from './video';
diff --git a/packages/core/src/sequencing/index.tsx b/packages/core/src/sequencing/index.tsx
index 15e7e09a94..967715e154 100644
--- a/packages/core/src/sequencing/index.tsx
+++ b/packages/core/src/sequencing/index.tsx
@@ -22,14 +22,16 @@ export type SequenceContextType = {
export const SequenceContext = createContext<SequenceContextType | null>(null);
-export const Sequence: React.FC<{
+export type SequenceProps = {
children: React.ReactNode;
from: number;
durationInFrames: number;
name?: string;
layout?: 'absolute-fill' | 'none';
showInTimeline?: boolean;
-}> = ({
+};
+
+export const Sequence: React.FC<SequenceProps> = ({
from,
durationInFrames,
children,
@@ -92,10 +94,10 @@ export const Sequence: React.FC<{
compositionDuration - from,
parentSequence
? Math.min(
- parentSequence.durationInFrames +
+ parentSequence.durationInFrames +
(parentSequence.cumulatedFrom + parentSequence.relativeFrom) -
actualFrom,
- durationInFrames
+ durationInFrames
)
: durationInFrames
);
@@ -160,8 +162,8 @@ export const Sequence: React.FC<{
absoluteFrame < actualFrom
? null
: absoluteFrame > endThreshold
- ? null
- : children;
+ ? null
+ : children;
return (
<SequenceContext.Provider value={contextValue}>
diff --git a/packages/core/src/stagger/index.tsx b/packages/core/src/stagger/index.tsx
index aef52707ba..445b459aa8 100644
--- a/packages/core/src/stagger/index.tsx
+++ b/packages/core/src/stagger/index.tsx
@@ -1,40 +1,59 @@
-import {Children, FC, useMemo} from 'react';
-import {Sequence} from '../sequencing';
+import React, {Children, FC, PropsWithChildren, useMemo} from 'react';
+import {Sequence, SequenceProps} from '../sequencing';
+import {validateDurationInFrames} from '../validation/validate-duration-in-frames';
-const Stagger: FC<{componentDuration: Array<number>}> = ({
- children,
- componentDuration,
-}) => {
- if (typeof componentDuration === 'undefined') {
- throw new Error(
- 'componentDuration is a required props, pass a number array'
- );
- }
+type StaggerChildProps = PropsWithChildren<
+ {
+ durationInFrames: number;
+ } & Pick<SequenceProps, 'layout' | 'name'>
+>;
- if (Children.count(children) !== componentDuration.length) {
- throw new Error(
- 'Length of componentDuration and number of component inside <Stagger /> should be same '
- );
- }
+const StaggerChild = ({children}: StaggerChildProps) => {
+ // eslint-disable-next-line react/jsx-no-useless-fragment
+ return <>{children}</>;
+};
+const Stagger: FC = ({children}) => {
const childrenValue = useMemo(() => {
- return Children.map(children, (child, index) => {
- let startFrame = 0;
- componentDuration.forEach((currentDuration, i) => {
- if (i < index) {
- startFrame += currentDuration - 1;
- }
- });
+ let startFrame = 0;
+ return Children.map(children, (child) => {
+ const castedChild = (child as unknown) as {
+ props: StaggerChildProps;
+ type: typeof StaggerChild;
+ };
+ if (!castedChild || !castedChild.props.children) {
+ throw new TypeError(`The StaggerChild component must have children.`);
+ }
+
+ if (castedChild.type !== StaggerChild) {
+ throw new TypeError(
+ "The <Stagger> component only accepts a list of <StaggerChild /> components as it's children"
+ );
+ }
+
+ const durationInFramesProp = castedChild.props.durationInFrames;
+ const {
+ durationInFrames,
+ children: _children,
+ ...passedProps
+ } = castedChild.props;
+ validateDurationInFrames(durationInFramesProp, `<StaggerChild />`);
+ const currentStartFrame = startFrame;
+ startFrame += durationInFramesProp;
return (
- <Sequence from={startFrame} durationInFrames={componentDuration[index]}>
+ <Sequence
+ from={currentStartFrame}
+ durationInFrames={durationInFramesProp}
+ {...passedProps}
+ >
{child}
</Sequence>
);
});
- }, [componentDuration, children]);
+ }, [children]);
/* eslint-disable react/jsx-no-useless-fragment */
return <>{childrenValue}</>;
};
-export {Stagger};
+export {Stagger, StaggerChild};
diff --git a/packages/core/src/test/stagger.test.tsx b/packages/core/src/test/stagger.test.tsx
new file mode 100644
index 0000000000..6c3a6185af
--- /dev/null
+++ b/packages/core/src/test/stagger.test.tsx
@@ -0,0 +1,8 @@
+test.todo('Basic stagger test');
+test.todo('Should not allow foreign elements');
+test.todo('Should allow layout prop');
+test.todo('Should not allow additional props');
+test.todo('Should throw if invalid or no duration provided');
+test.todo('Should allow whitespace');
+test.todo('Handle empty StaggerChild');
+test.todo('Should allow overlap prop');
diff --git a/packages/core/src/validation/validate-duration-in-frames.ts b/packages/core/src/validation/validate-duration-in-frames.ts
index eecd1e7781..1461342d9e 100644
--- a/packages/core/src/validation/validate-duration-in-frames.ts
+++ b/packages/core/src/validation/validate-duration-in-frames.ts
@@ -1,19 +1,22 @@
-export const validateDurationInFrames = (durationInFrames: number) => {
+export const validateDurationInFrames = (
+ durationInFrames: number,
+ componentName: string
+) => {
if (typeof durationInFrames !== 'number') {
throw new Error(
- `The "durationInFrames" of a composition must be a number, but you passed a value of type ${typeof durationInFrames}`
+ `The "durationInFrames" prop of a ${componentName} component must be a number, but you passed a value of type ${typeof durationInFrames}`
);
}
if (durationInFrames <= 0) {
throw new TypeError(
- `The "durationInFrames" of a composition must be positive, but got ${durationInFrames}.`
+ `The "durationInFrames" prop of a ${componentName} component must be positive, but got ${durationInFrames}.`
);
}
if (durationInFrames % 1 !== 0) {
throw new TypeError(
- `The "durationInFrames" of a composition must be an integer, but got ${durationInFrames}.`
+ `The "durationInFrames" prop of a ${componentName} component must be an integer, but got ${durationInFrames}.`
);
}
};
diff --git a/packages/example/src/StaggerTesting/index.tsx b/packages/example/src/StaggerTesting/index.tsx
index 39be41d6f7..f0a2ba65d7 100644
--- a/packages/example/src/StaggerTesting/index.tsx
+++ b/packages/example/src/StaggerTesting/index.tsx
@@ -1,14 +1,20 @@
import {FC} from 'react';
-import {Stagger} from 'remotion';
+import {Stagger, StaggerChild} from 'remotion';
import ReactSvg from '../ReactSvg';
import Tiles from '../Tiles';
const StaggerTesting: FC = () => {
return (
- <Stagger componentDuration={[10, 50, 80]}>
- <Tiles />
- <ReactSvg transparent={false} />
- <Tiles />
+ <Stagger>
+ <StaggerChild durationInFrames={10}>
+ <Tiles />
+ </StaggerChild>
+ <StaggerChild durationInFrames={50}>
+ <ReactSvg transparent={false} />
+ </StaggerChild>
+ <StaggerChild durationInFrames={80}>
+ <Tiles />
+ </StaggerChild>
</Stagger>
);
};
diff --git a/packages/player/src/Player.tsx b/packages/player/src/Player.tsx
index 7721e437b4..616ba45e2f 100644
--- a/packages/player/src/Player.tsx
+++ b/packages/player/src/Player.tsx
@@ -93,7 +93,7 @@ export const PlayerFn = <T,>(
Internals.validateDimension(compositionHeight, 'compositionHeight');
Internals.validateDimension(compositionWidth, 'compositionWidth');
- Internals.validateDurationInFrames(durationInFrames);
+ Internals.validateDurationInFrames(durationInFrames, '<Player />');
Internals.validateFps(fps);
if (typeof controls !== 'boolean' && typeof controls !== 'undefined') {