Skip to content

🎥 Make videos programmatically with React

28.4 KB
Raw Blame
import {StudioProtocolInternals} from '@remotion/studio-protocol';
import React, {
	useCallback,
	useContext,
	useEffect,
	useLayoutEffect,
	useMemo,
	useRef,
	useState,
} from 'react';
import {createPortal} from 'react-dom';
import {Internals} from 'remotion';
import {ShortcutHint} from '../error-overlay/remotion-overlay/ShortcutHint';
import {
	INPUT_BACKGROUND,
	LIGHT_TEXT,
	TRANSPARENT,
	WARNING_COLOR,
	WHITE,
	WHITE_ALPHA_12,
} from '../helpers/colors';
import {
	FOCUS_VISIBLE_ONLY_CLASS_NAME,
	HOVERABLE_CLASS_NAME,
	hoverableStyle,
} from '../helpers/hoverable';
import {resolvedStackToSymbolicated} from '../helpers/resolved-stack-to-symbolicated';
import {getRoute} from '../helpers/url-state';
import {useCreateComposition} from '../helpers/use-create-composition';
import {validateCompositionName} from '../helpers/validate-new-comp-data';
import type {
	ElementInstallModalState,
	ElementInstallPlan,
} from '../state/modals';
import {useZIndex} from '../state/z-index';
import {Button} from './Button';
import {prepareElementInstall} from './element-install-api';
import {insertElement} from './import-assets';
import {useSelectComposition} from './InitialCompositionLoader';
import {Flex, Row, Spacing} from './layout';
import {LazySyntaxHighlightedSource} from './LazySyntaxHighlightedSource';
import {
	HORIZONTAL_SCROLLBAR_CLASSNAME,
	VERTICAL_SCROLLBAR_CLASSNAME,
} from './Menu/is-menu-item';
import {getPortal} from './Menu/portals';
import {ModalButton} from './ModalButton';
import {ModalContainer} from './ModalContainer';
import {ModalFooterContainer} from './ModalFooter';
import {ModalHeader} from './ModalHeader';
import {
	NewCompositionFields,
	type NewCompositionFormValues,
} from './NewComposition/NewComposition';
import {RemotionInput} from './NewComposition/RemInput';
import {
	ValidationMessage,
	WarningTriangle,
} from './NewComposition/ValidationMessage';
import {RadioButton} from './RadioButton';
import {SegmentedControl} from './SegmentedControl';
import {
	hasResolvedStack,
	useResolvedStack,
} from './Timeline/use-resolved-stack';

const container: React.CSSProperties = {
	display: 'flex',
	flexDirection: 'column',
	gap: 20,
	color: LIGHT_TEXT,
	fontFamily: 'sans-serif',
	fontSize: 13,
	lineHeight: 1.5,
};

const dialogContent: React.CSSProperties = {
	...container,
	padding: 16,
	width: 'min(520px, calc(100vw - 40px))',
	maxHeight: 'min(720px, calc(100vh - 140px))',
	overflowY: 'auto',
};

const sectionStyle: React.CSSProperties = {
	display: 'flex',
	flexDirection: 'column',
	gap: 10,
};

const sectionTitleStyle: React.CSSProperties = {
	margin: 0,
	color: LIGHT_TEXT,
	fontFamily: 'sans-serif',
	fontSize: 13,
	fontWeight: 600,
	lineHeight: 1.5,
};

const metadataDescriptionStyle: React.CSSProperties = {
	margin: 0,
	minWidth: 0,
	color: LIGHT_TEXT,
	fontFamily: 'sans-serif',
	fontSize: 13,
	fontWeight: 400,
	lineHeight: 1.5,
	overflowWrap: 'anywhere',
};

const requestSourceStyle: React.CSSProperties = {
	display: 'flex',
	justifyContent: 'space-between',
	gap: 16,
	margin: 0,
};

