Skip to content

🎥 Make videos programmatically with React

Skip frame subscriptions for static interactive components

JonnyBurgerc76638b3b282
diff --git a/packages/core/src/timeline-position-state.ts b/packages/core/src/timeline-position-state.ts
index 4c838ec24d..446375268a 100644
--- a/packages/core/src/timeline-position-state.ts
+++ b/packages/core/src/timeline-position-state.ts
@@ -1,4 +1,4 @@
-import {useContext} from 'react';
+import {createContext, useContext} from 'react';
import {
SequenceActivityAbsoluteTimeContext,
SequenceActivityDormantContext,
@@ -22,6 +22,12 @@ export type PlayableMediaTag = {
type CurrentTimePerComposition = Record<string, number>;
+// Reading TimelineContext and ignoring its value would still schedule frame
+// updates. Inactive subscribers must read a separate, never-changing context.
+const UnsubscribedTimelineContext = createContext<TimelineContextValue | null>(
+ null,
+);
+
const makeKey = () => {
return `remotion.time-all`;
};
@@ -59,12 +65,16 @@ export const clampFrameToCompositionRange = (
};
const useTimelinePositionFromContext = (
- state: TimelineContextValue,
+ state: TimelineContextValue | null,
): number => {
const videoConfig = useVideo();
const env = useRemotionEnvironment();
const dormant = useContext(SequenceActivityDormantContext);
+ if (state === null) {
+ return 0;
+ }
+
if (!videoConfig) {
return typeof window === 'undefined'
? 0
@@ -112,6 +122,27 @@ export const useTimelinePosition = (): number => {
return useTimelinePositionFromContext(state);
};
+export const useTimelinePositionWithOptionalSubscription = (
+ subscribe: boolean,
+): number => {
+ const dormant = useContext(SequenceActivityDormantContext);
+ const state = useContext(
+ subscribe
+ ? dormant
+ ? SequenceActivityTimelineContext
+ : TimelineContext
+ : UnsubscribedTimelineContext,
+ );
+ const frame = useTimelinePositionFromContext(state);
+ if (subscribe && state === null) {
+ throw new Error(
+ 'TimelineContext is not available. This hook must be used inside a <Player> or the Remotion Studio.',
+ );
+ }
+
+ return frame;
+};
+
export const useIsInsideFreeze = (): boolean => {
return useTimelineContext().isInsideFreeze;
};
diff --git a/packages/core/src/use-current-frame.ts b/packages/core/src/use-current-frame.ts
index fd9f914955..ea14bbf7a2 100644
--- a/packages/core/src/use-current-frame.ts
+++ b/packages/core/src/use-current-frame.ts
@@ -1,14 +1,12 @@
import {useContext} from 'react';
import {CanUseRemotionHooks} from './CanUseRemotionHooks.js';
import {SequenceContext} from './SequenceContext.js';
-import {useTimelinePosition} from './timeline-position-state.js';
+import {useTimelinePositionWithOptionalSubscription} from './timeline-position-state.js';
import {useRemotionEnvironment} from './use-remotion-environment.js';
-/*
- * @description Retrieves the current frame of the video within a component. Frames are 0-indexed, and if the component is wrapped in a `<Sequence>`, it returns the frame relative to when the Sequence starts.
- * @see [Documentation](https://www.remotion.dev/docs/use-current-frame)
- */
-export const useCurrentFrame = (): number => {
+export const useCurrentFrameWithOptionalSubscription = (
+ subscribe: boolean,
+): number => {
const canUseRemotionHooks = useContext(CanUseRemotionHooks);
const env = useRemotionEnvironment();
@@ -24,12 +22,24 @@ export const useCurrentFrame = (): number => {
);
}
- const frame = useTimelinePosition();
+ const frame = useTimelinePositionWithOptionalSubscription(subscribe);
const context = useContext(SequenceContext);
+ if (!subscribe) {
+ return 0;
+ }
+
const contextOffset = context
? context.cumulatedFrom + context.relativeFrom
: 0;
return (frame - contextOffset) * (context?.playbackRate ?? 1);
};
+
+/*
+ * @description Retrieves the current frame of the video within a component. Frames are 0-indexed, and if the component is wrapped in a `<Sequence>`, it returns the frame relative to when the Sequence starts.
+ * @see [Documentation](https://www.remotion.dev/docs/use-current-frame)
+ */
+export const useCurrentFrame = (): number => {
+ return useCurrentFrameWithOptionalSubscription(true);
+};
diff --git a/packages/core/src/with-interactivity-schema.ts b/packages/core/src/with-interactivity-schema.ts
index cdf50b5434..a6b2c5b770 100644
--- a/packages/core/src/with-interactivity-schema.ts
+++ b/packages/core/src/with-interactivity-schema.ts
@@ -35,7 +35,7 @@ import {
useDragOverridesForNodePath,
VisualModePropStatusesContext,
} from './SequenceManager.js';
-import {useCurrentFrame} from './use-current-frame.js';
+import {useCurrentFrameWithOptionalSubscription} from './use-current-frame.js';
import {useRemotionEnvironment} from './use-remotion-environment.js';
import {computeEffectiveSchemaValuesDotNotation} from './use-schema.js';
import {useUnsafeVideoConfig} from './use-unsafe-video-config.js';
@@ -259,8 +259,6 @@ export const withInteractivitySchema = <
// eslint-disable-next-line react-hooks/rules-of-hooks
const nodePathMapping = useContext(OverrideIdsToNodePathsGettersContext);
// eslint-disable-next-line react-hooks/rules-of-hooks
- const frame = useCurrentFrame();
- // eslint-disable-next-line react-hooks/rules-of-hooks
const videoConfig = useUnsafeVideoConfig();
const durationInFrames = videoConfig?.durationInFrames;
const fps = videoConfig?.fps;
@@ -334,6 +332,26 @@ export const withInteractivitySchema = <
: (nodePathMapping.overrideIdToNodePathMappings[overrideId] ?? null);
// eslint-disable-next-line react-hooks/rules-of-hooks
const dragOverrides = useDragOverridesForNodePath(nodePath);
+ // eslint-disable-next-line react-hooks/rules-of-hooks
+ const propStatus = useMemo(
+ () =>
+ nodePath === null
+ ? undefined
+ : getPropStatusesCtx(propStatuses, {
+ ...nodePath,
+ videoConfigValues,
+ }),
+ [nodePath, propStatuses, videoConfigValues],
+ );
+ // Runtime props already update through their owner. Only overrides with
+ // keyframes need this wrapper to subscribe to the advancing clock.
+ const needsFrame = flatKeys.some(
+ (key) =>
+ propStatus?.[key]?.status === 'keyframed' ||
+ dragOverrides[key]?.type === 'keyframed',
+ );
+ // eslint-disable-next-line react-hooks/rules-of-hooks
+ const frame = useCurrentFrameWithOptionalSubscription(needsFrame);
// Read the runtime values for every flat key from the JSX props,
// memoized on the leaf values so the object reference is stable
@@ -404,23 +422,10 @@ export const withInteractivitySchema = <
schema: schemaWithSequenceName,
currentValue: currentRuntimeValueDotNotation,
overrideValues: dragOverrides,
- propStatus:
- nodePath === null
- ? undefined
- : getPropStatusesCtx(propStatuses, {
- ...nodePath,
- videoConfigValues,
- }),
+ propStatus,
frame,
});
- }, [
- currentRuntimeValueDotNotation,
- dragOverrides,
- nodePath,
- propStatuses,
- videoConfigValues,
- frame,
- ]);
+ }, [currentRuntimeValueDotNotation, dragOverrides, propStatus, frame]);
// 4. Eliminate values forbidden by the resolved discriminated union.
const activeKeys = selectActiveKeys(