Skip to content

🎥 Make videos programmatically with React

allow to defer registerRoot

Jonny Burger85a39a04ca2c
diff --git a/packages/bundler/src/renderEntry.tsx b/packages/bundler/src/renderEntry.tsx
index 9d5bf02391..7d0674deb5 100644
--- a/packages/bundler/src/renderEntry.tsx
+++ b/packages/bundler/src/renderEntry.tsx
@@ -25,17 +25,20 @@ import {Homepage} from './homepage/homepage';
Internals.CSSUtils.injectCSS(Internals.CSSUtils.makeDefaultCSS(null, '#fff'));
-const Root = Internals.getRoot();
-
-if (!Root) {
- throw new Error('Root has not been registered.');
-}
-
const handle = delayRender('Loading root component');
const GetVideo: React.FC<{state: BundleState}> = ({state}) => {
const video = Internals.useVideo();
const compositions = useContext(Internals.CompositionManager);
+ const [Root, setRoot] = useState<React.FC | null>(() => Internals.getRoot());
+ const [waitForRoot] = useState(() => {
+ if (Root) {
+ return 0;
+ }
+
+ return delayRender('Waiting for registerRoot()');
+ });
+
const [Component, setComponent] = useState<ComponentType<unknown> | null>(
null
);
@@ -84,6 +87,12 @@ const GetVideo: React.FC<{state: BundleState}> = ({state}) => {
}
}, [Component, state.type]);
+ useEffect(() => {
+ if (Root) {
+ continueRender(waitForRoot);
+ }
+ }, [Component, Root, video, waitForRoot]);
+
useEffect(() => {
if (!video) {
return;
@@ -100,27 +109,47 @@ const GetVideo: React.FC<{state: BundleState}> = ({state}) => {
};
}, [video]);
- if (!video) {
+ useEffect(() => {
+ if (Root) {
+ return;
+ }
+
+ const cleanup = Internals.waitForRoot((NewRoot) => {
+ setRoot(() => NewRoot);
+ });
+
+ return () => cleanup();
+ }, [Root, waitForRoot]);
+
+ if (!Root) {
return null;
}
+ if (!video) {
+ return <Root />;
+ }
+
return (
- <div
- ref={portalContainer}
- id="remotion-canvas"
- style={{
- width: video.width,
- height: video.height,
- display: 'flex',
- backgroundColor: 'transparent',
- }}
- />
+ <>
+ <Root />
+ <div
+ ref={portalContainer}
+ id="remotion-canvas"
+ style={{
+ width: video.width,
+ height: video.height,
+ display: 'flex',
+ backgroundColor: 'transparent',
+ }}
+ />
+ </>
);
};
const videoContainer = document.getElementById(
'video-container'
) as HTMLElement;
+
const explainerContainer = document.getElementById(
'explainer-container'
) as HTMLElement;
@@ -139,7 +168,6 @@ const renderContent = () => {
if (bundleMode.type === 'composition' || bundleMode.type === 'evaluation') {
const markup = (
<Internals.RemotionRoot>
- <Root />
<GetVideo state={bundleMode} />
</Internals.RemotionRoot>
);
diff --git a/packages/cli/src/editor/components/Editor.tsx b/packages/cli/src/editor/components/Editor.tsx
index c98eb28325..d6e92284f3 100644
--- a/packages/cli/src/editor/components/Editor.tsx
+++ b/packages/cli/src/editor/components/Editor.tsx
@@ -1,6 +1,8 @@
import {PlayerInternals, PreviewSize} from '@remotion/player';
-import React, {useCallback, useMemo, useState} from 'react';
+import React, {useCallback, useEffect, useMemo, useState} from 'react';
import {
+ continueRender,
+ delayRender,
Internals,
MediaVolumeContextValue,
SetMediaVolumeContextValue,
@@ -47,11 +49,17 @@ const background: React.CSSProperties = {
position: 'absolute',
};
-const Root = Internals.getRoot();
-
export const Editor: React.FC = () => {
const [emitter] = useState(() => new PlayerInternals.PlayerEmitter());
const [size, setSizeState] = useState(() => loadPreviewSizeOption());
+ const [Root, setRoot] = useState<React.FC | null>(() => Internals.getRoot());
+ const [waitForRoot] = useState(() => {
+ if (Root) {
+ return 0;
+ }
+
+ return delayRender('Waiting for registerRoot()');
+ });
const [checkerboard, setCheckerboardState] = useState(() =>
loadCheckerboardOption()
);
@@ -150,9 +158,19 @@ export const Editor: React.FC = () => {
};
}, [modalContextType]);
- if (!Root) {
- throw new Error('Root has not been registered. ');
- }
+ useEffect(() => {
+ if (Root) {
+ return;
+ }
+
+ const cleanup = Internals.waitForRoot((NewRoot) => {
+ setRoot(() => NewRoot);
+ });
+
+ continueRender(waitForRoot);
+
+ return () => cleanup();
+ }, [Root, waitForRoot]);
return (
<KeybindingContextProvider>
@@ -183,7 +201,7 @@ export const Editor: React.FC = () => {
onOutsideClick={noop}
>
<div style={background}>
- <Root />
+ {Root === null ? null : <Root />}
<FramePersistor />
<EditorContent />
<GlobalKeybindings />
diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.ts
index c2dabba794..267f5fd9fc 100644
--- a/packages/core/src/internals.ts
+++ b/packages/core/src/internals.ts
@@ -88,7 +88,7 @@ import {getPreviewDomElement} from './get-preview-dom-element';
import {isAudioCodec} from './is-audio-codec';
import * as perf from './perf';
import {portalNode} from './portal-node';
-import {getRoot} from './register-root';
+import {getRoot, waitForRoot} from './register-root';
import {RemotionRoot} from './RemotionRoot';
import {SequenceContext} from './sequencing';
import {ENV_VARIABLES_ENV_NAME, setupEnvVariables} from './setup-env-variables';
@@ -225,6 +225,7 @@ export const Internals = {
DELAY_RENDER_CALLSTACK_TOKEN,
useAbsoluteCurrentFrame,
portalNode,
+ waitForRoot,
};
export type {
diff --git a/packages/core/src/register-root.tsx b/packages/core/src/register-root.tsx
index 1b0a4a983d..2163b36928 100644
--- a/packages/core/src/register-root.tsx
+++ b/packages/core/src/register-root.tsx
@@ -1,15 +1,28 @@
import React from 'react';
-let root: React.FC | null = null;
+let Root: React.FC | null = null;
+
+let listeners: ((comp: React.FC) => void)[] = [];
export const registerRoot = (comp: React.FC) => {
- if (root) {
+ if (Root) {
throw new Error('registerRoot() was called more than once.');
}
- root = comp;
+ Root = comp;
+ listeners.forEach((l) => {
+ l(comp);
+ });
};
export const getRoot = () => {
- return root;
+ return Root;
+};
+
+export const waitForRoot = (fn: (comp: React.FC) => void) => {
+ listeners.push(fn);
+
+ return () => {
+ listeners = listeners.filter((l) => l !== fn);
+ };
};
diff --git a/packages/example/src/index.tsx b/packages/example/src/index.tsx
index 3ab387cd1a..9ccc5b137d 100644
--- a/packages/example/src/index.tsx
+++ b/packages/example/src/index.tsx
@@ -1,4 +1,6 @@
import {registerRoot} from 'remotion';
import {Index} from './Video';
-registerRoot(Index);
+setTimeout(() => {
+ registerRoot(Index);
+}, 500);