const requestSourceDescriptionStyle: React.CSSProperties = {
	...metadataDescriptionStyle,
	color: LIGHT_TEXT,
	textAlign: 'right',
};

const unverifiedRequestSourceStyle: React.CSSProperties = {
	...requestSourceDescriptionStyle,
	color: WARNING_COLOR,
};

const overwriteStyle: React.CSSProperties = {
	margin: 0,
	color: WARNING_COLOR,
	fontFamily: 'sans-serif',
	fontSize: 13,
	fontWeight: 500,
	lineHeight: 1.5,
};

const dependencyListStyle: React.CSSProperties = {
	display: 'flex',
	flexDirection: 'column',
	gap: 8,
	margin: 0,
	padding: 0,
	listStyleType: 'none',
	textAlign: 'right',
	minWidth: 0,
};

const dependencyNameStyle: React.CSSProperties = {
	minWidth: 0,
	color: LIGHT_TEXT,
	fontFamily: 'sans-serif',
	fontSize: 13,
	lineHeight: 1.5,
	overflowWrap: 'anywhere',
};

const warningStyle: React.CSSProperties = {
	display: 'flex',
	alignItems: 'flex-start',
	gap: 10,
	minWidth: 0,
};

const warningIconStyle: React.CSSProperties = {
	width: 14,
	height: 14,
	marginTop: 2,
	flexShrink: 0,
	fill: WARNING_COLOR,
};

const warningDescriptionStyle: React.CSSProperties = {
	margin: 0,
	minWidth: 0,
	color: LIGHT_TEXT,
	fontFamily: 'sans-serif',
	fontSize: 13,
	fontWeight: 400,
	lineHeight: 1.5,
};

const sourceDetailsStyle: React.CSSProperties = {
	paddingTop: 2,
	fontFamily: 'sans-serif',
	fontSize: 13,
	lineHeight: 1.5,
};

const sourceSummaryStyle: React.CSSProperties = {
	cursor: 'default',
	...hoverableStyle({
		idleBackground: TRANSPARENT,
		hoverBackground: TRANSPARENT,
		idleColor: LIGHT_TEXT,
		hoverColor: WHITE,
	}),
	fontFamily: 'sans-serif',
	fontSize: 13,
	fontWeight: 500,
	lineHeight: 1.5,
};

const sourceCodeContainerStyle: React.CSSProperties = {
	marginTop: 10,
	overflow: 'hidden',
	border: `1px solid ${WHITE_ALPHA_12}`,
	borderRadius: 6,
};

const sourceCodeBlockStyle: React.CSSProperties = {
	backgroundColor: INPUT_BACKGROUND,
	margin: 0,
	border: 0,
	borderRadius: 0,
	maxWidth: 'none',
	maxHeight: 240,
	overflow: 'auto',
	padding: 12,
	fontFamily: 'monospace',
	fontSize: 12,
	lineHeight: 1.5,
	whiteSpace: 'pre',
};

const sourceCodeStyle: React.CSSProperties = {
	color: 'inherit',
	fontFamily: 'inherit',
	fontSize: 'inherit',
	lineHeight: 'inherit',
};

const destinationControlStyle: React.CSSProperties = {
	display: 'flex',
	alignItems: 'center',
	justifyContent: 'space-between',
	columnGap: 16,
	rowGap: 10,
	flexWrap: 'wrap',
};

const footerStyle: React.CSSProperties = {
	minWidth: 0,
};

const cancelStyle: React.CSSProperties = {
	minWidth: 90,
};

const elementInstallTitleId = 'remotion-element-install-title';

const makeSourceControlsVisible = (sourceCode: string) => {
	return sourceCode.replace(
		/[\u200b-\u200f\u202a-\u202e\u2066-\u2069\ufeff]/g,
		(character) => {
			return `\\u${character.codePointAt(0)?.toString(16).padStart(4, '0')}`;
		},
	);
};

