Skip to content

🎥 Make videos programmatically with React

implement schema + type safety for inputProps

JonnyBurger51f07856421d
diff --git a/bun.lock b/bun.lock
index 9927f605e3..83ea1a9a35 100644
--- a/bun.lock
+++ b/bun.lock
@@ -2030,6 +2030,7 @@
"react-dom": "catalog:",
"vitest": "4.0.7",
"vitest-browser-react": "^2.0.2",
+ "zod": "catalog:",
},
"peerDependencies": {
"react": ">=18.0.0",
diff --git a/packages/web-renderer/package.json b/packages/web-renderer/package.json
index 98a2bfde97..16cfa1960e 100644
--- a/packages/web-renderer/package.json
+++ b/packages/web-renderer/package.json
@@ -26,7 +26,8 @@
"react": "catalog:",
"react-dom": "catalog:",
"vitest": "4.0.7",
- "vitest-browser-react": "^2.0.2"
+ "vitest-browser-react": "^2.0.2",
+ "zod": "catalog:"
},
"peerDependencies": {
"react": ">=18.0.0",
diff --git a/packages/web-renderer/src/props-if-has-props.ts b/packages/web-renderer/src/props-if-has-props.ts
new file mode 100644
index 0000000000..1431a9172c
--- /dev/null
+++ b/packages/web-renderer/src/props-if-has-props.ts
@@ -0,0 +1,24 @@
+import type {AnyZodObject, z} from 'zod';
+
+export type PropsIfHasProps<
+ Schema extends AnyZodObject,
+ Props,
+> = AnyZodObject extends Schema
+ ? {} extends Props
+ ? {
+ // Neither props nor schema specified
+ inputProps?: z.input<Schema> & Props;
+ }
+ : {
+ // Only props specified
+ inputProps: Props;
+ }
+ : {} extends Props
+ ? {
+ // Only schema specified
+ inputProps: z.input<Schema>;
+ }
+ : {
+ // Props and schema specified
+ inputProps: z.input<Schema> & Props;
+ };
diff --git a/packages/web-renderer/src/render-still-on-web.tsx b/packages/web-renderer/src/render-still-on-web.tsx
index a9faa7a21a..4ef4b9d1d0 100644
--- a/packages/web-renderer/src/render-still-on-web.tsx
+++ b/packages/web-renderer/src/render-still-on-web.tsx
@@ -3,35 +3,50 @@ import {flushSync} from 'react-dom';
import ReactDOM from 'react-dom/client';
import type {_InternalTypes, LogLevel} from 'remotion';
import {Internals} from 'remotion';
+import type {AnyZodObject} from 'zod';
+import type {PropsIfHasProps} from './props-if-has-props';
import {takeScreenshot} from './take-screenshot';
import {waitForReady} from './wait-for-ready';
import {withResolvers} from './with-resolvers';
-type MandatoryRenderStillOnWebOptions<T extends Record<string, unknown>> = {
- component: ComponentType<T>;
+type LooseComponentType<T> = ComponentType<T> | ((props: T) => React.ReactNode);
+
+type MandatoryRenderStillOnWebOptions<
+ Schema extends AnyZodObject,
+ Props extends Record<string, unknown>,
+> = {
+ component: LooseComponentType<Props>;
width: number;
height: number;
fps: number;
durationInFrames: number;
frame: number;
- inputProps: T;
-};
+} & PropsIfHasProps<Schema, Props>;
-type OptionalRenderStillOnWebOptions = {
+type OptionalRenderStillOnWebOptions<Schema extends AnyZodObject> = {
delayRenderTimeoutInMilliseconds: number;
logLevel: LogLevel;
+ schema: Schema | undefined;
};
-type InternalRenderStillOnWebOptions<T extends Record<string, unknown>> =
- MandatoryRenderStillOnWebOptions<T> & OptionalRenderStillOnWebOptions;
+type InternalRenderStillOnWebOptions<
+ Schema extends AnyZodObject,
+ Props extends Record<string, unknown>,
+> = MandatoryRenderStillOnWebOptions<Schema, Props> &
+ OptionalRenderStillOnWebOptions<Schema>;
-type RenderStillOnWebOptions<T extends Record<string, unknown>> =
- MandatoryRenderStillOnWebOptions<T> &
- Partial<OptionalRenderStillOnWebOptions>;
+export type RenderStillOnWebOptions<
+ Schema extends AnyZodObject,
+ Props extends Record<string, unknown>,
+> = MandatoryRenderStillOnWebOptions<Schema, Props> &
+ Partial<OptionalRenderStillOnWebOptions<Schema>>;
const COMP_ID = 'markup';
-async function internalRenderStillOnWeb<T extends Record<string, unknown>>({
+async function internalRenderStillOnWeb<
+ Schema extends AnyZodObject,
+ Props extends Record<string, unknown>,
+>({
component: Component,
width,
height,
@@ -41,7 +56,7 @@ async function internalRenderStillOnWeb<T extends Record<string, unknown>>({
delayRenderTimeoutInMilliseconds,
logLevel,
inputProps,
-}: InternalRenderStillOnWebOptions<T>) {
+}: InternalRenderStillOnWebOptions<Schema, Props>) {
const div = document.createElement('div');
// Match same behavior as renderEntry.tsx
@@ -83,6 +98,8 @@ async function internalRenderStillOnWeb<T extends Record<string, unknown>>({
remotion_attempt: 0,
};
+ const actualInputProps = inputProps ?? ({} as Props);
+
flushSync(() => {
root.render(
<Internals.RemotionEnvironmentContext
@@ -120,7 +137,7 @@ async function internalRenderStillOnWeb<T extends Record<string, unknown>>({
compositionId: COMP_ID,
},
currentCompositionMetadata: {
- props: inputProps,
+ props: inputProps ?? {},
durationInFrames,
fps,
height,
@@ -145,7 +162,9 @@ async function internalRenderStillOnWeb<T extends Record<string, unknown>>({
}}
>
<Internals.CanUseRemotionHooks value>
- <Component {...inputProps} />
+ {/**
+ * @ts-expect-error */}
+ <Component {...actualInputProps} />
</Internals.CanUseRemotionHooks>
</Internals.RemotionRoot>
</Internals.CompositionManager.Provider>
@@ -167,13 +186,17 @@ async function internalRenderStillOnWeb<T extends Record<string, unknown>>({
return imageData;
}
-export const renderStillOnWeb = <T extends Record<string, unknown>>(
- options: RenderStillOnWebOptions<T>,
+export const renderStillOnWeb = <
+ Schema extends AnyZodObject,
+ Props extends Record<string, unknown>,
+>(
+ options: RenderStillOnWebOptions<Schema, Props>,
) => {
- return internalRenderStillOnWeb({
+ return internalRenderStillOnWeb<Schema, Props>({
...options,
delayRenderTimeoutInMilliseconds:
options.delayRenderTimeoutInMilliseconds ?? 30000,
logLevel: options.logLevel ?? 'info',
+ schema: options.schema ?? undefined,
});
};
diff --git a/packages/web-renderer/src/test/input-props.test.tsx b/packages/web-renderer/src/test/input-props.test.tsx
index 03268cbca5..32aca5d5d1 100644
--- a/packages/web-renderer/src/test/input-props.test.tsx
+++ b/packages/web-renderer/src/test/input-props.test.tsx
@@ -27,6 +27,14 @@ const DisallowGetInputProps: React.FC<{}> = () => {
return null;
};
+const HasNoProps: React.FC<{}> = () => {
+ return null;
+};
+
+const HasProps: React.FC<{abc: 'def'}> = () => {
+ return null;
+};
+
test('cannot call getInputProps() while rendering client-side', async () => {
await renderStillOnWeb({
component: MustAcceptInputProps,
@@ -54,3 +62,55 @@ test('cannot call getInputProps() while rendering client-side', async () => {
'Cannot call `getInputProps()` - window.remotion_inputProps is not set. This API is only available if you are in the Studio, or while you are rendering server-side.',
);
});
+
+type MockSignature = typeof renderStillOnWeb;
+const mockFn: MockSignature = () => Promise.resolve(new Blob());
+
+test('Should be able to omit input props when component accepts no props', () => {
+ mockFn({
+ component: HasNoProps,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 30,
+ frame: 20,
+ });
+});
+
+test('Should not be able to omit input props when component accepts props', () => {
+ // @ts-expect-error - inputProps is required
+ mockFn({
+ component: HasProps,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 30,
+ frame: 20,
+ });
+
+ mockFn({
+ component: HasProps,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 30,
+ frame: 20,
+ inputProps: {
+ // @ts-expect-error - must match signature
+ abc: 'efg',
+ },
+ });
+
+ // No error, how it should be :)
+ mockFn({
+ component: HasProps,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 30,
+ frame: 20,
+ inputProps: {
+ abc: 'def',
+ },
+ });
+});