Skip to content

🎥 Make videos programmatically with React

feat(player): add validation to Player.props.component

SkyezerFoxb0f9d161203c
diff --git a/packages/player/src/Player.tsx b/packages/player/src/Player.tsx
index 91a5d8d903..2007cc79af 100644
--- a/packages/player/src/Player.tsx
+++ b/packages/player/src/Player.tsx
@@ -10,6 +10,7 @@ import React, {
useState,
} from 'react';
import {
+ Composition,
CompositionManagerContext,
CompProps,
Internals,
@@ -102,6 +103,13 @@ export const PlayerFn = <T,>(
);
}
+ // @ts-expect-error
+ if (componentProps.component === Composition) {
+ throw new TypeError(
+ `'component' must not be an instance of the '<Composition \\>' component.`
+ );
+ }
+
const component = Internals.useLazyComponent(componentProps);
const [frame, setFrame] = useState(0);
const [playing, setPlaying] = useState<boolean>(false);
diff --git a/packages/player/src/test/validate-prop.test.tsx b/packages/player/src/test/validate-prop.test.tsx
index be5f3c9ba6..08e8cde6f4 100644
--- a/packages/player/src/test/validate-prop.test.tsx
+++ b/packages/player/src/test/validate-prop.test.tsx
@@ -1,3 +1,4 @@
+import {Composition} from 'remotion';
import {Player} from '../index';
import {HelloWorld, render} from './test-utils';
@@ -188,6 +189,58 @@ test('playbackRate of undefined should be okay', () => {
expect(true).toBe(true);
});
+test('passing in <Composition /> instance should not be possible', () => {
+ const compositionProps = {
+ id: 'HelloWorld',
+ width: 500,
+ height: 400,
+ fps: 30,
+ durationInFrames: 500,
+ component: HelloWorld,
+ };
+ let threwError = false;
+ try {
+ render(
+ <Player
+ compositionWidth={500}
+ compositionHeight={400}
+ fps={30}
+ durationInFrames={500}
+ component={Composition}
+ controls
+ showVolumeControls
+ inputProps={{
+ id: 'HelloWorld',
+ width: 500,
+ height: 400,
+ fps: 30,
+ durationInFrames: 500,
+ component: HelloWorld,
+ }}
+ />
+ );
+ render(
+ <Player
+ compositionWidth={500}
+ compositionHeight={400}
+ fps={30}
+ durationInFrames={500}
+ component={() => <Composition {...compositionProps} />}
+ controls
+ showVolumeControls
+ inputProps={compositionProps}
+ />
+ );
+ } catch (e) {
+ threwError = true;
+ expect((e as Error).message).toMatch(
+ /'component' must not be an instance of the '<Composition \\>' component./
+ );
+ } finally {
+ expect(threwError).toBe(true);
+ }
+});
+
test.each([
['controls'],
['loop'],