I like the way you handle the props in your function, I just would prefer to immediately add some content to the new element. I slightly extended your function to add some content (a text, a child node or an array of child nodes)
functioncreateElement(nodeName,content,props={}){const{style={},...propsNoStyle}=props;constelement=Object.assign(document.createElement(nodeName),propsNoStyle);Object.entries(style).forEach(([key,value])=>{element.style[key]=value;});// append child or array of childsfunctionappend(cl){if(typeof(cl)==='string')cl=document.createTextNode(cl)element.appendChild(cl)}if(Array.isArray(content))content.map(cl=>append(cl))elseappend(content)returnelement;}
There is a really tricky pattern to build "creator" functions for DOM elements based on your function (thanks to Tao from VanJS) :
The code above could be written more compact, but this makes it really hard to understand. makefnc returns a function that creates a special type of element. You can use it that way:
consth1=makefnc("h1")letel=h1("This is a headline",{style:{color:"red"}})
Using the tags function is even more elegant. Below, I show a little code example:
const{div,h1,button}=tags// create tag functions for div, h1 and buttonleth,b,count=0h=h1(`${count}`,{style:{color:"red"}});(b=button("CLICK")).onclick=()=>h.innerText=++countdocument.body.append(div([h,b]))
There is an even more compact approach on the same principle presented here, that adds an auto-append feature, so you do not even need to append the elements manually.
I'm a fan of Open Source and have a growing interest in serverless and edge computing. I'm not a big fan of spiders, but they're doing good work eating bugs. I also stream on Twitch @ nickyt.live.
I like the way you handle the props in your function, I just would prefer to immediately add some content to the new element. I slightly extended your function to add some content (a text, a child node or an array of child nodes)
There is a really tricky pattern to build "creator" functions for DOM elements based on your function (thanks to Tao from VanJS) :
The code above could be written more compact, but this makes it really hard to understand. makefnc returns a function that creates a special type of element. You can use it that way:
Using the tags function is even more elegant. Below, I show a little code example:
There is an even more compact approach on the same principle presented here, that adds an auto-append feature, so you do not even need to append the elements manually.
I didn’t bother with the content as you can just add a prop
textContentor similar, but glad to see you’re extending it!Sure, in that case it's just a question of convenience. But you can also build nested elements, so it´s a way to compose your page or parts of it :
By the way, CSS styles use a syntax, which is a bit different from JSON:
With your approach, people need to translate the style definition like this:
As you need to process the styles anyway, would it not be better to use the usual syntax?
If you do not want to process the CSS manually, you can use this function: