Skip to content

🎥 Make videos programmatically with React

zod types internals

Jonny Burger2beac44b6021
diff --git a/packages/cli/src/color-math.ts b/packages/cli/src/color-math.ts
index 939fc78f93..b4607b4502 100644
--- a/packages/cli/src/color-math.ts
+++ b/packages/cli/src/color-math.ts
@@ -1,11 +1,11 @@
-import type {ZodColorType} from './editor/components/get-zod-if-possible';
+import type {ZodTypesType} from './editor/components/get-zod-if-possible';
export const colorWithNewOpacity = (
color: string,
opacity: number,
- zColor: ZodColorType
+ zColor: ZodTypesType
) => {
- const {r, g, b} = zColor.ZColorInternals.parseColor(color);
+ const {r, g, b} = zColor.ZodZypesInternals.parseColor(color);
if (opacity >= 255) {
return `#${r.toString(16)}${g.toString(16)}${b.toString(16)}`;
}
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
index 8d7482108b..4446988e62 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
@@ -6,8 +6,8 @@ import {
INPUT_BORDER_COLOR_UNHOVERED,
} from '../../../helpers/colors';
import {
- useZodColorIfPossible,
useZodIfPossible,
+ useZodTypesIfPossible,
} from '../../get-zod-if-possible';
import {Spacing} from '../../layout';
import {ValidationMessage} from '../../NewComposition/ValidationMessage';
@@ -70,7 +70,7 @@ export const ZodArrayEditor: React.FC<{
throw new Error('expected zod');
}
- const zColor = useZodColorIfPossible();
+ const zColor = useZodTypesIfPossible();
const typeName = def.typeName as z.ZodFirstPartyTypeKind;
if (typeName !== z.ZodFirstPartyTypeKind.ZodArray) {
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodColorEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodColorEditor.tsx
index ac5b1dd575..ece06140e7 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodColorEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodColorEditor.tsx
@@ -2,8 +2,8 @@ import React, {useCallback, useMemo, useState} from 'react';
import type {z} from 'zod';
import {colorWithNewOpacity} from '../../../../color-math';
import {
- useZodColorIfPossible,
useZodIfPossible,
+ useZodTypesIfPossible,
} from '../../get-zod-if-possible';
import {Row, Spacing} from '../../layout';
import {InputDragger} from '../../NewComposition/InputDragger';
@@ -51,8 +51,8 @@ export const ZodColorEditor: React.FC<{
throw new Error('expected zod');
}
- const zodColor = useZodColorIfPossible();
- if (!zodColor) {
+ const zodTypes = useZodTypesIfPossible();
+ if (!zodTypes) {
throw new Error('expected zod color');
}
@@ -79,7 +79,7 @@ export const ZodColorEditor: React.FC<{
);
const {a, b, g, r} = localValue.zodValidation.success
- ? zodColor.ZColorInternals.parseColor(localValue.value)
+ ? zodTypes.ZodZypesInternals.parseColor(localValue.value)
: {a: 1, b: 0, g: 0, r: 0};
const onChange: React.ChangeEventHandler<HTMLInputElement> = useCallback(
@@ -87,7 +87,7 @@ export const ZodColorEditor: React.FC<{
const newColor = colorWithNewOpacity(
e.target.value,
Math.round(a),
- zodColor
+ zodTypes
);
const safeParse = schema.safeParse(newColor);
const newLocalState: LocalState = {
@@ -99,7 +99,7 @@ export const ZodColorEditor: React.FC<{
setValue(newColor);
}
},
- [a, schema, setValue, zodColor]
+ [a, schema, setValue, zodTypes]
);
const onTextChange: React.ChangeEventHandler<HTMLInputElement> = useCallback(
@@ -145,7 +145,7 @@ export const ZodColorEditor: React.FC<{
const newColor = colorWithNewOpacity(
localValue.value,
Math.round((Number(newValue) / 100) * 255),
- zodColor
+ zodTypes
);
const safeParse = schema.safeParse(newColor);
const newLocalState: LocalState = {
@@ -157,7 +157,7 @@ export const ZodColorEditor: React.FC<{
setValue(newColor);
}
},
- [localValue.value, schema, setValue, zodColor]
+ [localValue.value, schema, setValue, zodTypes]
);
const onOpacityValueChange = useCallback(
@@ -165,7 +165,7 @@ export const ZodColorEditor: React.FC<{
const newColor = colorWithNewOpacity(
localValue.value,
Math.round((Number(newValue) / 100) * 255),
- zodColor
+ zodTypes
);
const safeParse = schema.safeParse(newColor);
@@ -178,7 +178,7 @@ export const ZodColorEditor: React.FC<{
setValue(newColor);
}
},
- [localValue.value, schema, setValue, zodColor]
+ [localValue.value, schema, setValue, zodTypes]
);
return (
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx
index b559ec214b..3c4485089e 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx
@@ -3,8 +3,8 @@ import type {z} from 'zod';
import {LIGHT_TEXT} from '../../../helpers/colors';
import {Checkbox} from '../../Checkbox';
import {
- useZodColorIfPossible,
useZodIfPossible,
+ useZodTypesIfPossible,
} from '../../get-zod-if-possible';
import {Spacing} from '../../layout';
import {createZodValues} from './create-zod-values';
@@ -60,7 +60,7 @@ export const ZodOrNullishEditor: React.FC<{
throw new Error('expected zod');
}
- const zColor = useZodColorIfPossible();
+ const zColor = useZodTypesIfPossible();
const isChecked = value === nullishValue;
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodSwitch.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodSwitch.tsx
index fdb229fd48..7edd36d800 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodSwitch.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodSwitch.tsx
@@ -1,8 +1,8 @@
import React from 'react';
import type {z} from 'zod';
import {
- useZodColorIfPossible,
useZodIfPossible,
+ useZodTypesIfPossible,
} from '../../get-zod-if-possible';
import type {JSONPath} from './zod-types';
import {ZodArrayEditor} from './ZodArrayEditor';
@@ -51,7 +51,7 @@ export const ZodSwitch: React.FC<{
throw new Error('expected zod');
}
- const zodColor = useZodColorIfPossible();
+ const zodTypes = useZodTypesIfPossible();
// TODO: (Maybe?) enable saving of inserted input props by cmd+s /ctrl + s (also for JSON view)
@@ -241,8 +241,9 @@ export const ZodSwitch: React.FC<{
if (typeName === z.ZodFirstPartyTypeKind.ZodEffects) {
if (
- zodColor &&
- schema._def.description === zodColor.ZColorInternals.REMOTION_COLOR_BRAND
+ zodTypes &&
+ schema._def.description ===
+ zodTypes.ZodZypesInternals.REMOTION_COLOR_BRAND
) {
return (
<ZodColorEditor
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 81652bb6d4..ce925d5654 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,10 +1,10 @@
import type {z} from 'zod';
-import type {ZodColorType, ZodType} from '../../get-zod-if-possible';
+import type {ZodType, ZodTypesType} from '../../get-zod-if-possible';
export const createZodValues = (
schema: Zod.ZodTypeAny,
zodRuntime: ZodType,
- zColorRuntime: ZodColorType | null
+ zColorRuntime: ZodTypesType | null
): unknown => {
if (!schema) {
throw new Error('Invalid zod schema');
@@ -76,7 +76,7 @@ export const createZodValues = (
if (
zColorRuntime &&
schema._def.description ===
- zColorRuntime.ZColorInternals.REMOTION_COLOR_BRAND
+ zColorRuntime.ZodZypesInternals.REMOTION_COLOR_BRAND
) {
return '#ffffff';
}
diff --git a/packages/cli/src/editor/components/get-zod-if-possible.tsx b/packages/cli/src/editor/components/get-zod-if-possible.tsx
index 7cf99e6a6a..3175948fcb 100644
--- a/packages/cli/src/editor/components/get-zod-if-possible.tsx
+++ b/packages/cli/src/editor/components/get-zod-if-possible.tsx
@@ -8,7 +8,7 @@ import React, {
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
export type ZodType = Awaited<typeof import('zod')>['z'];
-export type ZodColorType = Awaited<
+export type ZodTypesType = Awaited<
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
typeof import('@remotion/zod-types')
>;
@@ -22,7 +22,7 @@ export const getZodIfPossible = async (): Promise<ZodType | null> => {
}
};
-export const getZColorIfPossible = async (): Promise<ZodColorType | null> => {
+export const getZTypesIfPossible = async (): Promise<ZodTypesType | null> => {
try {
const mod = await import('@remotion/zod-types');
return mod;
@@ -36,14 +36,14 @@ export const useZodIfPossible = () => {
return context?.zod ?? null;
};
-export const useZodColorIfPossible = () => {
+export const useZodTypesIfPossible = () => {
const context = useContext(ZodContext);
- return context?.zodColor ?? null;
+ return context?.zodTypes ?? null;
};
type ContextType = {
zod: ZodType | null;
- zodColor: ZodColorType | null;
+ zodTypes: ZodTypesType | null;
};
const ZodContext = createContext<ContextType | null>(null);
@@ -52,22 +52,22 @@ export const ZodProvider: React.FC<{
children: React.ReactNode;
}> = ({children}) => {
const [zod, setZod] = useState<ZodType | null>(null);
- const [zodColor, setZodColor] = useState<ZodColorType | null>(null);
+ const [zodTypes, setZodTypes] = useState<ZodTypesType | null>(null);
useEffect(() => {
getZodIfPossible().then((z) => setZod(z));
}, []);
useEffect(() => {
- getZColorIfPossible().then((z) => setZodColor(z));
+ getZTypesIfPossible().then((z) => setZodTypes(z));
}, []);
const contextValue = useMemo(() => {
return {
zod,
- zodColor,
+ zodTypes,
};
- }, [zod, zodColor]);
+ }, [zod, zodTypes]);
return (
<ZodContext.Provider value={contextValue}>{children}</ZodContext.Provider>
diff --git a/packages/cli/src/test/color-math.test.ts b/packages/cli/src/test/color-math.test.ts
index a2e6ef7cc2..e87db9db9c 100644
--- a/packages/cli/src/test/color-math.test.ts
+++ b/packages/cli/src/test/color-math.test.ts
@@ -1,8 +1,8 @@
import {expect, test} from 'vitest';
-import {getZColorIfPossible} from '../editor/components/get-zod-if-possible';
+import {getZTypesIfPossible} from '../editor/components/get-zod-if-possible';
const getZColor = async () => {
- const z = await getZColorIfPossible();
+ const z = await getZTypesIfPossible();
if (!z) {
throw new Error('@remotion/zod-types not found');
}
@@ -12,7 +12,7 @@ const getZColor = async () => {
test('Color math', async () => {
const mod = await getZColor();
- expect(mod.ZColorInternals.parseColor('rgba(255, 255, 255, 0.5)')).toEqual({
+ expect(mod.ZodZypesInternals.parseColor('rgba(255, 255, 255, 0.5)')).toEqual({
a: 128,
r: 255,
b: 255,
diff --git a/packages/cli/src/test/create-zod-values.test.ts b/packages/cli/src/test/create-zod-values.test.ts
index 9e813604f9..de75d3c3c7 100644
--- a/packages/cli/src/test/create-zod-values.test.ts
+++ b/packages/cli/src/test/create-zod-values.test.ts
@@ -1,7 +1,7 @@
import {expect, test} from 'vitest';
import {
- getZColorIfPossible,
getZodIfPossible,
+ getZTypesIfPossible,
} from '../editor/components/get-zod-if-possible';
import {createZodValues} from '../editor/components/RenderModal/SchemaEditor/create-zod-values';
@@ -15,7 +15,7 @@ const getZ = async () => {
};
const getZColor = async () => {
- const z = await getZColorIfPossible();
+ const z = await getZTypesIfPossible();
if (!z) {
throw new Error('@remotion/zod-types not found');
}
diff --git a/packages/zod-types/src/index.ts b/packages/zod-types/src/index.ts
index 0f14da5c6b..c9bfbbefd9 100644
--- a/packages/zod-types/src/index.ts
+++ b/packages/zod-types/src/index.ts
@@ -2,7 +2,7 @@ import {REMOTION_COLOR_BRAND, parseColor} from './z-color.js';
export {zColor} from './z-color.js';
-export const ZColorInternals = {
+export const ZodZypesInternals = {
parseColor,
REMOTION_COLOR_BRAND,
};