Skip to content

🎥 Make videos programmatically with React

yes

Jonny Burger8c5646fdf995
diff --git a/packages/docs/src/components/VideoPlayer.tsx b/packages/docs/src/components/VideoPlayer.tsx
index 8709dcc52a..d1c57658cc 100644
--- a/packages/docs/src/components/VideoPlayer.tsx
+++ b/packages/docs/src/components/VideoPlayer.tsx
@@ -1,5 +1,6 @@
-import React, { SVGProps, useEffect, useRef } from "react";
+import React, { SVGProps, useEffect, useMemo, useRef } from "react";
import { ShowcaseVideo } from "../data/showcase-videos";
+import { useElementSize } from "../helpers/use-el-size";
import { VideoPlayerContent } from "./VideoPlayerContent";
const container: React.CSSProperties = {
@@ -58,6 +59,18 @@ export const VideoPlayer: React.FC<{
hasPrevious: boolean;
hasNext: boolean;
}> = ({ video, dismiss, toNext, toPrevious, hasNext, hasPrevious }) => {
+ const containerSize = useElementSize(
+ typeof document === "undefined" ? null : document.body
+ );
+ const mobileLayout = (containerSize?.width ?? Infinity) < 900;
+
+ const containerWithDirection: React.CSSProperties = useMemo(() => {
+ return {
+ ...container,
+ flexDirection: mobileLayout ? "column" : "row",
+ };
+ }, [mobileLayout]);
+
const outside = useRef<HTMLDivElement>(null);
const inside = useRef<HTMLDivElement>(null);
const backButton = useRef<HTMLDivElement>(null);
@@ -76,11 +89,11 @@ export const VideoPlayer: React.FC<{
return;
}
- if (!backButtonCurrent) {
+ if (!mobileLayout && !backButtonCurrent) {
return;
}
- if (!forwardButtonCurrent) {
+ if (!mobileLayout && !forwardButtonCurrent) {
return;
}
@@ -89,11 +102,21 @@ export const VideoPlayer: React.FC<{
return;
}
- if (backButtonCurrent.contains(event.target as Node | null)) {
+ if (
+ !mobileLayout &&
+ (backButtonCurrent as HTMLDivElement).contains(
+ event.target as Node | null
+ )
+ ) {
return;
}
- if (forwardButtonCurrent.contains(event.target as Node | null)) {
+ if (
+ !mobileLayout &&
+ (forwardButtonCurrent as HTMLDivElement).contains(
+ event.target as Node | null
+ )
+ ) {
return;
}
@@ -104,7 +127,7 @@ export const VideoPlayer: React.FC<{
return () => {
current.removeEventListener("click", onClick);
};
- }, [dismiss, video]);
+ }, [dismiss, mobileLayout, video]);
useEffect(() => {
if (!video) {
@@ -120,7 +143,7 @@ export const VideoPlayer: React.FC<{
toPrevious();
}
- if (hasPrevious && e.key === "Escape") {
+ if (e.key === "Escape") {
dismiss();
}
};
@@ -136,24 +159,28 @@ export const VideoPlayer: React.FC<{
}
return (
- <div ref={outside} style={container}>
- <div
- ref={backButton}
- style={hasPrevious ? changeButton : changeButtonInactive}
- onClick={toPrevious}
- >
- <IconLeft style={icon} />
- </div>
+ <div ref={outside} style={containerWithDirection}>
+ {mobileLayout ? null : (
+ <div
+ ref={backButton}
+ style={hasPrevious ? changeButton : changeButtonInactive}
+ onClick={toPrevious}
+ >
+ <IconLeft style={icon} />
+ </div>
+ )}
<div ref={inside}>
<VideoPlayerContent video={video} />
</div>
- <div
- ref={forwardButton}
- style={hasNext ? changeButton : changeButtonInactive}
- onClick={toNext}
- >
- <IconRight style={icon} />
- </div>
+ {mobileLayout ? null : (
+ <div
+ ref={forwardButton}
+ style={hasNext ? changeButton : changeButtonInactive}
+ onClick={toNext}
+ >
+ <IconRight style={icon} />
+ </div>
+ )}
</div>
);
};
diff --git a/packages/docs/src/components/VideoPlayerContent.tsx b/packages/docs/src/components/VideoPlayerContent.tsx
index a37d7f0e00..096477f062 100644
--- a/packages/docs/src/components/VideoPlayerContent.tsx
+++ b/packages/docs/src/components/VideoPlayerContent.tsx
@@ -11,13 +11,6 @@ import { useElementSize } from "../helpers/use-el-size";
import { Spinner } from "./Spinner";
import { VideoSidebar } from "./VideoSidebar";
-const containerCss: React.CSSProperties = {
- backgroundColor: "white",
- marginBottom: 0,
- display: "flex",
- flexDirection: "row",
-};
-
const videoCss: React.CSSProperties = {
marginBottom: 0,
backgroundColor: "white",
@@ -62,13 +55,19 @@ export const VideoPlayerContent: React.FC<{ video: ShowcaseVideo }> = ({
const container = useRef<HTMLDivElement>(null);
const vidUrl = getVideoToPlayUrl(video);
- const containerSize = useElementSize(document.body);
+ const containerSize = useElementSize(
+ typeof document === "undefined" ? null : document.body
+ );
+ const mobileLayout = (containerSize?.width ?? Infinity) < 900;
- const possibleVideoWidth =
- Math.min(containerSize?.width ?? 0, 1200) -
- RESERVED_FOR_SIDEBAR -
- PAGINATE_BUTTONS_WIDTH;
- const containerHeight = Math.min(containerSize?.height ?? 0, 800);
+ const possibleVideoWidth = mobileLayout
+ ? containerSize?.width
+ : Math.min(containerSize?.width ?? 0, 1200) -
+ (mobileLayout ? 0 : RESERVED_FOR_SIDEBAR) -
+ (mobileLayout ? 0 : PAGINATE_BUTTONS_WIDTH);
+ const containerHeight = mobileLayout
+ ? Infinity
+ : Math.min(containerSize?.height ?? 0, 800);
const heightRatio = (containerHeight ?? 0) / video.height;
const widthRatio = (possibleVideoWidth ?? 0) / video.width;
@@ -106,6 +105,20 @@ export const VideoPlayerContent: React.FC<{ video: ShowcaseVideo }> = ({
setLoaded(true);
}, []);
+ const containerCss: React.CSSProperties = useMemo(() => {
+ return {
+ backgroundColor: "white",
+ marginBottom: 0,
+ display: "flex",
+ flexDirection: mobileLayout ? "column" : "row",
+ height: mobileLayout ? "100%" : undefined,
+ width: mobileLayout ? "100%" : undefined,
+ position: mobileLayout ? "absolute" : undefined,
+ left: mobileLayout ? 0 : undefined,
+ top: mobileLayout ? 0 : undefined,
+ };
+ }, [mobileLayout]);
+
const loadingStyle = useMemo(() => {
return {
...loadingContainer,
diff --git a/packages/docs/src/data/showcase-videos.tsx b/packages/docs/src/data/showcase-videos.tsx
index 93729ed855..67ef514bb9 100644
--- a/packages/docs/src/data/showcase-videos.tsx
+++ b/packages/docs/src/data/showcase-videos.tsx
@@ -95,8 +95,7 @@ export const showcaseVideos: ShowcaseVideo[] = [
title: "Love, Death & React",
type: "mux_video",
muxId: "pEo7cREHlak5FxdpNOKB8BYlUCa19Klkfn1XtXxjfxc",
- description:
- "A recreation of Netflix's &quot;Love, Death &amp; React&quot; intro.",
+ description: "A recreation of Netflix's 'Love, Death & React' intro.",
width: 1280,
height: 720,
submittedOn: new Date("29-05-2021"),