Skip to content

🎥 Make videos programmatically with React

figure out the API

Jonny Burger0a272a1804a6
diff --git a/packages/core/src/Clipper.tsx b/packages/core/src/Clipper.tsx
index 7581549b1a..8ce8d7de51 100644
--- a/packages/core/src/Clipper.tsx
+++ b/packages/core/src/Clipper.tsx
@@ -11,7 +11,21 @@ export const Clipper: React.FC<{
const {setClipRegion} = useContext(NativeLayersContext);
useEffect(() => {
- setClipRegion({height, width, x, y});
+ setClipRegion((c) => {
+ if (c === 'hide') {
+ throw new Error(
+ 'Cannot render <Clipper>, because another <Null> is already rendered'
+ );
+ }
+
+ if (c === null) {
+ return {height, width, x, y};
+ }
+
+ throw new Error(
+ 'Cannot render <Clipper>, because another component clipping the region was already rendered (most likely <Clipper>)'
+ );
+ });
return () => {
setClipRegion(null);
diff --git a/packages/core/src/NativeLayers.tsx b/packages/core/src/NativeLayers.tsx
index 6ffd1f8c4b..0c32788d6d 100644
--- a/packages/core/src/NativeLayers.tsx
+++ b/packages/core/src/NativeLayers.tsx
@@ -1,11 +1,5 @@
import type {PropsWithChildren} from 'react';
-import {
- createContext,
- useCallback,
- useLayoutEffect,
- useMemo,
- useState,
-} from 'react';
+import {createContext, useLayoutEffect, useMemo, useState} from 'react';
export type ClipRegion =
| {
@@ -16,12 +10,12 @@ export type ClipRegion =
}
| 'hide';
-export type CompositionManagerContext = {
+export type TNativeLayersContext = {
clipRegion: ClipRegion | null;
- setClipRegion: (clip: ClipRegion | null) => void;
+ setClipRegion: React.Dispatch<React.SetStateAction<ClipRegion | null>>;
};
-export const NativeLayersContext = createContext<CompositionManagerContext>({
+export const NativeLayersContext = createContext<TNativeLayersContext>({
setClipRegion: () => {
throw new Error('NativeLayers not set');
},
@@ -31,11 +25,7 @@ export const NativeLayersContext = createContext<CompositionManagerContext>({
export const NativeLayersProvider: React.FC<PropsWithChildren> = ({
children,
}) => {
- const [clipRegion, setRegion] = useState<ClipRegion | null>(null);
-
- const setClipRegion = useCallback((region: ClipRegion | null) => {
- setRegion(region);
- }, []);
+ const [clipRegion, setClipRegion] = useState<ClipRegion | null>(null);
const context = useMemo(() => {
return {
diff --git a/packages/core/src/Null.tsx b/packages/core/src/Null.tsx
index 4b03e64aa4..1f6e1c2b21 100644
--- a/packages/core/src/Null.tsx
+++ b/packages/core/src/Null.tsx
@@ -5,7 +5,20 @@ export const Null: React.FC = () => {
const {setClipRegion} = useContext(NativeLayersContext);
useEffect(() => {
- setClipRegion('hide');
+ setClipRegion((c) => {
+ if (c === null) {
+ return 'hide';
+ }
+
+ // Rendering multiple <Null> is fine, because they are all hidden
+ if (c === 'hide') {
+ return 'hide';
+ }
+
+ throw new Error(
+ 'Cannot render <Null>, because another component clipping the region was already rendered (most likely <Clipper>)'
+ );
+ });
return () => {
setClipRegion(null);
diff --git a/packages/docs/docs/clipper.md b/packages/docs/docs/clipper.md
index 294ed76ffa..cc256820a0 100644
--- a/packages/docs/docs/clipper.md
+++ b/packages/docs/docs/clipper.md
@@ -44,7 +44,8 @@ The properties `x`, `y`, `width` and `height` define a rect in pixels that shoul
<Step>2</Step> If the <code>imageFormat</code> is <code>jpeg</code>, the remaining frame will become black, if the <code>imageFormat</code> is <code>png</code>, the remaining frame will become transparent.
</p>
<p>
-<Step>3</Step> If this component is rendered, an <a href="/docs/null"><code>{"<Experimental.Null>"}</code></a> component may not be rendered at the same time.
+<Step>3</Step> Only one <a href="/docs/null"><code>{"<Experimental.Null>"}</code></a> or <code>{"<Experimental.Clipper>"}</code> component can be rendered per frame.
+ Rendering multiple is an error.
</p>
## See also
diff --git a/packages/docs/docs/null.md b/packages/docs/docs/null.md
index 72f1e1ebbf..ed991ac4d9 100644
--- a/packages/docs/docs/null.md
+++ b/packages/docs/docs/null.md
@@ -29,19 +29,27 @@ export const MyComp: React.FC = () => {
## Rules
<p>
-<Step>1</Step> If this component is rendered, nothing visual in the frame will be rendered.
+<Step>1</Step> If this component is rendered, no screenshot will be taken during rendering
</p>
<p>
-<Step>2</Step> Even elements <strong>outside</strong> of the <code>{"<Null>"}</code> component will disappear.
+<Step>2</Step> In the preview, the content will be rendered with zero opacity.
</p>
<p>
-<Step>3</Step> Audio will still be rendered.
+<Step>3</Step> Even elements <strong>outside</strong> of the <code>{"<Null>"}</code> component will disappear.
</p>
<p>
-<Step>4</Step> If the <code>imageFormat</code> is <code>jpeg</code>, a black frame will be generated, if the <code>imageFormat</code> is <code>png</code>, a transparent frame will be generated.
+<Step>4</Step> Audio will still be rendered.
</p>
<p>
-<Step>5</Step> If this component is rendered, an <a href="/docs/clipper"><code>{"<Experimental.Clipper>"}</code></a> may not be rendered at the same time.
+<Step>5</Step> Effects will still be executed.
+</p>
+
+<p>
+<Step>6</Step> If the <code>imageFormat</code> is <code>jpeg</code>, a black frame will be generated, if the <code>imageFormat</code> is <code>png</code>, a transparent frame will be generated.
+</p>
+<p>
+<Step>7</Step> Only one <code>{"<Experimental.Null>"}</code> or <a href="/docs/clipper"><code>{"<Experimental.Clipper>"}</code></a> component can be rendered per frame.
+ Rendering multiple is an error.
</p>
## See also