Skip to content

🎥 Make videos programmatically with React

real generic demo

Jonny Burger9a927df50957
diff --git a/packages/docs/components/demos/control.tsx b/packages/docs/components/demos/control.tsx
new file mode 100644
index 0000000000..b8be564d65
--- /dev/null
+++ b/packages/docs/components/demos/control.tsx
@@ -0,0 +1,49 @@
+import React, { useMemo } from "react";
+import type { Option } from "./types";
+
+export const Control = ({
+ option,
+ value,
+ setValue,
+}: {
+ option: Option;
+ value: number;
+ setValue: (value: number) => void;
+}) => {
+ const labelStyle = useMemo<React.CSSProperties>(
+ () => ({
+ display: "flex",
+ flexDirection: "row",
+ alignItems: "center",
+ margin: 8,
+ }),
+ []
+ );
+
+ const inputStyle = useMemo<React.CSSProperties>(
+ () => ({
+ width: 90,
+ marginRight: 8,
+ }),
+ []
+ );
+
+ if (option.type !== "numeric") {
+ throw new Error("numeric");
+ }
+
+ return (
+ <label style={labelStyle}>
+ <input
+ type="range"
+ min={option.min}
+ max={option.max}
+ step={option.step}
+ value={value as number}
+ style={inputStyle}
+ onChange={(e) => setValue(Number(e.target.value))}
+ />
+ <code>{`${option.name}={${value}}`}</code>
+ </label>
+ );
+};
diff --git a/packages/docs/components/demos/index.tsx b/packages/docs/components/demos/index.tsx
index 619482f4c6..888089b4c0 100644
--- a/packages/docs/components/demos/index.tsx
+++ b/packages/docs/components/demos/index.tsx
@@ -1,24 +1,8 @@
import { Player } from "@remotion/player";
-import React, { useMemo, useState } from "react";
-import { NoiseComp } from "./NoiseDemo";
-
-type DemoType = {
- id: string;
- comp: React.FC;
- compWidth: number;
- compHeight: number;
- fps: number;
- durationInFrames: number;
-};
-
-const noiseDemo: DemoType = {
- comp: NoiseComp,
- compHeight: 720,
- compWidth: 1280,
- durationInFrames: 150,
- fps: 30,
- id: "noise",
-};
+import React, { useState } from "react";
+import { Control } from "./control";
+import type { DemoType } from "./types";
+import { noiseDemo } from "./types";
const container: React.CSSProperties = {
overflow: "hidden",
@@ -32,33 +16,20 @@ const demos: DemoType[] = [noiseDemo];
export const Demo: React.FC<{
type: string;
}> = ({ type }) => {
- const [maxOffset, setMaxOffset] = useState(50);
- const [speed, setSpeed] = useState(0.01);
- const [circleRadius, setCircleRadius] = useState(5);
-
const demo = demos.find((d) => d.id === type);
if (!demo) {
throw new Error("Demo not found");
}
- const inputStyle = useMemo<React.CSSProperties>(
- () => ({
- width: 90,
- marginRight: 8,
- }),
- []
- );
- const labelStyle = useMemo<React.CSSProperties>(
- () => ({
- display: "flex",
- flexDirection: "row",
- alignItems: "center",
- margin: 8,
- }),
- []
+ const [state, setState] = useState(() =>
+ demo.options
+ .map((o) => [o.name, o.default] as const)
+ .reduce((a, b) => {
+ a[b[0]] = b[1];
+ return a;
+ }, {})
);
-
return (
<div style={container}>
<Player
@@ -71,51 +42,26 @@ export const Demo: React.FC<{
width: "100%",
aspectRatio: demo.compWidth / demo.compHeight,
}}
- inputProps={{
- maxOffset,
- speed,
- circleRadius,
- }}
+ inputProps={state}
autoPlay
loop
/>
<div>
- <label style={labelStyle}>
- <input
- type="range"
- min={0.001}
- max={0.025}
- step={0.001}
- value={speed}
- style={inputStyle}
- onChange={(e) => setSpeed(Number(e.target.value))}
- />
- <code>{`speed={${speed}}`}</code>
- </label>
- <label style={labelStyle}>
- <input
- type="range"
- min={0}
- max={100}
- step={1}
- value={maxOffset}
- style={inputStyle}
- onChange={(e) => setMaxOffset(Number(e.target.value))}
- />
- <code>{`maxOffset={${maxOffset}}`}</code>
- </label>
- <label style={labelStyle}>
- <input
- type="range"
- min={2}
- max={20}
- step={1}
- value={circleRadius}
- style={inputStyle}
- onChange={(e) => setCircleRadius(Number(e.target.value))}
- />
- <code>{`circleRadius={${circleRadius}}`}</code>
- </label>
+ {demo.options.map((option) => {
+ return (
+ <Control
+ key={option.name}
+ option={option}
+ value={state[option.name]}
+ setValue={(value) => {
+ setState((s) => ({
+ ...s,
+ [option.name]: value,
+ }));
+ }}
+ />
+ );
+ })}
</div>
</div>
);
diff --git a/packages/docs/components/demos/types.ts b/packages/docs/components/demos/types.ts
new file mode 100644
index 0000000000..f8bc9fcb47
--- /dev/null
+++ b/packages/docs/components/demos/types.ts
@@ -0,0 +1,62 @@
+import { NoiseComp } from "./NoiseDemo";
+
+export type Option = { name: string } & (
+ | {
+ type: "numeric";
+ min: number;
+ default: number;
+ max: number;
+ step: number;
+ }
+ | {
+ type: "boolean";
+ default: boolean;
+ max: number;
+ step: number;
+ }
+);
+
+export type DemoType = {
+ id: string;
+ comp: React.FC;
+ compWidth: number;
+ compHeight: number;
+ fps: number;
+ durationInFrames: number;
+ options: Option[];
+};
+
+export const noiseDemo: DemoType = {
+ comp: NoiseComp,
+ compHeight: 720,
+ compWidth: 1280,
+ durationInFrames: 150,
+ fps: 30,
+ id: "noise",
+ options: [
+ {
+ default: 0.01,
+ max: 0.025,
+ min: 0.001,
+ step: 0.001,
+ type: "numeric",
+ name: "speed",
+ },
+ {
+ min: 0,
+ max: 100,
+ step: 1,
+ default: 50,
+ name: "maxOffset",
+ type: "numeric",
+ },
+ {
+ name: "circleRadius",
+ default: 5,
+ max: 20,
+ min: 2,
+ step: 1,
+ type: "numeric",
+ },
+ ],
+};