r/javascript • • Apr 06 '23

New Library for creating HTML with Javascript

https://github.com/Fighter178/micromarkup
0 Upvotes

22 comments sorted by

20

u/[deleted] Apr 06 '23

Why?

4

u/Initial_Low_5027 Apr 06 '23

Why not using JSX? Can be used independently of React.

1

u/Fighter178 Apr 06 '23 edited Apr 06 '23

I'm not using JSX because I needed to write this in pure js. It was annoying, and I would've just used JSX.

1

u/Initial_Low_5027 Apr 06 '23

JSX gets transpiled to pure JavaScript and looks quite similar to your stacked calls. It’s easy to overload Babel and put your own code there.

9

u/lazyinvader Apr 06 '23

Why, not? I guess someone learnt a bit by creating it.

No one force you to use it.

4

u/cocosin Apr 06 '23

I made almost the same thing for a pure js widget to inject on external sites in my latest project. Thanks for the lib, didn’t find anything like that in my latest research

A few of my comments on the code review:

  • Why do you create a div as a wrapper? You can create a fragment and return it. Now it adds additional unpredictable layer to the markup

  • You don’t have additional argument in the addEventListener function, so there’s no chance to add logic for the bubbling/capturing functionality

  • the mk and n functions look the same. You can use a main helper for this to reduce the size

  • In my product I needed to add raw html as a child when creating

2

u/Fighter178 Apr 06 '23 edited Apr 06 '23

I'm gonna update it. Also, I did intend to add code for the event func, but I forgot to add it. And yes, I should just return a document fragment. I'll probably update it. Also, if you need raw html, you can access the innerHTML of the element.

1

u/Fighter178 Apr 06 '23

I updated it, it returns a document fragment.

2

u/lookitsatoucan Apr 06 '23

Nice job! What problems is this library designed to solve?

2

u/Fighter178 Apr 06 '23

I needed to make a bunch of elements in pure js for a project, and I just made this to make it easier and faster to write.

1

u/Remedynn Apr 06 '23

How do you manage state? Or do the user just need to use document getters?

1

u/Fighter178 Apr 06 '23

I might add that, but if you need a library that makes HTML with state, check out another library I made, firefly.js

1

u/[deleted] Apr 06 '23

I see some potential to use this, I think it's gonna be helpful for me, especially with adding event listeners. One thing - can you add a functionality where I could change "mk" function HTMLElement wrapper from div to something else, like in "node" function?

1

u/Fighter178 Apr 06 '23

Sure, I might make it return a document fragment, or possibly allow you to change it.

1

u/[deleted] Apr 06 '23

Not to shit on op's post but i would myself rather use something like lit-html instead

0

u/Fighter178 Apr 06 '23

That's true, though for the original project I made this for, I had to use pure js, and now it can be useful for more people.

0

u/Fighter178 Apr 06 '23

Its also available on NPM: here

I know the docs are bad, you can fix them if you want.

1

u/Sipike Apr 07 '23

Reminds me of Mithril.js

-3

u/jeremrx Apr 06 '23

1

u/Fighter178 Jun 28 '23

Its meant to be fast to write. Imagine trying to write out "element" each time you need one. (Yes, I'm intelisense helps)

1

u/jeremrx Jun 28 '23

And this is the common junior mistake ;)