Skip to content

Instantly share code, notes, and snippets.

@guiseek
Created June 15, 2025 07:43
Show Gist options
  • Save guiseek/c9643a8d54e8b97df5d760c2aec375b1 to your computer and use it in GitHub Desktop.
Save guiseek/c9643a8d54e8b97df5d760c2aec375b1 to your computer and use it in GitHub Desktop.
Dom utility factory
type Tag = keyof HTMLElementTagNameMap
type Dom = {
[T in Tag]: <A extends HTMLElementTagNameMap[T]>(
attrs: Partial<A>
) => HTMLElementTagNameMap[T]
}
// prettier-ignore
const TAGS: Tag[] = ['a','abbr','address','area','article','aside','audio','b','base','bdi','bdo','blockquote','body','br','button','canvas','caption','cite','code','col','colgroup','data','datalist','dd','del','details','dfn','dialog','div','dl','dt','em','embed','fieldset','figcaption','figure','footer','form','h1','h2','h3','h4','h5','h6','head','header','hgroup','hr','html','i','iframe','img','input','ins','kbd','label','legend','li','link','main','map','mark','menu','meta','meter','nav','noscript','object','ol','optgroup','option','output','p','picture','pre','progress','q','rp','rt','ruby','s','samp','script','search','section','select','slot','small','source','span','strong','style','sub','summary','sup','table','tbody','td','template','textarea','tfoot','th','thead','time','title','tr','track','u','ul','var','video','wbr',]
const dom = TAGS.reduce((prev, curr) => {
const factory = <T extends HTMLElement>(attrs: Partial<T>) => {
const element = document.createElement(curr)
return Object.assign(element, attrs)
}
return {...prev, [curr]: factory}
}, {} as Dom)
export {dom}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment