Skip to content

🎥 Make videos programmatically with React

color editor

Jonny Burgerf38ef177740f
diff --git a/packages/cli/src/editor/components/NewComposition/RemInputTypeColor.tsx b/packages/cli/src/editor/components/NewComposition/RemInputTypeColor.tsx
new file mode 100644
index 0000000000..c5c0857b44
--- /dev/null
+++ b/packages/cli/src/editor/components/NewComposition/RemInputTypeColor.tsx
@@ -0,0 +1,89 @@
+import React, {
+ forwardRef,
+ useEffect,
+ useImperativeHandle,
+ useMemo,
+ useRef,
+ useState,
+} from 'react';
+import {INPUT_BACKGROUND} from '../../helpers/colors';
+import {useZIndex} from '../../state/z-index';
+import type {RemInputStatus} from './RemInput';
+import {getInputBorderColor} from './RemInput';
+
+type Props = React.DetailedHTMLProps<
+ React.InputHTMLAttributes<HTMLInputElement>,
+ HTMLInputElement
+> & {
+ status: RemInputStatus;
+};
+
+export const inputBaseStyle: React.CSSProperties = {
+ padding: 0,
+ borderStyle: 'solid',
+ borderWidth: 1,
+};
+
+const RemInputTypeColorForwardRef: React.ForwardRefRenderFunction<
+ HTMLInputElement,
+ Props
+> = ({status, ...props}, ref) => {
+ const [isFocused, setIsFocused] = useState(false);
+ const [isHovered, setIsHovered] = useState(false);
+ const inputRef = useRef<HTMLInputElement>(null);
+ const {tabIndex} = useZIndex();
+
+ const style = useMemo(() => {
+ return {
+ backgroundColor: INPUT_BACKGROUND,
+ ...inputBaseStyle,
+ borderColor: getInputBorderColor({isFocused, isHovered, status}),
+ ...(props.style ?? {}),
+ };
+ }, [isFocused, isHovered, props.style, status]);
+
+ useImperativeHandle(
+ ref,
+ () => {
+ return inputRef.current as HTMLInputElement;
+ },
+ []
+ );
+
+ useEffect(() => {
+ if (!inputRef.current) {
+ return;
+ }
+
+ const {current} = inputRef;
+
+ const onFocus = () => setIsFocused(true);
+ const onBlur = () => setIsFocused(false);
+ const onMouseEnter = () => setIsHovered(true);
+ const onMouseLeave = () => setIsHovered(false);
+
+ current.addEventListener('focus', onFocus);
+ current.addEventListener('blur', onBlur);
+ current.addEventListener('mouseenter', onMouseEnter);
+ current.addEventListener('mouseleave', onMouseLeave);
+
+ return () => {
+ current.removeEventListener('focus', onFocus);
+ current.removeEventListener('blur', onBlur);
+ current.removeEventListener('mouseenter', onMouseEnter);
+ current.removeEventListener('mouseleave', onMouseLeave);
+ };
+ }, [inputRef]);
+
+ return (
+ <input
+ ref={inputRef}
+ type="color"
+ tabIndex={tabIndex}
+ {...props}
+ style={style}
+ />
+ );
+};
+
+export const RemInputTypeColor = forwardRef(RemInputTypeColorForwardRef);
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodColorEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodColorEditor.tsx
new file mode 100644
index 0000000000..72579177f6
--- /dev/null
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodColorEditor.tsx
@@ -0,0 +1,127 @@
+import React, {useCallback, useMemo, useState} from 'react';
+import type {z} from 'remotion';
+import {Row, Spacing} from '../../layout';
+import {RemotionInput} from '../../NewComposition/RemInput';
+import {RemInputTypeColor} from '../../NewComposition/RemInputTypeColor';
+import {ValidationMessage} from '../../NewComposition/ValidationMessage';
+import {narrowOption, optionRow} from '../layout';
+import {SchemaLabel} from './SchemaLabel';
+import type {JSONPath} from './zod-types';
+
+type LocalState = {
+ value: string;
+ zodValidation: z.SafeParseReturnType<unknown, unknown>;
+};
+
+const fullWidth: React.CSSProperties = {
+ width: '100%',
+};
+
+export const ZodColorEditor: React.FC<{
+ schema: z.ZodTypeAny;
+ jsonPath: JSONPath;
+ value: string;
+ defaultValue: string;
+ setValue: React.Dispatch<React.SetStateAction<string>>;
+ onSave: (updater: (oldNum: unknown) => string) => void;
+ onRemove: null | (() => void);
+ compact: boolean;
+ showSaveButton: boolean;
+}> = ({
+ jsonPath,
+ value,
+ setValue,
+ showSaveButton,
+ defaultValue,
+ schema,
+ compact,
+ onSave,
+ onRemove,
+}) => {
+ const [localValue, setLocalValue] = useState<LocalState>(() => {
+ return {
+ value,
+ zodValidation: schema.safeParse(value),
+ };
+ });
+
+ const onValueChange = useCallback(
+ (newValue: string) => {
+ const safeParse = schema.safeParse(newValue);
+ const newLocalState: LocalState = {
+ value: newValue,
+ zodValidation: safeParse,
+ };
+ setLocalValue(newLocalState);
+ if (safeParse.success) {
+ setValue(newValue);
+ }
+ },
+ [schema, setValue]
+ );
+
+ const onChange: React.ChangeEventHandler<HTMLInputElement> = useCallback(
+ (e) => {
+ onValueChange(e.target.value);
+ },
+ [onValueChange]
+ );
+ const reset = useCallback(() => {
+ onValueChange(defaultValue);
+ }, [defaultValue, onValueChange]);
+
+ const save = useCallback(() => {
+ onSave(() => value);
+ }, [onSave, value]);
+
+ const status = localValue.zodValidation.success ? 'ok' : 'error';
+
+ const colorPicker: React.CSSProperties = useMemo(() => {
+ return {
+ height: 37,
+ };
+ }, []);
+
+ return (
+ <div style={compact ? narrowOption : optionRow}>
+ <SchemaLabel
+ compact={compact}
+ isDefaultValue={value === defaultValue}
+ jsonPath={jsonPath}
+ onReset={reset}
+ onSave={save}
+ showSaveButton={showSaveButton}
+ onRemove={onRemove}
+ />
+ <div style={fullWidth}>
+ <Row align="center">
+ <RemInputTypeColor
+ style={colorPicker}
+ type="color"
+ value={localValue.value}
+ onChange={onChange}
+ className="__remotion_color_picker"
+ status={status}
+ />
+ <Spacing x={1} />
+ <RemotionInput
+ value={localValue.value}
+ status={localValue.zodValidation.success ? 'ok' : 'error'}
+ placeholder={jsonPath.join('.')}
+ onChange={onChange}
+ />
+ </Row>
+ {!localValue.zodValidation.success && (
+ <>
+ <Spacing y={1} block />
+ <ValidationMessage
+ align="flex-end"
+ message={localValue.zodValidation.error.format()._errors[0]}
+ type="error"
+ />
+ </>
+ )}
+ </div>
+ </div>
+ );
+};
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodSwitch.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodSwitch.tsx
index 16e8f373d0..eb4d945be7 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodSwitch.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodSwitch.tsx
@@ -1,8 +1,9 @@
import React from 'react';
-import {z} from 'remotion';
+import {Internals, z} from 'remotion';
import type {JSONPath} from './zod-types';
import {ZodArrayEditor} from './ZodArrayEditor';
import {ZodBooleanEditor} from './ZodBooleanEditor';
+import {ZodColorEditor} from './ZodColorEditor';
import {ZodDateEditor} from './ZodDateEditor';
import {ZodEffectEditor} from './ZodEffectEditor';
import {ZodEnumEditor} from './ZodEnumEditor';
@@ -62,6 +63,22 @@ export const ZodSwitch: React.FC<{
}
if (typeName === z.ZodFirstPartyTypeKind.ZodString) {
+ if (schema._def.description === Internals.REMOTION_COLOR_BRAND) {
+ return (
+ <ZodColorEditor
+ value={value as string}
+ setValue={setValue as React.Dispatch<React.SetStateAction<string>>}
+ jsonPath={jsonPath}
+ schema={schema}
+ compact={compact}
+ onSave={onSave}
+ defaultValue={defaultValue as string}
+ showSaveButton={showSaveButton}
+ onRemove={onRemove}
+ />
+ );
+ }
+
return (
<ZodStringEditor
value={value as string}
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/create-zod-values.ts b/packages/cli/src/editor/components/RenderModal/SchemaEditor/create-zod-values.ts
index e1f1534bab..f569dfd43d 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/create-zod-values.ts
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/create-zod-values.ts
@@ -1,4 +1,4 @@
-import {z} from 'remotion';
+import {Internals, z} from 'remotion';
export const createZodValues = (schema: z.ZodTypeAny): unknown => {
if (!schema) {
@@ -10,6 +10,10 @@ export const createZodValues = (schema: z.ZodTypeAny): unknown => {
switch (typeName) {
case z.ZodFirstPartyTypeKind.ZodString:
+ if (schema._def.description === Internals.REMOTION_COLOR_BRAND) {
+ return '#ffffff';
+ }
+
return '';
case z.ZodFirstPartyTypeKind.ZodNumber:
return 0;
diff --git a/packages/cli/styles/styles.css b/packages/cli/styles/styles.css
index 2cf86b208c..300d32f9e2 100644
--- a/packages/cli/styles/styles.css
+++ b/packages/cli/styles/styles.css
@@ -37,3 +37,9 @@ a:focus {
border-radius: 0 !important;
}
+input[type="color"].__remotion_color_picker::-webkit-color-swatch-wrapper {
+ padding: 0;
+}
+input[type="color"].__remotion_color_picker::-webkit-color-swatch {
+ border: none;
+}
diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.ts
index 740fca39cb..16fe55d7a7 100644
--- a/packages/core/src/internals.ts
+++ b/packages/core/src/internals.ts
@@ -68,6 +68,7 @@ import {
RemotionContextProvider,
useRemotionContexts,
} from './wrap-remotion-context.js';
+import {REMOTION_COLOR_BRAND} from './z-color.js';
const Timeline = TimelinePosition;
// Mark them as Internals so use don't assume this is public
@@ -116,6 +117,7 @@ export const Internals = {
EditorPropsProvider,
EditorPropsContext,
usePreload,
+ REMOTION_COLOR_BRAND,
};
export type {
diff --git a/packages/core/src/z-color.ts b/packages/core/src/z-color.ts
index 2d29d74ee9..783bf95263 100644
--- a/packages/core/src/z-color.ts
+++ b/packages/core/src/z-color.ts
@@ -2,4 +2,4 @@ import {z} from 'zod';
export const REMOTION_COLOR_BRAND = '__remotion-color';
-export const zColor = z.string().describe(REMOTION_COLOR_BRAND);
+export const zColor = () => z.string().describe(REMOTION_COLOR_BRAND);
diff --git a/packages/example/src/BetaText/index.tsx b/packages/example/src/BetaText/index.tsx
index 4f45cabd82..66089d62f0 100644
--- a/packages/example/src/BetaText/index.tsx
+++ b/packages/example/src/BetaText/index.tsx
@@ -1,6 +1,13 @@
import {mix} from 'polished';
import React from 'react';
-import {interpolate, spring, useCurrentFrame, useVideoConfig} from 'remotion';
+import {
+ interpolate,
+ spring,
+ useCurrentFrame,
+ useVideoConfig,
+ z,
+ zColor,
+} from 'remotion';
import styled from 'styled-components';
const BRAND_GRADIENT = ['#5851db', '#405de6'];
@@ -19,13 +26,13 @@ const Label = styled.span<{
display: inline-block;
`;
-const lines = 7;
const Row: React.FC<{
videoWidth: number;
i: number;
text: string;
zoom: number;
-}> = ({videoWidth, i, text, zoom}) => {
+ color: string;
+}> = ({videoWidth, i, text, zoom, color}) => {
const frame = useCurrentFrame();
const videoConfig = useVideoConfig();
const progress = spring({
@@ -43,11 +50,7 @@ const Row: React.FC<{
const posX = interpolate(progress, [0, 1], [1, 0]);
const dir = i % 2 === 0 ? -1 : 1;
- const color = mix(
- Math.min(1, (lines - i - 1) / lines),
- BRAND_GRADIENT[0],
- BRAND_GRADIENT[1]
- );
+
return (
<div
style={{
@@ -90,11 +93,15 @@ const Row: React.FC<{
);
};
-type Props = {
- word1: string;
-};
+export const betaTextSchema = z.object({
+ word1: z.string(),
+ color: z.array(zColor()),
+});
-const BetaText = ({word1}: Props): React.ReactNode => {
+const BetaText = ({
+ word1,
+ color,
+}: z.infer<typeof betaTextSchema>): React.ReactNode => {
const videoConfig = useVideoConfig();
const frame = useCurrentFrame();
@@ -120,6 +127,7 @@ const BetaText = ({word1}: Props): React.ReactNode => {
height: videoConfig.height,
display: 'flex',
transform: `scale(${scale})`,
+ fontFamily: 'sans-serif',
}}
>
<div
@@ -131,20 +139,21 @@ const BetaText = ({word1}: Props): React.ReactNode => {
{new Array(17)
.fill(true)
.map((_, i) => i)
- .map((key) => {
+ .map((key, i) => {
return (
<Row
key={key}
+ color={color[i]}
zoom={progress}
text={
key === 7
- ? 'CET'
+ ? 'ONE'
: key === 5
- ? '5pm'
+ ? 'REMOTION'
: key === 3
- ? word1 ?? 'TOMORROW'
+ ? word1 ?? 'COOL'
: key === 1
- ? 'ANYSTICKER'
+ ? 'LIT'
: 'BETA'
}
i={key}
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index 2566932e1c..8c9b0c1280 100644
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@ -1,8 +1,8 @@
import {alias} from 'lib/alias';
import React from 'react';
-import {Composition, Folder, getInputProps, Still, z} from 'remotion';
+import {Composition, Folder, getInputProps, Still, z, zColor} from 'remotion';
import {TwentyTwoKHzAudio} from './22KhzAudio';
-import BetaText from './BetaText';
+import BetaText, {betaTextSchema} from './BetaText';
import {CancelRender} from './CancelRender';
import {ColorInterpolation} from './ColorInterpolation';
import {ComplexSounds} from './ComplexSounds';
@@ -224,9 +224,8 @@ export const Index: React.FC = () => {
height={1080}
fps={30}
durationInFrames={3 * 30}
- defaultProps={{
- word1: getInputProps().word1,
- }}
+ defaultProps={{word1: 'hithere' as const, color: ['#137c2d']}}
+ schema={betaTextSchema}
/>
<Composition
id="react-svg"
@@ -711,6 +710,7 @@ export const Index: React.FC = () => {
values: z.enum(['a', 'b', 'c']),
supersuperlongvalueabcdefghji: z.string(),
incompatible: z.null().or(z.undefined()),
+ color: zColor(),
longEnum: z.enum([
'a',
'b',
@@ -765,6 +765,7 @@ export const Index: React.FC = () => {
supersuperlongvalueabcdefghji: 'hi' as const,
incompatible: null,
longEnum: 'k' as const,
+ color: '#eb3a60' as const,
}}
durationInFrames={150}
/>