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.
This commit is contained in:
hypercross 2026-04-19 11:30:14 +08:00
parent 762ed1a63a
commit 3afe78f54f
1 changed files with 18 additions and 4 deletions

View File

@ -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) {
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;
}