Skip to content

🎥 Make videos programmatically with React

Preserve premounted content through CSS transitions

JonnyBurgere41f0f3dd1d4
diff --git a/packages/transitions/src/TransitionSeries.tsx b/packages/transitions/src/TransitionSeries.tsx
index 888794468c..4ab9e0d002 100644
--- a/packages/transitions/src/TransitionSeries.tsx
+++ b/packages/transitions/src/TransitionSeries.tsx
@@ -782,7 +782,25 @@ const TransitionSeriesChildren: FC<{readonly children: React.ReactNode}> = ({
);
}
- if (next && prev && nextProgress !== null && prevProgress !== null) {
+ // Built-in presentations can leave their effects inactive without
+ // replacing the child tree and discarding premounted media.
+ const renderNextPresentation =
+ next &&
+ (nextProgress !== null ||
+ (next.props.presentation ?? slide())
+ ._remotionInternalSupportsInactive);
+ const renderPrevPresentation =
+ prev &&
+ (prevProgress !== null ||
+ (prev.props.presentation ?? slide())
+ ._remotionInternalSupportsInactive);
+
+ if (
+ next &&
+ prev &&
+ renderNextPresentation &&
+ renderPrevPresentation
+ ) {
const nextPresentation = next.props.presentation ?? slide();
const prevPresentation = prev.props.presentation ?? slide();
@@ -802,17 +820,20 @@ const TransitionSeriesChildren: FC<{readonly children: React.ReactNode}> = ({
<UppercaseNextPresentation
passedProps={nextPresentation.props ?? {}}
presentationDirection="exiting"
- presentationProgress={nextProgress}
+ presentationProgress={nextProgress ?? 0}
+ _remotionInternalIsActive={nextProgress !== null}
presentationDurationInFrames={next.props.timing.getDurationInFrames(
{fps},
)}
- onElementImage={() => {
- throw new Error('Should not call when exiting');
+ onElementImage={(elementImage, draw) => {
+ onPrevElementImage(elementImage, nextProgress, draw, i + 1);
}}
onUnmount={() => {
- throw new Error('Should not call when exiting');
+ onPrevElementImage(null, null, null, i + 1);
}}
- bothEnteringAndExiting
+ bothEnteringAndExiting={
+ nextProgress !== null && prevProgress !== null
+ }
>
<WrapInExitingProgressContext
presentationProgress={nextProgress}
@@ -820,17 +841,21 @@ const TransitionSeriesChildren: FC<{readonly children: React.ReactNode}> = ({
<UppercasePrevPresentation
passedProps={prevPresentation.props ?? {}}
presentationDirection="entering"
- presentationProgress={prevProgress}
+ presentationProgress={prevProgress ?? 1}
+ _remotionInternalIsActive={prevProgress !== null}
presentationDurationInFrames={prev.props.timing.getDurationInFrames(
{fps},
)}
onElementImage={(elementImage, draw) => {
- onPrevElementImage(
- elementImage,
- nextProgress,
- draw,
- i + 1,
- );
+ if (nextProgress !== null) {
+ onPrevElementImage(
+ elementImage,
+ nextProgress,
+ draw,
+ i + 1,
+ );
+ }
+
onNextElementImage(
elementImage,
prevProgress,
@@ -842,7 +867,9 @@ const TransitionSeriesChildren: FC<{readonly children: React.ReactNode}> = ({
onPrevElementImage(null, null, null, i + 1);
onNextElementImage(null, null, null, i - 1);
}}
- bothEnteringAndExiting
+ bothEnteringAndExiting={
+ nextProgress !== null && prevProgress !== null
+ }
>
<WrapInEnteringProgressContext
presentationProgress={prevProgress}
@@ -856,7 +883,7 @@ const TransitionSeriesChildren: FC<{readonly children: React.ReactNode}> = ({
);
}
- if (prevProgress !== null && prev) {
+ if (renderPrevPresentation && prev) {
const prevPresentation = prev.props.presentation ?? slide();
const UppercasePrevPresentation = prevPresentation.component;
@@ -874,7 +901,8 @@ const TransitionSeriesChildren: FC<{readonly children: React.ReactNode}> = ({
<UppercasePrevPresentation
passedProps={prevPresentation.props ?? {}}
presentationDirection="entering"
- presentationProgress={prevProgress}
+ presentationProgress={prevProgress ?? 1}
+ _remotionInternalIsActive={prevProgress !== null}
presentationDurationInFrames={prev.props.timing.getDurationInFrames(
{fps},
)}
@@ -896,7 +924,7 @@ const TransitionSeriesChildren: FC<{readonly children: React.ReactNode}> = ({
);
}
- if (nextProgress !== null && next) {
+ if (renderNextPresentation && next) {
const nextPresentation = next.props.presentation ?? slide();
const UppercaseNextPresentation = nextPresentation.component;
@@ -914,7 +942,8 @@ const TransitionSeriesChildren: FC<{readonly children: React.ReactNode}> = ({
<UppercaseNextPresentation
passedProps={nextPresentation.props ?? {}}
presentationDirection="exiting"
- presentationProgress={nextProgress}
+ presentationProgress={nextProgress ?? 0}
+ _remotionInternalIsActive={nextProgress !== null}
presentationDurationInFrames={next.props.timing.getDurationInFrames(
{fps},
)}
diff --git a/packages/transitions/src/context.tsx b/packages/transitions/src/context.tsx
index 0e143a3480..ae041e7c16 100644
--- a/packages/transitions/src/context.tsx
+++ b/packages/transitions/src/context.tsx
@@ -1,4 +1,4 @@
-import React, {useMemo} from 'react';
+import React, {useContext, useMemo} from 'react';
type EnteringState = {
enteringProgress: number;
@@ -12,14 +12,17 @@ export const EnteringContext = React.createContext<EnteringState | null>(null);
export const ExitingContext = React.createContext<ExitingState | null>(null);
export const WrapInEnteringProgressContext: React.FC<{
- readonly presentationProgress: number;
+ readonly presentationProgress: number | null;
readonly children: React.ReactNode;
}> = ({presentationProgress, children}) => {
- const value: EnteringState = useMemo(() => {
- return {
- enteringProgress: presentationProgress,
- };
- }, [presentationProgress]);
+ const inheritedValue = useContext(EnteringContext);
+ const value: EnteringState | null = useMemo(() => {
+ return presentationProgress === null
+ ? inheritedValue
+ : {
+ enteringProgress: presentationProgress,
+ };
+ }, [inheritedValue, presentationProgress]);
return (
<EnteringContext.Provider value={value}>
@@ -29,14 +32,17 @@ export const WrapInEnteringProgressContext: React.FC<{
};
export const WrapInExitingProgressContext: React.FC<{
- readonly presentationProgress: number;
+ readonly presentationProgress: number | null;
readonly children: React.ReactNode;
}> = ({presentationProgress, children}) => {
- const value: ExitingState = useMemo(() => {
- return {
- exitingProgress: presentationProgress,
- };
- }, [presentationProgress]);
+ const inheritedValue = useContext(ExitingContext);
+ const value: ExitingState | null = useMemo(() => {
+ return presentationProgress === null
+ ? inheritedValue
+ : {
+ exitingProgress: presentationProgress,
+ };
+ }, [inheritedValue, presentationProgress]);
return (
<ExitingContext.Provider value={value}>{children}</ExitingContext.Provider>
diff --git a/packages/transitions/src/presentations/clock-wipe.tsx b/packages/transitions/src/presentations/clock-wipe.tsx
index 444c74cbd3..ba21dfcffe 100644
--- a/packages/transitions/src/presentations/clock-wipe.tsx
+++ b/packages/transitions/src/presentations/clock-wipe.tsx
@@ -18,7 +18,13 @@ export type ClockWipeProps = {
const ClockWipePresentation: React.FC<
TransitionPresentationComponentProps<ClockWipeProps>
-> = ({children, presentationDirection, presentationProgress, passedProps}) => {
+> = ({
+ children,
+ presentationDirection,
+ presentationProgress,
+ passedProps,
+ _remotionInternalIsActive = true,
+}) => {
const finishedRadius =
Math.sqrt(passedProps.width ** 2 + passedProps.height ** 2) / 2;
@@ -63,8 +69,14 @@ const ClockWipePresentation: React.FC<
]);
return (
- <AbsoluteFill showInTimeline={false} style={outerStyle}>
- <AbsoluteFill showInTimeline={false} style={style}>
+ <AbsoluteFill
+ showInTimeline={false}
+ style={_remotionInternalIsActive ? outerStyle : {display: 'contents'}}
+ >
+ <AbsoluteFill
+ showInTimeline={false}
+ style={_remotionInternalIsActive ? style : {display: 'contents'}}
+ >
{children}
</AbsoluteFill>
</AbsoluteFill>
@@ -78,5 +90,9 @@ const ClockWipePresentation: React.FC<
export const clockWipe = (
props: ClockWipeProps,
): TransitionPresentation<ClockWipeProps> => {
- return {component: ClockWipePresentation, props: props ?? {}};
+ return {
+ _remotionInternalSupportsInactive: true,
+ component: ClockWipePresentation,
+ props: props ?? {},
+ };
};
diff --git a/packages/transitions/src/presentations/fade.tsx b/packages/transitions/src/presentations/fade.tsx
index ec04d8ce4d..e605b86858 100644
--- a/packages/transitions/src/presentations/fade.tsx
+++ b/packages/transitions/src/presentations/fade.tsx
@@ -13,7 +13,13 @@ export type FadeProps = {
const FadePresentation: React.FC<
TransitionPresentationComponentProps<FadeProps>
-> = ({children, presentationDirection, presentationProgress, passedProps}) => {
+> = ({
+ children,
+ presentationDirection,
+ presentationProgress,
+ passedProps,
+ _remotionInternalIsActive = true,
+}) => {
const isEntering = presentationDirection === 'entering';
const style: React.CSSProperties = useMemo(() => {
return {
@@ -36,7 +42,10 @@ const FadePresentation: React.FC<
]);
return (
- <AbsoluteFill showInTimeline={false} style={style}>
+ <AbsoluteFill
+ showInTimeline={false}
+ style={_remotionInternalIsActive ? style : {display: 'contents'}}
+ >
{children}
</AbsoluteFill>
);
@@ -48,6 +57,7 @@ const FadePresentation: React.FC<
export const fade = (props?: FadeProps): TransitionPresentation<FadeProps> => {
return {
+ _remotionInternalSupportsInactive: true,
component: FadePresentation,
props: props ?? {},
};
diff --git a/packages/transitions/src/presentations/flip.tsx b/packages/transitions/src/presentations/flip.tsx
index b975048f52..14170f6bd4 100644
--- a/packages/transitions/src/presentations/flip.tsx
+++ b/packages/transitions/src/presentations/flip.tsx
@@ -24,6 +24,7 @@ const Flip: React.FC<TransitionPresentationComponentProps<FlipProps>> = ({
children,
presentationDirection,
presentationProgress,
+ _remotionInternalIsActive = true,
passedProps: {
direction = 'from-left',
perspective = 1000,
@@ -79,8 +80,14 @@ const Flip: React.FC<TransitionPresentationComponentProps<FlipProps>> = ({
}, [outerEnterStyle, outerExitStyle, perspective, presentationDirection]);
return (
- <AbsoluteFill showInTimeline={false} style={outer}>
- <AbsoluteFill showInTimeline={false} style={style}>
+ <AbsoluteFill
+ showInTimeline={false}
+ style={_remotionInternalIsActive ? outer : {display: 'contents'}}
+ >
+ <AbsoluteFill
+ showInTimeline={false}
+ style={_remotionInternalIsActive ? style : {display: 'contents'}}
+ >
{children}
</AbsoluteFill>
</AbsoluteFill>
@@ -92,5 +99,9 @@ const Flip: React.FC<TransitionPresentationComponentProps<FlipProps>> = ({
*/
export const flip = (props?: FlipProps): TransitionPresentation<FlipProps> => {
- return {component: Flip, props: props ?? {}};
+ return {
+ _remotionInternalSupportsInactive: true,
+ component: Flip,
+ props: props ?? {},
+ };
};
diff --git a/packages/transitions/src/presentations/iris.tsx b/packages/transitions/src/presentations/iris.tsx
index 455b01ef6b..cfb3c5071d 100644
--- a/packages/transitions/src/presentations/iris.tsx
+++ b/packages/transitions/src/presentations/iris.tsx
@@ -18,7 +18,13 @@ export type IrisProps = {
const IrisPresentation: React.FC<
TransitionPresentationComponentProps<IrisProps>
-> = ({children, presentationDirection, presentationProgress, passedProps}) => {
+> = ({
+ children,
+ presentationDirection,
+ presentationProgress,
+ passedProps,
+ _remotionInternalIsActive = true,
+}) => {
const maxRadius =
Math.sqrt(passedProps.width ** 2 + passedProps.height ** 2) / 2;
@@ -68,8 +74,14 @@ const IrisPresentation: React.FC<
]);
return (
- <AbsoluteFill showInTimeline={false} style={outerStyle}>
- <AbsoluteFill showInTimeline={false} style={style}>
+ <AbsoluteFill
+ showInTimeline={false}
+ style={_remotionInternalIsActive ? outerStyle : {display: 'contents'}}
+ >
+ <AbsoluteFill
+ showInTimeline={false}
+ style={_remotionInternalIsActive ? style : {display: 'contents'}}
+ >
{children}
</AbsoluteFill>
</AbsoluteFill>
@@ -81,5 +93,9 @@ const IrisPresentation: React.FC<
* @see [Documentation](https://www.remotion.dev/docs/transitions/presentations/iris)
*/
export const iris = (props: IrisProps): TransitionPresentation<IrisProps> => {
- return {component: IrisPresentation, props: props ?? {}};
+ return {
+ _remotionInternalSupportsInactive: true,
+ component: IrisPresentation,
+ props: props ?? {},
+ };
};
diff --git a/packages/transitions/src/presentations/none.tsx b/packages/transitions/src/presentations/none.tsx
index d2cb21406a..15862c3dfa 100644
--- a/packages/transitions/src/presentations/none.tsx
+++ b/packages/transitions/src/presentations/none.tsx
@@ -12,7 +12,12 @@ export type NoneProps = {
const NonePresentation: React.FC<
TransitionPresentationComponentProps<NoneProps>
-> = ({children, presentationDirection, passedProps}) => {
+> = ({
+ children,
+ presentationDirection,
+ passedProps,
+ _remotionInternalIsActive = true,
+}) => {
const style: React.CSSProperties = useMemo(() => {
return {
...(presentationDirection === 'entering'
@@ -22,7 +27,10 @@ const NonePresentation: React.FC<
}, [passedProps.enterStyle, passedProps.exitStyle, presentationDirection]);
return (
- <AbsoluteFill showInTimeline={false} style={style}>
+ <AbsoluteFill
+ showInTimeline={false}
+ style={_remotionInternalIsActive ? style : {display: 'contents'}}
+ >
{children}
</AbsoluteFill>
);
@@ -34,6 +42,7 @@ const NonePresentation: React.FC<
*/
export const none = (props?: NoneProps): TransitionPresentation<NoneProps> => {
return {
+ _remotionInternalSupportsInactive: true,
component: NonePresentation,
props: props ?? {},
};
diff --git a/packages/transitions/src/presentations/push-cut.tsx b/packages/transitions/src/presentations/push-cut.tsx
index 14157dce43..0406b357ad 100644
--- a/packages/transitions/src/presentations/push-cut.tsx
+++ b/packages/transitions/src/presentations/push-cut.tsx
@@ -85,6 +85,7 @@ const PushCutPresentation: React.FC<
presentationDurationInFrames,
presentationProgress,
passedProps,
+ _remotionInternalIsActive = true,
}) => {
const {
cutProgress = 5 / 11,
@@ -170,11 +171,17 @@ const PushCutPresentation: React.FC<
}, [innerEnterStyle, innerExitStyle, isEntering, scale, transformOrigin]);
return (
- <AbsoluteFill showInTimeline={false} style={outerStyle}>
- <AbsoluteFill showInTimeline={false} style={innerStyle}>
+ <AbsoluteFill
+ showInTimeline={false}
+ style={_remotionInternalIsActive ? outerStyle : {display: 'contents'}}
+ >
+ <AbsoluteFill
+ showInTimeline={false}
+ style={_remotionInternalIsActive ? innerStyle : {display: 'contents'}}
+ >
{children}
</AbsoluteFill>
- {flashOpacity > 0 ? (
+ {_remotionInternalIsActive && flashOpacity > 0 ? (
<AbsoluteFill
showInTimeline={false}
style={{
@@ -200,6 +207,7 @@ export const pushCut = (
validateProps(passedProps);
return {
+ _remotionInternalSupportsInactive: true,
component: PushCutPresentation,
props: passedProps,
};
diff --git a/packages/transitions/src/presentations/slide.tsx b/packages/transitions/src/presentations/slide.tsx
index 846d9b5eeb..61375f2d78 100644
--- a/packages/transitions/src/presentations/slide.tsx
+++ b/packages/transitions/src/presentations/slide.tsx
@@ -25,6 +25,7 @@ const SlidePresentation: React.FC<
children,
presentationProgress,
presentationDirection,
+ _remotionInternalIsActive = true,
passedProps: {direction = 'from-left', enterStyle, exitStyle},
}) => {
const directionStyle = useMemo((): React.CSSProperties => {
@@ -96,7 +97,10 @@ const SlidePresentation: React.FC<
}, [directionStyle, enterStyle, exitStyle, presentationDirection]);
return (
- <AbsoluteFill showInTimeline={false} style={style}>
+ <AbsoluteFill
+ showInTimeline={false}
+ style={_remotionInternalIsActive ? style : {display: 'contents'}}
+ >
{children}
</AbsoluteFill>
);
@@ -110,6 +114,7 @@ export const slide = (
props?: SlideProps,
): TransitionPresentation<SlideProps> => {
return {
+ _remotionInternalSupportsInactive: true,
component: SlidePresentation,
props: props ?? {},
};
diff --git a/packages/transitions/src/presentations/wipe.tsx b/packages/transitions/src/presentations/wipe.tsx
index c43c2cb815..0b102f8b3f 100644
--- a/packages/transitions/src/presentations/wipe.tsx
+++ b/packages/transitions/src/presentations/wipe.tsx
@@ -77,6 +77,7 @@ const WipePresentation: React.FC<
children,
presentationProgress,
presentationDirection,
+ _remotionInternalIsActive = true,
passedProps: {
direction = 'from-left',
innerEnterStyle,
@@ -115,8 +116,14 @@ const WipePresentation: React.FC<
}, [outerEnterStyle, outerExitStyle, presentationDirection]);
return (
- <AbsoluteFill showInTimeline={false} style={outerStyle}>
- <AbsoluteFill showInTimeline={false} style={style}>
+ <AbsoluteFill
+ showInTimeline={false}
+ style={_remotionInternalIsActive ? outerStyle : {display: 'contents'}}
+ >
+ <AbsoluteFill
+ showInTimeline={false}
+ style={_remotionInternalIsActive ? style : {display: 'contents'}}
+ >
{children}
</AbsoluteFill>
</AbsoluteFill>
@@ -129,6 +136,7 @@ const WipePresentation: React.FC<
*/
export const wipe = (props?: WipeProps): TransitionPresentation<WipeProps> => {
return {
+ _remotionInternalSupportsInactive: true,
component: WipePresentation,
props: props ?? {},
};
diff --git a/packages/transitions/src/types.ts b/packages/transitions/src/types.ts
index 1a9a23aaea..54930ec7a6 100644
--- a/packages/transitions/src/types.ts
+++ b/packages/transitions/src/types.ts
@@ -26,6 +26,8 @@ export type TransitionPresentation<
TransitionPresentationComponentProps<PresentationProps>
>;
props: PresentationProps;
+ /** @internal Keeps the presentation mounted when its visual effect is inactive. */
+ readonly _remotionInternalSupportsInactive?: boolean;
};
export type TransitionPresentationComponentProps<
@@ -39,6 +41,8 @@ export type TransitionPresentationComponentProps<
onElementImage: (elementImage: OffscreenCanvas, draw: DrawFunction) => void;
onUnmount: () => void;
bothEnteringAndExiting: boolean;
+ /** @internal Defaults to true for presentations rendered directly. */
+ readonly _remotionInternalIsActive?: boolean;
};
export type TransitionSeriesOverlayProps = Pick<