Skip to content

🎥 Make videos programmatically with React

Collapse inserted elements before timeline paint

Igor Samokhovets6c0a396c2861
diff --git a/packages/studio/src/components/Timeline/Timeline.tsx b/packages/studio/src/components/Timeline/Timeline.tsx
index 511baf07f4..eeddeeb608 100644
--- a/packages/studio/src/components/Timeline/Timeline.tsx
+++ b/packages/studio/src/components/Timeline/Timeline.tsx
@@ -2,7 +2,7 @@ import type {InsertJsxElementRequest} from '@remotion/studio-shared';
import React, {
useCallback,
useContext,
- useEffect,
+ useLayoutEffect,
useMemo,
useRef,
useState,
@@ -297,6 +297,54 @@ const TimelineInner: React.FC = () => {
overrideIdToNodePathMappings,
compositions,
]);
+ const durationInFrames = videoConfig?.durationInFrames ?? 0;
+
+ const {getDragOverrides} = useContext(
+ Internals.VisualModeDragOverridesContext,
+ );
+ const filtered = useMemo(() => {
+ return timeline.filter((t) => {
+ // Moving outside the composition can reduce the displayed duration to
+ // zero. Keep the drag owner mounted until its pending edit is saved.
+ if (
+ t.sequence.showInTimeline &&
+ t.nodePathInfo !== null &&
+ getDragOverrides(t.nodePathInfo.sequenceSubscriptionKey).from !==
+ undefined
+ ) {
+ return true;
+ }
+
+ return shouldShowTrackInTimeline(t, durationInFrames);
+ });
+ }, [durationInFrames, getDragOverrides, timeline]);
+
+ // Keep `filtered` complete so a future toggle can show every programmatic
+ // instance without recalculating the timeline or losing its instance index.
+ const collapsed = useMemo(() => {
+ const seenNodePaths = new Set<string>();
+ return filtered.filter((track) => {
+ if (track.nodePathInfo === null) {
+ return true;
+ }
+
+ const key = timelineSequenceNodePathToKey(
+ track.nodePathInfo.sequenceSubscriptionKey,
+ );
+ if (seenNodePaths.has(key)) {
+ return false;
+ }
+
+ seenNodePaths.add(key);
+ return true;
+ });
+ }, [filtered]);
+
+ const {visibleTracks, value: layerChildrenValue} = useTimelineLayerChildren(
+ collapsed,
+ sequences,
+ canvasContent?.type === 'composition' ? canvasContent.compositionId : null,
+ );
const pendingInsertedElementSelection = useSyncExternalStore(
subscribeToInsertedElementSelection,
getInsertedElementSelection,
@@ -309,21 +357,19 @@ const TimelineInner: React.FC = () => {
existingSequenceIds: Set<string>;
} | null>(null);
const {selectItems} = useTimelineSelection();
- useEffect(() => {
+ useLayoutEffect(() => {
if (pendingInsertedElementSelection === null) {
pendingSelectionStart.current = null;
return;
}
const matchesInsertedNodePath = (track: TimelineTrackData) =>
- track.nodePathInfo !== null &&
- (pendingInsertedElementSelection.nodePath === null ||
- (track.nodePathInfo.sequenceSubscriptionKey.absolutePath ===
+ pendingInsertedElementSelection.nodePath === null ||
+ (track.nodePathInfo !== null &&
+ track.nodePathInfo.sequenceSubscriptionKey.absolutePath ===
pendingInsertedElementSelection.nodePath.absolutePath &&
- JSON.stringify(
- track.nodePathInfo.sequenceSubscriptionKey.nodePath,
- ) ===
- JSON.stringify(pendingInsertedElementSelection.nodePath.nodePath)));
+ JSON.stringify(track.nodePathInfo.sequenceSubscriptionKey.nodePath) ===
+ JSON.stringify(pendingInsertedElementSelection.nodePath.nodePath));
if (
pendingSelectionStart.current?.selection !==
@@ -361,7 +407,16 @@ const TimelineInner: React.FC = () => {
track.sequence.id,
),
);
- if (!insertedTrack || insertedTrack.nodePathInfo === null) {
+ if (!insertedTrack) {
+ return;
+ }
+
+ const layerKey = layerChildrenValue.keys.get(insertedTrack.sequence.id);
+ if (layerKey !== undefined && !layerChildrenValue.collapsed[layerKey]) {
+ layerChildrenValue.toggle(layerKey);
+ }
+
+ if (insertedTrack.nodePathInfo === null) {
return;
}
@@ -376,59 +431,12 @@ const TimelineInner: React.FC = () => {
}, [
canvasContent,
fastRefreshes,
+ layerChildrenValue,
pendingInsertedElementSelection,
selectItems,
timeline,
]);
- const durationInFrames = videoConfig?.durationInFrames ?? 0;
-
- const {getDragOverrides} = useContext(
- Internals.VisualModeDragOverridesContext,
- );
- const filtered = useMemo(() => {
- return timeline.filter((t) => {
- // Moving outside the composition can reduce the displayed duration to
- // zero. Keep the drag owner mounted until its pending edit is saved.
- if (
- t.sequence.showInTimeline &&
- t.nodePathInfo !== null &&
- getDragOverrides(t.nodePathInfo.sequenceSubscriptionKey).from !==
- undefined
- ) {
- return true;
- }
-
- return shouldShowTrackInTimeline(t, durationInFrames);
- });
- }, [durationInFrames, getDragOverrides, timeline]);
-
- // Keep `filtered` complete so a future toggle can show every programmatic
- // instance without recalculating the timeline or losing its instance index.
- const collapsed = useMemo(() => {
- const seenNodePaths = new Set<string>();
- return filtered.filter((track) => {
- if (track.nodePathInfo === null) {
- return true;
- }
-
- const key = timelineSequenceNodePathToKey(
- track.nodePathInfo.sequenceSubscriptionKey,
- );
- if (seenNodePaths.has(key)) {
- return false;
- }
-
- seenNodePaths.add(key);
- return true;
- });
- }, [filtered]);
-
- const {visibleTracks, value: layerChildrenValue} = useTimelineLayerChildren(
- collapsed,
- sequences,
- canvasContent?.type === 'composition' ? canvasContent.compositionId : null,
- );
const maxTimelineTracks = getStudioMaxTimelineTracks();
const shown = useMemo(() => {
return maxTimelineTracks !== null &&