Skip to content

πŸŽ₯ Make videos programmatically with React

`remotion`: Add Sequence freeze prop

Jonny Burger01ddc980951a
diff --git a/packages/core/src/Sequence.tsx b/packages/core/src/Sequence.tsx
index 17d5e69e0e..b420088a48 100644
--- a/packages/core/src/Sequence.tsx
+++ b/packages/core/src/Sequence.tsx
@@ -55,6 +55,7 @@ export type SequencePropsWithoutDuration = {
readonly width?: number;
readonly height?: number;
readonly from?: number;
+ readonly freeze?: number;
readonly name?: string;
readonly showInTimeline?: boolean;
readonly hidden?: boolean;
@@ -116,6 +117,7 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
> = (
{
from = 0,
+ freeze,
durationInFrames = Infinity,
children,
name,
@@ -185,6 +187,26 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
);
}
+ if (typeof freeze !== 'undefined') {
+ if (typeof freeze !== 'number') {
+ throw new TypeError(
+ `The "freeze" prop of <Sequence /> must be a number, but is of type ${typeof freeze}.`,
+ );
+ }
+
+ if (Number.isNaN(freeze)) {
+ throw new TypeError(
+ `The "freeze" prop of <Sequence /> must be a real number, but it is NaN.`,
+ );
+ }
+
+ if (!Number.isFinite(freeze)) {
+ throw new TypeError(
+ `The "freeze" prop of <Sequence /> must be finite, but it is ${freeze}.`,
+ );
+ }
+ }
+
const absoluteFrame = useTimelinePosition();
const videoConfig = useVideoConfig();
@@ -402,6 +424,12 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
: absoluteFrame > endThreshold
? null
: children;
+ const frozenContent =
+ content === null || typeof freeze === 'undefined' ? (
+ content
+ ) : (
+ <Freeze frame={freeze}>{content}</Freeze>
+ );
const styleIfThere = other.layout === 'none' ? undefined : other.style;
@@ -426,15 +454,15 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
return (
<SequenceContext.Provider value={contextValue}>
- {content === null ? null : other.layout === 'none' ? (
- content
+ {frozenContent === null ? null : other.layout === 'none' ? (
+ frozenContent
) : (
<AbsoluteFill
ref={ref}
style={defaultStyle}
className={other.className}
>
- {content}
+ {frozenContent}
</AbsoluteFill>
)}
</SequenceContext.Provider>
diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.ts
index d5138dd709..3b6586db9d 100644
--- a/packages/core/src/internals.ts
+++ b/packages/core/src/internals.ts
@@ -119,6 +119,7 @@ import {
} from './ResolveCompositionConfig.js';
import {
durationInFramesField,
+ freezeField,
fromField,
hiddenField,
sequencePremountSchema,
@@ -407,6 +408,7 @@ export const Internals = {
getEffectPropStatusesCtx,
hiddenField,
durationInFramesField,
+ freezeField,
fromField,
} as const;
diff --git a/packages/core/src/sequence-field-schema.ts b/packages/core/src/sequence-field-schema.ts
index 16a50ad5a6..d0ba8f0bc8 100644
--- a/packages/core/src/sequence-field-schema.ts
+++ b/packages/core/src/sequence-field-schema.ts
@@ -265,10 +265,18 @@ export const fromField = {
hiddenFromList: true,
} as const satisfies SequenceFieldSchema;
+export const freezeField = {
+ type: 'number',
+ default: undefined,
+ step: 1,
+ hiddenFromList: true,
+} as const satisfies SequenceFieldSchema;
+
export const sequenceSchema = {
hidden: hiddenField,
showInTimeline: showInTimelineField,
from: fromField,
+ freeze: freezeField,
durationInFrames: durationInFramesField,
layout: {
type: 'enum',
@@ -284,6 +292,7 @@ export const sequenceSchema = {
export const sequenceSchemaWithoutFrom = {
hidden: hiddenField,
showInTimeline: showInTimelineField,
+ freeze: freezeField,
durationInFrames: durationInFramesField,
layout: sequenceSchema.layout,
} as const satisfies SequenceSchema;
diff --git a/packages/core/src/series/index.tsx b/packages/core/src/series/index.tsx
index 869b15ae37..1c289fd2ee 100644
--- a/packages/core/src/series/index.tsx
+++ b/packages/core/src/series/index.tsx
@@ -23,7 +23,10 @@ type SeriesSequenceProps = PropsWithChildren<
readonly durationInFrames: number;
readonly offset?: number;
readonly className?: string;
- } & Pick<SequenceProps, 'layout' | 'name' | 'hidden' | 'showInTimeline'> &
+ } & Pick<
+ SequenceProps,
+ 'layout' | 'name' | 'hidden' | 'showInTimeline' | 'freeze'
+ > &
LayoutAndStyle
>;
diff --git a/packages/core/src/test/nested-sequences.test.tsx b/packages/core/src/test/nested-sequences.test.tsx
index 951271b953..bbecba8bc5 100644
--- a/packages/core/src/test/nested-sequences.test.tsx
+++ b/packages/core/src/test/nested-sequences.test.tsx
@@ -1,5 +1,6 @@
import {afterEach, expect, test} from 'bun:test';
import {cleanup, render} from '@testing-library/react';
+import {useEffect} from 'react';
import {AbsoluteFill} from '../AbsoluteFill.js';
import {getTimelineDuration} from '../get-timeline-duration.js';
import {Sequence} from '../Sequence.js';
@@ -175,3 +176,72 @@ test('Nested media sequence with subframe duration stays mounted for final visib
expect(getForFrame(10, content)(/^Video$/i)).not.toBe(null);
});
+
+test('Sequence freeze pins the child frame without remounting the sequence', () => {
+ let mountCount = 0;
+
+ const FrozenChild = () => {
+ const frame = useCurrentFrame();
+ useEffect(() => {
+ mountCount++;
+ }, []);
+
+ return <div>{'frame' + frame}</div>;
+ };
+
+ const content = (
+ <Sequence from={10} durationInFrames={50} freeze={5}>
+ <FrozenChild />
+ </Sequence>
+ );
+
+ const {queryByText, rerender} = render(
+ <WrapSequenceContext>
+ <TimelineContext.Provider
+ value={{
+ frame: {
+ 'my-comp': 15,
+ },
+ playing: false,
+ rootId: 'hi',
+ imperativePlaying: {
+ current: false,
+ },
+ audioAndVideoTags: {
+ current: [],
+ },
+ }}
+ >
+ {content}
+ </TimelineContext.Provider>
+ </WrapSequenceContext>,
+ );
+
+ expect(queryByText(/^frame5$/i)).not.toBe(null);
+ expect(mountCount).toBe(1);
+
+ rerender(
+ <WrapSequenceContext>
+ <TimelineContext.Provider
+ value={{
+ frame: {
+ 'my-comp': 25,
+ },
+ playing: false,
+ rootId: 'hi',
+ imperativePlaying: {
+ current: false,
+ },
+ audioAndVideoTags: {
+ current: [],
+ },
+ }}
+ >
+ {content}
+ </TimelineContext.Provider>
+ </WrapSequenceContext>,
+ );
+
+ expect(queryByText(/^frame5$/i)).not.toBe(null);
+ expect(mountCount).toBe(1);
+});
diff --git a/packages/core/src/test/sequence-validation.test.tsx b/packages/core/src/test/sequence-validation.test.tsx
index e8b348b13b..31d716cf13 100644
--- a/packages/core/src/test/sequence-validation.test.tsx
+++ b/packages/core/src/test/sequence-validation.test.tsx
@@ -53,6 +53,44 @@ describe('Composition-validation render should throw with invalid props', () =>
);
});
});
+ describe('Throw with invalid freeze props', () => {
+ test('It should throw if "freeze" prop is not a number', () => {
+ expectToThrow(
+ () =>
+ render(
+ <WrapSequenceContext>
+ {/* @ts-expect-error */}
+ <Sequence freeze={'0'} durationInFrames={30} />
+ </WrapSequenceContext>,
+ ),
+ /The "freeze" prop of <Sequence \/> must be a number, but is of type string./,
+ );
+ });
+
+ test('It should throw if "freeze" prop is NaN', () => {
+ expectToThrow(
+ () =>
+ render(
+ <WrapSequenceContext>
+ <Sequence freeze={NaN} durationInFrames={30} />
+ </WrapSequenceContext>,
+ ),
+ /The "freeze" prop of <Sequence \/> must be a real number, but it is NaN./,
+ );
+ });
+
+ test('It should throw if "freeze" prop is Infinity', () => {
+ expectToThrow(
+ () =>
+ render(
+ <WrapSequenceContext>
+ <Sequence freeze={Infinity} durationInFrames={30} />
+ </WrapSequenceContext>,
+ ),
+ /The "freeze" prop of <Sequence \/> must be finite, but it is Infinity./,
+ );
+ });
+ });
test('It should throw for invalid layout value', () => {
expectToThrow(
() =>
diff --git a/packages/core/src/test/wrap-in-schema-helpers.test.ts b/packages/core/src/test/wrap-in-schema-helpers.test.ts
index 84dece344d..b29d9df0f9 100644
--- a/packages/core/src/test/wrap-in-schema-helpers.test.ts
+++ b/packages/core/src/test/wrap-in-schema-helpers.test.ts
@@ -41,6 +41,7 @@ test('getFlatSchema(sequenceSchema) exposes every variant key', () => {
'styleWhilePostmounted',
'durationInFrames',
'from',
+ 'freeze',
].sort(),
);
});
@@ -49,6 +50,7 @@ test('sequenceSchemaWithoutFrom does not expose from', () => {
const flat = getFlatSchemaWithAllKeys(sequenceSchemaWithoutFrom);
expect(Object.keys(flat)).not.toContain('from');
expect(Object.keys(flat)).toContain('durationInFrames');
+ expect(Object.keys(flat)).toContain('freeze');
});
test('style.scale does not impose a minimum value', () => {
@@ -75,7 +77,7 @@ test('selectActiveKeys returns only the hidden + layout keys when layout=none',
'style.scale': 2,
};
expect(selectActiveKeys(sequenceSchema, values).sort()).toEqual(
- ['hidden', 'layout', 'durationInFrames', 'from'].sort(),
+ ['hidden', 'layout', 'durationInFrames', 'from', 'freeze'].sort(),
);
});
@@ -90,6 +92,7 @@ test('selectActiveKeys exposes style.* keys when layout=absolute-fill', () => {
'layout',
'durationInFrames',
'from',
+ 'freeze',
'style.translate',
'style.scale',
'style.rotate',
@@ -105,7 +108,7 @@ test('selectActiveKeys exposes style.* keys when layout=absolute-fill', () => {
'style.scale': 2,
};
expect(selectActiveKeys(sequenceSchema, values2).sort()).toEqual(
- ['hidden', 'layout', 'durationInFrames', 'from'].sort(),
+ ['hidden', 'layout', 'durationInFrames', 'from', 'freeze'].sort(),
);
});
@@ -134,7 +137,7 @@ test('end-to-end: layout=none drops style.scale from active props', () => {
propsToDelete: new Set(),
});
expect(activeKeys.sort()).toEqual(
- ['hidden', 'layout', 'durationInFrames', 'from'].sort(),
+ ['hidden', 'layout', 'durationInFrames', 'from', 'freeze'].sort(),
);
// style.scale was not in activeKeys β†’ original style preserved, not overwritten
expect((merged.style as {scale: number}).scale).toBe(2);
diff --git a/packages/docs/docs/sequence.mdx b/packages/docs/docs/sequence.mdx
index d4c329723e..34994af4d4 100644
--- a/packages/docs/docs/sequence.mdx
+++ b/packages/docs/docs/sequence.mdx
@@ -192,6 +192,28 @@ From v3.2.36 onwards, this prop will be optional; by default, it will be 0.
For how many frames the sequence should be displayed. Children are unmounted if they are not within the time range of display. By default it will be `Infinity` to avoid limit the duration of the sequence.
+### `freeze?`<AvailableFrom v="4.0.476" />
+
+Freezes the children of the sequence at the specified frame.
+
+```tsx twoslash title="MyVideo.tsx"
+import {Sequence, useCurrentFrame} from 'remotion';
+
+const Child = () => <div>{useCurrentFrame()}</div>;
+
+// ---cut---
+
+export const MyVideo = () => {
+ return (
+ <Sequence freeze={20}>
+ <Child />
+ </Sequence>
+ );
+};
+```
+
+This is equivalent to wrapping the children in [`<Freeze frame={20}>`](/docs/freeze), but without remounting the sequence.
+
### `height?`<AvailableFrom v="4.0.80"/>
Gives the sequence a specific `style={{height: height}}` style and overrides `height` that is returned by the [`useVideoConfig()`](/docs/use-video-config) hook in child components. Useful for including a component that was designed for a specific height.
diff --git a/packages/docs/docs/series.mdx b/packages/docs/docs/series.mdx
index a680784346..472f4115aa 100644
--- a/packages/docs/docs/series.mdx
+++ b/packages/docs/docs/series.mdx
@@ -75,6 +75,10 @@ The offset does not apply to sequences that come before, but the sequences that
**Example 1**: Pass `10` to delay the sequence by 10 frames and create a blank space of 10 frames before it.
**Example 2**: Pass `-10` to start the sequence earlier and overlay the sequence on top of the previous one for 10 frames.
+### `freeze?`<AvailableFrom v="4.0.476" />
+
+Freezes the children of the sequence at the specified frame. See [`freeze`](/docs/sequence#freeze) on [`<Sequence>`](/docs/sequence).
+
### `layout?`
Either `"absolute-fill"` _(default)_ or `"none"` By default, your sequences will be absolutely positioned, so they will overlay each other. If you would like to opt out of it and handle layouting yourself, pass `layout="none"`.
diff --git a/packages/docs/docs/transitions/transitionseries.mdx b/packages/docs/docs/transitions/transitionseries.mdx
index bf8d64358e..8a6e4575e7 100644
--- a/packages/docs/docs/transitions/transitionseries.mdx
+++ b/packages/docs/docs/transitions/transitionseries.mdx
@@ -89,7 +89,7 @@ The `<TransitionSeries>` component can only contain `<TransitionSeries.Sequence>
### `<TransitionSeries.Sequence>`
-Inherits the [`durationInFrames`](/docs/sequence#durationinframes), [`name`](/docs/sequence#name), [`className`](/docs/sequence#classname), [`style`](/docs/sequence#style), [`showInTimeline`](/docs/sequence#showintimeline), [`premountFor`](/docs/sequence#premountfor), [`postmountFor`](/docs/sequence#postmountfor) and [`layout`](/docs/sequence#layout) props from [`<Sequence>`](/docs/sequence).
+Inherits the [`durationInFrames`](/docs/sequence#durationinframes), [`name`](/docs/sequence#name), [`className`](/docs/sequence#classname), [`style`](/docs/sequence#style), [`showInTimeline`](/docs/sequence#showintimeline), [`freeze`](/docs/sequence#freeze), [`premountFor`](/docs/sequence#premountfor), [`postmountFor`](/docs/sequence#postmountfor) and [`layout`](/docs/sequence#layout) props from [`<Sequence>`](/docs/sequence).
As children, put the contents of your scene.
diff --git a/packages/transitions/src/TransitionSeries.tsx b/packages/transitions/src/TransitionSeries.tsx
index 08c83866f0..19ef2de3e3 100644
--- a/packages/transitions/src/TransitionSeries.tsx
+++ b/packages/transitions/src/TransitionSeries.tsx
@@ -42,7 +42,7 @@ type SeriesSequenceProps = PropsWithChildren<
readonly offset?: number;
readonly className?: string;
} & LayoutBasedProps &
- Pick<SequencePropsWithoutDuration, 'name' | 'showInTimeline'>
+ Pick<SequencePropsWithoutDuration, 'name' | 'showInTimeline' | 'freeze'>
>;
const SeriesSequence = ({children}: SeriesSequenceProps) => {