Skip to content

🎥 Make videos programmatically with React

getting there

Jonny Burger39920fb8daf2
diff --git a/packages/docs/src/components/VideoPlayer.tsx b/packages/docs/src/components/VideoPlayer.tsx
index d1c57658cc..ab7cff1200 100644
--- a/packages/docs/src/components/VideoPlayer.tsx
+++ b/packages/docs/src/components/VideoPlayer.tsx
@@ -1,7 +1,9 @@
-import React, { SVGProps, useEffect, useMemo, useRef } from "react";
+import React, { useEffect, useMemo, useRef } from "react";
import { ShowcaseVideo } from "../data/showcase-videos";
import { useElementSize } from "../helpers/use-el-size";
+import { IconLeft, IconRight } from "../icons/arrows";
import { VideoPlayerContent } from "./VideoPlayerContent";
+import { VidPlayerHeader } from "./VideoPlayerHeader";
const container: React.CSSProperties = {
position: "fixed",
@@ -34,23 +36,6 @@ const icon: React.CSSProperties = {
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;
@@ -75,12 +60,14 @@ export const VideoPlayer: React.FC<{
const inside = useRef<HTMLDivElement>(null);
const backButton = useRef<HTMLDivElement>(null);
const forwardButton = useRef<HTMLDivElement>(null);
+ const headerBar = useRef<HTMLDivElement>(null);
useEffect(() => {
const { current } = outside;
const { current: insideCurrent } = inside;
const { current: backButtonCurrent } = backButton;
const { current: forwardButtonCurrent } = forwardButton;
+ const { current: headerBarCurrent } = headerBar;
if (!current) {
return;
}
@@ -97,6 +84,10 @@ export const VideoPlayer: React.FC<{
return;
}
+ if (mobileLayout && !headerBarCurrent) {
+ return;
+ }
+
const onClick = (event: MouseEvent) => {
if (insideCurrent.contains(event.target as Node | null)) {
return;
@@ -120,6 +111,15 @@ export const VideoPlayer: React.FC<{
return;
}
+ if (
+ mobileLayout &&
+ (headerBarCurrent as HTMLDivElement).contains(
+ event.target as Node | null
+ )
+ ) {
+ return;
+ }
+
dismiss();
};
@@ -172,6 +172,16 @@ export const VideoPlayer: React.FC<{
<div ref={inside}>
<VideoPlayerContent video={video} />
</div>
+ {mobileLayout ? (
+ <VidPlayerHeader
+ ref={headerBar}
+ onNext={toNext}
+ onDismiss={dismiss}
+ onPrevious={toPrevious}
+ hasNext={hasNext}
+ hasPrevious={hasPrevious}
+ />
+ ) : null}
{mobileLayout ? null : (
<div
ref={forwardButton}
diff --git a/packages/docs/src/components/VideoPlayerContent.tsx b/packages/docs/src/components/VideoPlayerContent.tsx
index 096477f062..a84e37e94f 100644
--- a/packages/docs/src/components/VideoPlayerContent.tsx
+++ b/packages/docs/src/components/VideoPlayerContent.tsx
@@ -9,11 +9,12 @@ import React, {
import { ShowcaseVideo } from "../data/showcase-videos";
import { useElementSize } from "../helpers/use-el-size";
import { Spinner } from "./Spinner";
+import { SHOWCASE_MOBILE_HEADER_HEIGHT } from "./VideoPlayerHeader";
import { VideoSidebar } from "./VideoSidebar";
const videoCss: React.CSSProperties = {
marginBottom: 0,
- backgroundColor: "white",
+ backgroundColor: "var(--ifm-hero-background-color)",
};
const RESERVED_FOR_SIDEBAR = 300;
@@ -107,7 +108,7 @@ export const VideoPlayerContent: React.FC<{ video: ShowcaseVideo }> = ({
const containerCss: React.CSSProperties = useMemo(() => {
return {
- backgroundColor: "white",
+ backgroundColor: "var(--ifm-hero-background-color)",
marginBottom: 0,
display: "flex",
flexDirection: mobileLayout ? "column" : "row",
@@ -116,6 +117,7 @@ export const VideoPlayerContent: React.FC<{ video: ShowcaseVideo }> = ({
position: mobileLayout ? "absolute" : undefined,
left: mobileLayout ? 0 : undefined,
top: mobileLayout ? 0 : undefined,
+ overflow: "auto",
};
}, [mobileLayout]);
@@ -129,6 +131,10 @@ export const VideoPlayerContent: React.FC<{ video: ShowcaseVideo }> = ({
return (
<div ref={container} style={containerCss}>
+ {mobileLayout ? (
+ <div style={{ height: SHOWCASE_MOBILE_HEADER_HEIGHT }} />
+ ) : null}
+
{loaded ? null : (
<div style={loadingStyle}>
<Spinner style={spinner} />
diff --git a/packages/docs/src/components/VideoPlayerHeader.tsx b/packages/docs/src/components/VideoPlayerHeader.tsx
new file mode 100644
index 0000000000..2613a7d942
--- /dev/null
+++ b/packages/docs/src/components/VideoPlayerHeader.tsx
@@ -0,0 +1,91 @@
+import useThemeContext from "@theme/hooks/useThemeContext";
+import React, { forwardRef } from "react";
+import { CancelIcon, IconLeft, IconRight } from "../icons/arrows";
+
+export const SHOWCASE_MOBILE_HEADER_HEIGHT = 48;
+
+const container: React.CSSProperties = {
+ height: 48,
+ paddingLeft: 14,
+ paddingRight: 14,
+ display: "flex",
+ alignItems: "center",
+ flexDirection: "row",
+ position: "fixed",
+ width: "100%",
+ backgroundColor: "var(--ifm-hero-background-color)",
+ top: 0,
+};
+
+const button: React.CSSProperties = {
+ padding: 10,
+ cursor: "pointer",
+ display: "inline-flex",
+ justifyContent: "center",
+ alignItems: "center",
+ appearance: "none",
+ border: "none",
+ backgroundColor: "transparent",
+};
+
+const iconStyle: React.CSSProperties = {
+ height: 24,
+};
+
+const flex: React.CSSProperties = {
+ flex: 1,
+};
+
+const space: React.CSSProperties = {
+ width: 20,
+};
+
+const VideoPlayerHeader: React.ForwardRefRenderFunction<
+ HTMLDivElement,
+ {
+ onDismiss: () => void;
+ onNext: () => void;
+ onPrevious: () => void;
+ hasNext: boolean;
+ hasPrevious: boolean;
+ }
+> = ({ onDismiss, onNext, onPrevious, hasNext, hasPrevious }, ref) => {
+ const { isDarkTheme } = useThemeContext();
+
+ const iconColor = isDarkTheme ? "#ddd" : "#222";
+ const borderBottom = isDarkTheme
+ ? "1px solid rgba(255, 255, 255, 0.1)"
+ : "1px solid rgba(0, 0, 0, 0.1)";
+ return (
+ <div ref={ref} style={{ ...container, borderBottom }}>
+ <button onClick={onDismiss} type="button" style={button}>
+ <CancelIcon color={iconColor} style={iconStyle} />
+ </button>
+ <div style={flex} />
+ <button onClick={onPrevious} type="button" style={button}>
+ <IconLeft
+ style={{
+ ...iconStyle,
+ opacity: hasPrevious ? 1 : 0.6,
+ color: iconColor,
+ cursor: hasPrevious ? "pointer" : "default",
+ }}
+ />
+ </button>
+ <div style={space} />
+ <button onClick={onNext} type="button" style={button}>
+ <IconRight
+ color={iconColor}
+ style={{
+ ...iconStyle,
+ opacity: hasNext ? 1 : 0.6,
+ color: iconColor,
+ cursor: hasNext ? "pointer" : "default",
+ }}
+ />
+ </button>
+ </div>
+ );
+};
+
+export const VidPlayerHeader = forwardRef(VideoPlayerHeader);
diff --git a/packages/docs/src/components/VideoPreview.tsx b/packages/docs/src/components/VideoPreview.tsx
index 626708cfac..0f827e73d1 100644
--- a/packages/docs/src/components/VideoPreview.tsx
+++ b/packages/docs/src/components/VideoPreview.tsx
@@ -79,9 +79,7 @@ export const VideoPreview: React.FC<
onClick={onClick}
>
<div className="text--center">
- <div style={style}>
- <img width={300} height={300} />
- </div>
+ <div style={style} />
</div>
<h3 style={videoTitle}>{title}</h3>
<p>{description}</p>
diff --git a/packages/docs/src/icons/arrows.tsx b/packages/docs/src/icons/arrows.tsx
new file mode 100644
index 0000000000..5ccf9be813
--- /dev/null
+++ b/packages/docs/src/icons/arrows.tsx
@@ -0,0 +1,39 @@
+import React, { SVGProps } from "react";
+
+export 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>
+);
+export 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 CancelIcon: React.FC<SVGProps<SVGSVGElement>> = (props) => {
+ return (
+ <svg
+ aria-hidden="true"
+ focusable="false"
+ data-prefix="fas"
+ data-icon="times"
+ className="svg-inline--fa fa-times fa-w-11"
+ role="img"
+ xmlns="http://www.w3.org/2000/svg"
+ viewBox="0 0 352 512"
+ {...props}>
+ >
+ <path
+ fill="currentColor"
+ d="M242.72 256l100.07-100.07c12.28-12.28 12.28-32.19 0-44.48l-22.24-22.24c-12.28-12.28-32.19-12.28-44.48 0L176 189.28 75.93 89.21c-12.28-12.28-32.19-12.28-44.48 0L9.21 111.45c-12.28 12.28-12.28 32.19 0 44.48L109.28 256 9.21 356.07c-12.28 12.28-12.28 32.19 0 44.48l22.24 22.24c12.28 12.28 32.2 12.28 44.48 0L176 322.72l100.07 100.07c12.28 12.28 32.2 12.28 44.48 0l22.24-22.24c12.28-12.28 12.28-32.19 0-44.48L242.72 256z"
+ />
+ </svg>
+ );
+};