Add Node component
Some checks failed
Lint / lint (push) Failing after 9s

This commit is contained in:
Julien Valverdé
2025-12-29 19:35:31 +01:00
parent fba9ca6193
commit bd31fd5ea4
5 changed files with 36 additions and 7 deletions

View File

@@ -1,3 +1,17 @@
import { Control } from "godot"
import { Node } from "react-godot-renderer"
export function TestUi1Component() { export function TestUi1Component() {
return <node2D /> return (
<Node
class={Control}
anchor_left={0}
/>
)
} }
Node({
class: Control,
})

View File

@@ -2,21 +2,21 @@ import type * as Godot from "godot"
import type { PascalToCamel } from "./utils.js" import type { PascalToCamel } from "./utils.js"
type NodeClass = { export type NodeClass = {
[K in keyof typeof Godot]: typeof Godot[K] extends new (...args: any[]) => Godot.Node [K in keyof typeof Godot]: typeof Godot[K] extends new (...args: any[]) => Godot.Node
? K ? K
: never : never
}[keyof typeof Godot] }[keyof typeof Godot]
type GodotIntrinsicElements = { export type GodotIntrinsicElements = {
[K in NodeClass as PascalToCamel<K>]: PropsFromInstance<InstanceType<(typeof Godot)[K]>> [K in NodeClass as PascalToCamel<K>]: PropsFromInstance<InstanceType<(typeof Godot)[K]>>
} & { } & {
custom: { element: {
class: new (...args: any[]) => Godot.Node class: new (...args: any[]) => Godot.Node
} }
} }
type PropsFromInstance<T> = { export type PropsFromInstance<T> = {
// biome-ignore lint/complexity/noBannedTypes: it's completely fine // biome-ignore lint/complexity/noBannedTypes: it's completely fine
[K in keyof T as T[K] extends Function ? never : K]?: T[K] [K in keyof T as T[K] extends Function ? never : K]?: T[K]
} }

View File

@@ -0,0 +1,14 @@
import type * as Godot from "godot"
import * as React from "react"
import type * as JSX from "./JSX.js"
export type NodeProps<T extends Godot.Node> = {
readonly class: new (...args: any[]) => T
} & JSX.PropsFromInstance<NoInfer<T>>
export function Node<T extends Godot.Node>(
props: NodeProps<T>
): React.JSX.Element {
return React.createElement("element", props)
}

View File

@@ -29,9 +29,9 @@ export const make = () => {
createInstance(type, props) { createInstance(type, props) {
let instance: Node let instance: Node
if (type === "custom") { if (type === "element") {
if (!hasProperty(props, "class")) if (!hasProperty(props, "class"))
throw new Error("Property 'class' required when using the 'custom' intrinsic type") throw new Error("Property 'class' required when using the 'element' intrinsic type")
instance = new (props.class as any)() instance = new (props.class as any)()
} }
else { else {

View File

@@ -1,3 +1,4 @@
export * as JSX from "./JSX.js" export * as JSX from "./JSX.js"
export * from "./Node.js"
export * as Reconciler from "./Reconciler.js" export * as Reconciler from "./Reconciler.js"
export * as Renderer from "./Renderer.js" export * as Renderer from "./Renderer.js"