UNPKG

@playcanvas/react

Version:

A React renderer for PlayCanvas – build interactive 3D applications using React's declarative paradigm.

79 lines (78 loc) 4.13 kB
import { Application } from "playcanvas"; import { Serializable } from "./types-utils"; export declare const warnOnce: (message: string) => void; export type PropValidator<T, InstanceType> = { validate: (value: unknown) => boolean; errorMsg: (value: unknown) => string; default: T | unknown; apply?: (container: InstanceType, props: Record<string, unknown>, key: string) => void; }; export type Schema<T, InstanceType> = { [K in keyof T]?: PropValidator<T[K], InstanceType>; }; export type ComponentDefinition<T, InstanceType> = { name: string; apiName?: string; schema: Schema<T, InstanceType>; }; /** * Validate and sanitize a prop. This will validate the prop and return the default value if the prop is invalid. * * @param value The value to validate. * @param propDef The prop definition. * @param propName The name of the prop. * @param componentName The name of the component. * @param apiName The API name of the component. eg `<Render/>`. Use for logging. */ export declare function validateAndSanitize<T, InstanceType>(value: unknown, propDef: PropValidator<T, InstanceType>, propName: string, componentName: string, apiName?: string): T; /** * Validate props partially. This iterates over props and validates them against the schema. * If a prop is not in the schema, it will be ignored. This will not return default values for missing props. * * @param rawProps The raw props to validate. * @param componentDef The component definition. * @param warnUnknownProps Whether to warn about unknown props. * @returns The validated props. */ export declare function validatePropsPartial<T, InstanceType>(rawProps: Serializable<T>, componentDef: ComponentDefinition<T, InstanceType>, warnUnknownProps?: boolean): T; /** * Validate props returning defaults. This iterates over a schema and uses the default value if the prop is not defined. * * @param rawProps The raw props to validate. * @param componentDef The component definition. * @param warnUnknownProps Whether to warn about unknown props. * @returns The validated props. */ export declare function validatePropsWithDefaults<T extends object, InstanceType>(rawProps: Serializable<T>, componentDef: ComponentDefinition<T, InstanceType>, warnUnknownProps?: boolean): T; /** * Apply props to an instance in a safe way. It will use the apply function if it exists, otherwise it will assign the value directly. * This is useful for components that need to map props to instance properties. eg [0, 1] => Vec2(0, 1). * * @param container The container to apply the props to * @param schema The schema of the container * @param props The props to apply */ export declare function applyProps<T extends Record<string, unknown>, InstanceType>(instance: InstanceType, schema: Schema<T, InstanceType>, props: T): void; /** * Get the pseudo public props of an instance. This is useful for creating a component definition from an instance. * @param container The container to get the pseudo public props from. * @returns The pseudo public props of the container. */ export declare function getPseudoPublicProps(container: Record<string, unknown>): any; /** * Create a component definition from an instance. A component definition is a schema that describes the props of a component, * and can be used to validate and apply props to an instance. * * @param name The name of the component. * @param createInstance A function that creates an instance of the component. * @param cleanup A function that cleans up the instance. * @param apiName The API name of the component. */ export declare function createComponentDefinition<T, InstanceType>(name: string, createInstance: () => InstanceType, cleanup?: (instance: InstanceType) => void, apiName?: string): ComponentDefinition<T, InstanceType>; /** * This is a mock application that is used to render the application without a canvas. * @private * @returns A mock application that is used to render the application without a canvas. */ export declare function getNullApplication(): Application; export declare const getStaticNullApplication: () => Application;