Skip to content

🎥 Make videos programmatically with React

fix: forbid nesting HtmlInCanvas and document the constraint

JonnyBurger560a8192ed35
diff --git a/packages/core/src/HtmlInCanvas.tsx b/packages/core/src/HtmlInCanvas.tsx
index 2be08018f0..7db58cd4c6 100644
--- a/packages/core/src/HtmlInCanvas.tsx
+++ b/packages/core/src/HtmlInCanvas.tsx
@@ -1,6 +1,8 @@
import React, {
+ createContext,
forwardRef,
useCallback,
+ useContext,
useLayoutEffect,
useMemo,
useRef,
@@ -261,6 +263,8 @@ export type HtmlInCanvasProps = Omit<
};
/* eslint-enable react/require-default-props */
+const HtmlInCanvasAncestorContext = createContext(false);
+
const htmlInCanvasSchema = {
'style.translate': {
type: 'translate',
@@ -313,6 +317,10 @@ const HtmlInCanvasInner = forwardRef<
},
ref,
) => {
+ const isInsideAncestorHtmlInCanvas = useContext(
+ HtmlInCanvasAncestorContext,
+ );
+
assertHtmlInCanvasDimensions(width, height);
const {continueRender, cancelRender} = useDelayRender();
@@ -513,6 +521,10 @@ const HtmlInCanvasInner = forwardRef<
};
}, [width, height, style]);
+ if (isInsideAncestorHtmlInCanvas) {
+ throw new Error('<HtmlInCanvas> effects cannot be nested together');
+ }
+
return (
<Sequence
durationInFrames={resolvedDuration}
@@ -521,11 +533,13 @@ const HtmlInCanvasInner = forwardRef<
layout="none"
{...sequenceProps}
>
- <canvas ref={setLayoutCanvasRef} width={width} height={height}>
- <div ref={divRef} style={innerStyle}>
- {children}
- </div>
- </canvas>
+ <HtmlInCanvasAncestorContext.Provider value>
+ <canvas ref={setLayoutCanvasRef} width={width} height={height}>
+ <div ref={divRef} style={innerStyle}>
+ {children}
+ </div>
+ </canvas>
+ </HtmlInCanvasAncestorContext.Provider>
</Sequence>
);
},
diff --git a/packages/docs/docs/remotion/html-in-canvas.mdx b/packages/docs/docs/remotion/html-in-canvas.mdx
index da3a66cd99..54b9647f9a 100644
--- a/packages/docs/docs/remotion/html-in-canvas.mdx
+++ b/packages/docs/docs/remotion/html-in-canvas.mdx
@@ -55,6 +55,12 @@ Height of the canvas and the inner layout area, in pixels. Must be a positive in
Children to draw to the canvas.
Children will be wrapped in a `<div>` with the given `width` and `height`.
+Do not nest `<HtmlInCanvas>` inside another `<HtmlInCanvas>`. Nesting is not supported and throws:
+
+```
+<HtmlInCanvas> effects cannot be nested together
+```
+
### `onPaint?`
Called when the children are updated and can be painted onto the canvas.