aboutsummaryrefslogtreecommitdiff
path: root/src/lib/h.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/h.ts')
-rw-r--r--src/lib/h.ts41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/lib/h.ts b/src/lib/h.ts
new file mode 100644
index 0000000..1e49f2f
--- /dev/null
+++ b/src/lib/h.ts
@@ -0,0 +1,41 @@
+type Child = Element | Text | string | number
+
+export type Children = Child[]
+
+export function h(
+ tagName: string,
+ attrs: object,
+ ...children: Children
+): Element {
+ const isSvg = tagName === 'svg' || tagName === 'path'
+
+ let elem = isSvg
+ ? document.createElementNS('http://www.w3.org/2000/svg', tagName)
+ : document.createElement(tagName)
+
+ if (isSvg) {
+ Object.entries(attrs).forEach(([key, value]) => {
+ elem.setAttribute(key, value)
+ })
+ } else {
+ elem = Object.assign(elem, attrs)
+ }
+
+ for (const child of children) {
+ if (typeof child === 'number')
+ elem.append(child.toString())
+ else
+ elem.append(child)
+ // if (Array.isArray(child))
+ // elem.append(...child)
+ // else
+ // elem.append(child)
+ }
+
+ return elem
+}
+
+export function concatClassName(attrs: any, className: string): object {
+ const existingClassName = 'className' in attrs ? attrs['className'] : undefined
+ return { ...attrs, className: `${className} ${existingClassName}` }
+}