Skip to content

🎥 Make videos programmatically with React

bigger refactor

Jonny Burgere272d06ea845
diff --git a/packages/bundler/renderEntry.tsx b/packages/bundler/renderEntry.tsx
index a3f0238860..ae93b5acab 100644
--- a/packages/bundler/renderEntry.tsx
+++ b/packages/bundler/renderEntry.tsx
@@ -352,6 +352,7 @@ if (typeof window !== 'undefined') {
composition: c,
signal: new AbortController().signal,
originalProps,
+ defaultProps: c.defaultProps ?? {},
});
const resolved = await Promise.resolve(comp);
@@ -412,6 +413,7 @@ if (typeof window !== 'undefined') {
composition: selectedComp,
originalProps,
signal: abortController.signal,
+ defaultProps: selectedComp.defaultProps ?? {},
}),
);
continueRender(handle);
diff --git a/packages/core/src/ResolveCompositionConfig.tsx b/packages/core/src/ResolveCompositionConfig.tsx
index b07852233a..d161be6715 100644
--- a/packages/core/src/ResolveCompositionConfig.tsx
+++ b/packages/core/src/ResolveCompositionConfig.tsx
@@ -7,13 +7,17 @@ import {
useEffect,
useImperativeHandle,
useMemo,
+ useRef,
useState,
} from 'react';
+import type {AnyZodObject} from 'zod';
+import type {CalculateMetadataFunction} from './Composition.js';
import type {AnyComposition} from './CompositionManager.js';
import {CompositionManager} from './CompositionManagerContext.js';
import {EditorPropsContext} from './EditorProps.js';
import {getInputProps} from './config/input-props.js';
import {getRemotionEnvironment} from './get-remotion-environment.js';
+import type {InferProps} from './props-if-has-props.js';
import {resolveVideoConfig} from './resolve-video-config.js';
import {validateDimension} from './validation/validate-dimensions.js';
import {validateDurationInFrames} from './validation/validate-duration-in-frames.js';
@@ -77,6 +81,12 @@ export const ResolveCompositionConfig: React.FC<
);
const {props: allEditorProps} = useContext(EditorPropsContext);
+ const inputProps = useMemo(() => {
+ return typeof window === 'undefined' || getRemotionEnvironment().isPlayer
+ ? {}
+ : getInputProps() ?? {};
+ }, []);
+
const [resolvedConfigs, setResolvedConfigs] = useState<
Record<string, VideoConfigState | undefined>
>({});
@@ -94,41 +104,39 @@ export const ResolveCompositionConfig: React.FC<
}, [allEditorProps, renderModalComposition]);
const doResolution = useCallback(
- (composition: AnyComposition, editorProps: object) => {
+ (
+ compositionId: string,
+ calculateMetadata: CalculateMetadataFunction<
+ InferProps<AnyZodObject, Record<string, unknown>>
+ > | null,
+ defaultProps: Record<string, unknown>,
+ combinedProps: Record<string, unknown>,
+ ) => {
const controller = new AbortController();
if (currentCompositionMetadata) {
return controller;
}
- const inputProps =
- typeof window === 'undefined' || getRemotionEnvironment().isPlayer
- ? {}
- : getInputProps() ?? {};
-
const {signal} = controller;
- const originalProps = {
- ...(composition.defaultProps ?? {}),
- ...(editorProps ?? {}),
- ...(inputProps ?? {}),
- };
-
const promOrNot = resolveVideoConfig({
- composition,
- originalProps,
+ compositionId,
+ calculateMetadata: selectedComposition?.calculateMetadata ?? null,
+ originalProps: combinedProps,
signal,
+ defaultProps,
});
if (typeof promOrNot === 'object' && 'then' in promOrNot) {
setResolvedConfigs((r) => {
- const prev = r[composition.id];
+ const prev = r[compositionId];
if (
prev?.type === 'success' ||
prev?.type === 'success-and-refreshing'
) {
return {
...r,
- [composition.id]: {
+ [compositionId]: {
type: 'success-and-refreshing',
result: prev.result,
},
@@ -137,7 +145,7 @@ export const ResolveCompositionConfig: React.FC<
return {
...r,
- [composition.id]: {
+ [compositionId]: {
type: 'loading',
},
};
@@ -150,7 +158,7 @@ export const ResolveCompositionConfig: React.FC<
setResolvedConfigs((r) => ({
...r,
- [composition.id]: {
+ [compositionId]: {
type: 'success',
result: c,
},
@@ -163,7 +171,7 @@ export const ResolveCompositionConfig: React.FC<
setResolvedConfigs((r) => ({
...r,
- [composition.id]: {
+ [compositionId]: {
type: 'error',
error: err,
},
@@ -172,7 +180,7 @@ export const ResolveCompositionConfig: React.FC<
} else {
setResolvedConfigs((r) => ({
...r,
- [composition.id]: {
+ [compositionId]: {
type: 'success',
result: promOrNot,
},
@@ -181,7 +189,7 @@ export const ResolveCompositionConfig: React.FC<
return controller;
},
- [currentCompositionMetadata],
+ [currentCompositionMetadata, selectedComposition?.calculateMetadata],
);
const currentComposition =
@@ -211,34 +219,106 @@ export const ResolveCompositionConfig: React.FC<
const editorProps = allEditorProps[currentComposition] ?? {};
- doResolution(composition, editorProps);
+ const defaultProps = {
+ ...(composition.defaultProps ?? {}),
+ ...(editorProps ?? {}),
+ };
+
+ const props = {
+ ...(inputProps ?? {}),
+ };
+
+ doResolution(composition, defaultProps, props);
},
};
},
- [allEditorProps, compositions, currentComposition, doResolution],
+ [
+ allEditorProps,
+ compositions,
+ currentComposition,
+ doResolution,
+ inputProps,
+ ],
);
const isTheSame = selectedComposition?.id === renderModalComposition?.id;
+ const lastDefaultProps = useRef({});
+ const currentDefaultProps = useMemo(() => {
+ const newDefaultProps = {
+ ...(selectedComposition?.defaultProps ?? {}),
+ ...(selectedEditorProps ?? {}),
+ };
+
+ if (
+ JSON.stringify(lastDefaultProps.current) ===
+ JSON.stringify(newDefaultProps)
+ ) {
+ return lastDefaultProps.current;
+ }
+
+ lastDefaultProps.current = newDefaultProps;
+ return newDefaultProps;
+ }, [selectedComposition?.defaultProps, selectedEditorProps]);
+
+ const lastOriginalProps = useRef({});
+ const originalProps = useMemo(() => {
+ const newOriginalProps = {
+ ...currentDefaultProps,
+ ...(inputProps ?? {}),
+ };
+
+ if (
+ JSON.stringify(lastOriginalProps.current) ===
+ JSON.stringify(newOriginalProps)
+ ) {
+ return lastOriginalProps.current;
+ }
+
+ lastOriginalProps.current = newOriginalProps;
+ return newOriginalProps;
+ }, [currentDefaultProps, inputProps]);
+
useEffect(() => {
if (selectedComposition && needsResolution(selectedComposition)) {
- const controller = doResolution(selectedComposition, selectedEditorProps);
+ const controller = doResolution(
+ selectedComposition,
+ currentDefaultProps,
+ originalProps,
+ );
return () => {
controller.abort();
};
}
- }, [doResolution, selectedComposition, selectedEditorProps]);
+ }, [currentDefaultProps, doResolution, originalProps, selectedComposition]);
useEffect(() => {
if (renderModalComposition && !isTheSame) {
- const controller = doResolution(renderModalComposition, renderModalProps);
+ const combinedProps = {
+ ...(renderModalComposition.defaultProps ?? {}),
+ ...(renderModalProps ?? {}),
+ ...(inputProps ?? {}),
+ };
+
+ const controller = doResolution(
+ renderModalComposition,
+ currentDefaultProps,
+ combinedProps,
+ );
return () => {
controller.abort();
};
}
- }, [doResolution, isTheSame, renderModalComposition, renderModalProps]);
+ }, [
+ currentDefaultProps,
+ doResolution,
+ inputProps,
+ isTheSame,
+ renderModalComposition,
+ renderModalProps,
+ ]);
const resolvedConfigsIncludingStaticOnes = useMemo(() => {
const staticComps = compositions.filter((c) => {
diff --git a/packages/core/src/resolve-video-config.ts b/packages/core/src/resolve-video-config.ts
index 3874db60dc..ff1cb40a3d 100644
--- a/packages/core/src/resolve-video-config.ts
+++ b/packages/core/src/resolve-video-config.ts
@@ -1,6 +1,9 @@
import type {AnyZodObject} from 'zod';
-import type {CalcMetadataReturnType} from './Composition.js';
-import type {TCompMetadataWithCalcFunction} from './CompositionManager.js';
+import type {
+ CalcMetadataReturnType,
+ CalculateMetadataFunction,
+} from './Composition.js';
+import type {InferProps} from './props-if-has-props.js';
import {validateDefaultCodec} from './validation/validate-default-codec.js';
import {validateDimension} from './validation/validate-dimensions.js';
import {validateDurationInFrames} from './validation/validate-duration-in-frames.js';
@@ -8,19 +11,24 @@ import {validateFps} from './validation/validate-fps.js';
import type {VideoConfig} from './video-config.js';
const validateCalculated = ({
- composition,
calculated,
+ compositionId,
+ compositionFps,
+ compositionHeight,
+ compositionWidth,
+ compositionDurationInFrames,
}: {
- composition: TCompMetadataWithCalcFunction<
- AnyZodObject,
- Record<string, unknown>
- >;
calculated: CalcMetadataReturnType<Record<string, unknown>> | null;
+ compositionId: string;
+ compositionWidth: number | null;
+ compositionHeight: number | null;
+ compositionFps: number | null;
+ compositionDurationInFrames: number | null;
}) => {
- const calculateMetadataErrorLocation = `calculated by calculateMetadata() for the composition "${composition.id}"`;
- const defaultErrorLocation = `of the "<Composition />" component with the id "${composition.id}"`;
+ const calculateMetadataErrorLocation = `calculated by calculateMetadata() for the composition "${compositionId}"`;
+ const defaultErrorLocation = `of the "<Composition />" component with the id "${compositionId}"`;
- const width = calculated?.width ?? composition.width ?? undefined;
+ const width = calculated?.width ?? compositionWidth ?? undefined;
validateDimension(
width,
@@ -28,7 +36,7 @@ const validateCalculated = ({
calculated?.width ? calculateMetadataErrorLocation : defaultErrorLocation,
);
- const height = calculated?.height ?? composition.height ?? undefined;
+ const height = calculated?.height ?? compositionHeight ?? undefined;
validateDimension(
height,
@@ -36,7 +44,7 @@ const validateCalculated = ({
calculated?.height ? calculateMetadataErrorLocation : defaultErrorLocation,
);
- const fps = calculated?.fps ?? composition.fps ?? null;
+ const fps = calculated?.fps ?? compositionFps ?? null;
validateFps(
fps,
@@ -45,11 +53,11 @@ const validateCalculated = ({
);
const durationInFrames =
- calculated?.durationInFrames ?? composition.durationInFrames ?? null;
+ calculated?.durationInFrames ?? compositionDurationInFrames ?? null;
validateDurationInFrames(durationInFrames, {
allowFloats: false,
- component: `of the "<Composition />" component with the id "${composition.id}"`,
+ component: `of the "<Composition />" component with the id "${compositionId}"`,
});
const defaultCodec = calculated?.defaultCodec;
@@ -59,23 +67,34 @@ const validateCalculated = ({
};
export const resolveVideoConfig = ({
- composition,
+ calculateMetadata,
signal,
+ defaultProps,
originalProps,
+ compositionId,
+ compositionDurationInFrames,
+ compositionFps,
+ compositionHeight,
+ compositionWidth,
}: {
- composition: TCompMetadataWithCalcFunction<
- AnyZodObject,
- Record<string, unknown>
- >;
+ compositionId: string;
+ compositionWidth: number | null;
+ compositionHeight: number | null;
+ compositionFps: number | null;
+ compositionDurationInFrames: number | null;
+ calculateMetadata: CalculateMetadataFunction<
+ InferProps<AnyZodObject, Record<string, unknown>>
+ > | null;
signal: AbortSignal;
+ defaultProps: Record<string, unknown>;
originalProps: Record<string, unknown>;
}): VideoConfig | Promise<VideoConfig> => {
- const calculatedProm = composition.calculateMetadata
- ? composition.calculateMetadata({
- defaultProps: composition.defaultProps ?? {},
+ const calculatedProm = calculateMetadata
+ ? calculateMetadata({
+ defaultProps,
props: originalProps,
abortSignal: signal,
- compositionId: composition.id,
+ compositionId,
})
: null;
@@ -88,15 +107,19 @@ export const resolveVideoConfig = ({
const {height, width, durationInFrames, fps, defaultCodec} =
validateCalculated({
calculated: c,
- composition,
+ compositionDurationInFrames,
+ compositionFps,
+ compositionHeight,
+ compositionWidth,
+ compositionId,
});
return {
width,
height,
fps,
durationInFrames,
- id: composition.id,
- defaultProps: composition.defaultProps ?? {},
+ id: compositionId,
+ defaultProps,
props: c.props ?? originalProps,
defaultCodec: defaultCodec ?? null,
};
@@ -105,14 +128,18 @@ export const resolveVideoConfig = ({
const data = validateCalculated({
calculated: calculatedProm,
- composition,
+ compositionDurationInFrames,
+ compositionFps,
+ compositionHeight,
+ compositionWidth,
+ compositionId,
});
if (calculatedProm === null) {
return {
...data,
- id: composition.id,
- defaultProps: composition.defaultProps ?? {},
+ id: compositionId,
+ defaultProps: defaultProps ?? {},
props: originalProps,
defaultCodec: null,
};
@@ -120,8 +147,8 @@ export const resolveVideoConfig = ({
return {
...data,
- id: composition.id,
- defaultProps: composition.defaultProps ?? {},
+ id: compositionId,
+ defaultProps: defaultProps ?? {},
props: calculatedProm.props ?? originalProps,
defaultCodec: calculatedProm.defaultCodec ?? null,
};
diff --git a/packages/core/src/test/composition-validation.test.tsx b/packages/core/src/test/composition-validation.test.tsx
index 895d86f33c..bb9246bf03 100644
--- a/packages/core/src/test/composition-validation.test.tsx
+++ b/packages/core/src/test/composition-validation.test.tsx
@@ -25,6 +25,7 @@ describe('Composition-validation render should throw with invalid props', () =>
defaultProps: {},
},
originalProps: {},
+ defaultProps: {},
signal: new AbortController().signal,
}),
@@ -45,6 +46,7 @@ describe('Composition-validation render should throw with invalid props', () =>
width: 100,
defaultProps: {},
},
+ defaultProps: {},
originalProps: {},
signal: new AbortController().signal,
}),
@@ -64,6 +66,7 @@ describe('Composition-validation render should throw with invalid props', () =>
id: 'id',
width: 100,
},
+ defaultProps: {},
originalProps: {},
signal: new AbortController().signal,
}),
@@ -103,6 +106,7 @@ describe('Composition-validation render should throw with invalid props', () =>
width: 100,
defaultProps: {},
},
+ defaultProps: {},
originalProps: {},
signal: new AbortController().signal,
}),
@@ -124,6 +128,7 @@ describe('Composition-validation render should throw with invalid props', () =>
width: -100,
defaultProps: {},
},
+ defaultProps: {},
originalProps: {},
signal: new AbortController().signal,
}),
@@ -143,6 +148,7 @@ describe('Composition-validation render should throw with invalid props', () =>
width: 0,
defaultProps: {},
},
+ defaultProps: {},
originalProps: {},
signal: new AbortController().signal,
}),
@@ -163,6 +169,7 @@ describe('Composition-validation render should throw with invalid props', () =>
width: '100',
defaultProps: {},
},
+ defaultProps: {},
editorProps: {},
signal: new AbortController().signal,
}),
@@ -184,6 +191,7 @@ describe('Composition-validation render should throw with invalid props', () =>
width: 100,
defaultProps: {},
},
+ defaultProps: {},
originalProps: {},
signal: new AbortController().signal,
}),
@@ -203,6 +211,7 @@ describe('Composition-validation render should throw with invalid props', () =>
width: 100,
defaultProps: {},
},
+ defaultProps: {},
originalProps: {},
signal: new AbortController().signal,
}),
@@ -222,6 +231,7 @@ describe('Composition-validation render should throw with invalid props', () =>
width: 100,
defaultProps: {},
},
+ defaultProps: {},
originalProps: {},
signal: new AbortController().signal,
}),
@@ -242,6 +252,7 @@ describe('Composition-validation render should throw with invalid props', () =>
width: 100,
defaultProps: {},
},
+ defaultProps: {},
editorProps: {},
signal: new AbortController().signal,
}),
@@ -263,6 +274,7 @@ describe('Composition-validation render should throw with invalid props', () =>
width: 100,
defaultProps: {},
},
+ defaultProps: {},
originalProps: {},
signal: new AbortController().signal,
}),
@@ -282,6 +294,7 @@ describe('Composition-validation render should throw with invalid props', () =>
width: 100,
defaultProps: {},
},
+ defaultProps: {},
originalProps: {},
signal: new AbortController().signal,
}),
@@ -439,6 +452,9 @@ test('should resolve props correctly with no calculateMetadata()', async () => {
c: 'd',
},
signal: new AbortController().signal,
+ defaultProps: {
+ a: 'b',
+ },
});
expect(resolved.props).toEqual({
a: 'b',