🎥 Make videos programmatically with React
start useCurrentZoom()
Jonny Burger77f36c1287c5
diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex db18870347..cbcbe57d62 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -133,6 +133,7 @@ export {staticFile} from './static-file.js';export * from './Still.js';export type {PlayableMediaTag} from './timeline-position-state.js';export {useCurrentFrame} from './use-current-frame.js';+export {useCurrentScale} from './use-current-scale';export * from './use-video-config.js';export * from './version.js';export * from './video-config.js';diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.tsindex 49ab4bc90a..b3f7de6cbc 100644--- a/packages/core/src/internals.ts+++ b/packages/core/src/internals.ts@@ -63,6 +63,7 @@ import {useTimelineSetFrame,} from './timeline-position-state.js';import {truthy} from './truthy.js';+import {CurrentScaleContext} from './use-current-scale.js';import {useLazyComponent} from './use-lazy-component.js';import {useUnsafeVideoConfig} from './use-unsafe-video-config.js';import {useVideo} from './use-video.js';@@ -148,6 +149,7 @@ export const Internals = {useMediaStartsAt,enableSequenceStackTraces,colorNames,+ CurrentScaleContext,} as const;export type {diff --git a/packages/core/src/use-current-scale.ts b/packages/core/src/use-current-scale.tsnew file mode 100644index 0000000000..41f169c772--- /dev/null+++ b/packages/core/src/use-current-scale.ts@@ -0,0 +1,33 @@+import React from 'react';++export const CurrentScaleContext = React.createContext<number | null>(null);++type Options = {+ dontThrowIfOutsideOfRemotion: boolean;+};++/**+ * Gets the current scale of the container in which the component is being rendered.+ * Only works in the Remotion Studio and in the Remotion Player.+ */+export const useCurrentScale = (options?: Options) => {+ const scale = React.useContext(CurrentScaleContext);+ const timelineZoomCtx = React.useContext(TimelineZoomCtx);++ if (scale === null) {+ if (options?.dontThrowIfOutsideOfRemotion) {+ return 1;+ }++ throw new Error(+ [+ 'useCurrentScale() was called outside of a Remotion context.',+ 'This hook can only be called in a component that is being rendered by Remotion.',+ 'If you want to this hook to return 1 outside of Remotion, pass {dontThrowIfOutsideOfRemotion: true} as an option.',+ 'If you think you called this hook in a Remotion component, make sure all versions of Remotion are aligned.',+ ].join('\n'),+ );+ }++ return scale;+};diff --git a/packages/eslint-config/src/auto-import-rules.ts b/packages/eslint-config/src/auto-import-rules.tsindex cf150e4089..193ba90bca 100644--- a/packages/eslint-config/src/auto-import-rules.ts+++ b/packages/eslint-config/src/auto-import-rules.ts@@ -31,6 +31,7 @@ export const autoImports = {Img: "import {Img} from 'remotion'",IFrame: "import {IFrame} from 'remotion'",Folder: "import {Folder} from 'remotion'",+ useCurrentScale: "import {useCurrentScale} from 'remotion'",VERSION: "import {VERSION} from 'remotion'",watchStaticFile: "import {watchStaticFile} from 'remotion'",z: "import {z} from 'zod'",diff --git a/packages/example/src/DynamicDuration.tsx b/packages/example/src/DynamicDuration.tsxindex ed9c3a5710..cf1e450c64 100644--- a/packages/example/src/DynamicDuration.tsx+++ b/packages/example/src/DynamicDuration.tsx@@ -1,5 +1,5 @@import React from 'react';-import {AbsoluteFill} from 'remotion';+import {AbsoluteFill, useCurrentScale} from 'remotion';import {z} from 'zod';export const dynamicDurationSchema = z.object({@@ -9,5 +9,6 @@ export const dynamicDurationSchema = z.object({export const DynamicDuration: React.FC<z.infer<typeof dynamicDurationSchema>> = () => {+ const scale = useCurrentScale();return <AbsoluteFill>hi</AbsoluteFill>;};diff --git a/packages/player/src/PlayerUI.tsx b/packages/player/src/PlayerUI.tsxindex 405e48bdd0..91b2d293fa 100644--- a/packages/player/src/PlayerUI.tsx+++ b/packages/player/src/PlayerUI.tsx@@ -502,10 +502,12 @@ const PlayerUI: React.ForwardRefRenderFunction<{VideoComponent ? (<ErrorBoundary onError={onError} errorFallback={errorFallback}><Internals.ClipComposition>- <VideoComponent- {...(video?.props ?? {})}- {...(inputProps ?? {})}- />+ <Internals.CurrentScaleContext.Provider value={scale}>+ <VideoComponent+ {...(video?.props ?? {})}+ {...(inputProps ?? {})}+ />+ </Internals.CurrentScaleContext.Provider></Internals.ClipComposition></ErrorBoundary>) : null}diff --git a/packages/player/src/ThumbnailUI.tsx b/packages/player/src/ThumbnailUI.tsxindex 41b37aa2b5..f304817dbf 100644--- a/packages/player/src/ThumbnailUI.tsx+++ b/packages/player/src/ThumbnailUI.tsx@@ -124,7 +124,12 @@ const ThumbnailUI: React.ForwardRefRenderFunction<<div style={containerStyle} className={PLAYER_CSS_CLASSNAME}>{VideoComponent ? (<ErrorBoundary onError={onError} errorFallback={errorFallback}>- <VideoComponent {...(video?.props ?? {})} {...(inputProps ?? {})} />+ <Internals.CurrentScaleContext.Provider value={scale}>+ <VideoComponent+ {...(video?.props ?? {})}+ {...(inputProps ?? {})}+ />+ </Internals.CurrentScaleContext.Provider></ErrorBoundary>) : null}</div>