Skip to content

🎥 Make videos programmatically with React

alright

Jonny Burger44b66daaf18f
diff --git a/packages/docs/.eslintrc b/packages/docs/.eslintrc
new file mode 100644
index 0000000000..27adb58b3d
--- /dev/null
+++ b/packages/docs/.eslintrc
@@ -0,0 +1,6 @@
+{
+ "extends": "@jonny",
+ "rules": {
+ "ts-exports/unused-exports": "off"
+ }
+}
diff --git a/packages/docs/src/components/VideoPlayer.tsx b/packages/docs/src/components/VideoPlayer.tsx
index 15df6bbb10..ffe20e7bdd 100644
--- a/packages/docs/src/components/VideoPlayer.tsx
+++ b/packages/docs/src/components/VideoPlayer.tsx
@@ -1,4 +1,4 @@
-import React, { useEffect, useRef } from "react";
+import React, { SVGProps, useEffect, useRef } from "react";
import { ShowcaseVideo } from "../data/showcase-videos";
import { VideoPlayerContent } from "./VideoPlayerContent";
@@ -14,40 +14,120 @@ const container: React.CSSProperties = {
display: "flex",
justifyContent: "center",
alignItems: "center",
+ flexDirection: "row",
};
+const changeButton: React.CSSProperties = {
+ padding: 20,
+ cursor: "pointer",
+};
+
+const changeButtonInactive: React.CSSProperties = {
+ padding: 20,
+ cursor: "default",
+ opacity: 0.5,
+};
+
+const icon: React.CSSProperties = {
+ width: 24,
+ color: "white",
+};
+
+const IconLeft: React.FC<SVGProps<SVGSVGElement>> = (props) => (
+ <svg viewBox="0 0 320 512" {...props}>
+ <path
+ fill="currentColor"
+ d="M34.52 239.03L228.87 44.69c9.37-9.37 24.57-9.37 33.94 0l22.67 22.67c9.36 9.36 9.37 24.52.04 33.9L131.49 256l154.02 154.75c9.34 9.38 9.32 24.54-.04 33.9l-22.67 22.67c-9.37 9.37-24.57 9.37-33.94 0L34.52 272.97c-9.37-9.37-9.37-24.57 0-33.94z"
+ />
+ </svg>
+);
+const IconRight: React.FC<SVGProps<SVGSVGElement>> = (props) => (
+ <svg viewBox="0 0 320 512" {...props}>
+ <path
+ fill="currentColor"
+ d="M285.476 272.971L91.132 467.314c-9.373 9.373-24.569 9.373-33.941 0l-22.667-22.667c-9.357-9.357-9.375-24.522-.04-33.901L188.505 256 34.484 101.255c-9.335-9.379-9.317-24.544.04-33.901l22.667-22.667c9.373-9.373 24.569-9.373 33.941 0L285.475 239.03c9.373 9.372 9.373 24.568.001 33.941z"
+ />
+ </svg>
+);
+
export const VideoPlayer: React.FC<{
video: ShowcaseVideo | null;
dismiss: () => void;
-}> = ({ video, dismiss }) => {
+ toNext: () => void;
+ toPrevious: () => void;
+ hasPrevious: boolean;
+ hasNext: boolean;
+}> = ({ video, dismiss, toNext, toPrevious, hasNext, hasPrevious }) => {
const outside = useRef<HTMLDivElement>(null);
const inside = useRef<HTMLDivElement>(null);
+ const backButton = useRef<HTMLDivElement>(null);
+ const forwardButton = useRef<HTMLDivElement>(null);
useEffect(() => {
const { current } = outside;
+ const { current: insideCurrent } = inside;
+ const { current: backButtonCurrent } = backButton;
+ const { current: forwardButtonCurrent } = forwardButton;
if (!current) {
return;
}
+
+ if (!insideCurrent) {
+ return;
+ }
+
+ if (!backButtonCurrent) {
+ return;
+ }
+
+ if (!forwardButtonCurrent) {
+ return;
+ }
+
const onClick = (event: MouseEvent) => {
- // @ts-expect-error
- if (inside.current.contains(event.target)) {
+ if (insideCurrent.contains(event.target as Node | null)) {
+ return;
+ }
+
+ if (backButtonCurrent.contains(event.target as Node | null)) {
+ return;
+ }
+
+ if (forwardButtonCurrent.contains(event.target as Node | null)) {
return;
}
+
dismiss();
};
+
current.addEventListener("click", onClick);
return () => {
current.removeEventListener("click", onClick);
};
- }, [video]);
+ }, [dismiss, video]);
if (!video) {
return null;
}
+
return (
<div ref={outside} style={container}>
+ <div
+ ref={backButton}
+ style={hasPrevious ? changeButton : changeButtonInactive}
+ onClick={toPrevious}
+ >
+ <IconLeft style={icon} />
+ </div>
<div ref={inside}>
- <VideoPlayerContent video={video}></VideoPlayerContent>
+ <VideoPlayerContent video={video} />
+ </div>
+ <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 39dd7f7610..14f92c207b 100644
--- a/packages/docs/src/components/VideoPlayerContent.tsx
+++ b/packages/docs/src/components/VideoPlayerContent.tsx
@@ -1,4 +1,3 @@
-import useBaseUrl from "@docusaurus/useBaseUrl";
import Hls from "hls.js";
import React, { useEffect, useRef } from "react";
import styled from "styled-components";
@@ -24,11 +23,12 @@ const Sidebar = styled.div`
width: ${RESERVED_FOR_SIDEBAR}px;
`;
-const getVideoToPlayUrl = (video: ShowcaseVideo, vidUrl: string) => {
+const getVideoToPlayUrl = (video: ShowcaseVideo) => {
if (video.type === "mux_video") {
return `https://stream.mux.com/${video.muxId}.m3u8`;
}
- return vidUrl;
+
+ throw new Error("no url");
};
export const VideoPlayerContent: React.FC<{ video: ShowcaseVideo }> = ({
@@ -36,10 +36,7 @@ export const VideoPlayerContent: React.FC<{ video: ShowcaseVideo }> = ({
}) => {
const videoRef = useRef<HTMLVideoElement>(null);
const container = useRef<HTMLDivElement>(null);
- const baseVidUrl = useBaseUrl(
- video.type === "video_url" ? video.videoUrl : undefined
- );
- const vidUrl = getVideoToPlayUrl(video, baseVidUrl);
+ const vidUrl = getVideoToPlayUrl(video);
const containerSize = useElementSize(document.body);
@@ -55,12 +52,10 @@ export const VideoPlayerContent: React.FC<{ video: ShowcaseVideo }> = ({
const width = ratio * video.width;
useEffect(() => {
- let hls;
+ let hls: Hls;
if (videoRef.current) {
- const current = videoRef.current;
- if (video.type === "video_url") {
- current.src = video.videoUrl;
- } else if (current.canPlayType("application/vnd.apple.mpegurl")) {
+ const { current } = videoRef;
+ if (current.canPlayType("application/vnd.apple.mpegurl")) {
// Some browers (safari and ie edge) support HLS natively
current.src = vidUrl;
} else if (Hls.isSupported()) {
@@ -78,13 +73,13 @@ export const VideoPlayerContent: React.FC<{ video: ShowcaseVideo }> = ({
hls.destroy();
}
};
- }, [videoRef]);
+ }, [vidUrl, videoRef]);
return (
<Container ref={container}>
- <Video ref={videoRef} height={height} width={width} autoPlay></Video>
+ <Video ref={videoRef} height={height} width={width} autoPlay />
<Sidebar>
- <VideoSidebar video={video}></VideoSidebar>
+ <VideoSidebar video={video} />
</Sidebar>
</Container>
);
diff --git a/packages/docs/src/components/VideoPreview.tsx b/packages/docs/src/components/VideoPreview.tsx
index fc02da84c9..1f91f109f6 100644
--- a/packages/docs/src/components/VideoPreview.tsx
+++ b/packages/docs/src/components/VideoPreview.tsx
@@ -41,12 +41,15 @@ export const VideoPreview: React.FC<
if (!current) {
return;
}
+
const onPointerEnter = () => {
setHover(true);
};
+
const onPointerLeave = () => {
setHover(false);
};
+
current.addEventListener("pointerenter", onPointerEnter);
current.addEventListener("pointerleave", onPointerLeave);
@@ -72,7 +75,7 @@ export const VideoPreview: React.FC<
backgroundPosition: "50% 50%",
}}
>
- <img width={300} height={300}></img>
+ <img width={300} height={300} />
</div>
</div>
<h3 style={videoTitle}>{title}</h3>
diff --git a/packages/docs/src/pages/showcase/index.tsx b/packages/docs/src/pages/showcase/index.tsx
index 480d82bdec..3a5257d2d8 100644
--- a/packages/docs/src/pages/showcase/index.tsx
+++ b/packages/docs/src/pages/showcase/index.tsx
@@ -1,6 +1,6 @@
import Layout from "@theme/Layout";
import clsx from "clsx";
-import React, { useCallback, useState } from "react";
+import React, { useCallback, useMemo, useState } from "react";
import { VideoPlayer } from "../../components/VideoPlayer";
import { VideoPreview } from "../../components/VideoPreview";
import { ShowcaseVideo, showcaseVideos } from "../../data/showcase-videos";
@@ -21,9 +21,37 @@ const PageHeader: React.FC = () => {
);
};
-function Showcase() {
+const Showcase = () => {
const [video, setVideo] = useState<ShowcaseVideo | null>(null);
+ const currentIndex = useMemo(() => {
+ if (video === null) {
+ return -1;
+ }
+
+ return showcaseVideos.findIndex((v) => v.muxId === video.muxId);
+ }, [video]);
+
+ const hasNext = currentIndex < showcaseVideos.length - 1;
+ const hasPrevious = currentIndex > 0;
+
+ const goToNextVideo = useCallback(() => {
+ console.log(currentIndex, showcaseVideos.length);
+ if (!hasNext) {
+ return;
+ }
+
+ setVideo(showcaseVideos[currentIndex + 1]);
+ }, [currentIndex, hasNext]);
+
+ const goToPreviousVideo = useCallback(() => {
+ if (!hasPrevious) {
+ return;
+ }
+
+ setVideo(showcaseVideos[currentIndex - 1]);
+ }, [currentIndex, hasPrevious]);
+
const dismiss = useCallback(() => {
setVideo(null);
}, []);
@@ -38,19 +66,26 @@ function Showcase() {
<PageHeader />
</div>
</header>
- <VideoPlayer dismiss={dismiss} video={video}></VideoPlayer>
+ <VideoPlayer
+ hasNext={hasNext}
+ hasPrevious={hasPrevious}
+ toNext={goToNextVideo}
+ toPrevious={goToPreviousVideo}
+ dismiss={dismiss}
+ video={video}
+ />
<main>
<section className={styles.videos}>
<div className="container">
<div className="row">
- {showcaseVideos.map((video, idx) => (
+ {showcaseVideos.map((vid) => (
<VideoPreview
+ key={vid.muxId}
onClick={() => {
- setVideo(video);
+ setVideo(vid);
}}
- key={idx}
- {...video}
+ {...vid}
/>
))}
</div>
@@ -59,6 +94,6 @@ function Showcase() {
</main>
</Layout>
);
-}
+};
export default Showcase;