Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Add alignment controls to inspector (#8964)

Shashank Singhea9b9d79f177
diff --git a/packages/studio/src/components/InspectorPanel/AlignmentControls.tsx b/packages/studio/src/components/InspectorPanel/AlignmentControls.tsx
new file mode 100644
index 0000000000..dc3955f70f
--- /dev/null
+++ b/packages/studio/src/components/InspectorPanel/AlignmentControls.tsx
@@ -0,0 +1,313 @@
+import React, {useCallback, useContext, useMemo} from 'react';
+import {Internals, type CanUpdateSequencePropStatus} from 'remotion';
+import {StudioServerConnectionCtx} from '../../helpers/client-id';
+import type {TrackWithHash} from '../../helpers/get-timeline-sequence-sort-key';
+import {AlignBottomIcon} from '../../icons/align-bottom';
+import {AlignCenterHorizontalIcon} from '../../icons/align-center-horizontal';
+import {AlignCenterVerticalIcon} from '../../icons/align-center-vertical';
+import {AlignLeftIcon} from '../../icons/align-left';
+import {AlignRightIcon} from '../../icons/align-right';
+import {AlignTopIcon} from '../../icons/align-top';
+import {InlineAction} from '../InlineAction';
+import {INSPECTOR_PANEL_HORIZONTAL_PADDING} from '../InspectorPanelLayout';
+import {getSelectedOutlineActiveSchema} from '../selected-outline-drag';
+import {translateFieldKey} from '../selected-outline-types';
+import {callAddSequenceKeyframe} from '../Timeline/call-add-keyframe';
+import {saveSequenceProps} from '../Timeline/save-sequence-prop';
+import {
+ parseTranslate,
+ serializeTranslate,
+} from '../Timeline/timeline-translate-utils';
+import {computeAlignedTranslate} from './alignment-controls';
+
+const isPropStatusDraggable = (
+ propStatus: CanUpdateSequencePropStatus | undefined,
+) => {
+ return (
+ !propStatus ||
+ propStatus.status === 'static' ||
+ (propStatus.status === 'keyframed' &&
+ propStatus.interpolationFunction === 'interpolate')
+ );
+};
+
+const container: React.CSSProperties = {
+ display: 'flex',
+ flexDirection: 'row',
+ gap: 4,
+ padding: `0 ${INSPECTOR_PANEL_HORIZONTAL_PADDING}px 12px`,
+ alignItems: 'center',
+};
+
+const iconStyle: React.CSSProperties = {
+ width: 16,
+ height: 16,
+};
+
+const verticalDivider: React.CSSProperties = {
+ width: 1,
+ height: 16,
+ backgroundColor: 'rgba(255, 255, 255, 0.1)',
+ margin: '0 8px',
+};
+
+const AlignmentButton: React.FC<{
+ readonly onClick: () => void;
+ readonly title: string;
+ readonly Icon: React.FC<React.SVGProps<SVGSVGElement>>;
+ readonly disabled: boolean;
+}> = ({onClick, title, Icon, disabled}) => {
+ return (
+ <InlineAction
+ title={title}
+ onClick={onClick}
+ renderAction={(color) => <Icon style={iconStyle} color={color} />}
+ disabled={disabled}
+ />
+ );
+};
+
+export const AlignmentControls: React.FC<{
+ readonly track: TrackWithHash;
+}> = ({track}) => {
+ const {previewServerState} = useContext(StudioServerConnectionCtx);
+ const {propStatuses} = useContext(Internals.VisualModePropStatusesContext);
+ const {getDragOverrides} = useContext(
+ Internals.VisualModeDragOverridesContext,
+ );
+ const {setPropStatuses} = useContext(Internals.VisualModeSettersContext);
+ const {canvasContent, compositions} = useContext(
+ Internals.CompositionManager,
+ );
+ const currentCompositionMetadata = useMemo(() => {
+ if (canvasContent?.type !== 'composition') return null;
+ return (
+ compositions.find((c) => c.id === canvasContent.compositionId) ?? null
+ );
+ }, [canvasContent, compositions]);
+
+ const timelinePosition = Internals.Timeline.useTimelinePosition();
+
+ const handleAlign = useCallback(
+ (
+ direction: 'left' | 'center-h' | 'right' | 'top' | 'center-v' | 'bottom',
+ ) => {
+ if (
+ previewServerState.type !== 'connected' ||
+ !track.nodePathInfo ||
+ !track.sequence.controls
+ ) {
+ return;
+ }
+
+ const ref = track.sequence.refForOutline?.current;
+ if (!ref) {
+ return;
+ }
+
+ const nodePath = track.nodePathInfo.sequenceSubscriptionKey;
+ const nodePropStatuses = Internals.getPropStatusesCtx(
+ propStatuses,
+ nodePath,
+ );
+ const sourceFrame = timelinePosition - track.keyframeDisplayOffset;
+ const dragOverrides = getDragOverrides(nodePath) ?? {};
+
+ const activeSchema = getSelectedOutlineActiveSchema({
+ schema: track.sequence.controls.schema,
+ currentRuntimeValueDotNotation:
+ track.sequence.controls.currentRuntimeValueDotNotation,
+ dragOverrides,
+ propStatus: nodePropStatuses,
+ frame: sourceFrame,
+ });
+
+ const fieldSchema = activeSchema?.[translateFieldKey];
+ const propStatus = nodePropStatuses?.[translateFieldKey];
+
+ if (fieldSchema?.type !== 'translate') {
+ return;
+ }
+
+ if (!isPropStatusDraggable(propStatus)) {
+ return;
+ }
+
+ let elementRect: DOMRect;
+ try {
+ elementRect = ref.getBoundingClientRect();
+ } catch {
+ return;
+ }
+
+ if (!currentCompositionMetadata?.width) {
+ return;
+ }
+
+ const compositionRect = Internals.portalNode().getBoundingClientRect();
+ if (compositionRect.width === 0 || compositionRect.height === 0) {
+ return;
+ }
+
+ const scale = compositionRect.width / currentCompositionMetadata.width;
+
+ const currentTranslate = propStatus
+ ? String(
+ Internals.getEffectiveVisualModeValue({
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ propStatus: propStatus as any,
+ dragOverrideValue: dragOverrides[translateFieldKey],
+ defaultValue: fieldSchema.default,
+ frame: sourceFrame,
+ shouldResortToDefaultValueIfUndefined: true,
+ }) ??
+ fieldSchema.default ??
+ '0px 0px',
+ )
+ : String(fieldSchema.default ?? '0px 0px');
+
+ const [currentX, currentY] = parseTranslate(currentTranslate);
+
+ const {x: newX, y: newY} = computeAlignedTranslate({
+ direction,
+ elementRect,
+ compositionRect,
+ currentTranslateX: currentX,
+ currentTranslateY: currentY,
+ scale,
+ });
+
+ const newValue = serializeTranslate(newX, newY);
+
+ const undoLabels = {
+ left: 'Align left',
+ 'center-h': 'Align center horizontally',
+ right: 'Align right',
+ top: 'Align top',
+ 'center-v': 'Align center vertically',
+ bottom: 'Align bottom',
+ };
+
+ const label = undoLabels[direction];
+
+ if (!propStatus || propStatus.status === 'static') {
+ saveSequenceProps({
+ changes: [
+ {
+ fileName: nodePath.absolutePath,
+ nodePath,
+ fieldKey: translateFieldKey,
+ value: newValue,
+ defaultValue:
+ fieldSchema.default !== undefined
+ ? JSON.stringify(fieldSchema.default)
+ : null,
+ schema: track.sequence.controls.schema,
+ },
+ ],
+ setPropStatuses,
+ clientId: previewServerState.clientId,
+ undoLabel: label,
+ redoLabel: label,
+ });
+ } else {
+ callAddSequenceKeyframe({
+ fileName: nodePath.absolutePath,
+ nodePath,
+ fieldKey: translateFieldKey,
+ sourceFrame,
+ value: newValue,
+ schema: track.sequence.controls.schema,
+ setPropStatuses,
+ clientId: previewServerState.clientId,
+ });
+ }
+ },
+ [
+ previewServerState,
+ track,
+ currentCompositionMetadata,
+ timelinePosition,
+ propStatuses,
+ getDragOverrides,
+ setPropStatuses,
+ ],
+ );
+
+ if (
+ previewServerState.type !== 'connected' ||
+ !track.nodePathInfo ||
+ !track.sequence.controls ||
+ !currentCompositionMetadata
+ ) {
+ return null;
+ }
+
+ const renderNodePath = track.nodePathInfo.sequenceSubscriptionKey;
+ const renderNodePropStatuses = Internals.getPropStatusesCtx(
+ propStatuses,
+ renderNodePath,
+ );
+ const renderSourceFrame = timelinePosition - track.keyframeDisplayOffset;
+ const renderDragOverrides = getDragOverrides(renderNodePath) ?? {};
+
+ const renderActiveSchema = getSelectedOutlineActiveSchema({
+ schema: track.sequence.controls.schema,
+ currentRuntimeValueDotNotation:
+ track.sequence.controls.currentRuntimeValueDotNotation,
+ dragOverrides: renderDragOverrides,
+ propStatus: renderNodePropStatuses,
+ frame: renderSourceFrame,
+ });
+
+ const renderFieldSchema = renderActiveSchema?.[translateFieldKey];
+ const renderPropStatus = renderNodePropStatuses?.[translateFieldKey];
+
+ if (renderFieldSchema?.type !== 'translate') {
+ return null;
+ }
+
+ const alignmentDisabled = !isPropStatusDraggable(renderPropStatus);
+
+ return (
+ <div style={container}>
+ <AlignmentButton
+ title="Align left"
+ onClick={() => handleAlign('left')}
+ Icon={AlignLeftIcon}
+ disabled={alignmentDisabled}
+ />
+ <AlignmentButton
+ title="Align center horizontally"
+ onClick={() => handleAlign('center-h')}
+ Icon={AlignCenterHorizontalIcon}
+ disabled={alignmentDisabled}
+ />
+ <AlignmentButton
+ title="Align right"
+ onClick={() => handleAlign('right')}
+ Icon={AlignRightIcon}
+ disabled={alignmentDisabled}
+ />
+ <div style={verticalDivider} />
+ <AlignmentButton
+ title="Align top"
+ onClick={() => handleAlign('top')}
+ Icon={AlignTopIcon}
+ disabled={alignmentDisabled}
+ />
+ <AlignmentButton
+ title="Align center vertically"
+ onClick={() => handleAlign('center-v')}
+ Icon={AlignCenterVerticalIcon}
+ disabled={alignmentDisabled}
+ />
+ <AlignmentButton
+ title="Align bottom"
+ onClick={() => handleAlign('bottom')}
+ Icon={AlignBottomIcon}
+ disabled={alignmentDisabled}
+ />
+ </div>
+ );
+};
diff --git a/packages/studio/src/components/InspectorPanel/SequenceSelectionInspector.tsx b/packages/studio/src/components/InspectorPanel/SequenceSelectionInspector.tsx
index 11b9b74dbd..dcef764a1b 100644
--- a/packages/studio/src/components/InspectorPanel/SequenceSelectionInspector.tsx
+++ b/packages/studio/src/components/InspectorPanel/SequenceSelectionInspector.tsx
@@ -11,6 +11,7 @@ import {
type TimelineSelection,
useTimelineSelection,
} from '../Timeline/TimelineSelection';
+import {AlignmentControls} from './AlignmentControls';
import {InspectorMessage, InspectorSectionHeader} from './common';
import type {SequenceSectionSelection} from './inspector-selection';
import {
@@ -90,6 +91,7 @@ const SequenceExpandedInspector: React.FC<{
renderSectionHeader={(children) => (
<InspectorSectionHeader>{children}</InspectorSectionHeader>
)}
+ renderTransformControls={() => <AlignmentControls track={track} />}
/>
</div>
);
diff --git a/packages/studio/src/components/InspectorPanel/alignment-controls.ts b/packages/studio/src/components/InspectorPanel/alignment-controls.ts
new file mode 100644
index 0000000000..27f4ccd4c4
--- /dev/null
+++ b/packages/studio/src/components/InspectorPanel/alignment-controls.ts
@@ -0,0 +1,65 @@
+export type AlignmentDirection =
+ | 'left'
+ | 'center-h'
+ | 'right'
+ | 'top'
+ | 'center-v'
+ | 'bottom';
+
+export const computeAlignedTranslate = ({
+ direction,
+ elementRect,
+ compositionRect,
+ currentTranslateX,
+ currentTranslateY,
+ scale,
+}: {
+ readonly direction: AlignmentDirection;
+ readonly elementRect: DOMRect;
+ readonly compositionRect: DOMRect;
+ readonly currentTranslateX: number;
+ readonly currentTranslateY: number;
+ readonly scale: number;
+}): {x: number; y: number} => {
+ let deltaX = 0;
+ let deltaY = 0;
+
+ // Note: elementRect and compositionRect are in screen space (pixels).
+ // We divide by scale to convert back to composition space.
+
+ switch (direction) {
+ case 'left':
+ deltaX = compositionRect.left - elementRect.left;
+ break;
+ case 'center-h': {
+ const compCenter = compositionRect.left + compositionRect.width / 2;
+ const elemCenter = elementRect.left + elementRect.width / 2;
+ deltaX = compCenter - elemCenter;
+ break;
+ }
+
+ case 'right':
+ deltaX = compositionRect.right - elementRect.right;
+ break;
+ case 'top':
+ deltaY = compositionRect.top - elementRect.top;
+ break;
+ case 'center-v': {
+ const compCenter = compositionRect.top + compositionRect.height / 2;
+ const elemCenter = elementRect.top + elementRect.height / 2;
+ deltaY = compCenter - elemCenter;
+ break;
+ }
+
+ case 'bottom':
+ deltaY = compositionRect.bottom - elementRect.bottom;
+ break;
+ default:
+ break;
+ }
+
+ return {
+ x: currentTranslateX + deltaX / scale,
+ y: currentTranslateY + deltaY / scale,
+ };
+};
diff --git a/packages/studio/src/components/InspectorSequenceSection.tsx b/packages/studio/src/components/InspectorSequenceSection.tsx
index 8395474787..c99b37476b 100644
--- a/packages/studio/src/components/InspectorSequenceSection.tsx
+++ b/packages/studio/src/components/InspectorSequenceSection.tsx
@@ -155,12 +155,14 @@ export const InspectorSequenceSection: React.FC<{
readonly nodePathInfo: SequenceNodePathInfo;
readonly keyframeDisplayOffset: number;
readonly renderSectionHeader: (children: React.ReactNode) => React.ReactNode;
+ readonly renderTransformControls: () => React.ReactNode;
}> = ({
sequence,
validatedLocation,
nodePathInfo,
keyframeDisplayOffset,
renderSectionHeader,
+ renderTransformControls,
}) => {
const {tree} = useTimelineExpandedTree({
sequence,
@@ -315,6 +317,7 @@ export const InspectorSequenceSection: React.FC<{
<React.Fragment key={group.id}>
{i === 0 ? null : <div style={controlsEffectsDivider} />}
{renderSectionHeader(group.label)}
+ {group.id === 'transforms' ? renderTransformControls() : null}
{group.rows.map(renderRow)}
</React.Fragment>
))}
diff --git a/packages/studio/src/components/Preview.tsx b/packages/studio/src/components/Preview.tsx
index 62a43646a8..b6a40680ac 100644
--- a/packages/studio/src/components/Preview.tsx
+++ b/packages/studio/src/components/Preview.tsx
@@ -231,7 +231,7 @@ const CompWhenItHasDimensions: React.FC<{
}
return (
- <div style={outer}>
+ <div className="remotion-studio-composition-container" style={outer}>
<PortalContainer
contentDimensions={contentDimensions as Dimensions}
scale={scale}
diff --git a/packages/studio/src/icons/align-bottom.tsx b/packages/studio/src/icons/align-bottom.tsx
new file mode 100644
index 0000000000..1be127eca7
--- /dev/null
+++ b/packages/studio/src/icons/align-bottom.tsx
@@ -0,0 +1,15 @@
+import type {SVGProps} from 'react';
+import React from 'react';
+import {AlignRightIcon} from './align-right';
+
+export const AlignBottomIcon: React.FC<SVGProps<SVGSVGElement>> = (props) => {
+ return (
+ <AlignRightIcon
+ {...props}
+ style={{
+ ...props.style,
+ transform: 'rotate(90deg)',
+ }}
+ />
+ );
+};
diff --git a/packages/studio/src/icons/align-center-horizontal.tsx b/packages/studio/src/icons/align-center-horizontal.tsx
new file mode 100644
index 0000000000..af63720b00
--- /dev/null
+++ b/packages/studio/src/icons/align-center-horizontal.tsx
@@ -0,0 +1,26 @@
+import type {SVGProps} from 'react';
+import React from 'react';
+
+export const AlignCenterHorizontalIcon: React.FC<SVGProps<SVGSVGElement>> = (
+ props,
+) => {
+ const color = props.color ?? 'currentColor';
+
+ return (
+ <svg
+ {...props}
+ viewBox="0 0 16 16"
+ fill="none"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <path
+ d="M8 2V14"
+ stroke={color}
+ strokeOpacity="0.5"
+ strokeLinecap="square"
+ />
+ <line x1="3" y1="6" x2="12" y2="6" stroke={color} strokeWidth="2" />
+ <line x1="4" y1="10" x2="11" y2="10" stroke={color} strokeWidth="2" />
+ </svg>
+ );
+};
diff --git a/packages/studio/src/icons/align-center-vertical.tsx b/packages/studio/src/icons/align-center-vertical.tsx
new file mode 100644
index 0000000000..731dd23a8a
--- /dev/null
+++ b/packages/studio/src/icons/align-center-vertical.tsx
@@ -0,0 +1,17 @@
+import type {SVGProps} from 'react';
+import React from 'react';
+import {AlignCenterHorizontalIcon} from './align-center-horizontal';
+
+export const AlignCenterVerticalIcon: React.FC<SVGProps<SVGSVGElement>> = (
+ props,
+) => {
+ return (
+ <AlignCenterHorizontalIcon
+ {...props}
+ style={{
+ ...props.style,
+ transform: 'rotate(90deg)',
+ }}
+ />
+ );
+};
diff --git a/packages/studio/src/icons/align-left.tsx b/packages/studio/src/icons/align-left.tsx
new file mode 100644
index 0000000000..5a74adcad8
--- /dev/null
+++ b/packages/studio/src/icons/align-left.tsx
@@ -0,0 +1,19 @@
+import type {SVGProps} from 'react';
+import React from 'react';
+
+export const AlignLeftIcon: React.FC<SVGProps<SVGSVGElement>> = (props) => {
+ const color = props.color ?? 'currentColor';
+
+ return (
+ <svg {...props} viewBox="0 0 16 16" fill="none">
+ <path
+ d="M2 2L2 14"
+ stroke={color}
+ strokeOpacity="0.5"
+ strokeLinecap="square"
+ />
+ <line x1="4" y1="6" x2="13" y2="6" stroke={color} strokeWidth="2" />
+ <line x1="4" y1="10" x2="11" y2="10" stroke={color} strokeWidth="2" />
+ </svg>
+ );
+};
diff --git a/packages/studio/src/icons/align-right.tsx b/packages/studio/src/icons/align-right.tsx
new file mode 100644
index 0000000000..9f05b52949
--- /dev/null
+++ b/packages/studio/src/icons/align-right.tsx
@@ -0,0 +1,19 @@
+import type {SVGProps} from 'react';
+import React from 'react';
+
+export const AlignRightIcon: React.FC<SVGProps<SVGSVGElement>> = (props) => {
+ const color = props.color ?? 'currentColor';
+
+ return (
+ <svg {...props} viewBox="0 0 16 16" fill="none">
+ <path
+ d="M14 2V14"
+ stroke={color}
+ strokeOpacity="0.5"
+ strokeLinecap="square"
+ />
+ <line x1="3" y1="6" x2="12" y2="6" stroke={color} strokeWidth="2" />
+ <line x1="5" y1="10" x2="12" y2="10" stroke={color} strokeWidth="2" />
+ </svg>
+ );
+};
diff --git a/packages/studio/src/icons/align-top.tsx b/packages/studio/src/icons/align-top.tsx
new file mode 100644
index 0000000000..7c43d2417a
--- /dev/null
+++ b/packages/studio/src/icons/align-top.tsx
@@ -0,0 +1,15 @@
+import type {SVGProps} from 'react';
+import React from 'react';
+import {AlignLeftIcon} from './align-left';
+
+export const AlignTopIcon: React.FC<SVGProps<SVGSVGElement>> = (props) => {
+ return (
+ <AlignLeftIcon
+ {...props}
+ style={{
+ ...props.style,
+ transform: 'rotate(90deg)',
+ }}
+ />
+ );
+};