Skip to content

πŸŽ₯ Make videos programmatically with React

alright

Jonny Burgerd1cd83583eac
diff --git a/packages/core/src/RemotionRoot.tsx b/packages/core/src/RemotionRoot.tsx
index 08c53a3931..bd460ffdcb 100644
--- a/packages/core/src/RemotionRoot.tsx
+++ b/packages/core/src/RemotionRoot.tsx
@@ -55,6 +55,7 @@ export const RemotionRoot: React.FC<{
const [playing, setPlaying] = useState<boolean>(false);
const imperativePlaying = useRef<boolean>(false);
const [fastRefreshes, setFastRefreshes] = useState(0);
+ const [manualRefreshes, setManualRefreshes] = useState(0);
const [playbackRate, setPlaybackRate] = useState(1);
const audioAndVideoTags = useRef<PlayableMediaTag[]>([]);
@@ -117,13 +118,14 @@ export const RemotionRoot: React.FC<{
return {
getNonce: () => counter++,
fastRefreshes,
+ manualRefreshes,
};
- }, [fastRefreshes]);
+ }, [fastRefreshes, manualRefreshes]);
const setNonceContext = useMemo((): TSetNonceContext => {
return {
- increaseNonce: () => {
- setFastRefreshes((i) => i + 1);
+ increaseManualRefreshes: () => {
+ setManualRefreshes((i) => i + 1);
},
};
}, []);
diff --git a/packages/core/src/ResolveCompositionConfig.tsx b/packages/core/src/ResolveCompositionConfig.tsx
index 2ed866cba2..10ffced13c 100644
--- a/packages/core/src/ResolveCompositionConfig.tsx
+++ b/packages/core/src/ResolveCompositionConfig.tsx
@@ -69,7 +69,13 @@ export const ResolveCompositionConfig: React.FC<
useState<string | null>(null);
const {compositions, canvasContent, currentCompositionMetadata} =
useContext(CompositionManager);
- const {fastRefreshes} = useContext(NonceContext);
+ const {fastRefreshes, manualRefreshes} = useContext(NonceContext);
+
+ // don't do anything, this component should should re-render if the value changes
+ if (manualRefreshes) {
+ /** */
+ }
+
const selectedComposition = useMemo(() => {
return compositions.find(
(c) =>
diff --git a/packages/core/src/nonce.ts b/packages/core/src/nonce.ts
index 767e3333ac..8b27362c3e 100644
--- a/packages/core/src/nonce.ts
+++ b/packages/core/src/nonce.ts
@@ -10,21 +10,21 @@ import {createContext, useContext, useEffect, useRef, useState} from 'react';
export type TNonceContext = {
getNonce: () => number;
fastRefreshes: number;
+ manualRefreshes: number;
};
export type TSetNonceContext = {
- increaseNonce: () => void;
+ increaseManualRefreshes: () => void;
};
export const NonceContext = createContext<TNonceContext>({
getNonce: () => 0,
fastRefreshes: 0,
+ manualRefreshes: 0,
});
export const SetNonceContext = createContext<TSetNonceContext>({
- increaseNonce: () => {
- throw new Error('increaseNonce is not implemented');
- },
+ increaseManualRefreshes: () => {},
});
export const useNonce = (): number => {
diff --git a/packages/docs/docs/studio/TableOfContents.tsx b/packages/docs/docs/studio/TableOfContents.tsx
index 2c844c9ff8..007fa5eb3f 100644
--- a/packages/docs/docs/studio/TableOfContents.tsx
+++ b/packages/docs/docs/studio/TableOfContents.tsx
@@ -68,6 +68,10 @@ export const TableOfContents: React.FC = () => {
<strong>{'reevaluateComposition()'}</strong>
<div>Re-runs calculateMetadata() on the current composition</div>
</TOCItem>
+ <TOCItem link="/docs/studio/visual-control">
+ <strong>{'visualControl()'}</strong>
+ <div>Create a control in the right sidebar of the Studio</div>
+ </TOCItem>
</Grid>
</div>
);
diff --git a/packages/docs/docs/studio/use-visual-controls.mdx b/packages/docs/docs/studio/use-visual-controls.mdx
deleted file mode 100644
index 21c85503a0..0000000000
--- a/packages/docs/docs/studio/use-visual-controls.mdx
+++ /dev/null
@@ -1,42 +0,0 @@
----
-title: useVisualControls()
-crumb: '@remotion/studio'
----
-
-# useVisualControls()
-
-Creates a control in the right sidebar of the [Remotion Studio](/docs/studio) that allows you to change a value.
-
-Useful for if you have a hardcoded constant that you want to find the optimal value for.
-
-## Example
-
-Consider you have a value `rotation` that you want to find the optimal value for:
-
-```tsx twoslash title="Root.tsx"
-import {useVideoConfig} from 'remotion';
-
-const MyComp: React.FC = () => {
- const rotation = 180;
-
- return <div style={{rotate: `${rotation}deg`}}>Hello</div>;
-};
-```
-
-Instead of changing the value of `rotation` in the code, you can create a control in the right sidebar of the [Remotion Studio](/docs/studio) that allows you to change the value:
-
-```tsx twoslash title="Root.tsx"
-import {useVisualControls} from '@remotion/studio';
-
-const MyComp: React.FC = () => {
- const {visualControl} = useVisualControls();
-
- const rotation = visualControl('rotation', 180);
-
- return <div style={{rotate: `${rotation}deg`}}>Hello</div>;
-};
-```
-
-## See also
-
-- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/studio/src/api/use-visual-controls.ts)
diff --git a/packages/docs/docs/studio/visual-control.mdx b/packages/docs/docs/studio/visual-control.mdx
new file mode 100644
index 0000000000..f66ea95bd9
--- /dev/null
+++ b/packages/docs/docs/studio/visual-control.mdx
@@ -0,0 +1,124 @@
+---
+title: visualControl()
+crumb: '@remotion/studio'
+---
+
+# visualControl()<AvailableFrom v="4.0.292" />
+
+:::warning
+**Preview feature:** A few issues are known, and filed under this [issue](https://github.com/remotion-dev/remotion/issues/5210).
+:::
+
+Creates a control in the right sidebar of the [Remotion Studio](/docs/studio) that allows you to change a value.
+
+Useful for if you have a hardcoded constant that you want to quickly find the optimal value for.
+
+## Example
+
+Consider you have a value `rotation` that you want to find the optimal value for:
+
+```tsx twoslash title="A simple composition"
+import {useVideoConfig} from 'remotion';
+
+const MyComp: React.FC = () => {
+ const rotation = 180;
+
+ return <div style={{rotate: `${rotation}deg`}}>Hello</div>;
+};
+```
+
+Instead of changing the value of `rotation` in the code, you can create a control in the right sidebar of the [Remotion Studio](/docs/studio) that allows you to change the value:
+
+```tsx twoslash title="Creating a visual control with the name 'rotation'"
+import {visualControl} from '@remotion/studio';
+
+const MyComp: React.FC = () => {
+ const rotation = visualControl('rotation', 180);
+
+ return <div style={{rotate: `${rotation}deg`}}>Hello</div>;
+};
+```
+
+Now, in the right sidebar of the [Remotion Studio](/docs/studio), you will see a control with the name `rotation` and a slider to change the value.
+
+<img style={{width: '400px'}} src="/img/visual-control.png" />
+
+Now you can change the value as you please, until you have found the optimal value.
+
+Once you are happy with the value, you can save it back to the code by clicking the save button in the right sidebar.
+
+## Defining a schema
+
+Only primitive values (`string` and `number`) automatically infer the type of the control.
+
+If you want to define a more complex schema, you can do so by passing a Zod schema as the third argument.
+
+```tsx twoslash title="Editing an object"
+import {visualControl} from '@remotion/studio';
+
+// ---cut---
+import {z} from 'zod';
+
+const data = visualControl(
+ 'my-data',
+ {
+ rotation: 180,
+ text: 'Hello',
+ },
+ z.object({
+ rotation: z.number(),
+ text: z.string(),
+ }),
+);
+```
+
+See: [Visual Editing](/docs/visual-editing)
+
+```tsx twoslash title="Editing a color"
+import {visualControl} from '@remotion/studio';
+import {zColor} from '@remotion/zod-types';
+
+const color = visualControl('my-color', '#fff', zColor());
+```
+
+See: [`zColor()`](/docs/zod-types/zcolor)
+
+```tsx twoslash title="Editing a matrix"
+import {visualControl} from '@remotion/studio';
+import {zMatrix} from '@remotion/zod-types';
+
+const matrix = visualControl('my-matrix', [1, 2, 3, 4], zMatrix());
+```
+
+See: [`zMatrix()`](/docs/zod-types/zmatrix)
+
+## When to use
+
+- If you want to create configuration options, use the [Props Editor](/docs/visual-editing).
+- If you want to quickly find the optimal value for a hardcoded constant, use `visualControl()`.
+
+## Important to know
+
+<Step>1</Step> The saving feature works only if the first argument of the `visualControl()` function is static, because static analysis is performed on the source file. The following will not work:
+
+```tsx twoslash title="❌ This is not possible"
+import {visualControl} from '@remotion/studio';
+
+// ---cut---
+
+// ❌ Not possible to use string interpolation
+const name = 'my-data';
+const data = visualControl(`rotation-${name}`, 180);
+
+// ❌ Not possible to use a variable
+const idOutside = 'rotation-my-data';
+const dataOutside = visualControl(idOutside, 180);
+```
+
+<Step>2</Step> At the moment, unsaved values are not applied when rendering the video, even if the preview shows them. Save the values before rendering.
+
+<Step>3</Step> Controls may not be automatically remove themselves when you remove them from your code.{' '}
+
+## See also
+
+- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/studio/src/api/visual-control.ts)
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 4e4e36e6e6..047953fa60 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -551,6 +551,7 @@ module.exports = {
'studio/update-default-props',
'studio/focus-default-props-path',
'studio/reevaluate-composition',
+ 'studio/visual-control',
],
},
{
diff --git a/packages/docs/static/img/visual-control.png b/packages/docs/static/img/visual-control.png
new file mode 100644
index 0000000000..9f33e0a270
Binary files /dev/null and b/packages/docs/static/img/visual-control.png differ
diff --git a/packages/example/src/VisualControls/index.tsx b/packages/example/src/VisualControls/index.tsx
index 124bf68d72..c46f9f9970 100644
--- a/packages/example/src/VisualControls/index.tsx
+++ b/packages/example/src/VisualControls/index.tsx
@@ -1,35 +1,17 @@
-import {makeTransform, matrix3d} from '@remotion/animation-utils';
import {visualControl} from '@remotion/studio';
-import {zMatrix, zTextarea} from '@remotion/zod-types';
-import {AbsoluteFill, staticFile} from 'remotion';
+import {AbsoluteFill} from 'remotion';
export const VisualControls = () => {
- const matrix = visualControl(
- 'my-matrix',
- [8.71, 0.83, 0, 0, 0, 2.79, 0, 0, 1.26, 0.35, 1, 0, 0, 0, 0, 1] as const,
- zMatrix(),
- );
-
- const matrix2 = visualControl(
- `my-matrix-3`,
- [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] as const,
- zMatrix(),
- );
- const val3 = visualControl(`my-matrix-4`, 1234);
- const val4 = visualControl(`value πŸ‹`, 'hi there');
- const val5 = visualControl(`text πŸ‹`, 'hi there', zTextarea());
-
- const val6 = visualControl('file', staticFile('clip001.mkv'));
+ const rotation = visualControl('rotation', 180);
return (
<AbsoluteFill
className="bg-red-300 text-7xl flex flex-1 items-center justify-center"
style={{
- transform: makeTransform([matrix3d(...matrix)]),
+ transform: `rotate(${rotation}deg)`,
}}
>
- <p>{matrix.join(', ')}</p>
- <p>{val5}</p>
+ Hello
</AbsoluteFill>
);
};
diff --git a/packages/studio/src/components/OptionsPanel.tsx b/packages/studio/src/components/OptionsPanel.tsx
index cc9cb79e6f..bd87886b93 100644
--- a/packages/studio/src/components/OptionsPanel.tsx
+++ b/packages/studio/src/components/OptionsPanel.tsx
@@ -184,6 +184,14 @@ export const OptionsPanel: React.FC<{
<div style={container} className="css-reset">
<div style={tabsContainer}>
<Tabs>
+ {visualControlsTabActivated ? (
+ <Tab
+ selected={panel === 'visual-controls'}
+ onClick={onVisualControlsSelected}
+ >
+ Controls
+ </Tab>
+ ) : null}
{composition ? (
<Tab
selected={panel === 'input-props'}
@@ -205,14 +213,6 @@ export const OptionsPanel: React.FC<{
selected={panel === 'renders'}
/>
)}
- {visualControlsTabActivated ? (
- <Tab
- selected={panel === 'visual-controls'}
- onClick={onVisualControlsSelected}
- >
- Controls
- </Tab>
- ) : null}
</Tabs>
</div>
{panel === `input-props` && composition ? (
@@ -227,7 +227,7 @@ export const OptionsPanel: React.FC<{
setSaving={setSaving}
readOnlyStudio={readOnlyStudio}
/>
- ) : panel === 'visual-controls' ? (
+ ) : panel === 'visual-controls' && visualControlsTabActivated ? (
<VisualControlsContent />
) : readOnlyStudio ? null : (
<RenderQueue />
diff --git a/packages/studio/src/components/Timeline/TimelineStack/source-attribution.ts b/packages/studio/src/components/Timeline/TimelineStack/source-attribution.ts
index c88057797f..293f799e46 100644
--- a/packages/studio/src/components/Timeline/TimelineStack/source-attribution.ts
+++ b/packages/studio/src/components/Timeline/TimelineStack/source-attribution.ts
@@ -11,8 +11,8 @@ export const getOriginalSourceAttribution = (
const last = split[split.length - 1];
if (last.startsWith('index')) {
const lastTwo = split[split.length - 2];
- return `${lastTwo}/${last}:${originalLocation.line}:${originalLocation.column}`;
+ return `${lastTwo}/${last}:${originalLocation.line}`;
}
- return `${last}:${originalLocation.line}:${originalLocation.column}`;
+ return `${last}:${originalLocation.line}`;
};
diff --git a/packages/studio/src/components/VisualControls/VisualControlHandle.tsx b/packages/studio/src/components/VisualControls/VisualControlHandle.tsx
index e8080dd416..726450e370 100644
--- a/packages/studio/src/components/VisualControls/VisualControlHandle.tsx
+++ b/packages/studio/src/components/VisualControls/VisualControlHandle.tsx
@@ -32,7 +32,7 @@ export const VisualControlHandle: React.FC<{
const state = useContext(VisualControlsContext);
const {updateValue} = useContext(SetVisualControlsContext);
const {fastRefreshes} = useContext(Internals.NonceContext);
- const {increaseNonce} = useContext(Internals.SetNonceContext);
+ const {increaseManualRefreshes} = useContext(Internals.SetNonceContext);
const [saving, setSaving] = useState(false);
@@ -47,7 +47,7 @@ export const VisualControlHandle: React.FC<{
schema: value.schema,
setValue: (updater) => {
updateValue(keyName, updater(currentValue));
- increaseNonce();
+ increaseManualRefreshes();
},
unsavedValue: currentValue,
savedValue: value.valueInCode,
diff --git a/packages/studio/src/visual-controls/VisualControls.tsx b/packages/studio/src/visual-controls/VisualControls.tsx
index 50355dd45d..ce7f7566b3 100644
--- a/packages/studio/src/visual-controls/VisualControls.tsx
+++ b/packages/studio/src/visual-controls/VisualControls.tsx
@@ -2,14 +2,13 @@ import React, {
createContext,
createRef,
useCallback,
- useContext,
useEffect,
useImperativeHandle,
useMemo,
useRef,
useState,
} from 'react';
-import {getRemotionEnvironment, Internals} from 'remotion';
+import {getRemotionEnvironment} from 'remotion';
import type {z, ZodTypeAny} from 'zod';
import {getZodSchemaFromPrimitive} from '../api/get-zod-schema-from-primitive';
import {useZodIfPossible} from '../components/get-zod-if-possible';
@@ -37,10 +36,6 @@ export const VisualControlsTabActivatedContext =
createContext<VisualControlsTabActivated>(false);
export type SetVisualControlsContextType = {
- setControl: (
- key: string,
- value: VisualControlValueWithoutUnsaved,
- ) => {changed: boolean; currentValue: unknown};
updateHandles: () => void;
updateValue: (key: string, value: unknown) => void;
visualControl: <T>(key: string, value: T, schema?: z.ZodTypeAny) => T;
@@ -59,9 +54,6 @@ export const visualControlRef = createRef<VisualControlRef>();
export const SetVisualControlsContext =
createContext<SetVisualControlsContextType>({
- setControl: () => {
- throw new Error('addControl is not implemented');
- },
updateHandles: () => {
throw new Error('updateHandles is not implemented');
},
@@ -81,7 +73,6 @@ export const VisualControlsProvider: React.FC<{
const [handles, setHandles] = useState<Record<string, VisualControlValue>>(
{},
);
- const {increaseNonce} = useContext(Internals.SetNonceContext);
const state: VisualControlsContextType = useMemo(() => {
return {
@@ -163,7 +154,6 @@ export const VisualControlsProvider: React.FC<{
(key: string, value: unknown) => {
imperativeHandles.current = {
...imperativeHandles.current,
-
[key]: {
...imperativeHandles.current[key],
unsavedValue: value,
@@ -179,6 +169,7 @@ export const VisualControlsProvider: React.FC<{
globalVisualControl: visualControl,
};
}, [visualControl]);
+
useEffect(() => {
const callback = () => {
if (imperativeHandles.current) {
@@ -192,7 +183,7 @@ export const VisualControlsProvider: React.FC<{
return () => {
clearInterval(interval);
};
- }, [increaseNonce, updateHandles]);
+ }, [updateHandles]);
const setState: SetVisualControlsContextType = useMemo(() => {
return {