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:
parent
762ed1a63a
commit
3afe78f54f
|
|
@ -1,14 +1,28 @@
|
||||||
|
import type { JSX } from "preact";
|
||||||
|
|
||||||
export interface GameUIOptions {
|
export interface GameUIOptions {
|
||||||
container: HTMLElement;
|
container: HTMLElement | string;
|
||||||
root: HTMLElement;
|
root: JSX.Element;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GameUI {
|
export class GameUI {
|
||||||
private container: HTMLElement;
|
private container: HTMLElement;
|
||||||
private root: HTMLElement;
|
private root: JSX.Element;
|
||||||
|
|
||||||
constructor(options: GameUIOptions) {
|
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;
|
this.root = options.root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue