Skip to content

🎥 Make videos programmatically with React

Slide between Series child labels and dim inactive content

Jonny Burgera698d4bd1079
diff --git a/packages/studio/src/components/InspectorPanel/SelectedInspector.tsx b/packages/studio/src/components/InspectorPanel/SelectedInspector.tsx
index b9190ac432..2e82bd89ea 100644
--- a/packages/studio/src/components/InspectorPanel/SelectedInspector.tsx
+++ b/packages/studio/src/components/InspectorPanel/SelectedInspector.tsx
@@ -1,5 +1,5 @@
import React, {useLayoutEffect, useRef, useState} from 'react';
-import {flushSync} from 'react-dom';
+import {startSlideViewTransition} from '../../helpers/slide-view-transition';
import {
getTimelineSequenceSelectionKey,
type TimelineSelection,
@@ -80,10 +80,7 @@ export const SelectedInspector: React.FC<{
(!forward && !backward) ||
getTimelineSequenceSelectionKey(previous.nodePathInfo) !==
getTimelineSequenceSelectionKey(selection.nodePathInfo) ||
- panel === null ||
- typeof document.startViewTransition !== 'function' ||
- typeof flushSync !== 'function' ||
- window.matchMedia('(prefers-reduced-motion: reduce)').matches
+ panel === null
) {
displayedSelectionRef.current = selection;
setDisplayedSelection(selection);
@@ -91,49 +88,22 @@ export const SelectedInspector: React.FC<{
}
// Defer only the inspector DOM swap; timeline selection stays synchronous.
- const root = document.documentElement;
- const transitionClass = forward
- ? '__remotion-inspector-forward'
- : '__remotion-inspector-backward';
- root.classList.add(transitionClass);
- panel.style.setProperty('view-transition-name', 'remotion-inspector');
- let cancelled = false;
-
- const cleanup = () => {
- root.classList.remove(transitionClass);
- panel.style.removeProperty('view-transition-name');
+ const update = () => {
+ displayedSelectionRef.current = selection;
+ setDisplayedSelection(selection);
};
- const transition = document.startViewTransition(() => {
- if (cancelled) {
- return;
- }
-
- flushSync(() => {
- displayedSelectionRef.current = selection;
- setDisplayedSelection(selection);
- });
+ const cancel = startSlideViewTransition({
+ panels: [{element: panel, name: 'remotion-inspector', clipY: null}],
+ direction: forward ? 'forward' : 'backward',
+ update,
});
- // A skipped transition still applies its update, but rejects `ready`.
- transition.ready.catch(() => undefined);
- transition.finished.then(
- () => {
- if (!cancelled) {
- cleanup();
- }
- },
- () => {
- if (!cancelled) {
- cleanup();
- }
- },
- );
+ if (cancel === null) {
+ update();
+ return;
+ }
- return () => {
- cancelled = true;
- transition.skipTransition();
- cleanup();
- };
+ return cancel;
}, [selection]);
return (
diff --git a/packages/studio/src/components/Timeline/TimelineList.tsx b/packages/studio/src/components/Timeline/TimelineList.tsx
index f45672cf55..3291e6f88f 100644
--- a/packages/studio/src/components/Timeline/TimelineList.tsx
+++ b/packages/studio/src/components/Timeline/TimelineList.tsx
@@ -1,8 +1,11 @@
-import React from 'react';
+import React, {useLayoutEffect, useMemo, useRef, useState} from 'react';
import {Internals} from 'remotion';
import {BACKGROUND, TIMELINE_TRACK_SEPARATOR} from '../../helpers/colors';
+import {startSlideViewTransition} from '../../helpers/slide-view-transition';
+import {timelineVerticalScroll} from './timeline-refs';
import {TIMELINE_PACKED_AUXILIARY_ROW_HEIGHT} from './timeline-track-groups';
import {TimelineSequenceItem} from './TimelineSequenceItem';
+import {TIMELINE_TIME_INDICATOR_HEIGHT} from './TimelineTimeIndicators';
import {
type TimelineVirtualRow,
useTimelineVirtualization,
@@ -63,25 +66,165 @@ const TimelineListTrack: React.FC<{
);
});
-// Only labels follow the playhead. The rows and the bars keep their layout.
-const TimelineSceneListTrack: React.FC<
- React.ComponentProps<typeof TimelineListTrack>
-> = React.memo((props) => {
- const {row} = props;
+type SceneListGroup = {
+ readonly id: string;
+ start: number;
+ end: number;
+ readonly items: React.ComponentProps<typeof TimelineListTrack>[];
+};
+
+// Only this child-label surface follows the playhead. Keep a view until the
+// browser captures it, then swap all changing Series together in one transition.
+const TimelineSceneList: React.FC<{
+ readonly groups: readonly SceneListGroup[];
+}> = React.memo(({groups}) => {
const frame = Internals.Timeline.useTimelinePosition();
- if (
- row.sceneRange === null ||
- frame < row.sceneRange.from ||
- frame >= row.sceneRange.end
- ) {
- return null;
- }
-
- return <TimelineListTrack {...props} />;
+ const lastFrame = useRef(frame);
+ const panels = useRef(new Map<string, HTMLDivElement>());
+ const view = groups.map((group) => ({
+ id: group.id,
+ rows: group.items
+ .filter(
+ ({row}) =>
+ row.sceneRange !== null &&
+ frame >= row.sceneRange.from &&
+ frame < row.sceneRange.end,
+ )
+ .map(({row}) => row.track.sequence.id),
+ }));
+ const key = JSON.stringify(view);
+ const [displayedView, setDisplayedView] = useState(view);
+ const displayedViewRef = useRef(view);
+ const latest = useRef({view, frame, previousFrame: frame});
+ latest.current = {view, frame, previousFrame: lastFrame.current};
+
+ useLayoutEffect(() => {
+ const next = latest.current;
+ const previous = displayedViewRef.current;
+ const scrollBounds =
+ timelineVerticalScroll.current?.getBoundingClientRect();
+ const changingPanels = next.view.flatMap((group, index) => {
+ const old = previous.find((item) => item.id === group.id);
+ const element = panels.current.get(group.id);
+ if (
+ !element ||
+ !scrollBounds ||
+ !old ||
+ old.rows.length === 0 ||
+ group.rows.length === 0 ||
+ JSON.stringify(old.rows) === JSON.stringify(group.rows)
+ ) {
+ return [];
+ }
+
+ // View-transition snapshots escape ancestor overflow. Clip the overlay
+ // to the visible list below the pinned ruler, including while scrolled.
+ const bounds = element.getBoundingClientRect();
+ const top = Math.max(
+ 0,
+ scrollBounds.top + TIMELINE_TIME_INDICATOR_HEIGHT - bounds.top,
+ );
+ const bottom = Math.max(0, bounds.bottom - scrollBounds.bottom);
+ if (top + bottom >= bounds.height) {
+ return [];
+ }
+
+ return [
+ {
+ element,
+ name: `remotion-timeline-list-${index}`,
+ clipY: {top, bottom},
+ },
+ ];
+ });
+ const update = () => {
+ displayedViewRef.current = next.view;
+ setDisplayedView(next.view);
+ };
+
+ // Mounting, Activity discovery and layout edits are immediate. Animate
+ // only a playhead crossing between two discovered scene views.
+ if (changingPanels.length === 0 || next.frame === next.previousFrame) {
+ update();
+ return;
+ }
+
+ const cancel = startSlideViewTransition({
+ panels: changingPanels,
+ direction: next.frame > next.previousFrame ? 'forward' : 'backward',
+ update,
+ });
+ if (cancel === null) {
+ update();
+ return;
+ }
+
+ return cancel;
+ }, [key]);
+ useLayoutEffect(() => {
+ lastFrame.current = frame;
+ }, [frame]);
+
+ return groups.map((group) => {
+ const activeRows =
+ displayedView.find((item) => item.id === group.id)?.rows ?? [];
+ return (
+ <div
+ key={group.id}
+ ref={(element) => {
+ if (element === null) {
+ panels.current.delete(group.id);
+ } else {
+ panels.current.set(group.id, element);
+ }
+ }}
+ style={{
+ position: 'absolute',
+ top: group.start,
+ left: 0,
+ width: '100%',
+ height: group.end - group.start,
+ }}
+ >
+ {group.items
+ .filter(({row}) => activeRows.includes(row.track.sequence.id))
+ .map(({row, start, height}) => (
+ <TimelineListTrack
+ key={row.track.sequence.id}
+ row={row}
+ start={start - group.start}
+ height={height}
+ />
+ ))}
+ </div>
+ );
+ });
});
export const TimelineList: React.FC = () => {
const {rows, tracksEnd, virtualItems} = useTimelineVirtualization();
+ const sceneGroups = useMemo(() => {
+ const groups = new Map<string, SceneListGroup>();
+ for (const item of virtualItems) {
+ const row = rows[item.index];
+ if (row.sceneGroupId === null) {
+ continue;
+ }
+
+ const group = groups.get(row.sceneGroupId) ?? {
+ id: row.sceneGroupId,
+ start: item.start,
+ end: item.end,
+ items: [],
+ };
+ group.start = Math.min(group.start, item.start);
+ group.end = Math.max(group.end, item.end);
+ group.items.push({row, start: item.start, height: item.size});
+ groups.set(row.sceneGroupId, group);
+ }
+
+ return [...groups.values()];
+ }, [rows, virtualItems]);
return (
<div
@@ -104,15 +247,9 @@ export const TimelineList: React.FC = () => {
start={virtualItem.start}
height={virtualItem.size}
/>
- ) : (
- <TimelineSceneListTrack
- key={virtualItem.key}
- row={rows[virtualItem.index]}
- start={virtualItem.start}
- height={virtualItem.size}
- />
- ),
+ ) : null,
)}
+ <TimelineSceneList groups={sceneGroups} />
</div>
);
};
diff --git a/packages/studio/src/components/Timeline/TimelineTracks.tsx b/packages/studio/src/components/Timeline/TimelineTracks.tsx
index f3964176d7..a4e9554169 100644
--- a/packages/studio/src/components/Timeline/TimelineTracks.tsx
+++ b/packages/studio/src/components/Timeline/TimelineTracks.tsx
@@ -28,7 +28,7 @@ const TimelineSceneTrackContent: React.FC<{
}> = React.memo(({children, sceneRange}) => {
const frame = Internals.Timeline.useTimelinePosition();
const active = frame >= sceneRange.from && frame < sceneRange.end;
- return <div style={{opacity: active ? 1 : 0.75}}>{children}</div>;
+ return <div style={{opacity: active ? 1 : 0.5}}>{children}</div>;
});
const TimelineTracksInner: React.FC<{
diff --git a/packages/studio/src/components/Timeline/TimelineVirtualization.tsx b/packages/studio/src/components/Timeline/TimelineVirtualization.tsx
index a7a6dc50d8..e1642b535d 100644
--- a/packages/studio/src/components/Timeline/TimelineVirtualization.tsx
+++ b/packages/studio/src/components/Timeline/TimelineVirtualization.tsx
@@ -40,6 +40,7 @@ export type TimelineVirtualRow = TimelineDisplayRow & {
readonly afterDropLineOffset: number;
readonly siblingIndex: number;
readonly sceneRange: TimelineSceneRange | null;
+ readonly sceneGroupId: string | null;
};
type TimelineVirtualizationContextValue = {
@@ -120,6 +121,12 @@ export const TimelineVirtualizationProvider: React.FC<{
const afterDropLineOffset = seriesLayout.afterDropLineOffsets[index];
const siblingIndex = seriesLayout.siblingIndexes[index];
const sceneRange = seriesLayout.sceneRanges[index];
+ const sceneGroupId =
+ sceneRange === null
+ ? null
+ : timeline[
+ seriesLayout.groups[seriesLayout.groupIndexes[index]].index
+ ].track.sequence.id;
const previous = rowsRef.current[index];
if (
previous?.track === track &&
@@ -127,6 +134,7 @@ export const TimelineVirtualizationProvider: React.FC<{
previous.auxiliaryRows === auxiliaryRows &&
previous.afterDropLineOffset === afterDropLineOffset &&
previous.siblingIndex === siblingIndex &&
+ previous.sceneGroupId === sceneGroupId &&
previous.sceneRange?.from === sceneRange?.from &&
previous.sceneRange?.end === sceneRange?.end
) {
@@ -140,6 +148,7 @@ export const TimelineVirtualizationProvider: React.FC<{
items,
auxiliaryRows,
sceneRange,
+ sceneGroupId,
};
},
);
diff --git a/packages/studio/src/helpers/inject-css.ts b/packages/studio/src/helpers/inject-css.ts
index b119497c60..27513fd6ce 100644
--- a/packages/studio/src/helpers/inject-css.ts
+++ b/packages/studio/src/helpers/inject-css.ts
@@ -27,55 +27,27 @@ const makeDefaultGlobalCSS = () => {
position: static !important;
}
- html.__remotion-inspector-forward,
- html.__remotion-inspector-backward {
+ html.__remotion-slide-forward,
+ html.__remotion-slide-backward {
view-transition-name: none;
- --remotion-inspector-slide-distance: 100%;
+ --remotion-slide-distance: 100%;
}
- html.__remotion-inspector-backward {
- --remotion-inspector-slide-distance: -100%;
+ html.__remotion-slide-backward {
+ --remotion-slide-distance: -100%;
}
- html.__remotion-inspector-forward::view-transition,
- html.__remotion-inspector-backward::view-transition {
+ html.__remotion-slide-forward::view-transition,
+ html.__remotion-slide-backward::view-transition {
pointer-events: none;
}
- ::view-transition-group(remotion-inspector) {
- animation: none;
- overflow: clip;
+ @keyframes remotion-slide-out {
+ to { transform: translateX(calc(-1 * var(--remotion-slide-distance))); }
}
- ::view-transition-old(remotion-inspector),
- ::view-transition-new(remotion-inspector) {
- animation-duration: 75ms;
- animation-timing-function: ease-out;
- animation-fill-mode: both;
- mix-blend-mode: normal;
- }
-
- ::view-transition-old(remotion-inspector) {
- animation-name: remotion-inspector-slide-out;
- }
-
- ::view-transition-new(remotion-inspector) {
- animation-name: remotion-inspector-slide-in;
- }
-
- @keyframes remotion-inspector-slide-out {
- to { transform: translateX(calc(-1 * var(--remotion-inspector-slide-distance))); }
- }
-
- @keyframes remotion-inspector-slide-in {
- from { transform: translateX(var(--remotion-inspector-slide-distance)); }
- }
-
- @media (prefers-reduced-motion: reduce) {
- ::view-transition-old(remotion-inspector),
- ::view-transition-new(remotion-inspector) {
- animation: none;
- }
+ @keyframes remotion-slide-in {
+ from { transform: translateX(var(--remotion-slide-distance)); }
}
.remotion-splitter {
diff --git a/packages/studio/src/helpers/slide-view-transition.ts b/packages/studio/src/helpers/slide-view-transition.ts
new file mode 100644
index 0000000000..87fcb41943
--- /dev/null
+++ b/packages/studio/src/helpers/slide-view-transition.ts
@@ -0,0 +1,106 @@
+import {flushSync} from 'react-dom';
+
+let finishCurrentTransition: (() => void) | null = null;
+
+export const startSlideViewTransition = ({
+ panels,
+ direction,
+ update,
+}: {
+ readonly panels: readonly {
+ readonly element: HTMLElement;
+ readonly name: string;
+ readonly clipY: {readonly top: number; readonly bottom: number} | null;
+ }[];
+ readonly direction: 'forward' | 'backward';
+ readonly update: () => void;
+}): (() => void) | null => {
+ if (
+ typeof document.startViewTransition !== 'function' ||
+ typeof flushSync !== 'function' ||
+ window.matchMedia('(prefers-reduced-motion: reduce)').matches
+ ) {
+ return null;
+ }
+
+ // A document can only run one view transition. Finish another panel's pending
+ // update before taking over, so its latest view is not lost.
+ finishCurrentTransition?.();
+ const root = document.documentElement;
+ const transitionClass = `__remotion-slide-${direction}`;
+ root.classList.add(transitionClass);
+ for (const {element, name} of panels) {
+ element.style.setProperty('view-transition-name', name);
+ }
+
+ const stylesheet = document.createElement('style');
+ stylesheet.textContent = panels
+ .map(
+ ({name, clipY}) => `
+ ::view-transition-group(${name}) {
+ animation: none;
+ overflow: clip;
+ ${clipY === null ? '' : `clip-path: inset(${clipY.top}px 0 ${clipY.bottom}px 0);`}
+ }
+ ::view-transition-old(${name}), ::view-transition-new(${name}) {
+ animation-duration: 75ms;
+ animation-timing-function: ease-out;
+ animation-fill-mode: both;
+ mix-blend-mode: normal;
+ }
+ ::view-transition-old(${name}) { animation-name: remotion-slide-out; }
+ ::view-transition-new(${name}) { animation-name: remotion-slide-in; }
+ @media (prefers-reduced-motion: reduce) {
+ ::view-transition-old(${name}), ::view-transition-new(${name}) { animation: none; }
+ }
+ `,
+ )
+ .join('\n');
+ document.head.appendChild(stylesheet);
+ let cancelled = false;
+ let cleaned = false;
+ let updated = false;
+ const cleanup = () => {
+ if (cleaned) {
+ return;
+ }
+
+ cleaned = true;
+ root.classList.remove(transitionClass);
+ for (const {element} of panels) {
+ element.style.removeProperty('view-transition-name');
+ }
+
+ stylesheet.remove();
+ if (finishCurrentTransition === finish) {
+ finishCurrentTransition = null;
+ }
+ };
+
+ const transition = document.startViewTransition(() => {
+ if (!cancelled) {
+ updated = true;
+ flushSync(update);
+ }
+ });
+ const cancel = () => {
+ cancelled = true;
+ transition.skipTransition();
+ cleanup();
+ };
+
+ const finish = () => {
+ if (!updated && !cancelled) {
+ updated = true;
+ update();
+ }
+
+ cancel();
+ };
+
+ finishCurrentTransition = finish;
+ // A skipped transition still calls its update, but rejects `ready`.
+ transition.ready.catch(() => undefined);
+ transition.finished.then(cleanup, cleanup);
+ return cancel;
+};