🎥 Make videos programmatically with React
reuse code from composition
Jonny Burgereffd8c42ed74
diff --git a/packages/core/src/Composition.tsx b/packages/core/src/Composition.tsxindex 5742c9e517..321a8ddeaa 100644--- a/packages/core/src/Composition.tsx+++ b/packages/core/src/Composition.tsx@@ -8,6 +8,7 @@ import {removeStaticComposition,} from './register-root';import {useLazyComponent} from './use-lazy-component';+import {validateDimension} from './validation/validate-dimensions';import {validateDurationInFrames} from './validation/validate-duration-in-frames';import {validateFps} from './validation/validate-fps';@@ -56,30 +57,8 @@ export const Composition = <T,>({);}- if (typeof width !== 'number') {- throw new Error(- `The "width" of a composition must be a number, but you passed a ${typeof width}`- );- }-- if (width <= 0) {- throw new TypeError(- `The "width" of a composition must be positive, but got ${width}.`- );- }-- if (typeof height !== 'number') {- throw new Error(- `The "height" of a composition must be a number, but you passed a ${typeof height}`- );- }-- if (height <= 0) {- throw new TypeError(- `The "height" of a composition must be positive, but got ${height}.`- );- }-+ validateDimension(width, 'width');+ validateDimension(height, 'height');validateDurationInFrames(durationInFrames);validateFps(fps);registerComposition<T>({diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.tsindex 5dc78e38de..1a53389d7d 100644--- a/packages/core/src/internals.ts+++ b/packages/core/src/internals.ts@@ -1,3 +1,4 @@+import {LooseAnyComponent} from './any-component';import {CompProps} from './Composition';import {CompositionManager,@@ -76,6 +77,9 @@ import {truthy} from './truthy';import {useLazyComponent} from './use-lazy-component';import {useUnsafeVideoConfig} from './use-unsafe-video-config';import {useVideo} from './use-video';+import {validateDimension} from './validation/validate-dimensions';+import {validateDurationInFrames} from './validation/validate-duration-in-frames';+import {validateFps} from './validation/validate-fps';import {MediaVolumeContext,MediaVolumeContextValue,@@ -145,6 +149,9 @@ export const Internals = {getServerPort,MediaVolumeContext,SetMediaVolumeContext,+ validateDurationInFrames,+ validateFps,+ validateDimension,};export type {@@ -161,4 +168,5 @@ export type {CompositionManagerContext,MediaVolumeContextValue,SetMediaVolumeContextValue,+ LooseAnyComponent,};diff --git a/packages/core/src/test/composition-validation.test.tsx b/packages/core/src/test/composition-validation.test.tsxindex f89127e872..3aa5655af8 100644--- a/packages/core/src/test/composition-validation.test.tsx+++ b/packages/core/src/test/composition-validation.test.tsx@@ -82,6 +82,24 @@ describe('Composition-validation render should throw with invalid props', () =>/The "height" of a composition must be a number, but you passed a string/);});+ test('It should throw if height is not an integer', () => {+ expectToThrow(+ () =>+ render(+ <RemotionRoot>+ <Composition+ lazyComponent={() => Promise.resolve({default: AnyComp})}+ durationInFrames={100}+ fps={30}+ height={100.01}+ width={100}+ id="id"+ />+ </RemotionRoot>+ ),+ /The "height" of a composition must be an integer, but is 100.01/+ );+ });});describe('Throw with invalid width props', () => {test('It should throw if width is a negative number', () => {diff --git a/packages/core/src/validation/validate-dimensions.ts b/packages/core/src/validation/validate-dimensions.tsnew file mode 100644index 0000000000..7b6bae1ff1--- /dev/null+++ b/packages/core/src/validation/validate-dimensions.ts@@ -0,0 +1,31 @@+export const validateDimension = (amount: number, nameOfProp: string) => {+ if (typeof amount !== 'number') {+ throw new Error(+ `The "${nameOfProp}" of a composition must be a number, but you passed a ${typeof amount}`+ );+ }++ if (isNaN(amount)) {+ throw new TypeError(+ `The "${nameOfProp}" of a composition must not be NaN, but is NaN.`+ );+ }++ if (!Number.isFinite(amount)) {+ throw new TypeError(+ `The "${nameOfProp}" of a composition must be finite, but is ${amount}.`+ );+ }++ if (amount % 1 !== 0) {+ throw new TypeError(+ `The "${nameOfProp}" of a composition must be an integer, but is ${amount}.`+ );+ }++ if (amount <= 0) {+ throw new TypeError(+ `The "${nameOfProp}" of a composition must be positive, but got ${amount}.`+ );+ }+};diff --git a/packages/player/src/Player.tsx b/packages/player/src/Player.tsxindex f624da631f..7721e437b4 100644--- a/packages/player/src/Player.tsx+++ b/packages/player/src/Player.tsx@@ -11,12 +11,12 @@ import {CompositionManagerContext,CompProps,Internals,+ LooseAnyComponent,MediaVolumeContextValue,SetMediaVolumeContextValue,SetTimelineContextValue,TimelineContextValue,} from 'remotion';-import {LooseAnyComponent} from 'remotion/src/any-component';import {PlayerEventEmitterContext} from './emitter-context';import {PlayerEmitter} from './event-emitter';import {PLAYER_CSS_CLASSNAME} from './player-css-classname';@@ -55,8 +55,8 @@ Internals.CSSUtils.injectCSS(export const PlayerFn = <T,>({durationInFrames,- compositionHeight: height,- compositionWidth: width,+ compositionHeight,+ compositionWidth,fps,inputProps,style,@@ -79,29 +79,22 @@ export const PlayerFn = <T,>(const [mediaMuted, setMediaMuted] = useState<boolean>(false);const [mediaVolume, setMediaVolume] = useState<number>(getPreferredVolume());- if (typeof height !== 'number') {+ if (typeof compositionHeight !== 'number') {throw new TypeError(- `'compositionHeight' must be a number but got '${typeof height}' instead`+ `'compositionHeight' must be a number but got '${typeof compositionHeight}' instead`);}- if (typeof width !== 'number') {+ if (typeof compositionWidth !== 'number') {throw new TypeError(- `'compositionWidth' must be a number but got '${typeof width}' instead`+ `'compositionWidth' must be a number but got '${typeof compositionWidth}' instead`);}- if (typeof durationInFrames !== 'number') {- throw new TypeError(- `'durationInFrames' must be a number but got '${typeof durationInFrames}' instead`- );- }-- if (typeof fps !== 'number') {- throw new TypeError(- `'fps' must be a number but got '${typeof fps}' instead`- );- }+ Internals.validateDimension(compositionHeight, 'compositionHeight');+ Internals.validateDimension(compositionWidth, 'compositionWidth');+ Internals.validateDurationInFrames(durationInFrames);+ Internals.validateFps(fps);if (typeof controls !== 'boolean' && typeof controls !== 'undefined') {throw new TypeError(@@ -191,8 +184,8 @@ export const PlayerFn = <T,>(LooseAnyComponent<unknown>>,durationInFrames,- height,- width,+ height: compositionHeight,+ width: compositionWidth,fps,id: 'player-comp',props: inputProps as unknown,@@ -210,7 +203,14 @@ export const PlayerFn = <T,>(unregisterAsset: () => undefined,assets: [],};- }, [component, durationInFrames, height, width, fps, inputProps]);+ }, [+ component,+ durationInFrames,+ compositionHeight,+ compositionWidth,+ fps,+ inputProps,+ ]);const passedInputProps = useMemo(() => {return inputProps ?? {};