Skip to content

🎥 Make videos programmatically with React

document player inside iframe

Jonny Burger6e542f062bc3
diff --git a/packages/docs/docs/miscellaneous/snippets/player-in-iframe.md b/packages/docs/docs/miscellaneous/snippets/player-in-iframe.md
new file mode 100644
index 0000000000..2857607e83
--- /dev/null
+++ b/packages/docs/docs/miscellaneous/snippets/player-in-iframe.md
@@ -0,0 +1,83 @@
+---
+title: "Embedding a <Player> into an <iframe>"
+---
+
+This snippet is useful if you want to isolate the global styles of your homepage from the global styles of the [`<Player>`](/docs/player), for example if you are using TailwindCSS.
+
+Don't forget to give your `<Player>` a `className` of `__player` if you use this snippet.
+
+```tsx title="IframePlayerWrapper.tsx"
+import React, { useEffect, useRef, useState } from "react";
+import ReactDOM from "react-dom";
+
+const className = "__player";
+const borderNone: React.CSSProperties = {
+ border: "none",
+};
+
+export const IframePlayerWrapper: React.FC<{ children: React.ReactNode }> = ({
+ children,
+}) => {
+ const [contentRef, setContentRef] = useState<HTMLIFrameElement | null>(null);
+ const resizeObserverRef = useRef<ResizeObserver | null>(null);
+
+ const mountNode = contentRef?.contentDocument?.body;
+
+ useEffect(() => {
+ if (!contentRef || !contentRef.contentDocument) return;
+
+ // Remove margin and padding so player fits snugly
+ contentRef.contentDocument.body.style.margin = "0";
+ contentRef.contentDocument.body.style.padding = "0";
+
+ // When player div is resized also resize iframe
+ resizeObserverRef.current = new ResizeObserver(([playerEntry]) => {
+ const playerRect = playerEntry.contentRect;
+ contentRef.width = String(playerRect.width);
+ contentRef.height = String(playerRect.height);
+ });
+
+ // The remotion player element
+ const playerElement = contentRef.contentDocument.querySelector(
+ "." + className
+ );
+
+ if (!playerElement) {
+ throw new Error(
+ 'Player element not found. Add a "' +
+ className +
+ '" class to the <Player>.'
+ );
+ }
+
+ // Watch the player element for size changes
+ resizeObserverRef.current.observe(playerElement as Element);
+
+ return () => {
+ // ContentRef changed: unobserve!
+ (resizeObserverRef.current as ResizeObserver).unobserve(
+ playerElement as Element
+ );
+ };
+ }, [contentRef]);
+
+ return (
+ // eslint-disable-next-line @remotion/warn-native-media-tag
+ <iframe ref={setContentRef} style={borderNone}>
+ {mountNode && ReactDOM.createPortal(children, mountNode)}
+ </iframe>
+ );
+};
+```
+
+```tsx title="Implementation"
+return (
+ <IframePlayerWrapper>
+ <Player className="__player" {/* ... */} />
+ </IframePlayerWrapper>
+)
+```
+
+## See also
+
+- [`<Player>` examples](/docs/player/examples)
diff --git a/packages/docs/docs/player/player-examples.md b/packages/docs/docs/player/player-examples.md
index d71d50a58a..ced5698f69 100644
--- a/packages/docs/docs/player/player-examples.md
+++ b/packages/docs/docs/player/player-examples.md
@@ -265,3 +265,7 @@ export const App: React.FC = () => {
);
};
```
+
+## See also
+
+- [Snippet: Embedding a `<Player>` in an iframe](/docs/miscellaneous/snippets/player-in-iframe)
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 7c56652853..1671109628 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -307,6 +307,11 @@ module.exports = {
"miscellaneous/typescript-aliases",
"miscellaneous/render-all",
"miscellaneous/render-in-browser",
+ {
+ type: "category",
+ label: "Snippets",
+ items: ["miscellaneous/snippets/player-in-iframe"],
+ },
],
},
"3-0-migration",