🎥 Make videos programmatically with React
Fixes #861
Jonny Burger3070fa06527b
diff --git a/packages/cli/src/editor/components/Splitter/SplitterHandle.tsx b/packages/cli/src/editor/components/Splitter/SplitterHandle.tsxindex 7d3842d5be..2666562380 100644--- a/packages/cli/src/editor/components/Splitter/SplitterHandle.tsx+++ b/packages/cli/src/editor/components/Splitter/SplitterHandle.tsx@@ -1,3 +1,4 @@+import {PlayerInternals} from '@remotion/player';import React, {useContext, useEffect, useRef, useState} from 'react';import {SplitterContext} from './SplitterContext';@@ -95,6 +96,7 @@ export const SplitterHandle: React.FC = () => {current.removeEventListener('pointerdown', onPointerDown);window.removeEventListener('pointermove', onPointerMove);+ PlayerInternals.updateAllElementsSizes();};current.addEventListener('pointerdown', onPointerDown);diff --git a/packages/cli/src/editor/types/ResizeObserver.d.ts b/packages/cli/src/editor/types/ResizeObserver.d.tsdeleted file mode 100644index f2da914d4a..0000000000--- a/packages/cli/src/editor/types/ResizeObserver.d.ts+++ /dev/null@@ -1,242 +0,0 @@-/**- * The **ResizeObserver** interface reports changes to the dimensions of an- * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element)'s content- * or border box, or the bounding box of an- * [SVGElement](https://developer.mozilla.org/en-US/docs/Web/API/SVGElement).- *- * > **Note**: The content box is the box in which content can be placed,- * > meaning the border box minus the padding and border width. The border box- * > encompasses the content, padding, and border. See- * > [The box model](https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/The_box_model)- * > for further explanation.- *- * `ResizeObserver` avoids infinite callback loops and cyclic dependencies that- * are often created when resizing via a callback function. It does this by only- * processing elements deeper in the DOM in subsequent frames. Implementations- * should, if they follow the specification, invoke resize events before paint- * and after layout.- *- * @see https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver- */-declare class ResizeObserver {- /**- * The **ResizeObserver** constructor creates a new `ResizeObserver` object,- * which can be used to report changes to the content or border box of an- * `Element` or the bounding box of an `SVGElement`.- *- * @example- * var ResizeObserver = new ResizeObserver(callback)- *- * @param callback- * The function called whenever an observed resize occurs. The function is- * called with two parameters:- * * **entries**- * An array of- * [ResizeObserverEntry](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry)- * objects that can be used to access the new dimensions of the element- * after each change.- * * **observer**- * A reference to the `ResizeObserver` itself, so it will definitely be- * accessible from inside the callback, should you need it. This could be- * used for example to automatically unobserve the observer when a certain- * condition is reached, but you can omit it if you don't need it.- *- * The callback will generally follow a pattern along the lines of:- * ```js- * function(entries, observer) {- * for (let entry of entries) {- * // Do something to each entry- * // and possibly something to the observer itself- * }- * }- * ```- *- * The following snippet is taken from the- * [resize-observer-text.html](https://mdn.github.io/dom-examples/resize-observer/resize-observer-text.html)- * ([see source](https://github.com/mdn/dom-examples/blob/master/resize-observer/resize-observer-text.html))- * example:- * @example- * const resizeObserver = new ResizeObserver(entries => {- * for (let entry of entries) {- * if(entry.contentBoxSize) {- * h1Elem.style.fontSize = Math.max(1.5, entry.contentBoxSize.inlineSize/200) + 'rem';- * pElem.style.fontSize = Math.max(1, entry.contentBoxSize.inlineSize/600) + 'rem';- * } else {- * h1Elem.style.fontSize = Math.max(1.5, entry.contentRect.width/200) + 'rem';- * pElem.style.fontSize = Math.max(1, entry.contentRect.width/600) + 'rem';- * }- * }- * });- *- * resizeObserver.observe(divElem);- */- constructor(callback: ResizeObserverCallback);-- /**- * The **disconnect()** method of the- * [ResizeObserver](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver)- * interface unobserves all observed- * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) or- * [SVGElement](https://developer.mozilla.org/en-US/docs/Web/API/SVGElement)- * targets.- */- disconnect: () => void;-- /**- * The `observe()` method of the- * [ResizeObserver](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver)- * interface starts observing the specified- * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) or- * [SVGElement](https://developer.mozilla.org/en-US/docs/Web/API/SVGElement).- *- * @example- * resizeObserver.observe(target, options);- *- * @param target- * A reference to an- * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) or- * [SVGElement](https://developer.mozilla.org/en-US/docs/Web/API/SVGElement)- * to be observed.- *- * @param options- * An options object allowing you to set options for the observation.- * Currently this only has one possible option that can be set.- */- observe: (target: Element, options?: ResizeObserverObserveOptions) => void;-- /**- * The **unobserve()** method of the- * [ResizeObserver](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver)- * interface ends the observing of a specified- * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) or- * [SVGElement](https://developer.mozilla.org/en-US/docs/Web/API/SVGElement).- */- unobserve: (target: Element) => void;-}--interface ResizeObserverObserveOptions {- /**- * Sets which box model the observer will observe changes to. Possible values- * are `content-box` (the default), and `border-box`.- *- * @default "content-box"- */- box?: 'content-box' | 'border-box';-}--/**- * The function called whenever an observed resize occurs. The function is- * called with two parameters:- *- * @param entries- * An array of- * [ResizeObserverEntry](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry)- * objects that can be used to access the new dimensions of the element after- * each change.- *- * @param observer- * A reference to the `ResizeObserver` itself, so it will definitely be- * accessible from inside the callback, should you need it. This could be used- * for example to automatically unobserve the observer when a certain condition- * is reached, but you can omit it if you don't need it.- *- * The callback will generally follow a pattern along the lines of:- * @example- * function(entries, observer) {- * for (let entry of entries) {- * // Do something to each entry- * // and possibly something to the observer itself- * }- * }- *- * @example- * const resizeObserver = new ResizeObserver(entries => {- * for (let entry of entries) {- * if(entry.contentBoxSize) {- * h1Elem.style.fontSize = Math.max(1.5, entry.contentBoxSize.inlineSize/200) + 'rem';- * pElem.style.fontSize = Math.max(1, entry.contentBoxSize.inlineSize/600) + 'rem';- * } else {- * h1Elem.style.fontSize = Math.max(1.5, entry.contentRect.width/200) + 'rem';- * pElem.style.fontSize = Math.max(1, entry.contentRect.width/600) + 'rem';- * }- * }- * });- *- * resizeObserver.observe(divElem);- */-type ResizeObserverCallback = (- entries: ResizeObserverEntry[],- observer: ResizeObserver-) => void;--/**- * The **ResizeObserverEntry** interface represents the object passed to the- * [ResizeObserver()](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver/ResizeObserver)- * constructor's callback function, which allows you to access the new- * dimensions of the- * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) or- * [SVGElement](https://developer.mozilla.org/en-US/docs/Web/API/SVGElement)- * being observed.- */-interface ResizeObserverEntry {- /**- * An object containing the new border box size of the observed element when- * the callback is run.- */- readonly borderBoxSize: ResizeObserverEntryBoxSize;-- /**- * An object containing the new content box size of the observed element when- * the callback is run.- */- readonly contentBoxSize: ResizeObserverEntryBoxSize;-- /**- * A [DOMRectReadOnly](https://developer.mozilla.org/en-US/docs/Web/API/DOMRectReadOnly)- * object containing the new size of the observed element when the callback is- * run. Note that this is better supported than the above two properties, but- * it is left over from an earlier implementation of the Resize Observer API,- * is still included in the spec for web compat reasons, and may be deprecated- * in future versions.- */- // node_modules/typescript/lib/lib.dom.d.ts- readonly contentRect: DOMRectReadOnly;-- /**- * A reference to the- * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) or- * [SVGElement](https://developer.mozilla.org/en-US/docs/Web/API/SVGElement)- * being observed.- */- readonly target: Element;-}--/**- * The **borderBoxSize** read-only property of the- * [ResizeObserverEntry](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry)- * interface returns an object containing the new border box size of the- * observed element when the callback is run.- */-interface ResizeObserverEntryBoxSize {- /**- * The length of the observed element's border box in the block dimension. For- * boxes with a horizontal- * [writing-mode](https://developer.mozilla.org/en-US/docs/Web/CSS/writing-mode),- * this is the vertical dimension, or height; if the writing-mode is vertical,- * this is the horizontal dimension, or width.- */- blockSize: number;-- /**- * The length of the observed element's border box in the inline dimension.- * For boxes with a horizontal- * [writing-mode](https://developer.mozilla.org/en-US/docs/Web/CSS/writing-mode),- * this is the horizontal dimension, or width; if the writing-mode is- * vertical, this is the vertical dimension, or height.- */- inlineSize: number;-}--interface Window {- ResizeObserver: typeof ResizeObserver;-}diff --git a/packages/player/src/index.tsx b/packages/player/src/index.tsxindex 6b15fb2251..b6c35a3a97 100644--- a/packages/player/src/index.tsx+++ b/packages/player/src/index.tsx@@ -4,7 +4,7 @@ import {CallbackListener, EventTypes, PlayerEmitter} from './event-emitter';import {useHoverState} from './use-hover-state';import {usePlayback} from './use-playback';import {usePlayer} from './use-player';-import {useElementSize} from './utils/use-element-size';+import {updateAllElementsSizes, useElementSize} from './utils/use-element-size';export {Player, ErrorFallback} from './Player';export type {RenderLoading} from './PlayerUI';@@ -20,6 +20,7 @@ export const PlayerInternals = {useElementSize,calculateScale,useHoverState,+ updateAllElementsSizes,};export type {CallbackListener, EventTypes};diff --git a/packages/player/src/types.d.ts b/packages/player/src/types.d.tsdeleted file mode 100644index 664a48bda4..0000000000--- a/packages/player/src/types.d.ts+++ /dev/null@@ -1,244 +0,0 @@-/**- * The **ResizeObserver** interface reports changes to the dimensions of an- * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element)'s content- * or border box, or the bounding box of an- * [SVGElement](https://developer.mozilla.org/en-US/docs/Web/API/SVGElement).- *- * > **Note**: The content box is the box in which content can be placed,- * > meaning the border box minus the padding and border width. The border box- * > encompasses the content, padding, and border. See- * > [The box model](https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/The_box_model)- * > for further explanation.- *- * `ResizeObserver` avoids infinite callback loops and cyclic dependencies that- * are often created when resizing via a callback function. It does this by only- * processing elements deeper in the DOM in subsequent frames. Implementations- * should, if they follow the specification, invoke resize events before paint- * and after layout.- *- * @see https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver- */-declare class ResizeObserver {- /**- * The **ResizeObserver** constructor creates a new `ResizeObserver` object,- * which can be used to report changes to the content or border box of an- * `Element` or the bounding box of an `SVGElement`.- *- * @example- * var ResizeObserver = new ResizeObserver(callback)- *- * @param callback- * The function called whenever an observed resize occurs. The function is- * called with two parameters:- * * **entries**- * An array of- * [ResizeObserverEntry](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry)- * objects that can be used to access the new dimensions of the element- * after each change.- * * **observer**- * A reference to the `ResizeObserver` itself, so it will definitely be- * accessible from inside the callback, should you need it. This could be- * used for example to automatically unobserve the observer when a certain- * condition is reached, but you can omit it if you don't need it.- *- * The callback will generally follow a pattern along the lines of:- * ```js- * function(entries, observer) {- * for (let entry of entries) {- * // Do something to each entry- * // and possibly something to the observer itself- * }- * }- * ```- *- * The following snippet is taken from the- * [resize-observer-text.html](https://mdn.github.io/dom-examples/resize-observer/resize-observer-text.html)- * ([see source](https://github.com/mdn/dom-examples/blob/master/resize-observer/resize-observer-text.html))- * example:- * @example- * const resizeObserver = new ResizeObserver(entries => {- * for (let entry of entries) {- * if(entry.contentBoxSize) {- * h1Elem.style.fontSize = Math.max(1.5, entry.contentBoxSize.inlineSize/200) + 'rem';- * pElem.style.fontSize = Math.max(1, entry.contentBoxSize.inlineSize/600) + 'rem';- * } else {- * h1Elem.style.fontSize = Math.max(1.5, entry.contentRect.width/200) + 'rem';- * pElem.style.fontSize = Math.max(1, entry.contentRect.width/600) + 'rem';- * }- * }- * });- *- * resizeObserver.observe(divElem);- */- constructor(callback: ResizeObserverCallback);-- /**- * The **disconnect()** method of the- * [ResizeObserver](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver)- * interface unobserves all observed- * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) or- * [SVGElement](https://developer.mozilla.org/en-US/docs/Web/API/SVGElement)- * targets.- */- disconnect: () => void;-- /**- * The `observe()` method of the- * [ResizeObserver](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver)- * interface starts observing the specified- * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) or- * [SVGElement](https://developer.mozilla.org/en-US/docs/Web/API/SVGElement).- *- * @example- * resizeObserver.observe(target, options);- *- * @param target- * A reference to an- * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) or- * [SVGElement](https://developer.mozilla.org/en-US/docs/Web/API/SVGElement)- * to be observed.- *- * @param options- * An options object allowing you to set options for the observation.- * Currently this only has one possible option that can be set.- */- // eslint-disable-next-line @typescript-eslint/no-unused-vars- observe: (target: Element, options?: ResizeObserverObserveOptions) => void;-- /**- * The **unobserve()** method of the- * [ResizeObserver](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver)- * interface ends the observing of a specified- * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) or- * [SVGElement](https://developer.mozilla.org/en-US/docs/Web/API/SVGElement).- */- // eslint-disable-next-line @typescript-eslint/no-unused-vars- unobserve: (target: Element) => void;-}--interface ResizeObserverObserveOptions {- /**- * Sets which box model the observer will observe changes to. Possible values- * are `content-box` (the default), and `border-box`.- *- * @default "content-box"- */- box?: 'content-box' | 'border-box';-}--/**- * The function called whenever an observed resize occurs. The function is- * called with two parameters:- *- * @param entries- * An array of- * [ResizeObserverEntry](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry)- * objects that can be used to access the new dimensions of the element after- * each change.- *- * @param observer- * A reference to the `ResizeObserver` itself, so it will definitely be- * accessible from inside the callback, should you need it. This could be used- * for example to automatically unobserve the observer when a certain condition- * is reached, but you can omit it if you don't need it.- *- * The callback will generally follow a pattern along the lines of:- * @example- * function(entries, observer) {- * for (let entry of entries) {- * // Do something to each entry- * // and possibly something to the observer itself- * }- * }- *- * @example- * const resizeObserver = new ResizeObserver(entries => {- * for (let entry of entries) {- * if(entry.contentBoxSize) {- * h1Elem.style.fontSize = Math.max(1.5, entry.contentBoxSize.inlineSize/200) + 'rem';- * pElem.style.fontSize = Math.max(1, entry.contentBoxSize.inlineSize/600) + 'rem';- * } else {- * h1Elem.style.fontSize = Math.max(1.5, entry.contentRect.width/200) + 'rem';- * pElem.style.fontSize = Math.max(1, entry.contentRect.width/600) + 'rem';- * }- * }- * });- *- * resizeObserver.observe(divElem);- */-type ResizeObserverCallback = (- entries: ResizeObserverEntry[],- observer: ResizeObserver-) => void;--/**- * The **ResizeObserverEntry** interface represents the object passed to the- * [ResizeObserver()](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver/ResizeObserver)- * constructor's callback function, which allows you to access the new- * dimensions of the- * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) or- * [SVGElement](https://developer.mozilla.org/en-US/docs/Web/API/SVGElement)- * being observed.- */-interface ResizeObserverEntry {- /**- * An object containing the new border box size of the observed element when- * the callback is run.- */- readonly borderBoxSize: ResizeObserverEntryBoxSize;-- /**- * An object containing the new content box size of the observed element when- * the callback is run.- */- readonly contentBoxSize: ResizeObserverEntryBoxSize;-- /**- * A [DOMRectReadOnly](https://developer.mozilla.org/en-US/docs/Web/API/DOMRectReadOnly)- * object containing the new size of the observed element when the callback is- * run. Note that this is better supported than the above two properties, but- * it is left over from an earlier implementation of the Resize Observer API,- * is still included in the spec for web compat reasons, and may be deprecated- * in future versions.- */- // node_modules/typescript/lib/lib.dom.d.ts- readonly contentRect: DOMRectReadOnly;-- /**- * A reference to the- * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) or- * [SVGElement](https://developer.mozilla.org/en-US/docs/Web/API/SVGElement)- * being observed.- */- readonly target: Element;-}--/**- * The **borderBoxSize** read-only property of the- * [ResizeObserverEntry](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry)- * interface returns an object containing the new border box size of the- * observed element when the callback is run.- */-interface ResizeObserverEntryBoxSize {- /**- * The length of the observed element's border box in the block dimension. For- * boxes with a horizontal- * [writing-mode](https://developer.mozilla.org/en-US/docs/Web/CSS/writing-mode),- * this is the vertical dimension, or height; if the writing-mode is vertical,- * this is the horizontal dimension, or width.- */- blockSize: number;-- /**- * The length of the observed element's border box in the inline dimension.- * For boxes with a horizontal- * [writing-mode](https://developer.mozilla.org/en-US/docs/Web/CSS/writing-mode),- * this is the horizontal dimension, or width; if the writing-mode is- * vertical, this is the vertical dimension, or height.- */- inlineSize: number;-}--interface Window {- ResizeObserver: typeof ResizeObserver;-}diff --git a/packages/player/src/utils/use-element-size.ts b/packages/player/src/utils/use-element-size.tsindex e33266e12f..698b14f2a9 100644--- a/packages/player/src/utils/use-element-size.ts+++ b/packages/player/src/utils/use-element-size.ts@@ -7,6 +7,20 @@ export type Size = {top: number;};+// If a pane has been moved, it will cause a layout shift without+// the window having been resized. Those UI elements can call this API to+// force an update++type ElementSizeForceUpdate = () => void;++let elementSizeHooks: ElementSizeForceUpdate[] = [];++export const updateAllElementsSizes = () => {+ for (const listener of elementSizeHooks) {+ listener();+ }+};+export const useElementSize = (ref: React.RefObject<HTMLElement>,options: {@@ -97,5 +111,13 @@ export const useElementSize = (};}, [options.triggerOnWindowResize, updateSize]);+ useEffect(() => {+ elementSizeHooks.push(updateSize);++ return () => {+ elementSizeHooks = elementSizeHooks.filter((e) => e !== updateSize);+ };+ }, [updateSize]);+return size;};