-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lightweight
h
utility to construct UIs
- Loading branch information
1 parent
192d50d
commit 296644f
Showing
2 changed files
with
125 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
export type H<Tag extends HTag = HTag> = HTMLElementTagNameMap[Tag] | ||
export type HProps<_Tag extends HTag = HTag> = Record<string, any> | ||
export type HTag = keyof HTMLElementTagNameMap | ||
|
||
export type HFn = { | ||
<Tag extends HTag>(tag: Tag): HNode<Tag>, | ||
<Tag extends HTag>(tag: Tag, options: HProps): HNode<Tag>, | ||
<Tag extends HTag>(tag: Tag, children: HNode[]): HNode<Tag>, | ||
<Tag extends HTag>(tag: Tag, options: HProps, children: HNode[]): HNode<Tag>, | ||
} | ||
|
||
export type HNamedFn = { | ||
<Tag extends HTag>(): HNode<Tag>, | ||
<Tag extends HTag>(options: HProps): HNode<Tag>, | ||
<Tag extends HTag>(children: HNode[]): HNode<Tag>, | ||
<Tag extends HTag>(options: HProps, children: HNode[]): HNode<Tag>, | ||
} | ||
|
||
export type HNode<Tag extends HTag = HTag> = { | ||
tag: Tag, | ||
options: HProps<Tag>, | ||
children: HNode[], | ||
} | ||
|
||
const isVoidTag = (tag: string): boolean => { | ||
return voidTags.includes(tag) | ||
} | ||
|
||
const voidTags = Object.freeze([ | ||
'area', | ||
'base', | ||
'br', | ||
'col', | ||
'embed', | ||
'hr', | ||
'img', | ||
'input', | ||
'link', | ||
'meta', | ||
'param', | ||
'source', | ||
'track', | ||
'wbr', | ||
]) | ||
|
||
export const createElement = <Tag extends HTag>(hNode: HNode<Tag>): H<Tag> => { | ||
const element = document.createElement(hNode.tag) | ||
|
||
for (const [key, value] of Object.entries(hNode.options)) { | ||
element.setAttribute(key, value) | ||
} | ||
|
||
if (!isVoidTag(hNode.tag)) { | ||
for (const child of hNode.children) { | ||
element.append(createElement(child)) | ||
} | ||
} | ||
|
||
return element | ||
} | ||
|
||
export const createString = <Tag extends HTag>(hNode: HNode<Tag>): string => { | ||
const { tag } = hNode | ||
|
||
const attributes = Object.entries(hNode.options).map(([key, value]) => `${key}="${value}"`).join(' ') | ||
|
||
if (isVoidTag(tag)) { | ||
return `<${tag} ${attributes} />` | ||
} | ||
|
||
const children = hNode.children.map(createString).join('') | ||
|
||
return `<${tag} ${attributes}>${children}</${tag}>` | ||
} | ||
|
||
export const h: HFn = (tag, optionsOrChildren?: any, maybeChildren?: any) => { | ||
const options: HProps = Array.isArray(optionsOrChildren) ? {} : optionsOrChildren | ||
const children: HNode[] = (Array.isArray(optionsOrChildren) ? optionsOrChildren : maybeChildren) ?? [] | ||
|
||
return { | ||
tag, | ||
options, | ||
children: isVoidTag(tag) ? [] : children, | ||
} | ||
} |