Skip to content

🎥 Make videos programmatically with React

fix ssr

Jonny Burger47114b55b31a
diff --git a/packages/core/src/use-lazy-component.ts b/packages/core/src/use-lazy-component.ts
index aa98fe5c39..a0c77013c4 100644
--- a/packages/core/src/use-lazy-component.ts
+++ b/packages/core/src/use-lazy-component.ts
@@ -2,14 +2,25 @@ 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>) => {
+export const useLazyComponent = (
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ compProps: CompProps<any>
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+): React.LazyExoticComponent<React.ComponentType<any>> => {
const lazy = useMemo(() => {
if ('lazyComponent' in compProps) {
return React.lazy(compProps.lazyComponent);
}
if ('component' in compProps) {
+ // In SSR, suspense is not yet supported, we cannot use React.lazy
+ if (typeof document === 'undefined') {
+ return (compProps.component as unknown) as React.LazyExoticComponent<
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ React.ComponentType<any>
+ >;
+ }
+
return React.lazy(() => Promise.resolve({default: compProps.component}));
}
diff --git a/packages/docs/components/Player.tsx b/packages/docs/components/Player.tsx
index 0ae9cd75a0..77e264e098 100644
--- a/packages/docs/components/Player.tsx
+++ b/packages/docs/components/Player.tsx
@@ -1,6 +1,6 @@
import { Player } from "@remotion/player";
import React from "react";
-import { useCurrentFrame } from "../../babel-loader/node_modules/remotion/dist";
+import { useCurrentFrame } from "remotion";
const Comp: React.FC = () => {
const frame = useCurrentFrame();
@@ -11,11 +11,16 @@ export const PlayerExample: React.FC = () => {
return (
<Player
component={Comp}
- width={900}
- height={500}
+ compositionWidth={1280}
+ compositionHeight={720}
controls
durationInFrames={100}
fps={30}
+ style={{
+ aspectRatio: "16 / 9",
+ height: undefined,
+ width: "100%",
+ }}
loop
></Player>
);
diff --git a/packages/docs/docs/player.md b/packages/docs/docs/player.md
index 25f193078d..a90de3bf5b 100644
--- a/packages/docs/docs/player.md
+++ b/packages/docs/docs/player.md
@@ -10,6 +10,8 @@ import { ExperimentalBadge } from "../components/Experimental.tsx";
<ExperimentalBadge/>
+<PlayerExample />
+
Using the Remotion Player you can embed Remotion videos in any React app and customize the video content at runtime.
## Installation
@@ -250,5 +252,4 @@ Before we mark the player as stable, we are looking to improve in the following
- Better props validation
- Customize error UI
- Volume slider
-
-<PlayerExample />
+- Automatically adapt correct aspect ratio if passing styles like "width: 100%".
diff --git a/packages/docs/package.json b/packages/docs/package.json
index 5ab46200f6..e54b1c2e7c 100644
--- a/packages/docs/package.json
+++ b/packages/docs/package.json
@@ -15,6 +15,7 @@
"@docusaurus/core": "2.0.0-alpha.75",
"@docusaurus/preset-classic": "2.0.0-alpha.75",
"@mdx-js/react": "^1.6.21",
+ "remotion": "^2.0.7",
"@remotion/player": "^2.0.7",
"@splitbee/web": "^0.2.4",
"@types/react": "^17.0.4",
diff --git a/packages/player/src/PlayerUI.tsx b/packages/player/src/PlayerUI.tsx
index e13eebe735..e60a2e4967 100644
--- a/packages/player/src/PlayerUI.tsx
+++ b/packages/player/src/PlayerUI.tsx
@@ -38,9 +38,7 @@ const PlayerUI: React.ForwardRefRenderFunction<
const [hasPausedToResume, setHasPausedToResume] = useState(false);
const [shouldAutoplay, setShouldAutoPlay] = useState(autoPlay);
- const [isFullscreen, setIsFullscreen] = useState(() =>
- IS_NODE ? false : document.fullscreenElement !== null
- );
+ const [isFullscreen, setIsFullscreen] = useState(() => false);
usePlayback({loop});
const player = usePlayer();
@@ -209,34 +207,36 @@ const PlayerUI: React.ForwardRefRenderFunction<
return null;
}
- return (
- <Suspense fallback={<h1>Loading...</h1>}>
- <div ref={container} style={outerStyle}>
- <div style={outer}>
- <div style={containerStyle} className={PLAYER_CSS_CLASSNAME}>
- {VideoComponent ? (
- <ErrorBoundary onError={onError}>
- <VideoComponent
- {...(((video?.props as unknown) as {}) ?? {})}
- />
- </ErrorBoundary>
- ) : null}
- </div>
+ const content = (
+ <div ref={container} style={outerStyle}>
+ <div style={outer}>
+ <div style={containerStyle} className={PLAYER_CSS_CLASSNAME}>
+ {VideoComponent ? (
+ <ErrorBoundary onError={onError}>
+ <VideoComponent {...(((video?.props as unknown) as {}) ?? {})} />
+ </ErrorBoundary>
+ ) : null}
</div>
- {controls ? (
- <Controls
- fps={config.fps}
- durationInFrames={config.durationInFrames}
- hovered={hovered}
- player={player}
- requestFullScreenAccess={requestFullScreenAccess}
- isFullscreen={isFullscreen}
- allowFullscreen={allowFullscreen}
- />
- ) : null}
</div>
- </Suspense>
+ {controls ? (
+ <Controls
+ fps={config.fps}
+ durationInFrames={config.durationInFrames}
+ hovered={hovered}
+ player={player}
+ requestFullScreenAccess={requestFullScreenAccess}
+ isFullscreen={isFullscreen}
+ allowFullscreen={allowFullscreen}
+ />
+ ) : null}
+ </div>
);
+ // Don't render suspense on
+ if (IS_NODE) {
+ return content;
+ }
+
+ return <Suspense fallback={<h1>Loading...</h1>}>{content}</Suspense>;
};
export default forwardRef(PlayerUI);