From 3afe78f54ff9424f227f077d45e9c9f4d041f7e8 Mon Sep 17 00:00:00 2001 From: hypercross Date: Sun, 19 Apr 2026 11:30:14 +0800 Subject: [PATCH] refactor(framework): allow string ID for GameUI container Update GameUIOptions to accept either an HTMLElement or a string ID. If a string is provided, the class will attempt to find the element by ID or create a new div if it does not exist. Also update the root property to accept a JSX.Element. --- packages/framework/src/ui/GameUI.tsx | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/ui/GameUI.tsx b/packages/framework/src/ui/GameUI.tsx index d9e0161..412194f 100644 --- a/packages/framework/src/ui/GameUI.tsx +++ b/packages/framework/src/ui/GameUI.tsx @@ -1,14 +1,28 @@ +import type { JSX } from "preact"; + export interface GameUIOptions { - container: HTMLElement; - root: HTMLElement; + container: HTMLElement | string; + root: JSX.Element; } export class GameUI { private container: HTMLElement; - private root: HTMLElement; + private root: JSX.Element; constructor(options: GameUIOptions) { - this.container = options.container; + if (typeof options.container === "string") { + const existing = document.getElementById(options.container); + if (existing) { + this.container = existing; + } else { + const newContainer = document.createElement("div"); + newContainer.id = options.container; + document.body.appendChild(newContainer); + this.container = newContainer; + } + } else { + this.container = options.container; + } this.root = options.root; }