🎥 Make videos programmatically with React
Open Remotion Elements inside Studio (#10688)
Igor Samokhovets0c00a1fab5b9
diff --git a/packages/example/e2e/studio-protocol.test.mts b/packages/example/e2e/studio-protocol.test.mtsindex 4595f55c12..ed26e67dab 100644--- a/packages/example/e2e/studio-protocol.test.mts+++ b/packages/example/e2e/studio-protocol.test.mts@@ -200,31 +200,47 @@ export const MyComponent = () => {],});+ const decoyStudioPage = await context.newPage();+ await decoyStudioPage.goto(studioUrl);+ await expect(decoyStudioPage.getByText('MyComp').first()).toBeVisible();+ await decoyStudioPage.bringToFront();+ await decoyStudioPage.mouse.click(500, 300);++ await studioPage.bringToFront();await studioPage.locator('[data-sidebar-toggle="right"]').click();const browseElements = studioPage.getByRole('button', {name: 'Browse Elements',});await expect(browseElements).toBeVisible();- const senderPagePromise = context.waitForEvent('page', {timeout: 10_000});await browseElements.click();- const senderPage = await senderPagePromise;- await senderPage.waitForLoadState('domcontentloaded');+ const elementsIframe = studioPage.locator(+ 'iframe[title="Remotion Elements library"]',+ );+ await expect(elementsIframe).toBeVisible();+ await expect(elementsIframe).toHaveAttribute(+ 'allow',+ 'local-network-access; loopback-network',+ );+ await expect(elementsIframe).toHaveAttribute('credentialless', '');+ expect(context.pages()).toHaveLength(2);expect(elementsLibraryRequests).toEqual(['https://www.remotion.dev/elements',]);- await expect(senderPage).toHaveURL(senderUrl);- await senderPage.getByRole('button', {name: 'Install in Studio'}).click();- await senderPage.waitForFunction(- () => document.querySelector('#status')?.textContent !== '',+ const elementsFrame = studioPage.frameLocator(+ 'iframe[title="Remotion Elements library"]',);- const senderStatus = await senderPage.locator('#status').textContent();- expect(senderStatus).toContain('awaiting-confirmation');- expect(senderStatus).toContain('remotion-studio-protocol-');+ const installInStudio = elementsFrame.getByRole('button', {+ name: 'Install in Studio',+ });+ await expect(installInStudio).toBeVisible();+ await installInStudio.click();- await studioPage.bringToFront();const dialog = studioPage.getByRole('dialog');await expect(dialog.getByText('Install Element')).toBeVisible();await expect(dialog.getByText(/Protocol Element.*MyComp/)).toBeVisible();+ await expect(dialog.getByText(senderUrl, {exact: true})).toBeVisible();+ await expect(decoyStudioPage.getByText('Install Element')).toHaveCount(0);+ await expect(elementsIframe).toHaveCount(0);await dialog.getByRole('button', {name: /Install/}).click();const elementFile = path.join(@@ -245,7 +261,8 @@ export const MyComponent = () => {await studioPage.bringToFront();await studioPage.mouse.click(500, 300);- await senderPage.bringToFront();+ const senderPage = await context.newPage();+ await senderPage.goto(senderUrl);await senderPage.getByRole('button', {name: 'Configure license in Studio'}).click();diff --git a/packages/studio/src/components/ElementLibraryModal.tsx b/packages/studio/src/components/ElementLibraryModal.tsxnew file mode 100644index 0000000000..86e8bae890--- /dev/null+++ b/packages/studio/src/components/ElementLibraryModal.tsx@@ -0,0 +1,50 @@+import React, {useLayoutEffect, useRef} from 'react';+import {getMaxModalHeight, getMaxModalWidth} from './ModalContainer';+import {ModalHeader} from './ModalHeader';+import {DismissableModal} from './NewComposition/DismissableModal';++const panelStyle: React.CSSProperties = {+ display: 'flex',+ flexDirection: 'column',+ height: getMaxModalHeight(1000),+ overflow: 'hidden',+ width: getMaxModalWidth(1400),+};++const iframeStyle: React.CSSProperties = {+ border: 0,+ flex: 1,+ minHeight: 0,+ width: '100%',+};++export const ElementLibraryModal: React.FC<{+ readonly name: string;+ readonly url: string;+}> = ({name, url}) => {+ const iframeRef = useRef<HTMLIFrameElement>(null);++ useLayoutEffect(() => {+ const iframe = iframeRef.current;+ if (iframe === null) {+ return;+ }++ // Studio is cross-origin isolated. A credentialless iframe may embed a+ // library that does not set Cross-Origin-Resource-Policy headers.+ iframe.setAttribute('credentialless', '');+ iframe.src = url;+ }, [url]);++ return (+ <DismissableModal panelStyle={panelStyle}>+ <ModalHeader title={name} />+ <iframe+ ref={iframeRef}+ allow="local-network-access; loopback-network"+ style={iframeStyle}+ title={`${name} library`}+ />+ </DismissableModal>+ );+};diff --git a/packages/studio/src/components/InspectorPanel/CompositionInspector.tsx b/packages/studio/src/components/InspectorPanel/CompositionInspector.tsxindex c6275cc098..bc4c6d30cb 100644--- a/packages/studio/src/components/InspectorPanel/CompositionInspector.tsx+++ b/packages/studio/src/components/InspectorPanel/CompositionInspector.tsx@@ -16,6 +16,7 @@ import {CloudDownloadIcon} from '../../icons/cloud-download';import {PicIcon} from '../../icons/frame';import {SolidIcon} from '../../icons/solid';import {FilmIcon} from '../../icons/video';+import {SetSelectedModalContext} from '../../state/modals';import {VisualControlsContext} from '../../visual-controls/VisualControls';import {DefaultPropsEditor} from '../DefaultPropsEditor';import {useZodIfPossible, useZodTypesIfPossible} from '../get-zod-if-possible';@@ -94,7 +95,10 @@ const CompositionActions: React.FC = () => {insertComposition,insertSolid,} = useCompositionActions();- const downloadProject = getBrowserStudioOperations()?.downloadProject ?? null;+ const {setSelectedModal} = useContext(SetSelectedModalContext);+ const browserStudioOperations = getBrowserStudioOperations();+ const downloadProject = browserStudioOperations?.downloadProject ?? null;+ const isBrowserStudio = browserStudioOperations !== null;const onDownloadProject = useCallback(async () => {if (downloadProject === null) {@@ -122,12 +126,21 @@ const CompositionActions: React.FC = () => {}, [downloadProject]);const openElementsLibrary = useCallback(() => {- window.open(- 'https://www.remotion.dev/elements',- '_blank',- 'noopener,noreferrer',- );- }, []);+ if (isBrowserStudio) {+ window.open(+ 'https://www.remotion.dev/elements',+ '_blank',+ 'noopener,noreferrer',+ );+ return;+ }++ setSelectedModal({+ type: 'element-library',+ name: 'Remotion Elements',+ url: 'https://www.remotion.dev/elements',+ });+ }, [isBrowserStudio, setSelectedModal]);if (!canShowInsertAsset &&@@ -181,23 +194,29 @@ const CompositionActions: React.FC = () => {renderIcon={(color) => (<BrowseElementsIcon color={color} style={browseElementsIconStyle} />)}- title="Open the Remotion Elements library in a new tab. Install an Element there to send it to this composition."+ title={+ isBrowserStudio+ ? 'Open the Remotion Elements library in a new tab. Install an Element there to send it to this composition.'+ : 'Browse the Remotion Elements library inside Studio.'+ }>Browse Elements- <svg- aria-hidden="true"- viewBox="0 0 16 16"- style={browseElementsArrowStyle}- >- <path- d="M4 12 12 4M6 4h6v6"- fill="none"- stroke={CURRENT_COLOR}- strokeLinecap="round"- strokeLinejoin="round"- strokeWidth="1.5"- />- </svg>+ {isBrowserStudio ? (+ <svg+ aria-hidden="true"+ viewBox="0 0 16 16"+ style={browseElementsArrowStyle}+ >+ <path+ d="M4 12 12 4M6 4h6v6"+ fill="none"+ stroke={CURRENT_COLOR}+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ />+ </svg>+ ) : null}</InspectorQuickAction>) : null}{downloadProject ? (diff --git a/packages/studio/src/components/Modals.tsx b/packages/studio/src/components/Modals.tsxindex 7df3f83a78..f1077c5505 100644--- a/packages/studio/src/components/Modals.tsx+++ b/packages/studio/src/components/Modals.tsx@@ -6,6 +6,7 @@ import {SelectedModalContext, SetSelectedModalContext} from '../state/modals';import {AskAiModal} from './AskAiModal';import {ConfirmationDialog} from './ConfirmationDialog';import {EffectPickerModal} from './EffectPickerModal';+import {ElementLibraryModal} from './ElementLibraryModal';import {FixComputedValueModal} from './FixComputedValueModal';import {DeleteComposition} from './NewComposition/DeleteComposition';import {DeleteFolder} from './NewComposition/DeleteFolder';@@ -212,6 +213,12 @@ export const Modals: React.FC<{compositionSelection={modalContextType.compositionSelection}/>)}+ {modalContextType && modalContextType.type === 'element-library' && (+ <ElementLibraryModal+ name={modalContextType.name}+ url={modalContextType.url}+ />+ )}{modalContextType && modalContextType.type === 'add-effect' && (<EffectPickerModal state={modalContextType} />)}diff --git a/packages/studio/src/state/modals.ts b/packages/studio/src/state/modals.tsindex eae4bc9d84..4f02372998 100644--- a/packages/studio/src/state/modals.ts+++ b/packages/studio/src/state/modals.ts@@ -231,6 +231,11 @@ export type ModalState =onSelected: (composition: _InternalTypes['AnyComposition']) => void;} | null;}+ | {+ type: 'element-library';+ name: string;+ url: string;+ }| AddEffectModalState| ConfirmationDialogState| SvgImportDialogState;