π₯ Make videos programmatically with React
Update interactivity best practices docs
JonnyBurger7d3836efd5d6
diff --git a/packages/docs/docs/studio/interactivity-best-practices.mdx b/packages/docs/docs/studio/interactivity-best-practices.mdxindex 3caccbe3f0..971a3e0d93 100644--- a/packages/docs/docs/studio/interactivity-best-practices.mdx+++ b/packages/docs/docs/studio/interactivity-best-practices.mdx@@ -6,193 +6,168 @@ crumb: "Remotion Studio"# Interactivity best practices-Write editable Remotion components so the [Studio interactivity](/docs/studio/interactivity) can understand the values that should be changed visually.+By writing Remotion markup in a specific way, the [Remotion Studio](/docs/studio) is able to recognize the structure of the code and makes it [interactive](/docs/studio/interactivity):-When a value is shown as computed in the Studio, it often means that the value is hidden behind a variable, helper function, conditional expression or transform string.+- Allowing items to be selected by clicking on them+- Allowing drag+drop, resizing and rotation+- Editing the CSS styles+- Making keyframes and easing values editable-## Quick checklist {#quick-checklist}+If the markup is too complex for the Studio to make it interactive, then the values become grayed out.+Use the [`/remotion-interactivity` skill](/docs/ai/skills#remotion-interactivity) skill to instruct an agent to restructure the markup to make it editable.-When converting a component for Studio interactivity, apply all of these rules together:+## Make an HTML element interactive using `Interactive`-- Use `Interactive.*` for editable HTML or SVG elements, or [`Interactive.withSchema()`](/docs/interactive-with-schema) for custom components.-- Add a descriptive `name` prop to the editable element or custom component.-- Keep every editable or keyframed value inline at the JSX prop that the Studio edits.-- Write animations as inline `interpolate()` calls.-- Keep `interpolate()` input and output ranges as hardcoded arrays, such as `[0, 30]`.-- Use Studio-editable style props such as `translate`, `scale`, `rotate`, `transformOrigin` and `opacity` instead of hiding values in a `transform` string.-- Use `translate` for animated movement and canvas dragging. Keep `top`, `left`, `right` and `bottom` for static anchoring.-- Keep `<Composition>` metadata and `defaultProps` inline unless the value is truly dynamic.--## Use `Interactive.*` for tweakable elements {#use-interactive-for-tweakable-elements}--Use the [`Interactive`](/docs/interactive) namespace when an HTML or SVG element should be selected, moved or edited in the Studio.+Every HTML and SVG element such as `<div>` can be turned interactive using `Interactive`:```tsx title="Interactive elements"-// π Selectable and editable in the Studio<Interactive.Div name="Greeting card" style={{fontSize: 80, padding: 24}}>Hello</Interactive.Div>--// π A regular element is not Studio-editable-<div style={{fontSize: 80, padding: 24}}>Hello</div>```-For a custom component, see [Make a component interactive](/docs/studio/make-component-interactive).+This allows styles and keyframes to be set in the Studio. Be sensible, if a component has many elements, the timeline might get messy.## Give interactive elements a descriptive name {#give-interactive-elements-a-descriptive-name}-Add a `name` prop to interactive elements and custom interactive components.-Use a short label that describes the element in the scene.--The name is shown in the Studio timeline and helps agents identify the correct element when making visual edits.+Add a `name` prop to elements to make them easily identifyable.```tsx title="Interactive names"-// π Easy to find in the Studio timeline and by agents-<Interactive.Div name="Hero title" style={{fontSize: 80}}>- Launch day-</Interactive.Div>--// π Omitted names are hard to identify in the Studio timeline and by agents-<Interactive.Div style={{fontSize: 80}}>- Launch day-</Interactive.Div>+<>+ <Interactive.Div name="Hero title" style={{fontSize: 80}}>+ Launch day+ </Interactive.Div>+ <Img name="Avatar" src="https://remotion.media/image.jpeg" />+ <Video name="Background" src="https://remotion.media/video.mp4" />+ <Sequence name="Title">+ Launch day+ </Sequence>+</>```-## Keep editable values visible {#keep-editable-values-visible}--<SuggestedPrompt prompt="/remotion-interactivity Inline the values to make them editable." />--Put values that should be edited in the Studio directly into the JSX.-This applies to static style values, keyframes, easing and transform props.--Write animations as inline `interpolate()` calls on the property that changes.-Do not calculate the value in a local variable and pass the variable into `style`.-Do not animate with frame math such as `frame * 2`, modulo expressions, helper functions or transform strings.-Keep the input range and output range directly in the `interpolate()` call.-The input range must use hardcoded numbers, not variables or expressions such as `[sendStartFrame, sendStartFrame + duration]`.+## Keep all CSS styles inline-Do not use object spreads for interactive props.-Write the values inline so the Studio can detect and edit them.+The best way is to just pass a plain object to `style` - no referring to constants, no object spreading, no math.-```tsx title="Inline values"-// π Inline values can be standardized and keyframed+```tsx title="Interactive example"<Interactive.Div- name="Product card"style={{- color: 'white',fontSize: 80,- scale: interpolate(frame, [0, 30], [0, 1], {- easing: Easing.spring({damping: 200}),- }),- rotate: interpolate(frame, [0, 30], ['0deg', '20deg']),- translate: interpolate(frame, [0, 30], ['0px 0px', '0px 120px']),+ color: 'red',}}-/>+>+ Hello World!+</Interactive.Div>+```-// π Values hidden behind variables, helpers or strings become computed-const container: React.CSSProperties = {- color: 'white',- fontSize: 80,-};-const translateY = interpolate(frame, [0, 30], [0, 120]);-const rotation = interpolate(frame, [0, 30], [0, 20]);+```tsx title="β Bad for interactivity"+const baseStyle = useMemo(() => {+ return {+ fontSize: 12 // β Non-inline styles are not supported+ }+}, [])<Interactive.Div- name="Product card"style={{- ...container,- transform: `translateY(${translateY}px) rotate(${rotation}deg)`,+ ...baseStyle, // β Spreading is not supported+ color: RED, // β Referring to constants is not supported+ scale: frame * 10 // β Math is not supported}}-/>+>+ Hello World!+</Interactive.Div>```-Use standalone [`spring()`](/docs/spring) only when the animation needs a frame-driven physical spring simulation.-Use individual CSS transform properties such as `scale`, `rotate` and `translate` instead of composing a `transform` string.-Pass easing to the same `interpolate()` call instead of nesting another `interpolate()` call to create an eased progress value.+## Animate using `interpolate()`++<SuggestedPrompt prompt="/remotion-interactivity Inline the values to make them editable." />++Write animations as inline `interpolate()` calls on the property that changes.+All values should also be hardcoded values: Input range, output range, easing, extrapolation, `output` property.++No math should be performed, except basic arithmetic 2-side arithmetic with `durationInFrames`, `fps`, `width` and `height` from `useVideoConfig()`.-```tsx title="Animated position"-// π Motion, opacity and scale are visible to the Studio+```tsx title="Inline values"+const {fps} = useVideoConfig();++// π Inline values can be standardized and keyframed<Interactive.Div- name="Prompt bubble"+ name="Product card"style={{- position: 'absolute',- right: 56,- top: 96,- opacity: interpolate(frame, [30, 50], [0, 1], {+ color: 'white',+ fontSize: 80,+ scale: interpolate(frame, [0, fps], [0, 1], {+ easing: Easing.spring({damping: 200}),+ output: 'perceptual-scale',extrapolateLeft: 'clamp',- extrapolateRight: 'clamp',+ extrapolateRight: 'clamp'}),- translate: interpolate(frame, [30, 50], ['0px 32px', '0px 0px'], {- easing: Easing.out(Easing.cubic),+ rotate: interpolate(frame, [0, 1 * fps], ['0deg', '20deg'], {+ easing: Easing.spring({damping: 200}),extrapolateLeft: 'clamp',- extrapolateRight: 'clamp',+ extrapolateRight: 'clamp'}),- scale: interpolate(frame, [30, 50], [0.92, 1], {+ translate: interpolate(frame, [durationInFrames - 30, durationInFrames], ['0px 0px', '0px 120px'], {+ easing: Easing.spring({damping: 200}),+ output: 'perceptual-scale',extrapolateLeft: 'clamp',- extrapolateRight: 'clamp',+ extrapolateRight: 'clamp'}),- transformOrigin: '100% 100%',}}/>+```-// π The Studio sees computed values instead of editable props-const sendStartFrame = 30;-const promptTravelDurationInFrames = 20;-const promptBubbleTop = interpolate(frame, [30, 50], [128, 96]);-const promptBubbleOpacity = interpolate(frame, [30, 50], [0, 1]);-const easedProgress = Easing.out(Easing.cubic)(- interpolate(- frame,- [sendStartFrame, sendStartFrame + promptTravelDurationInFrames],- [0, 1],- ),-);+```tsx title="β Bad interactivity"+const translateY = interpolate(frame, [0, 30], [0, 120]); // β Math should be directly in the markup<Interactive.Div- name="Prompt bubble"+ name="Product card"style={{- position: 'absolute',- right: 56,- top: promptBubbleTop,- opacity: promptBubbleOpacity,- translate: interpolate(easedProgress, [0, 1], ['0px 32px', '0px 0px']),- transformOrigin: '100% 100%',+ translate: translateY, // β Only inline interpolate() calls are supported,+ rotate: interpolate(frame, [start, start + 10], [0, Math.PI]), // β Cannot use math with arbitrary variables, cannot use constants+ scale: interpolate(anyVariable, [0, 30], [0, 1]) // β Can only interpret the `frame` variable.}}/>```-Use `top`, `left`, `right` and `bottom` for static layout anchors.-When the value changes over time or should be draggable/keyframable in the Studio, put the moving part into `translate`.+## Use `scale`, `translate`, `rotate` CSS properties++Avoid the `transform` CSS property.+If possible, use `scale`, `rotate` and `translate` instead because only they are interactively editable.```txt title="Prompt"Make this interactive in the Remotion Studio: https://www.remotion.dev/docs/studio/interactivity-best-practices.md```-## Keep composition metadata inline {#keep-composition-metadata-inline}+## Keep composition metadata inline-When scaffolding a composition, keep the component and its [`<Composition>`](/docs/composition) registration in the same file.-Keep static `width`, `height`, `fps`, `durationInFrames` and [`defaultProps`](/docs/composition#defaultprops) inline.+When scaffolding a composition, keep `width`, `height`, `fps`, `durationInFrames` and [`defaultProps`](/docs/composition#defaultprops) inline and make no type assertions.The Props editor can save visual edits back to your code when `defaultProps` is an inline object literal on `<Composition>` or [`<Still>`](/docs/still).-If the Studio shows `` `defaultProps` prop must be a hardcoded value in the <Composition/> tag ``, move the values into the JSX.-```tsx title="Composition metadata"-// π Static metadata and defaults are inline and nearby+```tsx+// π Static values are in <Composition>, dynamic values are in calculateMetadata()+const calculateMetadata = useMemo(async () => {+ const dimensions = await getDimensions(); // just an example+ return {width: dimensions.width, height: dimensions.height};+});+<Compositionid="my-video"component={MyComponent}durationInFrames={150}fps={30}- width={1920}- height={1080}+ calculateMetadata={calculateMetadata}defaultProps={{title: 'Hello', color: '#0b84ff'}}/>+```-// π Static metadata is hidden behind helpers or variables-const defaultProps = {title: 'Hello', color: '#0b84ff'};-const calculateMetadata = () => {+```tsx title="Negative examples"+const defaultProps = {title: 'Hello', color: '#0b84ff'}; // β Don't extract defaultProps, must be inline+const calculateMetadata = useMemo(() => {+ // β Unnecessary because no calculation is being done,return {durationInFrames: 150, fps: 30, width: 1920, height: 1080};-};+});<Compositionid="my-video"@@ -200,20 +175,20 @@ const calculateMetadata = () => {calculateMetadata={calculateMetadata}defaultProps={{title: 'Hello',- }}+ } as Props} // β Don't have type assertions, instead type MyComponent correctly/>```-Use [`calculateMetadata()`](/docs/calculate-metadata) when metadata depends on dynamic input props, fetched data or asset metadata.+Use only [`calculateMetadata()`](/docs/calculate-metadata) for the part of the metadata that is dynamic.```txt title="Prompt"Make this interactive in the Remotion Studio: https://www.remotion.dev/docs/studio/interactivity-best-practices.md```-## Keep effects editable {#keep-effects-editable}+## Effects should be inline too-Keep editable effect parameters inline in the effect call and keep the effect array shape unconditional.-If an effect parameter is animated, write it as an inline `interpolate()` call in the effect call.+The effects array should not be computed.+The same rules for setting keyframes as `interpolate()` apply too here: All values should also be hardcoded: Input range, output range, easing, extrapolation, `output` property.```tsx title="Effects"// π Parameters are inline and the array shape is stable@@ -227,20 +202,23 @@ If an effect parameter is animated, write it as an inline `interpolate()` call iwidth: 1.2,height: 0.8,start: 0.2,+ disabled: true,rotation: interpolate(frame, [0, 120], [0, 180]),}),]}/>-// π Values are hidden and the effect only exists conditionallyconst center = [0.5, 0.5] as const;const rotation = frame * 1.5;+<CanvasImagesrc={src}width={1280}height={720}+ // β Conditional effect is not animateableeffects={enabled ? [radialProgressiveBlur({+ // β Not inlinecenter,rotation,}),@@ -254,6 +232,12 @@ Render separate elements if one version should have effects and another should nMake this interactive in the Remotion Studio: https://www.remotion.dev/docs/studio/interactivity-best-practices.md```+## Make props of a custom component editable++If you have a custom non-top-level component and want to make the props of that editable, see: [Make a component interactive](/docs/studio/make-component-interactive).+++## See also- [Studio interactivity](/docs/studio/interactivity)