export const ElementLibraryAddConfirmation: React.FC<{
	readonly origin: string;
	readonly url: string;
	readonly captionStylesUrl: string | null;
}> = ({origin, url, captionStylesUrl}) => {
	return (
		<p
			style={{
				color: LIGHT_TEXT,
				fontFamily: 'sans-serif',
				fontSize: 13,
				fontWeight: 400,
				lineHeight: 1.5,
				margin: 0,
				overflowWrap: 'anywhere',
			}}
		>
			{origin.replace(/^https?:\/\//, '')} wants to add{' '}
			{url.replace(/^https?:\/\//, '')} as an Element library to{' '}
			{window.remotion_projectName ?? 'this Studio project'}.
			{captionStylesUrl === null ? null : (
				<> Caption styles will be loaded from {captionStylesUrl}.</>
			)}
		</p>
	);
};

type PreparedElementInstallation = {
	basePlan: ElementInstallPlan;
	name: string;
	refresh: number;
	plan: ElementInstallPlan;
};

type NewCompositionPlanState =
	| {
			readonly type: 'loading';
			readonly folderStack: string;
	  }
	| {
			readonly type: 'ready';
			readonly folderStack: string | null;
			readonly plan: ElementInstallPlan;
	  }
	| {
			readonly type: 'error';
			readonly folderStack: string;
			readonly reason: string;
	  };

export const ElementInstallConfirmation: React.FC<{
	readonly state: ElementInstallModalState;
}> = ({state}) => {
	const {
		currentPlan,
		missingPackages,
		newPlan,
		onCancel,
		onClose,
		request,
		sourceIsUnverified,
		sourceLabel,
	} = state;
	const isOfficialRemotionSource =
		request.source.type === 'studio-protocol' &&
		request.source.origin === 'https://www.remotion.dev';
	const sourcePreview = useMemo(
		() => makeSourceControlsVisible(request.element.sourceCode),
		[request.element.sourceCode],
	);
	const currentDestination = useMemo(() => {
		if (request.compositionFile === null || request.compositionId === null) {
			return null;
		}

		return {
			compositionFile: request.compositionFile,
			compositionId: request.compositionId,
		};
	}, [request.compositionFile, request.compositionId]);
	const config = Internals.useUnsafeVideoConfig();
	const {canvasContent, compositions} = useContext(
		Internals.CompositionManager,
	);
	const selectComposition = useSelectComposition();
	const currentCompositionMetadata =
		config === null ||
		canvasContent?.type !== 'composition' ||
		canvasContent.compositionId !== request.compositionId
			? null
			: {
					durationInFrames: config.durationInFrames,
					fps: config.fps,
					height: config.height,
					width: config.width,
				};
	const {currentZIndex} = useZIndex();
	const [mode, setMode] = useState<'current-composition' | 'new-composition'>(
		currentPlan === null || request.source.type === 'browser-studio-link'
			? 'new-composition'
			: 'current-composition',
	);
	const [submitting, setSubmitting] = useState(false);
	const inputRef = useRef<HTMLInputElement>(null);
	const [newCompositionValues, setNewCompositionValues] =
		useState<NewCompositionFormValues>(() => {
			const elementComponentName =
				StudioProtocolInternals.getElementComponentNameFromSourceCode(
					request.element.sourceCode,
				) ?? 'Element';
			const baseName = `${elementComponentName}Composition`;
			let candidate = baseName;
			let suffix = 2;
			while (validateCompositionName(candidate, compositions) !== null) {
				candidate = `${baseName}${suffix}`;
				suffix++;
			}

			const size =
				request.element.dimensions ??
				(currentCompositionMetadata === null
					? {width: 1920, height: 1080}
					: {
							width: currentCompositionMetadata.width,
							height: currentCompositionMetadata.height,
						});

			return {
				durationInFrames:
					request.element.durationInFrames ??
					currentCompositionMetadata?.durationInFrames ??
					150,
				folder: {folderName: null, parentName: null, stack: null},
				fps: currentCompositionMetadata?.fps ?? 30,
				id: candidate,
				size,
			};
		});
	const selectedFolderStack = newCompositionValues.folder.stack;
	const resolvedFolderLocation = useResolvedStack(selectedFolderStack);
	const folderSymbolicatedStack = useMemo(
		() => resolvedStackToSymbolicated(resolvedFolderLocation),
		[resolvedFolderLocation],
	);
	const folderCompositionFile =
		folderSymbolicatedStack?.originalFileName ?? null;
	const [newCompositionPlanState, setNewCompositionPlanState] =
		useState<NewCompositionPlanState>({
			folderStack: null,
			type: 'ready',
			plan: newPlan,
		});

	useEffect(() => {
		if (selectedFolderStack === null) {
			setNewCompositionPlanState({
				folderStack: null,
				type: 'ready',
				plan: newPlan,
			});
			return;
		}

		if (!hasResolvedStack(selectedFolderStack)) {
			setNewCompositionPlanState({
				folderStack: selectedFolderStack,
				type: 'loading',
			});
			return;
		}

		if (folderCompositionFile === null) {
			setNewCompositionPlanState({
				folderStack: selectedFolderStack,
				type: 'error',
				reason:
					'Could not determine where the new composition should be created.',
			});
			return;
		}

		let canceled = false;
		setNewCompositionPlanState({
			folderStack: selectedFolderStack,
			type: 'loading',
		});
		prepareElementInstall({
			installationName: null,
			destination: {
				type: 'new-composition',
				compositionFile: folderCompositionFile,
			},
			element: request.element,
		})
			.then((result) => {
				if (canceled) {
					return;
				}

				setNewCompositionPlanState(
					result.success
						? {
								folderStack: selectedFolderStack,
								type: 'ready',
								plan: result.plan,
							}
						: {
								folderStack: selectedFolderStack,
								type: 'error',
								reason: result.reason,
							},
				);
			})
			.catch((error) => {
				if (!canceled) {
					setNewCompositionPlanState({
						folderStack: selectedFolderStack,
						type: 'error',
						reason: error instanceof Error ? error.message : String(error),
					});
				}
			});

		return () => {
			canceled = true;
		};
	}, [folderCompositionFile, newPlan, request.element, selectedFolderStack]);

	const {
		options: newCompositionOptions,
		compositionId: newCompositionId,
		heightValidationMessage,
		nameValidationMessage,
		valid: newCompositionValuesAreValid,
		widthValidationMessage,
	} = useCreateComposition({
		asset: null,
		canvasCapture: null,
		compositions,
		durationInFrames: newCompositionValues.durationInFrames,
		folderName: newCompositionValues.folder.folderName,
		newId: newCompositionValues.id,
		parentName: newCompositionValues.folder.parentName,
		selectedFrameRate: newCompositionValues.fps,
		size: newCompositionValues.size,
	});
	const activeNewCompositionPlanState =
		newCompositionPlanState.folderStack === selectedFolderStack
			? newCompositionPlanState
			: null;
	const selectedNewCompositionPlan =
		activeNewCompositionPlanState?.type === 'ready'
			? activeNewCompositionPlanState.plan
			: null;
	const selectedPlan =
		mode === 'current-composition' ? currentPlan : selectedNewCompositionPlan;
	const elementBaseName = request.element.slug.split('/').at(-1) ?? '';
	const [refreshPlan, setRefreshPlan] = useState(0);
	const [input, setInput] = useState<{
		basePlan: ElementInstallPlan | null;
		baseName: string;
		refresh: number;
		name: string | null;
		overwritePlan: ElementInstallPlan | null;
	}>({
		basePlan: selectedPlan,
		baseName: elementBaseName,
		refresh: refreshPlan,
		name: null,
		overwritePlan: null,
	});
	if (
		input.basePlan !== selectedPlan ||
		input.baseName !== elementBaseName ||
		input.refresh !== refreshPlan
	) {
		setInput({
			basePlan: selectedPlan,
			baseName: elementBaseName,
			refresh: refreshPlan,
			name: null,
			overwritePlan: null,
		});
	}

	const requestedName = input.name;
	const [preparations, setPreparations] = useState<
		Array<{
			basePlan: ElementInstallPlan;
			refresh: number;
			requestedName: string | null;
			installation: PreparedElementInstallation | null;
			error: string | null;
		}>
	>([]);
	const currentPreparation =
		preparations.find(
			(preparation) =>
				preparation.basePlan === selectedPlan &&
				preparation.refresh === refreshPlan &&
				preparation.requestedName === requestedName,
		) ?? null;
	const preparedInstallation = currentPreparation?.installation ?? null;
	const planError = currentPreparation?.error ?? null;
	const installationName =
		requestedName ?? preparedInstallation?.name ?? elementBaseName;
	const [existingInstallations, setExistingInstallations] = useState<
		PreparedElementInstallation[]
	>([]);
	const existingDestination =
		existingInstallations.find(
			(installation) =>
				installation.basePlan === selectedPlan &&
				installation.refresh === refreshPlan,
		) ?? null;
	const overwriteExisting =
		existingDestination !== null &&
		input.overwritePlan === existingDestination.plan;
	const activePlan = overwriteExisting
		? existingDestination.plan
		: (preparedInstallation?.plan ?? null);
	const [displayedPlans, setDisplayedPlans] = useState<
		Array<{
			basePlan: ElementInstallPlan;
			refresh: number;
			plan: ElementInstallPlan;
		}>
	>([]);
	const rememberedDisplayedPlan =
		displayedPlans.find(
			(displayed) =>
				displayed.basePlan === selectedPlan &&
				displayed.refresh === refreshPlan,
		)?.plan ?? null;
	const displayedPlan = activePlan ?? rememberedDisplayedPlan ?? selectedPlan;
	useLayoutEffect(() => {
		if (activePlan === null || selectedPlan === null) {
			return;
		}

		setDisplayedPlans((previous) => {
			const existing = previous.find(
				(displayed) =>
					displayed.basePlan === selectedPlan &&
					displayed.refresh === refreshPlan,
			);
			if (existing?.plan === activePlan) {
				return previous;
			}

			return [
				...previous.filter(
					(displayed) =>
						displayed.basePlan !== selectedPlan ||
						displayed.refresh !== refreshPlan,
				),
				{basePlan: selectedPlan, refresh: refreshPlan, plan: activePlan},
			];
		});
	}, [activePlan, refreshPlan, selectedPlan]);
	useEffect(() => {
		if (selectedPlan === null) {
			return;
		}

		const destination =
			mode === 'current-composition'
				? currentDestination === null
					? null
					: {
							type: 'current-composition' as const,
							compositionFile: currentDestination.compositionFile,
							compositionId: currentDestination.compositionId,
						}
				: {
						type: 'new-composition' as const,
						compositionFile: selectedPlan.compositionFile,
					};
		if (destination === null) {
			return;
		}

		let canceled = false;
		(async () => {
			let candidate = requestedName ?? elementBaseName;
			let copyNumber = 1;
			while (true) {
				const result = await prepareElementInstall({
					installationName: candidate,
					destination,
					element: request.element,
				});
				if (canceled) return;
				if (!result.success) throw new Error(result.reason);

				const prepared = {
					basePlan: selectedPlan,
					name: candidate,
					refresh: refreshPlan,
					plan: result.plan,
				};
				if (result.plan.expectedFileState.exists) {
					setExistingInstallations((previous) =>
						previous.some(
							(installation) =>
								installation.basePlan === selectedPlan &&
								installation.refresh === refreshPlan,
						)
							? previous
							: [...previous, prepared],
					);
					if (requestedName === null) {
						candidate = `${elementBaseName}-copy${copyNumber === 1 ? '' : `-${copyNumber}`}`;
						copyNumber++;
						continue;
					}
				}

				setPreparations((previous) => [
					...previous.filter(
						(preparation) =>
							preparation.basePlan !== selectedPlan ||
							preparation.refresh !== refreshPlan ||
							preparation.requestedName !== requestedName,
					),
					{
						basePlan: selectedPlan,
						refresh: refreshPlan,
						requestedName,
						installation: prepared,
						error: null,
					},
				]);
				return;
			}
		})().catch((error) => {
			if (!canceled) {
				setPreparations((previous) => [
					...previous.filter(
						(preparation) =>
							preparation.basePlan !== selectedPlan ||
							preparation.refresh !== refreshPlan ||
							preparation.requestedName !== requestedName,
					),
					{
						basePlan: selectedPlan,
						refresh: refreshPlan,
						requestedName,
						installation: null,
						error: error instanceof Error ? error.message : String(error),
					},
				]);
			}
		});
		return () => {
			canceled = true;
		};
	}, [
		elementBaseName,
		requestedName,
		mode,
		currentDestination,
		refreshPlan,
		request,
		selectedPlan,
	]);

	const installationNameError =
		installationName.length > 0
			? (planError ??
				(requestedName !== null && activePlan?.expectedFileState.exists
					? 'Name already taken.'
					: null))
			: null;
	const folderTargetIsReady =
		selectedFolderStack === null ||
		(hasResolvedStack(selectedFolderStack) && folderCompositionFile !== null);
	const title = `Install ${request.element.displayName}${request.element.displayName.endsWith(' Element') ? '' : ' Element'}`;
	const canSubmit =
		!submitting &&
		(overwriteExisting ||
			(planError === null &&
				StudioProtocolInternals.makeElementFileNameFromSlug(
					installationName,
				) === `${installationName}.element.tsx` &&
				(activePlan !== null
					? !activePlan.expectedFileState.exists
					: requestedName !== null))) &&
		(mode === 'current-composition'
			? currentPlan !== null && currentDestination !== null
			: newCompositionValuesAreValid &&
				folderTargetIsReady &&
				selectedNewCompositionPlan !== null);

	const submit = useCallback(async () => {
		if (!canSubmit || selectedPlan === null) {
			return;
		}

		const destination =
			mode === 'new-composition'
				? {
						compositionFile: selectedPlan.compositionFile,
						compositionId: newCompositionId,
					}
				: currentDestination;
		if (destination === null) {
			return;
		}

		setSubmitting(true);
		const installed = await insertElement({
			installationName: overwriteExisting
				? (existingDestination?.name ?? installationName)
				: installationName,
			compositionFile: destination.compositionFile,
			compositionId: destination.compositionId,
			element: request.element,
			// The insert operation revalidates this even if the name preflight is pending.
			expectedFileState: activePlan?.expectedFileState ?? {exists: false},
			from: mode === 'new-composition' ? null : request.from,
			overwriteExisting,
			position: mode === 'new-composition' ? null : request.position,
			undoRedoNavigation:
				mode === 'new-composition'
					? {
							undoRoute: getRoute(),
							redoRoute: `/${newCompositionId}`,
						}
					: null,
			newComposition:
				mode === 'new-composition'
					? {
							options: newCompositionOptions,
							symbolicatedStack:
								selectedFolderStack === null ? null : folderSymbolicatedStack,
						}
					: null,
		});
		if (installed) {
			if (mode === 'new-composition') {
				selectComposition(
					{
						id: newCompositionId,
						folderName: newCompositionValues.folder.folderName,
						parentFolderName: newCompositionValues.folder.parentName,
					},
					true,
				);
			}

			onClose();
		} else {
			setSubmitting(false);
			setRefreshPlan((value) => value + 1);
		}
	}, [
		activePlan,
		existingDestination,
		installationName,
		overwriteExisting,
		canSubmit,
		currentDestination,
		folderSymbolicatedStack,
		mode,
		newCompositionOptions,
		newCompositionId,
		onClose,
		request,
		selectedFolderStack,
		selectedPlan,
		selectComposition,
		newCompositionValues.folder.folderName,
		newCompositionValues.folder.parentName,
	]);

	const cancel = useCallback(() => {
		if (!submitting) {
			onCancel();
		}
	}, [onCancel, submitting]);

	const destinationOptions = useMemo(
		() => [
			{
				key: 'current-composition',
				label: 'Current composition',
				selected: mode === 'current-composition',
				onClick:
					currentPlan === null ? null : () => setMode('current-composition'),
			},
			{
				key: 'new-composition',
				label: 'New composition',
				selected: mode === 'new-composition',
				onClick: () => {
					setMode('new-composition');
					requestAnimationFrame(() => inputRef.current?.select());
				},
			},
		],
		[currentPlan, mode],
	);

	const onSubmit: React.FormEventHandler<HTMLFormElement> = useCallback(
		(event) => {
			event.preventDefault();
			submit();
		},
		[submit],
	);

	return createPortal(
		<ModalContainer
			ariaLabelledBy={elementInstallTitleId}
			onOutsideClick={cancel}
			onEscape={cancel}
		>
			<div id={elementInstallTitleId}>
				<ModalHeader title={title} onClose={cancel} />
			</div>
			<form onSubmit={onSubmit}>
				<div style={dialogContent} className={VERTICAL_SCROLLBAR_CLASSNAME}>
					<dl style={requestSourceStyle} aria-label="Request source">
						<dt style={sectionTitleStyle}>From</dt>
						<dd
							style={
								sourceIsUnverified
									? unverifiedRequestSourceStyle
									: requestSourceDescriptionStyle
							}
						>
							{sourceLabel}
						</dd>
					</dl>

					<dl
						style={{
							...requestSourceStyle,
							visibility: displayedPlan === null ? 'hidden' : 'visible',
						}}
						aria-hidden={displayedPlan === null}
					>
						<dt style={sectionTitleStyle}>Destination</dt>
						<dd style={requestSourceDescriptionStyle}>
							{displayedPlan?.filePath ?? '\u00A0'}
						</dd>
					</dl>

					<div style={destinationControlStyle}>
						<div style={sectionTitleStyle}>Add to</div>
						<div aria-label="Add to" role="group" style={{marginLeft: 'auto'}}>
							<SegmentedControl
								items={destinationOptions}
								needsWrapping={false}
								size="medium"
							/>
						</div>
					</div>

					{currentPlan === null && request.compositionId !== null ? (
						<div style={warningStyle} role="status">
							<WarningTriangle style={warningIconStyle} />
							<p style={warningDescriptionStyle}>
								Studio could not find a safe place in “{request.compositionId}”
								to insert the Element. Install it into a new composition
								instead.
							</p>
						</div>
					) : null}

					{mode === 'new-composition' ? (
						<section
							style={{...sectionStyle, marginInline: -16}}
							aria-label="New composition settings"
						>
							<NewCompositionFields
								compositionId={newCompositionId}
								heightValidationMessage={heightValidationMessage}
								inputRef={inputRef}
								nameValidationMessage={nameValidationMessage}
								setValues={setNewCompositionValues}
								values={newCompositionValues}
								widthValidationMessage={widthValidationMessage}
							/>
							{activeNewCompositionPlanState?.type === 'error' ? (
								<ValidationMessage
									align="flex-start"
									message={activeNewCompositionPlanState.reason}
									type="error"
								/>
							) : null}
						</section>
					) : null}

					<section style={sectionStyle} aria-label="Element implementation">
						{existingDestination ? (
							<div
								role="radiogroup"
								aria-label="Existing Element file"
								style={sectionStyle}
							>
								<RadioButton
									checked={!overwriteExisting}
									disabled={submitting}
									onClick={() =>
										setInput((previous) => ({...previous, overwritePlan: null}))
									}
								>
									Create a copy
								</RadioButton>
								{!overwriteExisting ? (
									<div
										style={{
											...sectionStyle,
											paddingLeft: 28,
											maxWidth: 320,
											minWidth: 0,
										}}
									>
										<RemotionInput
											rightAlign={false}
											status={installationNameError ? 'error' : 'ok'}
											id="element-install-name"
											name="installationName"
											aria-label="Installation name"
											placeholder="New name"
											value={installationName}
											disabled={submitting}
											onChange={(event) => {
												const name = event.target.value;
												setInput((previous) => ({
													...previous,
													name,
													overwritePlan: null,
												}));
											}}
										/>
										{installationNameError ? (
											<ValidationMessage
												align="flex-start"
												message={installationNameError}
												type="error"
											/>
										) : null}
									</div>
								) : null}
								<div style={sectionStyle}>
									<RadioButton
										checked={overwriteExisting}
										disabled={submitting}
										onClick={() =>
											setInput((previous) => ({
												...previous,
												overwritePlan: existingDestination.plan,
											}))
										}
									>
										Replace existing
									</RadioButton>
									<p
										style={{
											...warningDescriptionStyle,
											paddingLeft: 28,
											overflowWrap: 'anywhere',
										}}
									>
										{existingDestination.plan.filePath}
									</p>
									{overwriteExisting ? (
										<p style={{...overwriteStyle, paddingLeft: 28}}>
											Overwrites customizations for all usages.
										</p>
									) : null}
								</div>
							</div>
						) : !activePlan && planError ? (
							<ValidationMessage
								align="flex-start"
								message={planError}
								type="error"
							/>
						) : null}
					</section>

					{missingPackages.length > 0 ? (
						<section
							style={requestSourceStyle}
							aria-labelledby="element-install-dependencies"
						>
							<h3 id="element-install-dependencies" style={sectionTitleStyle}>
								Packages to install
							</h3>
							<ul style={dependencyListStyle} role="list">
								{missingPackages.map((packageName) => (
									<li key={packageName} style={dependencyNameStyle}>
										{packageName}
									</li>
								))}
							</ul>
						</section>
					) : null}

					{isOfficialRemotionSource ? null : (
						<div style={warningStyle}>
							<WarningTriangle style={warningIconStyle} />
							<p style={warningDescriptionStyle}>
								Installed code can access your files and network.
							</p>
						</div>
					)}

					<details style={sourceDetailsStyle}>
						<summary
							className={`${HOVERABLE_CLASS_NAME} ${FOCUS_VISIBLE_ONLY_CLASS_NAME}`}
							style={sourceSummaryStyle}
						>
							Source code
						</summary>
						<div style={sourceCodeContainerStyle}>
							<pre
								className={`language-tsx ${HORIZONTAL_SCROLLBAR_CLASSNAME} ${VERTICAL_SCROLLBAR_CLASSNAME} ${FOCUS_VISIBLE_ONLY_CLASS_NAME}`}
								style={sourceCodeBlockStyle}
							>
								<code style={sourceCodeStyle}>
									<LazySyntaxHighlightedSource source={sourcePreview} />
								</code>
							</pre>
						</div>
					</details>
				</div>
				<ModalFooterContainer style={footerStyle}>
					<Row align="center">
						<Flex />
						<Button disabled={submitting} onClick={cancel} style={cancelStyle}>
							Cancel
						</Button>
						<Spacing x={1} />
						<ModalButton
							autoFocus={mode === 'current-composition'}
							disabled={!canSubmit}
							onClick={submit}
						>
							{submitting
								? 'Installing…'
								: overwriteExisting
									? 'Replace and insert'
									: 'Install'}
							<ShortcutHint keyToPress="↵" cmdOrCtrl={false} />
						</ModalButton>
					</Row>
				</ModalFooterContainer>
			</form>
		</ModalContainer>,
		getPortal(currentZIndex),
	);
};