Skip to content

🎥 Make videos programmatically with React

Add Studio composition menu

Cursor Agent2f58db0f3c04
diff --git a/packages/studio/src/components/CompositionSelectorItem.tsx b/packages/studio/src/components/CompositionSelectorItem.tsx
index 9d0b7538de..b5b806e3f1 100644
--- a/packages/studio/src/components/CompositionSelectorItem.tsx
+++ b/packages/studio/src/components/CompositionSelectorItem.tsx
@@ -8,12 +8,14 @@ import React, {
useState,
} from 'react';
import {type _InternalTypes} from 'remotion';
+import {StudioServerConnectionCtx} from '../helpers/client-id';
import {
BACKGROUND,
LIGHT_TEXT,
getBackgroundFromHoverState,
} from '../helpers/colors';
import {isCompositionStill} from '../helpers/is-composition-still';
+import {noop} from '../helpers/noop';
import {
markCompositionSidebarScrollFromRowClick,
maybeScrollCompositionSidebarRowIntoView,
@@ -22,12 +24,13 @@ import {CollapsedFolderIcon, ExpandedFolderIcon} from '../icons/folder';
import {StillIcon} from '../icons/still';
import {FilmIcon} from '../icons/video';
import {ModalsContext} from '../state/modals';
+import {getCompositionMenuItems} from './composition-menu-items';
import {CompositionContextButton} from './CompositionContextButton';
import {ContextMenu} from './ContextMenu';
import {Row, Spacing} from './layout';
import type {ComboboxValue} from './NewComposition/ComboBox';
-import {showNotification} from './Notifications/NotificationCenter';
import {SidebarRenderButton} from './SidebarRenderButton';
+import {useResolvedStack} from './Timeline/use-resolved-stack';
const COMPOSITION_ITEM_HEIGHT = 32;
@@ -171,94 +174,25 @@ export const CompositionSelectorItem: React.FC<{
);
const {setSelectedModal} = useContext(ModalsContext);
+ const connectionStatus = useContext(StudioServerConnectionCtx)
+ .previewServerState.type;
+ const resolvedLocation = useResolvedStack(
+ item.type === 'composition' ? item.composition.stack : null,
+ );
const contextMenu = useMemo((): ComboboxValue[] => {
if (item.type === 'composition') {
- return [
- {
- id: 'duplicate',
- keyHint: null,
- label: `Duplicate...`,
- leftItem: null,
- onClick: () => {
- setSelectedModal({
- type: 'duplicate-comp',
- compositionId: item.composition.id,
- compositionType:
- item.composition.durationInFrames === 1
- ? 'still'
- : 'composition',
- });
- },
- quickSwitcherLabel: null,
- subMenu: null,
- type: 'item',
- value: 'duplicate',
- },
- {
- id: 'rename',
- keyHint: null,
- label: `Rename...`,
- leftItem: null,
- onClick: () => {
- setSelectedModal({
- type: 'rename-comp',
- compositionId: item.composition.id,
- });
- },
- quickSwitcherLabel: null,
- subMenu: null,
- type: 'item',
- value: 'rename',
- },
- {
- id: 'delete',
- keyHint: null,
- label: `Delete...`,
- leftItem: null,
- onClick: () => {
- setSelectedModal({
- type: 'delete-comp',
- compositionId: item.composition.id,
- });
- },
- quickSwitcherLabel: null,
- subMenu: null,
- type: 'item',
- value: 'delete',
- },
- {
- type: 'divider',
- id: 'copy-id-divider',
- },
- {
- id: 'copy-id',
- keyHint: null,
- label: `Copy ID`,
- leftItem: null,
- onClick: () => {
- navigator.clipboard
- .writeText(item.composition.id)
- .catch((err) => {
- showNotification(
- `Could not copy to clipboard: ${err.message}`,
- 1000,
- );
- })
- .then(() => {
- showNotification('Copied to clipboard', 1000);
- });
- },
- quickSwitcherLabel: null,
- subMenu: null,
- type: 'item',
- value: 'remove',
- },
- ];
+ return getCompositionMenuItems({
+ closeMenu: noop,
+ composition: item.composition,
+ connectionStatus,
+ resolvedLocation,
+ setSelectedModal,
+ });
}
return [];
- }, [item, setSelectedModal]);
+ }, [connectionStatus, item, resolvedLocation, setSelectedModal]);
if (item.type === 'folder') {
return (
diff --git a/packages/studio/src/components/Menu/MenuItem.tsx b/packages/studio/src/components/Menu/MenuItem.tsx
index 7caa0e8402..306c9c51c7 100644
--- a/packages/studio/src/components/Menu/MenuItem.tsx
+++ b/packages/studio/src/components/Menu/MenuItem.tsx
@@ -27,6 +27,7 @@ export type MenuId =
| 'remotion'
| 'file'
| 'view'
+ | 'composition'
| 'install'
| 'tools'
| 'help';
diff --git a/packages/studio/src/components/composition-menu-items.ts b/packages/studio/src/components/composition-menu-items.ts
new file mode 100644
index 0000000000..19867e3b41
--- /dev/null
+++ b/packages/studio/src/components/composition-menu-items.ts
@@ -0,0 +1,139 @@
+import type {SetStateAction} from 'react';
+import type {ResolvedStackLocation, _InternalTypes} from 'remotion';
+import {openOriginalPositionInEditor} from '../helpers/open-in-editor';
+import type {ModalState} from '../state/modals';
+import type {ComboboxValue} from './NewComposition/ComboBox';
+import {showNotification} from './Notifications/NotificationCenter';
+
+const truthy = <T>(value: T | null): value is T => value !== null;
+
+export const getCompositionMenuItems = ({
+ composition,
+ connectionStatus,
+ resolvedLocation,
+ setSelectedModal,
+ closeMenu,
+}: {
+ composition: _InternalTypes['AnyComposition'];
+ connectionStatus: string;
+ resolvedLocation: ResolvedStackLocation | null;
+ setSelectedModal: (value: SetStateAction<ModalState | null>) => void;
+ closeMenu: () => void;
+}): ComboboxValue[] => {
+ const editorName = window.remotion_editorName;
+
+ return [
+ editorName
+ ? {
+ id: 'show-in-editor',
+ keyHint: null,
+ label: `Show in ${editorName}`,
+ leftItem: null,
+ onClick: async () => {
+ closeMenu();
+ if (!resolvedLocation) {
+ return;
+ }
+
+ try {
+ await openOriginalPositionInEditor(resolvedLocation);
+ } catch (err) {
+ showNotification((err as Error).message, 2000);
+ }
+ },
+ quickSwitcherLabel: `Show composition in ${editorName}`,
+ subMenu: null,
+ type: 'item' as const,
+ value: 'show-in-editor',
+ disabled: connectionStatus !== 'connected' || !resolvedLocation,
+ }
+ : null,
+ editorName
+ ? {
+ type: 'divider' as const,
+ id: 'show-in-editor-divider',
+ }
+ : null,
+ {
+ id: 'rename',
+ keyHint: null,
+ label: `Rename...`,
+ leftItem: null,
+ onClick: () => {
+ closeMenu();
+ setSelectedModal({
+ type: 'rename-comp',
+ compositionId: composition.id,
+ });
+ },
+ quickSwitcherLabel: `Composition: Rename ${composition.id}`,
+ subMenu: null,
+ type: 'item' as const,
+ value: 'rename',
+ },
+ {
+ id: 'duplicate',
+ keyHint: null,
+ label: `Duplicate...`,
+ leftItem: null,
+ onClick: () => {
+ closeMenu();
+ setSelectedModal({
+ type: 'duplicate-comp',
+ compositionId: composition.id,
+ compositionType:
+ composition.durationInFrames === 1 ? 'still' : 'composition',
+ });
+ },
+ quickSwitcherLabel: `Composition: Duplicate ${composition.id}`,
+ subMenu: null,
+ type: 'item' as const,
+ value: 'duplicate',
+ },
+ {
+ id: 'delete',
+ keyHint: null,
+ label: `Delete...`,
+ leftItem: null,
+ onClick: () => {
+ closeMenu();
+ setSelectedModal({
+ type: 'delete-comp',
+ compositionId: composition.id,
+ });
+ },
+ quickSwitcherLabel: `Composition: Delete ${composition.id}`,
+ subMenu: null,
+ type: 'item' as const,
+ value: 'delete',
+ },
+ {
+ type: 'divider' as const,
+ id: 'copy-id-divider',
+ },
+ {
+ id: 'copy-id',
+ keyHint: null,
+ label: `Copy ID`,
+ leftItem: null,
+ onClick: () => {
+ closeMenu();
+ navigator.clipboard
+ .writeText(composition.id)
+ .then(() => {
+ showNotification('Copied to clipboard', 1000);
+ })
+ .catch((err) => {
+ showNotification(
+ `Could not copy to clipboard: ${err.message}`,
+ 1000,
+ );
+ });
+ },
+ quickSwitcherLabel: `Composition: Copy ID ${composition.id}`,
+ subMenu: null,
+ type: 'item' as const,
+ value: 'copy-id',
+ },
+ ].filter(truthy);
+};
diff --git a/packages/studio/src/helpers/use-menu-structure.tsx b/packages/studio/src/helpers/use-menu-structure.tsx
index 540f879be8..417f37a443 100644
--- a/packages/studio/src/helpers/use-menu-structure.tsx
+++ b/packages/studio/src/helpers/use-menu-structure.tsx
@@ -3,6 +3,7 @@ import {Internals} from 'remotion';
import {NoReactInternals} from 'remotion/no-react';
import {restartStudio} from '../api/restart-studio';
import {askAiModalRef} from '../components/AskAiModal';
+import {getCompositionMenuItems} from '../components/composition-menu-items';
import {Row} from '../components/layout';
import type {Menu} from '../components/Menu/MenuItem';
import type {
@@ -12,6 +13,7 @@ import type {
import {showNotification} from '../components/Notifications/NotificationCenter';
import type {TQuickSwitcherResult} from '../components/QuickSwitcher/QuickSwitcherResult';
import {getPreviewSizeLabel, getUniqueSizes} from '../components/SizeSelector';
+import {useResolvedStack} from '../components/Timeline/use-resolved-stack';
import {inOutHandles} from '../components/TimelineInOutToggle';
import {cmdOrCtrlCharacter} from '../error-overlay/remotion-overlay/ShortcutHint';
import {Checkmark} from '../icons/Checkmark';
@@ -196,6 +198,9 @@ export const useMenuStructure = (
EditorShowGuidesContext,
);
const {size, setSize} = useContext(Internals.PreviewSizeContext);
+ const {canvasContent, compositions} = useContext(
+ Internals.CompositionManager,
+ );
const {type} = useContext(StudioServerConnectionCtx).previewServerState;
const {
@@ -214,6 +219,18 @@ export const useMenuStructure = (
);
const mobileLayout = useMobileLayout();
+ const currentComposition = useMemo(() => {
+ if (canvasContent === null || canvasContent.type !== 'composition') {
+ return null;
+ }
+
+ return (
+ compositions.find((c) => c.id === canvasContent.compositionId) ?? null
+ );
+ }, [canvasContent, compositions]);
+ const resolvedCompositionLocation = useResolvedStack(
+ currentComposition?.stack ?? null,
+ );
const structure = useMemo((): Structure => {
let struct: Structure = [
{
@@ -662,6 +679,21 @@ export const useMenuStructure = (
: null,
].filter(Internals.truthy),
},
+ currentComposition
+ ? {
+ id: 'composition' as const,
+ label: 'Composition',
+ leaveLeftPadding: false,
+ items: getCompositionMenuItems({
+ closeMenu,
+ composition: currentComposition,
+ connectionStatus: type,
+ resolvedLocation: resolvedCompositionLocation,
+ setSelectedModal,
+ }),
+ quickSwitcherLabel: null,
+ }
+ : null,
{
id: 'tools' as const,
label: 'Tools',
@@ -925,6 +957,8 @@ export const useMenuStructure = (
type,
sizePreselectIndex,
sizes,
+ currentComposition,
+ resolvedCompositionLocation,
editorZoomGestures,
editorShowRulers,
editorShowGuides,