-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
53 lines (46 loc) · 1.51 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/// <reference path="./definitions/index.d.ts" />
import { DocumentWrapper } from "./dom/document";
import { DomWrapper } from "./dom/dom";
/**
* OneJS's own h function. Use this to quickly create elements in jsx-like syntax
* @param type
* @param props
* @param children
* @returns
*/
export function h(type: any, props: any, ...children: any[]): any {
const element = typeof type === "string" ? document.createElement(type) : type;
// Assign properties to the element
for (const [key, value] of Object.entries(props || {})) {
if (key.startsWith("on") && typeof value === "function") {
element.addEventListener(key.substring(2).toLowerCase(), value);
} else if (key === "style" && typeof value === "object") {
Object.assign(element.style, value);
} else {
element.setAttribute(key, value);
}
}
// Append children
for (const child of children) {
if (typeof child === "string") {
element.appendChild(document.createTextNode(child));
} else {
element.appendChild(child);
}
}
return element;
}
export { emo } from "./styling/index"
declare global {
interface Document extends DocumentWrapper { }
interface Element extends DomWrapper {
classname: string
nodeType: number
ve: CS.UnityEngine.UIElements.VisualElement
}
}
// @ts-ignore
if (typeof ___document != "undefined") {
// @ts-ignore
globalThis.document = new DocumentWrapper(___document)
}