🎥 Make videos programmatically with React
`remotion`: Add `color` sequence schema field type (#7385)
Jonny Burger8423379c6f43
diff --git a/packages/core/src/sequence-field-schema.ts b/packages/core/src/sequence-field-schema.tsindex fcfcca7825..a23c0d40b0 100644--- a/packages/core/src/sequence-field-schema.ts+++ b/packages/core/src/sequence-field-schema.ts@@ -31,6 +31,12 @@ export type TranslateFieldSchema = {description?: string;};+export type ColorFieldSchema = {+ type: 'color';+ default: string;+ description?: string;+};+export type EnumFieldSchema = {type: 'enum';default: string;@@ -43,6 +49,7 @@ export type VisibleFieldSchema =| BooleanFieldSchema| RotationFieldSchema| TranslateFieldSchema+ | ColorFieldSchema| EnumFieldSchema;export type SequenceFieldSchema = VisibleFieldSchema | HiddenFieldSchema;diff --git a/packages/effects/src/tint.ts b/packages/effects/src/tint.tsindex e7a4eec1d0..eb8caeb21a 100644--- a/packages/effects/src/tint.ts+++ b/packages/effects/src/tint.ts@@ -4,8 +4,14 @@ import {Internals} from 'remotion';const {createEffect} = Internals;const DEFAULT_AMOUNT = 0.5 as const;+const DEFAULT_COLOR = '#ff0000' as const;export const tintSchema = {+ color: {+ type: 'color',+ default: DEFAULT_COLOR,+ description: 'Color',+ },amount: {type: 'number',min: 0,diff --git a/packages/studio-shared/src/schema-field-info.ts b/packages/studio-shared/src/schema-field-info.tsindex ae9e24e4ff..3ca568fa37 100644--- a/packages/studio-shared/src/schema-field-info.ts+++ b/packages/studio-shared/src/schema-field-info.ts@@ -42,6 +42,7 @@ const SUPPORTED_SCHEMA_TYPES = ['boolean','rotation','translate',+ 'color','enum','hidden',] as const;diff --git a/packages/studio/src/components/Timeline/TimelineColorField.tsx b/packages/studio/src/components/Timeline/TimelineColorField.tsxnew file mode 100644index 0000000000..adc23ed800--- /dev/null+++ b/packages/studio/src/components/Timeline/TimelineColorField.tsx@@ -0,0 +1,273 @@+import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';+import type {CanUpdateSequencePropStatus} from 'remotion';+import {NoReactInternals} from 'remotion/no-react';+import {+ INPUT_BORDER_COLOR_HOVERED,+ INPUT_BORDER_COLOR_UNHOVERED,+} from '../../helpers/colors';+import type {+ SchemaFieldInfo,+ TimelineFieldOnDragValueChange,+ TimelineFieldOnSave,+} from '../../helpers/timeline-layout';+import {EyedropperIcon} from '../../icons/eyedropper';+import {useZIndex} from '../../state/z-index';++const SWATCH_WIDTH = 20;+const SWATCH_HEIGHT = 15;++const containerStyle: React.CSSProperties = {+ display: 'flex',+ alignItems: 'center',+ gap: 3,+};++const swatchWrapperBase: React.CSSProperties = {+ position: 'relative',+ width: SWATCH_WIDTH,+ height: SWATCH_HEIGHT,+ display: 'inline-block',+ borderRadius: 3,+ overflow: 'hidden',+ cursor: 'pointer',+ borderStyle: 'solid',+ borderWidth: 1,+};++const hiddenInputStyle: React.CSSProperties = {+ position: 'absolute',+ inset: 0,+ width: '100%',+ height: '100%',+ opacity: 0,+ cursor: 'pointer',+ border: 'none',+ padding: 0,+ margin: 0,+};++const swatchFillStyle: React.CSSProperties = {+ width: '100%',+ height: '100%',+};++const eyedropperButtonBase: React.CSSProperties = {+ background: 'transparent',+ border: 'none',+ padding: 0,+ margin: 0,+ cursor: 'pointer',+ display: 'inline-flex',+ alignItems: 'center',+ justifyContent: 'center',+ width: 20,+ height: 20,+ color: 'rgba(255, 255, 255, 0.7)',+};++const eyedropperIconStyle: React.CSSProperties = {+ width: 16,+ height: 16,+};++// Normalizes any color string the user provided (e.g. `red`, `rgb(...)`, `#fff`)+// into a `#rrggbb` string that `<input type="color">` accepts.+const toHex = (value: string): string => {+ try {+ const argb = NoReactInternals.processColor(value);+ const r = (argb >>> 16) & 0xff;+ const g = (argb >>> 8) & 0xff;+ const b = argb & 0xff;+ return `#${r.toString(16).padStart(2, '0')}${g+ .toString(16)+ .padStart(2, '0')}${b.toString(16).padStart(2, '0')}`;+ } catch {+ return '#000000';+ }+};++const hasEyeDropper = (): boolean =>+ typeof window !== 'undefined' && 'EyeDropper' in window;++export const TimelineColorField: React.FC<{+ readonly field: SchemaFieldInfo;+ readonly effectiveValue: unknown;+ readonly propStatus: CanUpdateSequencePropStatus;+ readonly onSave: TimelineFieldOnSave;+ readonly onDragValueChange: TimelineFieldOnDragValueChange;+ readonly onDragEnd: () => void;+}> = ({+ field,+ effectiveValue,+ propStatus,+ onSave,+ onDragValueChange,+ onDragEnd,+}) => {+ const inputRef = useRef<HTMLInputElement>(null);+ const [isHovered, setIsHovered] = useState(false);+ const [isFocused, setIsFocused] = useState(false);+ const {tabIndex} = useZIndex();++ const commitTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);+ const pendingCommitRef = useRef<(() => void) | null>(null);++ // `<input type="color">` doesn't fire an event when dismissed; debounce+ // commits and flush any pending commit on unmount so we never lose the+ // final value.+ useEffect(() => {+ return () => {+ if (commitTimeoutRef.current) {+ clearTimeout(commitTimeoutRef.current);+ }++ if (pendingCommitRef.current) {+ pendingCommitRef.current();+ }+ };+ }, []);++ const currentValue =+ typeof effectiveValue === 'string'+ ? effectiveValue+ : field.fieldSchema.type === 'color'+ ? field.fieldSchema.default+ : '';++ const hexValue = useMemo(() => toHex(currentValue), [currentValue]);++ const onColorChange: React.ChangeEventHandler<HTMLInputElement> = useCallback(+ (e) => {+ const newValue = e.target.value;+ if (!propStatus.canUpdate) {+ return;+ }++ onDragValueChange(newValue);++ if (commitTimeoutRef.current) {+ clearTimeout(commitTimeoutRef.current);+ }++ const commit = () => {+ pendingCommitRef.current = null;+ if (propStatus.canUpdate && newValue !== propStatus.codeValue) {+ onSave(newValue);+ }++ onDragEnd();+ };++ pendingCommitRef.current = commit;++ commitTimeoutRef.current = setTimeout(() => {+ commitTimeoutRef.current = null;+ commit();+ }, 500);+ },+ [onSave, onDragValueChange, onDragEnd, propStatus],+ );++ const onPickColor = useCallback(() => {+ // `EyeDropper` is a Chromium-only API; it's feature-detected at render+ // time so this only runs in supported browsers.+ const EyeDropperCtor =+ typeof window !== 'undefined'+ ? (+ window as unknown as {+ EyeDropper?: new () => {open: () => Promise<{sRGBHex: string}>};+ }+ ).EyeDropper+ : undefined;+ if (!EyeDropperCtor) {+ return;+ }++ const eyeDropper = new EyeDropperCtor();+ eyeDropper+ .open()+ .then((result) => {+ if (propStatus.canUpdate && result.sRGBHex !== propStatus.codeValue) {+ onSave(result.sRGBHex);+ }+ })+ .catch(() => {+ // User aborted or picker failed; safe to ignore.+ });+ }, [onSave, propStatus]);++ const swatchWrapperStyle = useMemo<React.CSSProperties>(() => {+ return {+ ...swatchWrapperBase,+ borderColor:+ isHovered || isFocused+ ? INPUT_BORDER_COLOR_HOVERED+ : INPUT_BORDER_COLOR_UNHOVERED,+ cursor: propStatus.canUpdate ? 'pointer' : 'not-allowed',+ marginLeft: 5,+ };+ }, [isFocused, isHovered, propStatus.canUpdate]);++ const swatchFill = useMemo<React.CSSProperties>(() => {+ return {+ ...swatchFillStyle,+ backgroundColor: currentValue || hexValue,+ position: 'absolute',+ display: 'block',+ };+ }, [currentValue, hexValue]);++ const onMouseEnter = useCallback(() => setIsHovered(true), []);+ const onMouseLeave = useCallback(() => setIsHovered(false), []);+ const onFocus = useCallback(() => setIsFocused(true), []);+ const onBlur = useCallback(() => setIsFocused(false), []);++ const onSwatchClick = useCallback(() => {+ if (!propStatus.canUpdate) {+ return;+ }++ inputRef.current?.click();+ }, [propStatus.canUpdate]);++ const showEyeDropper = hasEyeDropper();++ return (+ <span style={containerStyle}>+ <span+ style={swatchWrapperStyle}+ onMouseEnter={onMouseEnter}+ onMouseLeave={onMouseLeave}+ onClick={onSwatchClick}+ title={currentValue}+ >+ <span style={swatchFill} />+ <input+ ref={inputRef}+ type="color"+ value={hexValue}+ onChange={onColorChange}+ onFocus={onFocus}+ onBlur={onBlur}+ disabled={!propStatus.canUpdate}+ name={field.key}+ tabIndex={tabIndex}+ style={hiddenInputStyle}+ />+ </span>+ {showEyeDropper ? (+ <button+ type="button"+ onClick={onPickColor}+ disabled={!propStatus.canUpdate}+ style={eyedropperButtonBase}+ tabIndex={tabIndex}+ title="Pick color from screen"+ aria-label="Pick color from screen"+ >+ <EyedropperIcon style={eyedropperIconStyle} />+ </button>+ ) : null}+ </span>+ );+};diff --git a/packages/studio/src/components/Timeline/TimelineSchemaField.tsx b/packages/studio/src/components/Timeline/TimelineSchemaField.tsxindex e111918e57..d2e0620690 100644--- a/packages/studio/src/components/Timeline/TimelineSchemaField.tsx+++ b/packages/studio/src/components/Timeline/TimelineSchemaField.tsx@@ -9,6 +9,7 @@ import type {TimelineFieldOnSave,} from '../../helpers/timeline-layout';import {TimelineBooleanField} from './TimelineBooleanField';+import {TimelineColorField} from './TimelineColorField';import {TimelineEnumField} from './TimelineEnumField';import {TimelineNumberField} from './TimelineNumberField';import {TimelineRotationField} from './TimelineRotationField';@@ -129,6 +130,21 @@ export const TimelineFieldValue: React.FC<{);}+ if (field.typeName === 'color') {+ return (+ <span style={wrapperStyle}>+ <TimelineColorField+ field={field}+ propStatus={propStatus}+ onSave={onSave}+ onDragValueChange={onDragValueChange}+ onDragEnd={onDragEnd}+ effectiveValue={effectiveValue}+ />+ </span>+ );+ }+if (field.typeName === 'enum') {return (<span style={inlineWrapper}>diff --git a/packages/studio/src/icons/eyedropper.tsx b/packages/studio/src/icons/eyedropper.tsxnew file mode 100644index 0000000000..12f4ff0b68--- /dev/null+++ b/packages/studio/src/icons/eyedropper.tsx@@ -0,0 +1,12 @@+import type {SVGProps} from 'react';++export const EyedropperIcon: React.FC<+ SVGProps<SVGSVGElement> & {color?: string}+> = ({color, ...props}) => (+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640" {...props}>+ <path+ fill={color ?? 'currentColor'}+ d="M263 233C278.6 248.6 315.3 285.3 373.1 343.1C400.1 370.1 414 384 415 385C424.4 394.4 439.6 394.4 448.9 385C458.2 375.6 458.3 360.4 448.9 351.1L440.9 343.1C457.3 326.7 492.4 291.6 546.1 237.9C565.2 218.8 575.9 192.9 575.9 165.9C575.9 109.7 530.3 64.1 474.1 64.1C447.1 64.1 421.2 74.8 402.1 93.9C348.4 147.6 313.3 182.7 296.9 199.1L288.9 191.1C279.5 181.7 264.3 181.7 255 191.1C245.7 200.5 245.6 215.7 255 225L255 225L263 233zM512.2 203.9L407 309.1L330.9 233L436.1 127.8C446.2 117.7 459.9 112 474.2 112C503.9 112 528 136.1 528 165.8C528 180.1 522.3 193.8 512.2 203.9zM117.1 393C103.6 406.5 96 424.8 96 443.9L96 496.8L68 538.8C61.7 548.3 62.9 561 71 569.1C79.1 577.2 91.8 578.4 101.3 572.1L143.3 544.1L196.2 544.1C215.3 544.1 233.6 536.5 247.1 523L366.1 404L332.2 370.1L213.2 489.1C208.7 493.6 202.6 496.1 196.2 496.1L144.1 496.1L144.1 444C144.1 437.6 146.6 431.5 151.1 427L270.1 308L236.2 274.1L117.2 393.1z"+ />+ </svg>+);