Skip to content

🎥 Make videos programmatically with React

Elements: Create Studio payloads on interaction

Igor Samokhovets66f5122c7448
diff --git a/packages/docs/src/components/Elements/ElementInstallFallbackModal.tsx b/packages/docs/src/components/Elements/ElementInstallFallbackModal.tsx
index a6f58cd82a..36e2fa207b 100644
--- a/packages/docs/src/components/Elements/ElementInstallFallbackModal.tsx
+++ b/packages/docs/src/components/Elements/ElementInstallFallbackModal.tsx
@@ -1,25 +1,25 @@
-import type {StudioElementPayload} from '@remotion/studio-protocol';
import React, {type RefObject, useEffect, useId, useRef, useState} from 'react';
import {createPortal} from 'react-dom';
+import type {ElementDefinition} from './element-definitions';
import {ElementStudioAction} from './ElementStudioAction';
import styles from './ElementInstallFallbackModal.module.css';
export const ElementInstallFallbackModal: React.FC<{
+ readonly definition: ElementDefinition;
readonly installFailureCount: number;
readonly isInstalling: boolean;
readonly isOpen: boolean;
readonly onClose: () => void;
readonly onInstall: () => void;
- readonly payload: StudioElementPayload;
readonly posterRef: RefObject<HTMLImageElement | null>;
readonly sourceCode: string;
}> = ({
+ definition,
installFailureCount,
isInstalling,
isOpen,
onClose,
onInstall,
- payload,
posterRef,
sourceCode,
}) => {
@@ -102,11 +102,12 @@ export const ElementInstallFallbackModal: React.FC<{
<div className={styles.installAction}>
<ElementStudioAction
buttonLabel={installFailureCount > 1 ? 'Oops!' : 'Install'}
+ definition={definition}
loading={isInstalling}
onClick={onInstall}
- payload={payload}
posterRef={posterRef}
showDragHandle
+ sourceCode={sourceCode}
title="Install in the most recently focused Remotion Studio"
/>
{installFailureCount > 1 ? (
diff --git a/packages/docs/src/components/Elements/ElementLibrary.tsx b/packages/docs/src/components/Elements/ElementLibrary.tsx
index 42362cf70e..d90d0e57ed 100644
--- a/packages/docs/src/components/Elements/ElementLibrary.tsx
+++ b/packages/docs/src/components/Elements/ElementLibrary.tsx
@@ -1,9 +1,10 @@
-import {installInStudio, setStudioDragData} from '@remotion/studio-protocol';
-import React, {useEffect, useMemo, useRef, useState} from 'react';
+import {setStudioDragData} from '@remotion/studio-protocol';
+import React, {useEffect, useRef, useState} from 'react';
import {BlueButton} from '../../../components/layout/Button';
import type {ElementDefinition} from './element-definitions';
import {
createElementPayloadFromDefinition,
+ installElementInStudio,
setElementDragImage,
} from './element-drag-data';
import {
@@ -52,26 +53,6 @@ const ElementCard: React.FC<{
const videoRef = useRef<HTMLVideoElement>(null);
const shouldPlay =
!prefersReducedMotion && !playbackFailed && (isFocused || isPointerOver);
- const elementPayload = useMemo(
- () =>
- createElementPayloadFromDefinition({
- definition,
- sourceCode,
- installAssets: false,
- }),
- [definition, sourceCode],
- );
- const assetPayload = useMemo(
- () =>
- definition.assets.length === 0
- ? null
- : createElementPayloadFromDefinition({
- definition,
- sourceCode,
- installAssets: true,
- }),
- [definition, sourceCode],
- );
useEffect(() => {
const video = videoRef.current;
@@ -106,10 +87,7 @@ const ElementCard: React.FC<{
const installElement = async () => {
setIsInstalling(true);
try {
- const result = await installInStudio({
- payload: assetPayload ?? elementPayload,
- fallbackPayload: assetPayload === null ? undefined : elementPayload,
- });
+ const result = await installElementInStudio({definition, sourceCode});
if (!result.success) {
setInstallFailureCount((count) => count + 1);
setIsInstallFallbackOpen(true);
@@ -149,7 +127,11 @@ const ElementCard: React.FC<{
onDragStart={(event) => {
setStudioDragData({
dataTransfer: event.dataTransfer,
- payload: elementPayload,
+ payload: createElementPayloadFromDefinition({
+ definition,
+ sourceCode,
+ installAssets: false,
+ }),
});
setElementDragImage(event.dataTransfer, posterRef.current);
}}
@@ -200,6 +182,7 @@ const ElementCard: React.FC<{
</BlueButton>
</div>
<ElementInstallFallbackModal
+ definition={definition}
installFailureCount={installFailureCount}
isInstalling={isInstalling}
isOpen={isInstallFallbackOpen}
@@ -208,7 +191,6 @@ const ElementCard: React.FC<{
setInstallFailureCount(0);
}}
onInstall={installElement}
- payload={elementPayload}
posterRef={posterRef}
sourceCode={sourceCode}
/>
diff --git a/packages/docs/src/components/Elements/ElementPage.tsx b/packages/docs/src/components/Elements/ElementPage.tsx
index 74d8ea58b4..898372d4b4 100644
--- a/packages/docs/src/components/Elements/ElementPage.tsx
+++ b/packages/docs/src/components/Elements/ElementPage.tsx
@@ -1,6 +1,5 @@
import Head from '@docusaurus/Head';
import {
- installInStudio,
isInsideStudio,
StudioProtocolInternals,
} from '@remotion/studio-protocol';
@@ -17,7 +16,10 @@ import React, {
import {PlainButton} from '../../../components/layout/Button';
import {Seo} from '../Seo';
import type {ElementDefinition} from './element-definitions';
-import {createElementPayloadFromDefinition} from './element-drag-data';
+import {
+ createElementPayloadFromDefinition,
+ installElementInStudio,
+} from './element-drag-data';
import {getElementDimensionsLabel} from './element-utils';
import {ElementInstallFallbackModal} from './ElementInstallFallbackModal';
import {ElementPreview} from './ElementPreview';
@@ -59,30 +61,6 @@ export const ElementPage: React.FC<ElementPageProps> = ({
const {height: previewHeight, width: previewWidth} =
getElementPreviewDimensions(definition);
- const elementPayload = useMemo(() => {
- if (!sourceCode) {
- return null;
- }
-
- return createElementPayloadFromDefinition({
- definition,
- sourceCode,
- installAssets: false,
- });
- }, [definition, sourceCode]);
-
- const assetPayload = useMemo(() => {
- if (!sourceCode || definition.assets.length === 0) {
- return null;
- }
-
- return createElementPayloadFromDefinition({
- definition,
- sourceCode,
- installAssets: true,
- });
- }, [definition, sourceCode]);
-
useLayoutEffect(() => {
setIsEmbeddedInStudio(isInsideStudio());
}, []);
@@ -131,7 +109,7 @@ export const ElementPage: React.FC<ElementPageProps> = ({
}, []);
const installElement = useCallback(async () => {
- if (elementPayload === null) {
+ if (!sourceCode) {
return;
}
@@ -139,10 +117,7 @@ export const ElementPage: React.FC<ElementPageProps> = ({
setInstallStatus({type: 'installing'});
try {
- const result = await installInStudio({
- payload: assetPayload ?? elementPayload,
- fallbackPayload: assetPayload === null ? undefined : elementPayload,
- });
+ const result = await installElementInStudio({definition, sourceCode});
if (!result.success) {
setInstallStatus({type: 'idle'});
setInstallFailureCount((count) => count + 1);
@@ -168,18 +143,22 @@ export const ElementPage: React.FC<ElementPageProps> = ({
setInstallFailureCount((count) => count + 1);
setIsInstallFallbackOpen(true);
}
- }, [assetPayload, definition.slug, elementPayload, isEmbeddedInStudio]);
+ }, [definition, isEmbeddedInStudio, sourceCode]);
const openInBrowserStudio = useCallback(() => {
- if (elementPayload === null) {
+ if (!sourceCode) {
return;
}
StudioProtocolInternals.openInBrowserStudio({
endpoint: null,
- payload: elementPayload,
+ payload: createElementPayloadFromDefinition({
+ definition,
+ sourceCode,
+ installAssets: false,
+ }),
});
- }, [elementPayload]);
+ }, [definition, sourceCode]);
const PreviewComponent = useMemo(() => {
return () => <ElementPreviewComposition definition={definition} />;
@@ -254,18 +233,19 @@ export const ElementPage: React.FC<ElementPageProps> = ({
className={styles.actionsColumn}
>
<div>
- {elementPayload === null ? null : (
+ {sourceCode ? (
<>
<div className={styles.actionRow}>
<ElementStudioAction
buttonLabel={
installStatus.type === 'success' ? 'Sent to Studio' : 'Use'
}
+ definition={definition}
loading={installStatus.type === 'installing'}
onClick={installElement}
- payload={elementPayload}
posterRef={posterRef}
showDragHandle={isEmbeddedInStudio === false}
+ sourceCode={sourceCode}
title={
isEmbeddedInStudio
? 'Use this Element in Studio'
@@ -291,7 +271,7 @@ export const ElementPage: React.FC<ElementPageProps> = ({
</p>
) : null}
</>
- )}
+ ) : null}
<div className={styles.details}>
<p className={styles.description}>{description}</p>
@@ -373,8 +353,9 @@ export const ElementPage: React.FC<ElementPageProps> = ({
) : null}
</div>
</aside>
- {elementPayload === null || sourceCode === undefined ? null : (
+ {sourceCode ? (
<ElementInstallFallbackModal
+ definition={definition}
installFailureCount={installFailureCount}
isInstalling={installStatus.type === 'installing'}
isOpen={isInstallFallbackOpen}
@@ -383,11 +364,10 @@ export const ElementPage: React.FC<ElementPageProps> = ({
setInstallFailureCount(0);
}}
onInstall={installElement}
- payload={elementPayload}
posterRef={posterRef}
sourceCode={sourceCode}
/>
- )}
+ ) : null}
</div>
);
};
diff --git a/packages/docs/src/components/Elements/ElementStudioAction.tsx b/packages/docs/src/components/Elements/ElementStudioAction.tsx
index f290b6ae6f..ec0c89c9f8 100644
--- a/packages/docs/src/components/Elements/ElementStudioAction.tsx
+++ b/packages/docs/src/components/Elements/ElementStudioAction.tsx
@@ -1,33 +1,40 @@
-import {
- setStudioDragData,
- type StudioElementPayload,
-} from '@remotion/studio-protocol';
+import {setStudioDragData} from '@remotion/studio-protocol';
import React, {type RefObject} from 'react';
import {BlueButton} from '../../../components/layout/Button';
-import {setElementDragImage} from './element-drag-data';
+import type {ElementDefinition} from './element-definitions';
+import {
+ createElementPayloadFromDefinition,
+ setElementDragImage,
+} from './element-drag-data';
import styles from './ElementStudioAction.module.css';
export const ElementStudioAction: React.FC<{
readonly buttonLabel: string;
+ readonly definition: ElementDefinition;
readonly loading: boolean;
readonly onClick: () => void;
- readonly payload: StudioElementPayload;
readonly posterRef: RefObject<HTMLImageElement | null>;
readonly showDragHandle: boolean;
+ readonly sourceCode: string;
readonly title: string;
}> = ({
buttonLabel,
+ definition,
loading,
onClick,
- payload,
posterRef,
showDragHandle,
+ sourceCode,
title,
}) => {
const onDragStart = (event: React.DragEvent<HTMLElement>) => {
setStudioDragData({
dataTransfer: event.dataTransfer,
- payload,
+ payload: createElementPayloadFromDefinition({
+ definition,
+ sourceCode,
+ installAssets: false,
+ }),
});
setElementDragImage(event.dataTransfer, posterRef.current);
};
diff --git a/packages/docs/src/components/Elements/element-drag-data.ts b/packages/docs/src/components/Elements/element-drag-data.ts
index f448104499..323b9f9843 100644
--- a/packages/docs/src/components/Elements/element-drag-data.ts
+++ b/packages/docs/src/components/Elements/element-drag-data.ts
@@ -1,5 +1,6 @@
import {
createElementPayload,
+ installInStudio,
type StudioElementPayload,
} from '@remotion/studio-protocol';
import type {ElementDefinition} from './element-definitions';
@@ -37,6 +38,32 @@ export const createElementPayloadFromDefinition = ({
});
};
+export const installElementInStudio = ({
+ definition,
+ sourceCode,
+}: {
+ readonly definition: ElementDefinition;
+ readonly sourceCode: string;
+}) => {
+ const elementPayload = createElementPayloadFromDefinition({
+ definition,
+ sourceCode,
+ installAssets: false,
+ });
+ if (definition.assets.length === 0) {
+ return installInStudio({payload: elementPayload});
+ }
+
+ return installInStudio({
+ payload: createElementPayloadFromDefinition({
+ definition,
+ sourceCode,
+ installAssets: true,
+ }),
+ fallbackPayload: elementPayload,
+ });
+};
+
export const setElementDragImage = (
dataTransfer: DataTransfer,
poster: HTMLImageElement | null,