🎥 Make videos programmatically with React
refactor a bit
Jonny Burgere33a4aec3ff3
diff --git a/packages/core/src/Composition.tsx b/packages/core/src/Composition.tsxindex 5ca52c33d6..bb068ac8f9 100644--- a/packages/core/src/Composition.tsx+++ b/packages/core/src/Composition.tsx@@ -1,12 +1,13 @@-import React, {ComponentType, useContext, useEffect, useMemo} from 'react';+import {ComponentType, useContext, useEffect} from 'react';import {CompositionManager} from './CompositionManager';import {addStaticComposition,getIsEvaluation,removeStaticComposition,} from './register-root';+import {useLazyComponent} from './use-lazy-component';-type CompProps<T> =+export type CompProps<T> =| {lazyComponent: () => Promise<{default: ComponentType<T>}>;}@@ -36,17 +37,7 @@ export const Composition = <T,>({CompositionManager);- const lazy = useMemo(() => {- if ('lazyComponent' in compProps) {- return React.lazy(compProps.lazyComponent);- }- if ('component' in compProps) {- return React.lazy(() => Promise.resolve({default: compProps.component}));- }- throw new Error("You must pass either 'component' or 'lazyComponent'");- // @ts-expect-error- // eslint-disable-next-line react-hooks/exhaustive-deps- }, [compProps.lazyComponent, compProps.component]);+ const lazy = useLazyComponent(compProps);useEffect(() => {// Ensure it's a URL safe iddiff --git a/packages/core/src/internals.ts b/packages/core/src/internals.tsindex fc4589a3ad..d378ba71f0 100644--- a/packages/core/src/internals.ts+++ b/packages/core/src/internals.ts@@ -1,5 +1,7 @@+import {CompProps} from './Composition';import {CompositionManager,+ CompositionManagerContext,TCompMetadata,TComposition,TSequence,@@ -52,6 +54,7 @@ import {getCompositionName, getIsEvaluation, getRoot} from './register-root';import {RemotionRoot} from './RemotionRoot';import * as Timeline from './timeline-position-state';import {TimelineContextValue} from './timeline-position-state';+import {useLazyComponent} from './use-lazy-component';import {useUnsafeVideoConfig} from './use-unsafe-video-config';import {useVideo} from './use-video';@@ -94,6 +97,7 @@ export const Internals = {validateSelectedPixelFormatAndCodecCombination,validateFrameRange,getWebpackCaching,+ useLazyComponent,};export type {@@ -103,4 +107,6 @@ export type {TSequence,WebpackOverrideFn,TimelineContextValue,+ CompProps,+ CompositionManagerContext,};diff --git a/packages/core/src/use-lazy-component.ts b/packages/core/src/use-lazy-component.tsnew file mode 100644index 0000000000..8ecb433937--- /dev/null+++ b/packages/core/src/use-lazy-component.ts@@ -0,0 +1,19 @@+import React, {useMemo} from 'react';+import {CompProps} from './internals';++// Expected, it can be any component props+// eslint-disable-next-line @typescript-eslint/no-explicit-any+export const useLazyComponent = (compProps: CompProps<any>) => {+ const lazy = useMemo(() => {+ if ('lazyComponent' in compProps) {+ return React.lazy(compProps.lazyComponent);+ }+ if ('component' in compProps) {+ return React.lazy(() => Promise.resolve({default: compProps.component}));+ }+ throw new Error("You must pass either 'component' or 'lazyComponent'");+ // @ts-expect-error+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [compProps.lazyComponent, compProps.component]);+ return lazy;+};diff --git a/packages/player-example/src/App.tsx b/packages/player-example/src/App.tsxindex a31dd465c7..18fdee9e46 100644--- a/packages/player-example/src/App.tsx+++ b/packages/player-example/src/App.tsx@@ -1,5 +1,14 @@-import {RemotionPlayer} from '@remotion/player';+import {Player} from '@remotion/player';+import CarSlideshow from './CarSlideshow';export default function App() {- return <RemotionPlayer id={'main'} />;+ return (+ <Player+ width={768}+ height={432}+ fps={30}+ durationInFrames={500}+ component={CarSlideshow}+ />+ );}diff --git a/packages/player-example/src/CarSlideshow.tsx b/packages/player-example/src/CarSlideshow.tsxindex 2946640e06..0be3f6b455 100644--- a/packages/player-example/src/CarSlideshow.tsx+++ b/packages/player-example/src/CarSlideshow.tsx@@ -1,36 +1,36 @@-import React, { FunctionComponent } from "react";-import { useCurrentFrame, useVideoConfig, interpolate } from "remotion";+import React, {FunctionComponent} from 'react';+import {interpolate, useCurrentFrame, useVideoConfig} from 'remotion';const CarSlideshow: FunctionComponent = () => {- const frame = useCurrentFrame();- const { width, height, durationInFrames } = useVideoConfig();- const left = interpolate(frame, [0, durationInFrames], [width, width * -1]);- return (- <div- style={{- backgroundColor: "hotpink",- width: width,- height: height,- position: "absolute",- left: 0,- top: 0- }}- >- <h1- style={{- fontSize: "5em",- fontWeight: "bold",- position: "absolute",- top: height / 2 - 100,- left,- color: "white",- whiteSpace: "nowrap"- }}- >- OMG BUY THIS AWESOME CAR!!1!- </h1>- </div>- );+ const frame = useCurrentFrame();+ const {width, height, durationInFrames} = useVideoConfig();+ const left = interpolate(frame, [0, durationInFrames], [width, width * -1]);+ return (+ <div+ style={{+ backgroundColor: 'hotpink',+ width: width,+ height: height,+ position: 'absolute',+ left: 0,+ top: 0,+ }}+ >+ <h1+ style={{+ fontSize: '5em',+ fontWeight: 'bold',+ position: 'absolute',+ top: height / 2 - 100,+ left,+ color: 'white',+ whiteSpace: 'nowrap',+ }}+ >+ OMG BUY THIS AWESOME CAR!!1!+ </h1>+ </div>+ );};export default CarSlideshow;diff --git a/packages/player-example/tsconfig.json b/packages/player-example/tsconfig.jsonindex 5f488e8e73..bad070d5fa 100644--- a/packages/player-example/tsconfig.json+++ b/packages/player-example/tsconfig.json@@ -1,25 +1,21 @@{- "include": [- "./src/**/*"- ],- "compilerOptions": {- "strict": true,- "esModuleInterop": true,- "lib": [- "dom",- "es2015"- ],- "jsx": "react-jsx",- "target": "es5",- "allowJs": true,- "skipLibCheck": true,- "allowSyntheticDefaultImports": true,- "forceConsistentCasingInFileNames": true,- "noFallthroughCasesInSwitch": true,- "module": "esnext",- "moduleResolution": "node",- "resolveJsonModule": true,- "isolatedModules": true,- "noEmit": true- }+ "include": ["./src/**/*"],+ "compilerOptions": {+ "strict": true,+ "esModuleInterop": true,+ "lib": ["dom", "es2015"],+ "jsx": "react-jsx",+ "target": "es5",+ "allowJs": true,+ "skipLibCheck": true,+ "allowSyntheticDefaultImports": true,+ "forceConsistentCasingInFileNames": true,+ "noFallthroughCasesInSwitch": true,+ "module": "esnext",+ "moduleResolution": "node",+ "resolveJsonModule": true,+ "isolatedModules": true,+ "noEmit": true+ },+ "references": [{"path": "../player"}, {"path": "../core"}]}diff --git a/packages/player/.eslintrc b/packages/player/.eslintrcindex f811da34c3..58011e33c5 100644--- a/packages/player/.eslintrc+++ b/packages/player/.eslintrc@@ -1,3 +1,9 @@{- "extends": "@jonny"+ "extends": "@jonny",+ "parserOptions": {+ "project": "./tsconfig.json"+ },+ "rules": {+ "ts-exports/unused-exports": "off"+ }}diff --git a/packages/player/src/RemotionPlayer.tsx b/packages/player/src/RemotionPlayer.tsxindex f9a4dfa180..9e633e412c 100644--- a/packages/player/src/RemotionPlayer.tsx+++ b/packages/player/src/RemotionPlayer.tsx@@ -1,30 +1,53 @@-import React, {useEffect, useRef, useState} from 'react';-import {Internals} from 'remotion';-import RemotionRootComponent from './RemotionRootComponent';+import React, {useMemo} from 'react';+import {CompositionManagerContext, CompProps, Internals} from 'remotion';+import RootComponent from './RootComponent';-type Props = {- id: string;-};--const Loading: React.FC = () => <h1>Loading…</h1>;+type Props<T> = {+ durationInFrames: number;+ width: number;+ height: number;+ fps: number;+ props?: T;+} & CompProps<T>;-export const RemotionPlayer: React.FC<Props> = ({id}) => {- const rootRef = useRef<React.FC | null>(null);- const Root = rootRef.current;- const [isInitialized, setIsInitialized] = useState(false);+export const Player = <T,>({+ durationInFrames,+ height,+ width,+ fps,+ props,+ ...componentProps+}: Props<T>) => {+ const component = Internals.useLazyComponent(componentProps);- useEffect(() => {- const Root = Internals.getRoot();- rootRef.current = Root;- setIsInitialized(true);- }, []);+ const compositionManagerContext: CompositionManagerContext = useMemo(() => {+ return {+ compositions: [+ {+ component,+ durationInFrames,+ height,+ width,+ fps,+ id: 'player-comp',+ props,+ },+ ],+ currentComposition: 'player-comp',+ registerComposition: () => void 0,+ registerSequence: () => void 0,+ sequences: [],+ setCurrentComposition: () => void 0,+ unregisterComposition: () => void 0,+ unregisterSequence: () => void 0,+ };+ }, [component, props, durationInFrames, fps, height, width]);- return Root && isInitialized ? (+ return (<Internals.RemotionRoot>- <Root />- <div className="App">- <RemotionRootComponent id={id} />- </div>+ <Internals.CompositionManager.Provider value={compositionManagerContext}>+ <RootComponent />+ </Internals.CompositionManager.Provider></Internals.RemotionRoot>- ) : null;+ );};diff --git a/packages/player/src/RemotionRootComponent.tsx b/packages/player/src/RootComponent.tsxsimilarity index 59%rename from packages/player/src/RemotionRootComponent.tsxrename to packages/player/src/RootComponent.tsxindex cc5a080183..c65c10bea8 100644--- a/packages/player/src/RemotionRootComponent.tsx+++ b/packages/player/src/RootComponent.tsx@@ -1,33 +1,16 @@-import React, {Suspense, useContext, useEffect} from 'react';+import React, {Suspense} from 'react';import {Internals} from 'remotion';import {usePlaybackTime} from './PlayPause';-const Loading: React.FC = () => <h1>Loading…</h1>;--const RemotionRootComponent: React.FC<{id: string}> = ({id}) => {- const {setCurrentComposition, currentComposition} = useContext(- Internals.CompositionManager- );+const RootComponent: React.FC = () => {const config = Internals.useUnsafeVideoConfig();const [toggle] = usePlaybackTime();const [playing] = Internals.Timeline.usePlayingState();const video = Internals.useVideo();const VideoComponent = video ? video.component : null;- useEffect(() => {- if (!currentComposition) {- setCurrentComposition(id);- }-- if (!config) {- return;- }- }, [config, currentComposition, setCurrentComposition]);-- console.log(currentComposition, 'video');-return (- <Suspense fallback={Loading}>+ <Suspense fallback={<h1>Loading...</h1>}><h1>Remotion Video Player</h1><divstyle={{@@ -38,6 +21,7 @@ const RemotionRootComponent: React.FC<{id: string}> = ({id}) => {}}><button+ type="button"style={{position: 'absolute',left: '10px',@@ -56,4 +40,4 @@ const RemotionRootComponent: React.FC<{id: string}> = ({id}) => {);};-export default RemotionRootComponent;+export default RootComponent;diff --git a/packages/player/src/index.tsx b/packages/player/src/index.tsxindex fb2a0e29b3..082b2554d9 100644--- a/packages/player/src/index.tsx+++ b/packages/player/src/index.tsx@@ -1,2 +1,2 @@export * from './PlayPause';-export {RemotionPlayer} from './RemotionPlayer';+export {Player} from './RemotionPlayer';