🎥 Make videos programmatically with React
import {useContext, useMemo} from 'react';
import type React from 'react';
import {DefaultPremountContext} from './DefaultPremountContext.js';
import {getSequenceBoundaryTolerance} from './get-sequence-boundary-tolerance.js';
import {PremountContext} from './PremountContext.js';
import {SequenceContext} from './SequenceContext.js';
import {useTimelinePositionWithOptionalSubscription} from './timeline-position-state.js';
import {useCurrentFrameWithOptionalSubscription} from './use-current-frame.js';
import {useRemotionEnvironment} from './use-remotion-environment.js';
import {useVideoConfig} from './use-video-config.js';
export const usePremounting = ({
from,
durationInFrames,
premountFor,
postmountFor,
style,
styleWhilePremounted,
styleWhilePostmounted,
hideWhilePremounted,
}: {
readonly from: number;
readonly durationInFrames: number;
readonly premountFor: number | null;
readonly postmountFor: number | null;
readonly style: React.CSSProperties | null;
readonly styleWhilePremounted: React.CSSProperties | null;
readonly styleWhilePostmounted: React.CSSProperties | null;
readonly hideWhilePremounted: 'opacity' | 'display-none';
}) => {
const parentPremountContext = useContext(PremountContext);
const environment = useRemotionEnvironment();
const {fps} = useVideoConfig();
const defaultPremountInSeconds = useContext(DefaultPremountContext);
const effectivePremountFor =
premountFor ?? Math.round(defaultPremountInSeconds * fps);
const effectivePostmountFor = postmountFor ?? 0;
// Premounting is disabled during rendering and cannot activate with empty ranges.
const needsFrame =
!environment.isRendering &&
(effectivePremountFor !== 0 || effectivePostmountFor !== 0);
const frame =
useCurrentFrameWithOptionalSubscription(needsFrame) -
parentPremountContext.premountFramesRemaining;
const endExclusive = from + durationInFrames;
const sequenceContext = useContext(SequenceContext);
const boundaryTolerance = getSequenceBoundaryTolerance({
absoluteFrame: useTimelinePositionWithOptionalSubscription(needsFrame),
cumulatedFrom: sequenceContext
? sequenceContext.cumulatedFrom + sequenceContext.relativeFrom
: 0,
from,
parentPlaybackRate: sequenceContext?.playbackRate ?? 1,
durationInFrames,
});
const premountingActive =
!environment.isRendering &&
frame - from < -boundaryTolerance &&
frame - (from - effectivePremountFor) >= -boundaryTolerance;
const postmountingActive =
!environment.isRendering &&
frame - endExclusive >= -boundaryTolerance &&
frame - (endExclusive + effectivePostmountFor) < -boundaryTolerance;
const isPremountingOrPostmounting = premountingActive || postmountingActive;
const freezeFrame = premountingActive
? from
: postmountingActive
? from + durationInFrames - 1
: 0;
const premountingStyle = useMemo((): React.CSSProperties | null => {
if (!isPremountingOrPostmounting) {
return style;
}
return {
...style,
...(hideWhilePremounted === 'opacity' ? {opacity: 0} : {display: 'none'}),
pointerEvents: 'none',
...(premountingActive ? styleWhilePremounted : {}),
...(postmountingActive ? styleWhilePostmounted : {}),
};
}, [
isPremountingOrPostmounting,
hideWhilePremounted,
postmountingActive,
premountingActive,
style,
styleWhilePostmounted,
styleWhilePremounted,
]);
return {
effectivePremountFor,
effectivePostmountFor,
premountingActive,
postmountingActive,
isPremountingOrPostmounting,
freezeFrame,
premountingStyle,
};
};