Skip to content

🎥 Make videos programmatically with React

nice progress

Jonny Burger3171e0575879
diff --git a/packages/example/src/Transitions/index.tsx b/packages/example/src/Transitions/index.tsx
index 8722bccb68..2579625a5f 100644
--- a/packages/example/src/Transitions/index.tsx
+++ b/packages/example/src/Transitions/index.tsx
@@ -1,9 +1,8 @@
import {
makeLinearTiming,
makeSpringTiming,
- SlideTransition,
+ makeWipePresentation,
TransitionSeries,
- WipePresentation,
} from '@remotion/transitions';
import {AbsoluteFill, Easing} from 'remotion';
@@ -25,7 +24,7 @@ export const BasicTransition: React.FC = () => {
</AbsoluteFill>
</TransitionSeries.Sequence>
<TransitionSeries.Transition
- presentation={WipePresentation}
+ presentation={makeWipePresentation('left')}
timing={makeLinearTiming({
durationInFrames: 30,
easing: Easing.bounce,
@@ -46,7 +45,7 @@ export const BasicTransition: React.FC = () => {
</AbsoluteFill>
</TransitionSeries.Sequence>
<TransitionSeries.Transition
- presentation={SlideTransition}
+ presentation={makeWipePresentation('top-left')}
timing={makeSpringTiming({
config: {
damping: 200,
diff --git a/packages/transitions/package.json b/packages/transitions/package.json
index 5e9f2f9ead..f12a256ea1 100644
--- a/packages/transitions/package.json
+++ b/packages/transitions/package.json
@@ -31,7 +31,7 @@
"react": "18.0.0",
"react-dom": "18.0.0",
"@types/react": "18.0.26",
- "@types/react-dom": "^18.0.5"
+ "@types/react-dom": "18.0.10"
},
"peerDependencies": {
"react": ">=16.8.0",
diff --git a/packages/transitions/src/TransitionSeries.tsx b/packages/transitions/src/TransitionSeries.tsx
index 69e11772c3..2306721d74 100644
--- a/packages/transitions/src/TransitionSeries.tsx
+++ b/packages/transitions/src/TransitionSeries.tsx
@@ -4,9 +4,13 @@ import type {SequenceProps} from 'remotion';
import {Internals, Sequence, useCurrentFrame, useVideoConfig} from 'remotion';
import type {TransitionSeriesTransitionProps} from './types';
-const TransitionSeriesTransition: React.FC<
- TransitionSeriesTransitionProps
-> = () => {
+// eslint-disable-next-line react/function-component-definition
+const TransitionSeriesTransition = function <
+ PresentationProps extends Record<string, unknown>
+>(
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
+ _props: TransitionSeriesTransitionProps<PresentationProps>
+) {
return null;
};
@@ -22,17 +26,17 @@ const SeriesSequence = ({children}: SeriesSequenceProps) => {
return <>{children}</>;
};
-type TransitionType = {
- props: TransitionSeriesTransitionProps;
+type TransitionType<PresentationProps extends Record<string, unknown>> = {
+ props: TransitionSeriesTransitionProps<PresentationProps>;
type: typeof TransitionSeriesTransition;
};
-type TypeChild =
+type TypeChild<PresentationProps extends Record<string, unknown>> =
| {
props: SeriesSequenceProps;
type: typeof SeriesSequence;
}
- | TransitionType
+ | TransitionType<PresentationProps>
| string;
const TransitionSeries: FC<{
@@ -48,7 +52,9 @@ const TransitionSeries: FC<{
let startFrame = 0;
const flattedChildren = flattenChildren(children);
return Children.map(flattedChildren, (child, i) => {
- const castedChild = child as unknown as TypeChild;
+ const castedChild = child as unknown as TypeChild<
+ Record<string, unknown>
+ >;
if (typeof castedChild === 'string') {
// Don't throw if it's just some accidential whitespace
if (castedChild.trim() === '') {
@@ -112,21 +118,25 @@ const TransitionSeries: FC<{
);
}
- const hasPrev = flattedChildren[i - 1] as TypeChild;
- const nextPrev = flattedChildren[i + 1] as TypeChild;
+ const hasPrev = flattedChildren[i - 1] as TypeChild<
+ Record<string, unknown>
+ >;
+ const nextPrev = flattedChildren[i + 1] as TypeChild<
+ Record<string, unknown>
+ >;
- const prev: TransitionType | null =
+ const prev: TransitionType<Record<string, unknown>> | null =
typeof hasPrev === 'string' || typeof hasPrev === 'undefined'
? null
: hasPrev.type === TransitionSeriesTransition
- ? (hasPrev as TransitionType)
+ ? (hasPrev as TransitionType<Record<string, unknown>>)
: null;
- const next: TransitionType | null =
+ const next: TransitionType<Record<string, unknown>> | null =
typeof nextPrev === 'string' || typeof nextPrev === 'undefined'
? null
: nextPrev.type === TransitionSeriesTransition
- ? (nextPrev as TransitionType)
+ ? (nextPrev as TransitionType<Record<string, unknown>>)
: null;
const currentStartFrame = startFrame + offset;
@@ -173,12 +183,23 @@ const TransitionSeries: FC<{
: null;
if (next && prev && nextProgress !== null && prevProgress !== null) {
- const UppercaseNextPresentation = next.props.presentation;
- const UppercasePrevPresentation = prev.props.presentation;
+ const UppercaseNextPresentation = next.props.presentation.component;
+ const UppercasePrevPresentation = prev.props.presentation.component;
return (
- <UppercaseNextPresentation direction="out" progress={nextProgress}>
- <UppercasePrevPresentation direction="in" progress={prevProgress}>
+ // @ts-expect-error
+ <UppercaseNextPresentation
+ passedProps={next.props.presentation.props ?? {}}
+ presentationDirection="out"
+ presentationProgress={nextProgress}
+ >
+ {/**
+ // @ts-expect-error */}
+ <UppercasePrevPresentation
+ passedProps={prev.props.presentation.props ?? {}}
+ presentationDirection="in"
+ presentationProgress={prevProgress}
+ >
{inner}
</UppercasePrevPresentation>
</UppercaseNextPresentation>
@@ -186,20 +207,31 @@ const TransitionSeries: FC<{
}
if (prevProgress !== null && prev) {
- const UppercasePrevPresentation = prev.props.presentation;
+ const UppercasePrevPresentation = prev.props.presentation.component;
return (
- <UppercasePrevPresentation direction="in" progress={prevProgress}>
+ // @ts-expect-error
+
+ <UppercasePrevPresentation
+ passedProps={prev.props.presentation.props ?? {}}
+ presentationDirection="in"
+ presentationProgress={prevProgress}
+ >
{inner}
</UppercasePrevPresentation>
);
}
if (nextProgress !== null && next) {
- const UppercaseNextPresentation = next.props.presentation;
+ const UppercaseNextPresentation = next.props.presentation.component;
return (
- <UppercaseNextPresentation direction="out" progress={nextProgress}>
+ // @ts-expect-error
+ <UppercaseNextPresentation
+ passedProps={next.props.presentation.props ?? {}}
+ presentationDirection="out"
+ presentationProgress={nextProgress}
+ >
{inner}
</UppercaseNextPresentation>
);
diff --git a/packages/transitions/src/index.ts b/packages/transitions/src/index.ts
index 20ce021106..d2b302ac1f 100644
--- a/packages/transitions/src/index.ts
+++ b/packages/transitions/src/index.ts
@@ -1,6 +1,4 @@
-export {SlideTransition} from './presentations/Slide';
-export * from './presentations/Wipe';
-export {WipePresentation} from './presentations/Wipe';
+export {makeWipePresentation} from './presentations/Wipe';
export {makeLinearTiming} from './timings/linear';
export {makeSpringTiming} from './timings/spring';
export {TransitionSeries} from './TransitionSeries';
diff --git a/packages/transitions/src/presentations/Slide.tsx b/packages/transitions/src/presentations/Slide.tsx
deleted file mode 100644
index 458a73da23..0000000000
--- a/packages/transitions/src/presentations/Slide.tsx
+++ /dev/null
@@ -1,63 +0,0 @@
-import {useState} from 'react';
-import {AbsoluteFill, random} from 'remotion';
-import type {TransitionPresentation} from '../types';
-
-export const SlideTransition: TransitionPresentation = ({
- children,
- progress,
- direction,
-}) => {
- const width = 1;
- const height = 1;
- const [clipId] = useState(() => String(random(null)));
-
- const progressInDirection = direction === 'in' ? progress : 1 - progress;
-
- const pathIn = `
- M 0 0
- L ${progressInDirection * width} 0
- L ${progressInDirection * width} ${height}
- L ${0} ${height}
- Z`;
-
- const pathOut = `
- M ${width} ${height}
- L ${width - progressInDirection * width} ${height}
- L ${width - progressInDirection * width} ${0}
- L ${width} ${0}
- Z
- `;
-
- const path = direction === 'in' ? pathIn : pathOut;
-
- return (
- <AbsoluteFill>
- <AbsoluteFill
- style={{
- width: '100%',
- height: '100%',
- justifyContent: 'center',
- alignItems: 'center',
- clipPath: `url(#${clipId})`,
- }}
- >
- {children}
- </AbsoluteFill>
- <AbsoluteFill>
- <svg
- viewBox={`0 0 ${width} ${height}`}
- style={{
- width: '100%',
- height: '100%',
- }}
- >
- <defs>
- <clipPath id={clipId} clipPathUnits="objectBoundingBox">
- <path d={path} fill="red" />
- </clipPath>
- </defs>
- </svg>
- </AbsoluteFill>
- </AbsoluteFill>
- );
-};
diff --git a/packages/transitions/src/presentations/Wipe.tsx b/packages/transitions/src/presentations/Wipe.tsx
index 717597db91..f2ffecf76a 100644
--- a/packages/transitions/src/presentations/Wipe.tsx
+++ b/packages/transitions/src/presentations/Wipe.tsx
@@ -1,11 +1,28 @@
-import {useState} from 'react';
+import React, {useState} from 'react';
import {AbsoluteFill, random} from 'remotion';
-import type {TransitionPresentation} from '../types';
+import type {
+ TransitionPresentation,
+ TransitionPresentationComponentProps,
+} from '../types';
const width = 1;
const height = 1;
-const makePathIn = (progress: number) => {
+type WipeDirection = 'left' | 'top-left';
+type WipeProps = {
+ direction: WipeDirection;
+};
+
+const makePathIn = (progress: number, direction: WipeDirection) => {
+ if (direction === 'left') {
+ return `
+ M 0 0
+ L ${progress * width} 0
+ L ${progress * width} ${height}
+ L ${0} ${height}
+ Z`;
+ }
+
return `
M 0 0
L ${progress * width * 2} 0
@@ -13,7 +30,17 @@ L ${0} ${height * 2 * progress}
Z`.trim();
};
-const makePathOut = (progress: number) => {
+const makePathOut = (progress: number, direction: WipeDirection) => {
+ if (direction === 'left') {
+ return `
+ M ${width} ${height}
+ L ${width - progress * width} ${height}
+ L ${width - progress * width} ${0}
+ L ${width} ${0}
+ Z
+ `;
+ }
+
return `
M ${width} ${height}
L ${width - 2 * progress * width} ${height}
@@ -22,19 +49,25 @@ Z
`.trim();
};
-export const WipePresentation: TransitionPresentation = ({
+export const WipePresentation: React.FC<
+ TransitionPresentationComponentProps<WipeProps>
+> = ({
children,
- progress,
- direction,
+ presentationProgress,
+ presentationDirection,
+ passedProps: {direction: wipeDirection},
}) => {
const [clipId] = useState(() => String(random(null)));
- const progressInDirection = direction === 'in' ? progress : 1 - progress;
+ const progressInDirection =
+ presentationDirection === 'in'
+ ? presentationProgress
+ : 1 - presentationProgress;
const path =
- direction === 'in'
- ? makePathIn(progressInDirection)
- : makePathOut(progressInDirection);
+ presentationDirection === 'in'
+ ? makePathIn(progressInDirection, wipeDirection)
+ : makePathOut(progressInDirection, wipeDirection);
return (
<AbsoluteFill>
@@ -67,3 +100,12 @@ export const WipePresentation: TransitionPresentation = ({
</AbsoluteFill>
);
};
+
+export const makeWipePresentation = (
+ direction: WipeDirection
+): TransitionPresentation<WipeProps> => {
+ return {
+ component: WipePresentation,
+ props: {direction},
+ };
+};
diff --git a/packages/transitions/src/types.ts b/packages/transitions/src/types.ts
index aad503c71f..153203d48f 100644
--- a/packages/transitions/src/types.ts
+++ b/packages/transitions/src/types.ts
@@ -1,3 +1,5 @@
+import type {ComponentType} from 'react';
+
export type TransitionDirection = 'in' | 'out';
export type TransitionTiming = {
@@ -5,13 +7,29 @@ export type TransitionTiming = {
getProgress: (options: {frame: number; fps: number}) => number;
};
-export type TransitionSeriesTransitionProps = {
+export type TransitionSeriesTransitionProps<
+ PresentationProps extends Record<string, unknown>
+> = {
timing: TransitionTiming;
- presentation: TransitionPresentation;
+ presentation: TransitionPresentation<PresentationProps>;
};
-export type TransitionPresentation = React.FC<{
- progress: number;
+type LooseComponentType<T> = ComponentType<T> | ((props: T) => React.ReactNode);
+
+export type TransitionPresentation<
+ PresentationProps extends Record<string, unknown>
+> = {
+ component: LooseComponentType<
+ TransitionPresentationComponentProps<PresentationProps>
+ >;
+ props: PresentationProps;
+};
+
+export type TransitionPresentationComponentProps<
+ PresentationProps extends Record<string, unknown>
+> = {
+ presentationProgress: number;
children: React.ReactNode;
- direction: TransitionDirection;
-}>;
+ presentationDirection: TransitionDirection;
+ passedProps: PresentationProps;
+};
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 03ed847b2c..385b9559c6 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1769,8 +1769,8 @@ importers:
specifier: 18.0.26
version: 18.0.26
'@types/react-dom':
- specifier: ^18.0.5
- version: 18.0.11
+ specifier: 18.0.10
+ version: 18.0.10
eslint:
specifier: 8.42.0
version: 8.42.0
@@ -9242,7 +9242,7 @@ packages:
resolution: {integrity: sha512-E42GW/JA4Qv15wQdqJq8DL4JhNpB3prJgjgapN3qJT9K2zO5IIAQh4VXvCEDupoqAwnz0cY4RlXeC/ajX5SFHg==}
dependencies:
- '@types/react': 18.0.26
+ '@types/react': 18.0.38
resolution: {integrity: sha512-O38bPbI2CWtgw/OoQoY+BRelw7uysmXbWvw3nLWO21H1HSh+GOlqPuXshJfjmpNlKiiSDG9cc1JZAaMmVdcTlw==}