Skip to content

🎥 Make videos programmatically with React

more premounting

Jonny Burger4102d6d7359b
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 583c234e37..fefc361ba3 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -120,7 +120,10 @@ export {
RandomSeed,
} from './no-react';
export {prefetch, PrefetchOnProgress} from './prefetch.js';
-export {PremountedSequence} from './premounted-sequence';
+export {
+ PremountedSequence,
+ PremountedSequenceProps,
+} from './premounted-sequence';
export {registerRoot} from './register-root.js';
export {
LayoutAndStyle,
diff --git a/packages/core/src/premounted-sequence.tsx b/packages/core/src/premounted-sequence.tsx
index 55778f2800..d9f06a0066 100644
--- a/packages/core/src/premounted-sequence.tsx
+++ b/packages/core/src/premounted-sequence.tsx
@@ -1,15 +1,19 @@
-import React, {useMemo} from 'react';
+import React, {forwardRef, useMemo} from 'react';
import {Freeze} from './freeze';
import type {SequenceProps} from './Sequence';
import {Sequence} from './Sequence';
import {useCurrentFrame} from './use-current-frame';
-export const PremountedSequence: React.FC<
+export type PremountedSequenceProps = SequenceProps & {
+ premountFor: number;
+};
+
+const PremountedSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
+ HTMLDivElement,
{
- children: React.ReactNode;
premountFor: number;
} & SequenceProps
-> = ({premountFor, ...props}) => {
+> = ({premountFor, ...props}, ref) => {
const frame = useCurrentFrame();
if (props.layout === 'none') {
@@ -17,7 +21,7 @@ export const PremountedSequence: React.FC<
}
const {style: passedStyle, from = 0, ...otherProps} = props;
- const active = frame < premountFor + from;
+ const active = frame < from;
const style = useMemo(() => {
return {
@@ -28,13 +32,18 @@ export const PremountedSequence: React.FC<
}, [active, passedStyle]);
return (
- <Freeze frame={frame >= from ? from : -1} active={active}>
+ <Freeze frame={from} active={active}>
<Sequence
+ ref={ref}
name={`<Premount premountFor={${premountFor}}>`}
- from={from + (active ? 0 : premountFor)}
+ from={from + (active ? -premountFor : 0)}
style={style}
{...otherProps}
/>
</Freeze>
);
};
+
+export const PremountedSequence = forwardRef(
+ PremountedSequenceRefForwardingFunction,
+);
diff --git a/packages/core/src/series/index.tsx b/packages/core/src/series/index.tsx
index d86a6313b2..861d6fd8c0 100644
--- a/packages/core/src/series/index.tsx
+++ b/packages/core/src/series/index.tsx
@@ -1,6 +1,8 @@
import type {FC, PropsWithChildren} from 'react';
import {Children, forwardRef, useMemo} from 'react';
import {addSequenceStackTraces} from '../enable-sequence-stack-traces.js';
+import type {PremountedSequenceProps} from '../index.js';
+import {PremountedSequence} from '../index.js';
import type {LayoutAndStyle, SequenceProps} from '../Sequence.js';
import {Sequence} from '../Sequence.js';
import {validateDurationInFrames} from '../validation/validate-duration-in-frames.js';
@@ -15,6 +17,15 @@ type SeriesSequenceProps = PropsWithChildren<
LayoutAndStyle
>;
+type SeriesPremountedSequenceProps = PropsWithChildren<
+ {
+ durationInFrames: number;
+ offset?: number;
+ className?: string;
+ } & Pick<PremountedSequenceProps, 'name' | 'premountFor'> &
+ LayoutAndStyle
+>;
+
const SeriesSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
HTMLDivElement,
SeriesSequenceProps
@@ -25,7 +36,20 @@ const SeriesSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
return <>{children}</>;
};
+const SeriesPremountedSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
+ HTMLDivElement,
+ PremountedSequenceProps
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
+> = ({children}, _ref) => {
+ // Discard ref
+ // eslint-disable-next-line react/jsx-no-useless-fragment
+ return <>{children}</>;
+};
+
const SeriesSequence = forwardRef(SeriesSequenceRefForwardingFunction);
+const SeriesPremountedSequence = forwardRef(
+ SeriesPremountedSequenceRefForwardingFunction,
+);
/**
* @description with this component, you can easily stitch together scenes that should play sequentially after another.
@@ -35,6 +59,7 @@ const Series: FC<{
children: React.ReactNode;
}> & {
Sequence: typeof SeriesSequence;
+ PremountedSequence: typeof SeriesPremountedSequence;
} = ({children}) => {
const childrenValue = useMemo(() => {
let startFrame = 0;
@@ -46,6 +71,11 @@ const Series: FC<{
type: typeof SeriesSequence;
ref: React.MutableRefObject<HTMLDivElement>;
}
+ | {
+ props: SeriesPremountedSequenceProps;
+ type: typeof PremountedSequence;
+ ref: React.MutableRefObject<HTMLDivElement>;
+ }
| string;
if (typeof castedChild === 'string') {
// Don't throw if it's just some accidential whitespace
@@ -58,7 +88,10 @@ const Series: FC<{
);
}
- if (castedChild.type !== SeriesSequence) {
+ if (
+ castedChild.type !== SeriesSequence &&
+ castedChild.type !== SeriesPremountedSequence
+ ) {
throw new TypeError(
`The <Series /> component only accepts a list of <Series.Sequence /> components as its children, but got ${castedChild} instead`,
);
@@ -73,13 +106,6 @@ const Series: FC<{
}
const durationInFramesProp = castedChild.props.durationInFrames;
- const {
- durationInFrames,
- children: _children,
- from,
- name,
- ...passedProps
- } = castedChild.props as SeriesSequenceProps & {from: never}; // `from` is not accepted and must be filtered out if used in JS
if (
i !== flattenedChildren.length - 1 ||
@@ -112,17 +138,57 @@ const Series: FC<{
const currentStartFrame = startFrame + offset;
startFrame += durationInFramesProp + offset;
- return (
- <Sequence
- name={name || '<Series.Sequence>'}
- from={currentStartFrame}
- durationInFrames={durationInFramesProp}
- {...passedProps}
- ref={castedChild.ref}
- >
- {child}
- </Sequence>
- );
+ if (castedChild.type === SeriesPremountedSequence) {
+ const {
+ durationInFrames,
+ children: _children,
+ from,
+ name,
+ premountFor,
+ ...passedProps
+ } = castedChild.props as SeriesPremountedSequenceProps & {from: never}; // `from` is not accepted and must be filtered out if used in JS
+
+ if (passedProps.layout === 'none') {
+ throw new Error(
+ '`<Series.PremountedSequence>` does not support layout="none"',
+ );
+ }
+
+ return (
+ <PremountedSequence
+ name={name || '<Series.PremountedSeriesSequence>'}
+ from={currentStartFrame}
+ durationInFrames={durationInFramesProp}
+ premountFor={premountFor}
+ {...passedProps}
+ ref={castedChild.ref}
+ >
+ {child}
+ </PremountedSequence>
+ );
+ }
+
+ if (castedChild.type === SeriesSequence) {
+ const {
+ durationInFrames,
+ children: _children,
+ from,
+ name,
+ ...passedProps
+ } = castedChild.props as SeriesSequenceProps & {from: never}; // `from` is not accepted and must be filtered out if used in JS
+
+ return (
+ <Sequence
+ name={name || '<Series.Sequence>'}
+ from={currentStartFrame}
+ durationInFrames={durationInFramesProp}
+ {...passedProps}
+ ref={castedChild.ref}
+ >
+ {child}
+ </Sequence>
+ );
+ }
});
}, [children]);
@@ -131,6 +197,7 @@ const Series: FC<{
};
Series.Sequence = SeriesSequence;
+Series.PremountedSequence = SeriesPremountedSequence;
export {Series};
diff --git a/packages/example/src/Layers/index.tsx b/packages/example/src/Layers/index.tsx
index 20caa7ebca..a7773a50d2 100644
--- a/packages/example/src/Layers/index.tsx
+++ b/packages/example/src/Layers/index.tsx
@@ -1,32 +1,21 @@
import React from 'react';
-import {Audio, Loop, Sequence, Series, staticFile, Video} from 'remotion';
-import {BasicTransition} from '../Transitions/BasicTransition';
+import {Series, useCurrentFrame} from 'remotion';
+
+const Premounted: React.FC = () => {
+ const frame = useCurrentFrame();
+ return (
+ <div style={{background: 'red', height: 100, width: 100}}>{frame}</div>
+ );
+};
export const Layers: React.FC = () => {
return (
- <Sequence>
- <Sequence from={90}>hi</Sequence>
- <Sequence from={10}>hi</Sequence>
- <Sequence from={10}>hi</Sequence>
- <Sequence from={10}>hi</Sequence>
- <Sequence from={10}>hi</Sequence>
- <Sequence from={10}>hi</Sequence>
- <Sequence from={10}>hi there</Sequence>
- <Sequence from={10}>hi yaaa</Sequence>
- <Sequence from={10}>hi</Sequence>
- <Sequence from={10}>hi</Sequence>
- <Sequence from={10}>hi</Sequence>
- <Sequence from={10}>hi</Sequence>
- <Loop durationInFrames={30}>
- <Sequence>hi</Sequence>
- </Loop>
- <Audio src={staticFile('music.mp3')} />
- <Video src={staticFile('vid1.mp4')} />
- <BasicTransition />
- <Series>
- <Series.Sequence durationInFrames={40}>hi there</Series.Sequence>
- <Series.Sequence durationInFrames={40}>hi there</Series.Sequence>
- </Series>
- </Sequence>
+ <Series>
+ <Series.Sequence durationInFrames={40}>hi there</Series.Sequence>
+ <Series.Sequence durationInFrames={40}>hi there</Series.Sequence>
+ <Series.PremountedSequence premountFor={10} durationInFrames={40}>
+ <Premounted />
+ </Series.PremountedSequence>
+ </Series>
);
};