Skip to content

🎥 Make videos programmatically with React

`remotion`: Add text schema utilities (#8574)

Jonny Burger12c9084c3a4b
diff --git a/packages/core/src/Interactive.tsx b/packages/core/src/Interactive.tsx
index aa0bec40ff..766e46c79e 100644
--- a/packages/core/src/Interactive.tsx
+++ b/packages/core/src/Interactive.tsx
@@ -5,6 +5,7 @@ import {
baseSchema,
premountSchema,
sequenceSchema,
+ textSchema,
transformSchema,
type InteractivitySchema,
} from './interactivity-schema.js';
@@ -96,6 +97,7 @@ type InteractiveElementComponent<Tag extends InteractiveTag> =
const interactiveElementSchema = {
...baseSchema,
...transformSchema,
+ ...textSchema,
} as const satisfies InteractivitySchema;
const setRef = <ElementType,>(
@@ -188,6 +190,7 @@ const makeInteractiveElement = <Tag extends InteractiveTag>(
export const Interactive = {
baseSchema,
transformSchema,
+ textSchema,
premountSchema,
sequenceSchema,
withSchema: withInteractivitySchema,
diff --git a/packages/core/src/interactivity-schema.ts b/packages/core/src/interactivity-schema.ts
index f957a12244..db56d8abbf 100644
--- a/packages/core/src/interactivity-schema.ts
+++ b/packages/core/src/interactivity-schema.ts
@@ -211,6 +211,78 @@ export const transformSchema = {
export const sequenceVisualStyleSchema = transformSchema;
+export const textSchema = {
+ 'style.color': {
+ type: 'color',
+ default: undefined,
+ description: 'Color',
+ },
+ 'style.fontSize': {
+ type: 'number',
+ default: undefined,
+ min: 0,
+ step: 1,
+ description: 'Font size',
+ hiddenFromList: false,
+ },
+ 'style.lineHeight': {
+ type: 'number',
+ default: undefined,
+ min: 0,
+ step: 0.05,
+ description: 'Line height',
+ hiddenFromList: false,
+ },
+ 'style.fontWeight': {
+ type: 'enum',
+ default: '400',
+ description: 'Font weight',
+ variants: {
+ '100': {},
+ '200': {},
+ '300': {},
+ '400': {},
+ '500': {},
+ '600': {},
+ '700': {},
+ '800': {},
+ '900': {},
+ normal: {},
+ bold: {},
+ },
+ },
+ 'style.fontStyle': {
+ type: 'enum',
+ default: 'normal',
+ description: 'Font style',
+ variants: {
+ normal: {},
+ italic: {},
+ oblique: {},
+ },
+ },
+ 'style.textAlign': {
+ type: 'enum',
+ default: 'left',
+ description: 'Text align',
+ variants: {
+ left: {},
+ center: {},
+ right: {},
+ justify: {},
+ start: {},
+ end: {},
+ },
+ },
+ 'style.letterSpacing': {
+ type: 'number',
+ default: undefined,
+ step: 0.1,
+ description: 'Letter spacing',
+ hiddenFromList: false,
+ },
+} as const satisfies InteractivitySchema;
+
export const premountSchema = {
premountFor: {
type: 'number',
diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.ts
index eae481ae34..b191a47cee 100644
--- a/packages/core/src/internals.ts
+++ b/packages/core/src/internals.ts
@@ -99,6 +99,7 @@ import {
sequenceSchema,
sequenceStyleSchema,
sequenceVisualStyleSchema,
+ textSchema,
transformSchema,
type ArrayFieldSchema,
type ArrayItemFieldSchema,
@@ -297,6 +298,7 @@ export const Internals = {
sequenceStyleSchema,
sequenceVisualStyleSchema,
sequencePremountSchema,
+ textSchema,
transformSchema,
premountSchema,
flattenActiveSchema,
diff --git a/packages/core/src/test/with-interactivity-schema-helpers.test.ts b/packages/core/src/test/with-interactivity-schema-helpers.test.ts
index 9cd11f4485..c0e508002d 100644
--- a/packages/core/src/test/with-interactivity-schema-helpers.test.ts
+++ b/packages/core/src/test/with-interactivity-schema-helpers.test.ts
@@ -7,6 +7,7 @@ import {
sequenceSchema,
sequenceSchemaWithoutFrom,
sequenceStyleSchema,
+ textSchema,
transformSchema,
} from '../interactivity-schema.js';
import {
@@ -86,6 +87,68 @@ test('style.scale does not impose a minimum value', () => {
expect('min' in scaleSchema).toBe(false);
});
+test('textSchema exposes common text style fields', () => {
+ expect(Object.keys(textSchema).sort()).toEqual(
+ [
+ 'style.color',
+ 'style.fontSize',
+ 'style.fontStyle',
+ 'style.fontWeight',
+ 'style.letterSpacing',
+ 'style.lineHeight',
+ 'style.textAlign',
+ ].sort(),
+ );
+ expect(textSchema['style.color'].type).toBe('color');
+ expect(textSchema['style.color'].default).toBeUndefined();
+ expect(textSchema['style.fontSize']).toMatchObject({
+ type: 'number',
+ default: undefined,
+ min: 0,
+ step: 1,
+ hiddenFromList: false,
+ });
+ expect(textSchema['style.lineHeight']).toMatchObject({
+ type: 'number',
+ default: undefined,
+ min: 0,
+ step: 0.05,
+ hiddenFromList: false,
+ });
+ expect(Object.keys(textSchema['style.fontWeight'].variants)).toEqual([
+ '100',
+ '200',
+ '300',
+ '400',
+ '500',
+ '600',
+ '700',
+ '800',
+ '900',
+ 'normal',
+ 'bold',
+ ]);
+ expect(Object.keys(textSchema['style.fontStyle'].variants)).toEqual([
+ 'normal',
+ 'italic',
+ 'oblique',
+ ]);
+ expect(Object.keys(textSchema['style.textAlign'].variants)).toEqual([
+ 'left',
+ 'center',
+ 'right',
+ 'justify',
+ 'start',
+ 'end',
+ ]);
+ expect(textSchema['style.letterSpacing']).toMatchObject({
+ type: 'number',
+ default: undefined,
+ step: 0.1,
+ hiddenFromList: false,
+ });
+});
+
test('readValuesFromProps reads dot-notation keys via getNestedValue', () => {
const props = {
layout: 'absolute-fill',
diff --git a/packages/docs/docs/interactive.mdx b/packages/docs/docs/interactive.mdx
index 720d2d123b..fb110c3830 100644
--- a/packages/docs/docs/interactive.mdx
+++ b/packages/docs/docs/interactive.mdx
@@ -88,6 +88,14 @@ Controls for transform-related style props:
Use it for components that accept a `style` prop and apply it to the rendered element.
+### `Interactive.textSchema`<AvailableFrom v="4.0.481" />
+
+Controls for text-related style props:
+
+`style.color`, `style.fontSize`, `style.lineHeight`, `style.fontWeight`, `style.fontStyle`, `style.textAlign` and `style.letterSpacing`.
+
+Use it for components that accept a `style` prop and render text.
+
### `Interactive.premountSchema`<AvailableFrom v="4.0.479" />
Controls for mounting behavior:
diff --git a/packages/docs/docs/interactivity-schema.mdx b/packages/docs/docs/interactivity-schema.mdx
index 3e97910951..272d20a1af 100644
--- a/packages/docs/docs/interactivity-schema.mdx
+++ b/packages/docs/docs/interactivity-schema.mdx
@@ -43,6 +43,10 @@ Timeline props inherited from [`<Sequence>`](/docs/sequence): [`durationInFrames
Transform-related style props: `style.transformOrigin`, `style.translate`, `style.scale`, `style.rotate` and `style.opacity`.
+### `Interactive.textSchema`<AvailableFrom v="4.0.481" />
+
+Text-related style props: `style.color`, `style.fontSize`, `style.lineHeight`, `style.fontWeight`, `style.fontStyle`, `style.textAlign` and `style.letterSpacing`.
+
### `Interactive.premountSchema`
Mounting props: `premountFor`, `postmountFor`, `styleWhilePremounted` and `styleWhilePostmounted`.
diff --git a/packages/example/src/VisualModeTests/InteractiveComponents.tsx b/packages/example/src/VisualModeTests/InteractiveComponents.tsx
index 007c0183d5..e88e61297d 100644
--- a/packages/example/src/VisualModeTests/InteractiveComponents.tsx
+++ b/packages/example/src/VisualModeTests/InteractiveComponents.tsx
@@ -64,7 +64,7 @@ export const InteractiveHtmlElements: React.FC = () => {
style={{
position: 'absolute',
left: 650,
- top: 300,
+ top: 240,
border: 0,
borderRadius: 999,
padding: '28px 44px',
@@ -77,6 +77,23 @@ export const InteractiveHtmlElements: React.FC = () => {
>
Button
</Interactive.Button>
+ <Interactive.P
+ style={{
+ position: 'absolute',
+ left: 620,
+ top: 390,
+ width: 350,
+ margin: 0,
+ fontFamily: 'sans-serif',
+ fontSize: 38,
+ color: '#facc15',
+ letterSpacing: 2,
+ lineHeight: 1.15,
+ textAlign: 'center',
+ }}
+ >
+ Editable text schema
+ </Interactive.P>
<Interactive.Section
style={{
position: 'absolute',
diff --git a/packages/studio/src/components/NewComposition/InputDragger.tsx b/packages/studio/src/components/NewComposition/InputDragger.tsx
index 1f715b8953..0d7461794c 100644
--- a/packages/studio/src/components/NewComposition/InputDragger.tsx
+++ b/packages/studio/src/components/NewComposition/InputDragger.tsx
@@ -62,6 +62,26 @@ export const deriveInputDraggerStep = ({
return 0.0001;
};
+export const deriveInputDraggerDragStartValue = ({
+ min,
+ value,
+}: {
+ readonly min: React.InputHTMLAttributes<HTMLInputElement>['min'];
+ readonly value: React.InputHTMLAttributes<HTMLInputElement>['value'];
+}) => {
+ const numericValue = Number(value);
+ if (Number.isFinite(numericValue)) {
+ return numericValue;
+ }
+
+ const numericMin = Number(min);
+ if (Number.isFinite(numericMin)) {
+ return numericMin;
+ }
+
+ return 0;
+};
+
const InputDraggerForwardRefFn: React.ForwardRefRenderFunction<
HTMLButtonElement,
Props
@@ -198,6 +218,10 @@ const InputDraggerForwardRefFn: React.ForwardRefRenderFunction<
const step = Number(_step ?? 1);
const min = Number(_min ?? 0);
const max = Number(_max ?? Infinity);
+ const dragStartValue = deriveInputDraggerDragStartValue({
+ min: _min,
+ value,
+ });
if (distanceFromStart > 4) {
setClickLock(true);
@@ -211,7 +235,7 @@ const InputDraggerForwardRefFn: React.ForwardRefRenderFunction<
[-5, -4, 0, 4, 5],
[-step, 0, 0, 0, step],
);
- const newValue = Math.min(max, Math.max(min, Number(value) + diff));
+ const newValue = Math.min(max, Math.max(min, dragStartValue + diff));
const nextValue = snapToStep
? roundToStep(newValue, step)
: dragDecimalPlaces === undefined
diff --git a/packages/studio/src/components/Timeline/timeline-field-display-utils.ts b/packages/studio/src/components/Timeline/timeline-field-display-utils.ts
index 458a69df9a..c774944509 100644
--- a/packages/studio/src/components/Timeline/timeline-field-display-utils.ts
+++ b/packages/studio/src/components/Timeline/timeline-field-display-utils.ts
@@ -346,6 +346,10 @@ export const formatTimelineFieldValueForDisplay = ({
return formatUnknownTimelineValueForDisplay(value);
}
+ if (value === undefined) {
+ return 'unset';
+ }
+
switch (fieldSchema.type) {
case 'number':
return (
diff --git a/packages/studio/src/test/input-dragger.test.ts b/packages/studio/src/test/input-dragger.test.ts
index 487dc09be0..c818d6d316 100644
--- a/packages/studio/src/test/input-dragger.test.ts
+++ b/packages/studio/src/test/input-dragger.test.ts
@@ -1,5 +1,8 @@
import {expect, test} from 'bun:test';
-import {deriveInputDraggerStep} from '../components/NewComposition/InputDragger';
+import {
+ deriveInputDraggerDragStartValue,
+ deriveInputDraggerStep,
+} from '../components/NewComposition/InputDragger';
test('deriveInputDraggerStep disables HTML step validation if snapping is disabled', () => {
expect(
@@ -20,3 +23,26 @@ test('deriveInputDraggerStep keeps configured step while snapping is enabled', (
}),
).toBe(0.5);
});
+
+test('deriveInputDraggerDragStartValue falls back to a finite start value', () => {
+ expect(
+ deriveInputDraggerDragStartValue({
+ min: -Infinity,
+ value: undefined,
+ }),
+ ).toBe(0);
+
+ expect(
+ deriveInputDraggerDragStartValue({
+ min: 10,
+ value: undefined,
+ }),
+ ).toBe(10);
+
+ expect(
+ deriveInputDraggerDragStartValue({
+ min: 10,
+ value: 24,
+ }),
+ ).toBe(24);
+});
diff --git a/packages/studio/src/test/timeline-field-utils.test.ts b/packages/studio/src/test/timeline-field-utils.test.ts
index 3c807b698a..f79a584bfc 100644
--- a/packages/studio/src/test/timeline-field-utils.test.ts
+++ b/packages/studio/src/test/timeline-field-utils.test.ts
@@ -137,3 +137,23 @@ test('formatTimelineFieldValueForDisplay rounds unknown numeric fallbacks', () =
}),
).toBe('10');
});
+
+test('formatTimelineFieldValueForDisplay shows undefined schema values as unset', () => {
+ expect(
+ formatTimelineFieldValueForDisplay({
+ fieldSchema: {
+ type: 'number',
+ default: undefined,
+ hiddenFromList: false,
+ },
+ value: undefined,
+ }),
+ ).toBe('unset');
+
+ expect(
+ formatTimelineFieldValueForDisplay({
+ fieldSchema: undefined,
+ value: undefined,
+ }),
+ ).toBe('undefined');
+});