Abra.js

Static page example for Abra.js

I love making simple tools, and my favorite tools for making simple tools is HTML + JS. Something I run into every time in these small projects is creating and appending DOM nodes, a problem for which there are 5 trillion solutions. In a big project, one might use React to solve this problem. In a medium project, one might use jQuery to solve this problem. But what about small projects? Like really small projects with no dependencies?

My favorite solution has been to use template strings:

Nice. Awesome. Template strings are cool. My text editor even knows how to highlight the stuff in the template string. It even works with nested template strings:

Still pretty cool. It looks a bit like JSX, and we’re still using vanilla. But I don’t love double (or triple…) nesting template strings. Also, we lose a lot of the power of our editors (like linters) inside of template strings. But what about binding functions?

That’s rough. We have to either use the name of the function on the window to call it from our template string or query for it after we create it. Both ways suck.

Wouldn’t it be nice if we could create elements with JavaScript objects? This is how React started (sort of) and it’s what Abra does:

Does it look better? In some ways. It loses the JSX feel of HTML-in-your-JS, but it makes the DOM-nodes-are-just-JS-objects thing way more clear.

I made Abra because I was tired of querying for elements I had just created, and I would be pleasantly shocked if other people found it useful.

Use it if you want, but please don’t use it if you are already using jQuery or React. Those are bigger, more powerful tools that solve this problem more completely. Use Abra if you just want to copy a few lines of JS into your project to make creating elements easier.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store