Skip to content

🎥 Make videos programmatically with React

`remotion`: Add Interactive components (#8269)

Jonny Burger21a39d61ffb6
diff --git a/packages/core/src/Interactive.tsx b/packages/core/src/Interactive.tsx
new file mode 100644
index 0000000000..35e8ecd6f3
--- /dev/null
+++ b/packages/core/src/Interactive.tsx
@@ -0,0 +1,212 @@
+import React, {forwardRef, useCallback, useRef} from 'react';
+import type {SequenceControls} from './CompositionManager.js';
+import {addSequenceStackTraces} from './enable-sequence-stack-traces.js';
+import {
+ durationInFramesField,
+ fromField,
+ hiddenField,
+ sequenceVisualStyleSchema,
+ type SequenceSchema,
+} from './sequence-field-schema.js';
+import type {SequenceProps} from './Sequence.js';
+import {Sequence} from './Sequence.js';
+import {wrapInSchema} from './wrap-in-schema.js';
+
+type InteractiveHtmlTag =
+ | 'a'
+ | 'article'
+ | 'aside'
+ | 'button'
+ | 'code'
+ | 'div'
+ | 'em'
+ | 'footer'
+ | 'h1'
+ | 'h2'
+ | 'h3'
+ | 'h4'
+ | 'h5'
+ | 'h6'
+ | 'header'
+ | 'label'
+ | 'li'
+ | 'main'
+ | 'nav'
+ | 'ol'
+ | 'p'
+ | 'pre'
+ | 'section'
+ | 'small'
+ | 'span'
+ | 'strong'
+ | 'ul';
+
+type InteractiveSvgTag =
+ | 'circle'
+ | 'ellipse'
+ | 'g'
+ | 'line'
+ | 'path'
+ | 'rect'
+ | 'svg'
+ | 'text';
+
+type InteractiveTag = InteractiveHtmlTag | InteractiveSvgTag;
+
+type ElementForTag<Tag extends InteractiveTag> =
+ Tag extends keyof HTMLElementTagNameMap
+ ? HTMLElementTagNameMap[Tag]
+ : Tag extends keyof SVGElementTagNameMap
+ ? SVGElementTagNameMap[Tag]
+ : Element;
+
+type InteractiveSequenceProps = Pick<
+ SequenceProps,
+ 'durationInFrames' | 'from' | 'hidden' | 'name' | 'showInTimeline'
+> & {
+ /**
+ * @deprecated For internal use only
+ */
+ readonly stack?: string;
+};
+
+type InteractiveElementProps<Tag extends InteractiveTag> = Omit<
+ React.ComponentPropsWithoutRef<Tag>,
+ keyof InteractiveSequenceProps
+> &
+ InteractiveSequenceProps;
+
+type InteractiveElementComponent<Tag extends InteractiveTag> =
+ React.ComponentType<
+ InteractiveElementProps<Tag> & React.RefAttributes<ElementForTag<Tag>>
+ >;
+
+const interactiveElementSchema = {
+ durationInFrames: durationInFramesField,
+ from: fromField,
+ ...sequenceVisualStyleSchema,
+ hidden: hiddenField,
+} as const satisfies SequenceSchema;
+
+const setRef = <ElementType,>(
+ ref: React.ForwardedRef<ElementType>,
+ value: ElementType | null,
+) => {
+ if (typeof ref === 'function') {
+ ref(value);
+ } else if (ref) {
+ ref.current = value;
+ }
+};
+
+const makeInteractiveElement = <Tag extends InteractiveTag>(
+ tag: Tag,
+ displayName: string,
+): InteractiveElementComponent<Tag> => {
+ type ElementType = ElementForTag<Tag>;
+ type Props = InteractiveElementProps<Tag>;
+
+ const Inner = forwardRef<
+ ElementType,
+ Props & {
+ readonly _experimentalControls: SequenceControls | undefined;
+ }
+ >((propsWithControls, ref) => {
+ const {
+ durationInFrames,
+ from,
+ hidden,
+ name,
+ showInTimeline,
+ stack,
+ _experimentalControls,
+ ...props
+ } = propsWithControls as Props & {
+ readonly _experimentalControls: SequenceControls | undefined;
+ };
+ const refForOutline = useRef<ElementType | null>(null);
+ const callbackRef = useCallback(
+ (element: ElementType | null) => {
+ refForOutline.current = element;
+ setRef(ref, element);
+ },
+ [ref],
+ );
+
+ return (
+ <Sequence
+ layout="none"
+ from={from ?? 0}
+ durationInFrames={durationInFrames ?? Infinity}
+ hidden={hidden}
+ name={name ?? displayName}
+ showInTimeline={showInTimeline ?? true}
+ _experimentalControls={_experimentalControls}
+ _remotionInternalStack={stack}
+ _remotionInternalRefForOutline={refForOutline}
+ >
+ {React.createElement(tag, {
+ ...props,
+ ref: callbackRef,
+ })}
+ </Sequence>
+ );
+ });
+
+ Inner.displayName = displayName;
+
+ const Wrapped = wrapInSchema({
+ Component: Inner,
+ schema: interactiveElementSchema,
+ supportsEffects: false,
+ }) as InteractiveElementComponent<Tag>;
+
+ Wrapped.displayName = displayName;
+ addSequenceStackTraces(Wrapped);
+
+ return Wrapped;
+};
+
+/**
+ * @description HTML and SVG elements that are registered in the Remotion Studio timeline and can be visually edited.
+ */
+export const Interactive = {
+ A: makeInteractiveElement('a', '<Interactive.A>'),
+ Article: makeInteractiveElement('article', '<Interactive.Article>'),
+ Aside: makeInteractiveElement('aside', '<Interactive.Aside>'),
+ Button: makeInteractiveElement('button', '<Interactive.Button>'),
+ Circle: makeInteractiveElement('circle', '<Interactive.Circle>'),
+ Code: makeInteractiveElement('code', '<Interactive.Code>'),
+ Div: makeInteractiveElement('div', '<Interactive.Div>'),
+ Ellipse: makeInteractiveElement('ellipse', '<Interactive.Ellipse>'),
+ Em: makeInteractiveElement('em', '<Interactive.Em>'),
+ Footer: makeInteractiveElement('footer', '<Interactive.Footer>'),
+ G: makeInteractiveElement('g', '<Interactive.G>'),
+ H1: makeInteractiveElement('h1', '<Interactive.H1>'),
+ H2: makeInteractiveElement('h2', '<Interactive.H2>'),
+ H3: makeInteractiveElement('h3', '<Interactive.H3>'),
+ H4: makeInteractiveElement('h4', '<Interactive.H4>'),
+ H5: makeInteractiveElement('h5', '<Interactive.H5>'),
+ H6: makeInteractiveElement('h6', '<Interactive.H6>'),
+ Header: makeInteractiveElement('header', '<Interactive.Header>'),
+ Label: makeInteractiveElement('label', '<Interactive.Label>'),
+ Li: makeInteractiveElement('li', '<Interactive.Li>'),
+ Line: makeInteractiveElement('line', '<Interactive.Line>'),
+ Main: makeInteractiveElement('main', '<Interactive.Main>'),
+ Nav: makeInteractiveElement('nav', '<Interactive.Nav>'),
+ Ol: makeInteractiveElement('ol', '<Interactive.Ol>'),
+ P: makeInteractiveElement('p', '<Interactive.P>'),
+ Path: makeInteractiveElement('path', '<Interactive.Path>'),
+ Pre: makeInteractiveElement('pre', '<Interactive.Pre>'),
+ Rect: makeInteractiveElement('rect', '<Interactive.Rect>'),
+ Section: makeInteractiveElement('section', '<Interactive.Section>'),
+ Small: makeInteractiveElement('small', '<Interactive.Small>'),
+ Span: makeInteractiveElement('span', '<Interactive.Span>'),
+ Strong: makeInteractiveElement('strong', '<Interactive.Strong>'),
+ Svg: makeInteractiveElement('svg', '<Interactive.Svg>'),
+ Text: makeInteractiveElement('text', '<Interactive.Text>'),
+ Ul: makeInteractiveElement('ul', '<Interactive.Ul>'),
+};
+
+export type InteractiveProps<Tag extends InteractiveTag> =
+ InteractiveElementProps<Tag>;
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 12ca8ac28e..a027a2d88a 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -190,6 +190,7 @@ export {getRemotionEnvironment} from './get-remotion-environment.js';
export {getStaticFiles, StaticFile} from './get-static-files.js';
export * from './IFrame.js';
export {Img, ImgProps} from './Img.js';
+export {Interactive, type InteractiveProps} from './Interactive.js';
export * from './internals.js';
export {
interpolateColors,
diff --git a/packages/core/src/test/sequence-register-once.test.tsx b/packages/core/src/test/sequence-register-once.test.tsx
index b3d0f66eaf..dd6a710c44 100644
--- a/packages/core/src/test/sequence-register-once.test.tsx
+++ b/packages/core/src/test/sequence-register-once.test.tsx
@@ -4,6 +4,7 @@ import React, {useCallback, useMemo, useState} from 'react';
import {AnimatedImage} from '../animated-image/index.js';
import type {TSequence} from '../CompositionManager.js';
import {Img} from '../Img.js';
+import {Interactive} from '../Interactive.js';
import {Internals} from '../internals.js';
import {Sequence} from '../Sequence.js';
import type {SequenceManagerContext} from '../SequenceManager.js';
@@ -238,6 +239,56 @@ test('Img registers a refForOutline pointing to the rendered image element', ()
expect(registeredSequences[0]?.refForOutline?.current?.tagName).toBe('IMG');
});
+test('Interactive elements register their rendered element for Studio outlines', () => {
+ const registeredSequences: TSequence[] = [];
+ const divRef = React.createRef<HTMLDivElement>();
+
+ render(
+ <SequenceTestWrapper
+ onRegisterSequence={(sequence) => {
+ registeredSequences.push(sequence);
+ }}
+ >
+ <Interactive.Div ref={divRef}>Hello</Interactive.Div>
+ <Interactive.Span>World</Interactive.Span>
+ <Interactive.Svg viewBox="0 0 100 100">
+ <Interactive.Rect width={100} height={100} />
+ </Interactive.Svg>
+ </SequenceTestWrapper>,
+ );
+
+ expect(
+ registeredSequences.map((sequence) => sequence.displayName).sort(),
+ ).toEqual([
+ '<Interactive.Div>',
+ '<Interactive.Rect>',
+ '<Interactive.Span>',
+ '<Interactive.Svg>',
+ ]);
+
+ const getByName = (displayName: string) =>
+ registeredSequences.find(
+ (sequence) => sequence.displayName === displayName,
+ );
+
+ expect(getByName('<Interactive.Div>')?.refForOutline?.current?.tagName).toBe(
+ 'DIV',
+ );
+ expect(getByName('<Interactive.Span>')?.refForOutline?.current?.tagName).toBe(
+ 'SPAN',
+ );
+ expect(getByName('<Interactive.Svg>')?.refForOutline?.current?.tagName).toBe(
+ 'svg',
+ );
+ expect(getByName('<Interactive.Rect>')?.refForOutline?.current?.tagName).toBe(
+ 'rect',
+ );
+ expect(getByName('<Interactive.Div>')?.refForOutline?.current).toBe(
+ divRef.current,
+ );
+ expect(getByName('<Interactive.Div>')?.controls).not.toBe(null);
+});
+
test('Imperative sequence refs update without rerendering ref-only consumers', async () => {
const nodePath = {
absolutePath: 'test.tsx',
diff --git a/packages/docs/docs/interactive.mdx b/packages/docs/docs/interactive.mdx
new file mode 100644
index 0000000000..7d3922bf96
--- /dev/null
+++ b/packages/docs/docs/interactive.mdx
@@ -0,0 +1,101 @@
+---
+image: /generated/articles-docs-interactive.png
+id: interactive
+title: Interactive
+crumb: 'API'
+---
+
+# `Interactive`<AvailableFrom v="4.0.475" />
+
+`Interactive` exposes HTML and SVG elements that can be visually edited in the Remotion Studio.
+
+Use it when a regular element should be selectable and draggable in the preview:
+
+```tsx twoslash title="MyComp.tsx"
+import {AbsoluteFill, Interactive} from 'remotion';
+
+export const MyComp: React.FC = () => {
+ return (
+ <AbsoluteFill>
+ <Interactive.Div style={{backgroundColor: 'white', padding: 24}}>
+ Hello World
+ </Interactive.Div>
+ </AbsoluteFill>
+ );
+};
+```
+
+## Components
+
+The component name is the PascalCase version of the underlying element:
+
+```tsx twoslash title="MyComp.tsx"
+import {Interactive} from 'remotion';
+
+export const MyComp: React.FC = () => {
+ return (
+ <Interactive.Svg viewBox="0 0 100 100" width={100} height={100}>
+ <Interactive.Rect width={100} height={100} fill="blue" />
+ <Interactive.Text x={50} y={50} textAnchor="middle" fill="white">
+ Hi
+ </Interactive.Text>
+ </Interactive.Svg>
+ );
+};
+```
+
+Available HTML elements:
+
+`A`, `Article`, `Aside`, `Button`, `Code`, `Div`, `Em`, `Footer`, `H1`, `H2`, `H3`, `H4`, `H5`, `H6`, `Header`, `Label`, `Li`, `Main`, `Nav`, `Ol`, `P`, `Pre`, `Section`, `Small`, `Span`, `Strong`, `Ul`.
+
+Available SVG elements:
+
+`Circle`, `Ellipse`, `G`, `Line`, `Path`, `Rect`, `Svg`, `Text`.
+
+## Props
+
+### Inherited props
+
+Every `Interactive` component inherits [`from`](/docs/sequence#from), [`durationInFrames`](/docs/sequence#durationinframes), [`name`](/docs/sequence#name), [`showInTimeline`](/docs/sequence#showintimeline) and [`hidden`](/docs/sequence#hidden) from [`<Sequence>`](/docs/sequence).
+
+```tsx twoslash title="Timed element"
+import {Interactive} from 'remotion';
+
+export const MyComp: React.FC = () => {
+ return (
+ <Interactive.Div from={30} durationInFrames={90}>
+ Visible from frame 30 to 119
+ </Interactive.Div>
+ );
+};
+```
+
+### `ref?`
+
+You can add a [React ref](https://react.dev/learn/manipulating-the-dom-with-refs) to the rendered element.
+
+```tsx twoslash title="Using a ref"
+import {useRef} from 'react';
+import {Interactive} from 'remotion';
+
+export const MyComp: React.FC = () => {
+ const ref = useRef<HTMLDivElement>(null);
+
+ return <Interactive.Div ref={ref}>Hello World</Interactive.Div>;
+};
+```
+
+### Other props
+
+All props of the corresponding HTML or SVG element are forwarded.
+
+For example, `<Interactive.Div>` accepts `<div>` props, and `<Interactive.Svg>` accepts `<svg>` props.
+
+## Compatibility
+
+<CompatibilityTable chrome firefox safari nodejs="" bun="" serverlessFunctions="" clientSideRendering serverSideRendering player studio hideServers />
+
+## See also
+
+- [Source code for this API](https://github.com/remotion-dev/remotion/blob/main/packages/core/src/Interactive.tsx)
+- [`<Sequence>`](/docs/sequence)
diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.ts
index 570d0e23c2..b60d816175 100644
--- a/packages/docs/sidebars.ts
+++ b/packages/docs/sidebars.ts
@@ -159,6 +159,7 @@ const sidebars: SidebarsConfig = {
'remotion/html-in-canvas',
'iframe',
'img',
+ 'interactive',
'interpolate-colors',
'interpolate',
'loop',
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index 1984e886fa..62f2af25e4 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -2546,6 +2546,15 @@ export const articles = [
noAi: false,
slug: 'install-whisper-cpp/transcribe',
},
+ {
+ id: 'interactive',
+ title: 'Interactive',
+ relativePath: 'docs/interactive.mdx',
+ compId: 'articles-docs-interactive',
+ crumb: 'API',
+ noAi: false,
+ slug: 'interactive',
+ },
{
id: 'interpolate-colors',
title: 'interpolateColors()',
diff --git a/packages/docs/static/generated/articles-docs-interactive.png b/packages/docs/static/generated/articles-docs-interactive.png
new file mode 100644
index 0000000000..ef4a11344a
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-interactive.png differ
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index ecef4af697..10dbd0fcaa 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -239,6 +239,10 @@ import {RemotionMediaVideoTexture} from './VideoTexture';
import {VisualControls} from './VisualControls';
import {FastUpdates} from './VisualModeTests/FastUpdates';
import {FastUpdatesNested} from './VisualModeTests/FastUpdatesNested';
+import {
+ InteractiveHtmlElements,
+ InteractiveSvgElements,
+} from './VisualModeTests/InteractiveComponents';
import {VoiceVisualization} from './voice-visualization';
import {WhisperWeb} from './WhisperWeb';
@@ -2496,6 +2500,22 @@ export const Index: React.FC = () => {
fps={30}
durationInFrames={60}
/>
+ <Composition
+ id="interactive-html-elements"
+ component={InteractiveHtmlElements}
+ width={1080}
+ height={1080}
+ fps={30}
+ durationInFrames={90}
+ />
+ <Composition
+ id="interactive-svg-elements"
+ component={InteractiveSvgElements}
+ width={1080}
+ height={1080}
+ fps={30}
+ durationInFrames={90}
+ />
</Folder>
<ChangingTrimBeforeValue />
<Composition
diff --git a/packages/example/src/VisualModeTests/InteractiveComponents.tsx b/packages/example/src/VisualModeTests/InteractiveComponents.tsx
new file mode 100644
index 0000000000..007c0183d5
--- /dev/null
+++ b/packages/example/src/VisualModeTests/InteractiveComponents.tsx
@@ -0,0 +1,166 @@
+import React from 'react';
+import {AbsoluteFill, Interactive, useCurrentFrame} from 'remotion';
+
+const labelStyle: React.CSSProperties = {
+ fontFamily: 'sans-serif',
+ fontSize: 28,
+ color: '#e2e8f0',
+ margin: 0,
+};
+
+export const InteractiveHtmlElements: React.FC = () => {
+ const frame = useCurrentFrame();
+
+ return (
+ <AbsoluteFill style={{backgroundColor: '#0f172a'}}>
+ <Interactive.H1
+ style={{
+ position: 'absolute',
+ top: 70,
+ left: 90,
+ margin: 0,
+ fontFamily: 'sans-serif',
+ fontSize: 82,
+ color: '#f8fafc',
+ letterSpacing: -4,
+ }}
+ >
+ Interactive HTML
+ </Interactive.H1>
+ <Interactive.Div
+ style={{
+ position: 'absolute',
+ top: 220,
+ left: 110,
+ width: 420,
+ padding: 36,
+ borderRadius: 28,
+ background: 'linear-gradient(135deg, #7c3aed, #2563eb)',
+ boxShadow: '0 30px 80px rgba(37, 99, 235, 0.35)',
+ }}
+ >
+ <Interactive.P
+ style={{
+ ...labelStyle,
+ fontSize: 36,
+ fontWeight: 800,
+ marginBottom: 18,
+ }}
+ >
+ Drag the card
+ </Interactive.P>
+ <Interactive.Span
+ style={{
+ ...labelStyle,
+ display: 'block',
+ lineHeight: 1.35,
+ }}
+ >
+ This is an <Interactive.Strong>Interactive.Div</Interactive.Strong>{' '}
+ containing interactive text elements.
+ </Interactive.Span>
+ </Interactive.Div>
+ <Interactive.Button
+ style={{
+ position: 'absolute',
+ left: 650,
+ top: 300,
+ border: 0,
+ borderRadius: 999,
+ padding: '28px 44px',
+ backgroundColor: '#f8fafc',
+ color: '#0f172a',
+ fontSize: 34,
+ fontWeight: 800,
+ boxShadow: '0 20px 60px rgba(248, 250, 252, 0.22)',
+ }}
+ >
+ Button
+ </Interactive.Button>
+ <Interactive.Section
+ style={{
+ position: 'absolute',
+ left: 570,
+ top: 520,
+ width: 390,
+ padding: 28,
+ borderRadius: 24,
+ border: '3px solid #38bdf8',
+ transform: `rotate(${Math.sin(frame / 20) * 4}deg)`,
+ }}
+ >
+ <Interactive.Code
+ style={{
+ fontSize: 26,
+ color: '#67e8f9',
+ fontFamily: 'monospace',
+ }}
+ >
+ &lt;Interactive.Section /&gt;
+ </Interactive.Code>
+ </Interactive.Section>
+ </AbsoluteFill>
+ );
+};
+
+export const InteractiveSvgElements: React.FC = () => {
+ const frame = useCurrentFrame();
+ const radius = 130 + Math.sin(frame / 18) * 12;
+
+ return (
+ <AbsoluteFill
+ style={{
+ background: 'linear-gradient(180deg, #020617, #111827)',
+ alignItems: 'center',
+ justifyContent: 'center',
+ }}
+ >
+ <Interactive.Svg
+ width={720}
+ height={720}
+ viewBox="0 0 720 720"
+ style={{
+ overflow: 'visible',
+ }}
+ >
+ <Interactive.Circle
+ cx={360}
+ cy={360}
+ r={radius}
+ fill="#38bdf8"
+ opacity={0.22}
+ stroke="#67e8f9"
+ strokeWidth={8}
+ />
+ <Interactive.Rect
+ x={175}
+ y={185}
+ width={370}
+ height={260}
+ rx={44}
+ fill="#7c3aed"
+ opacity={0.82}
+ />
+ <Interactive.Path
+ d="M190 500 C290 415 430 585 540 485"
+ fill="none"
+ stroke="#facc15"
+ strokeLinecap="round"
+ strokeWidth={28}
+ />
+ <Interactive.Text
+ x={360}
+ y={360}
+ textAnchor="middle"
+ dominantBaseline="middle"
+ fill="#fff"
+ fontFamily="sans-serif"
+ fontSize={52}
+ fontWeight={800}
+ >
+ SVG
+ </Interactive.Text>
+ </Interactive.Svg>
+ </AbsoluteFill>
+ );
+};