A place to cache linked articles (think custom and personal wayback machine)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.md 9.0KB

4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. title: Introducing Bliss: A 3KB library for happier Vanilla JS
  2. url: http://lea.verou.me/2015/12/introducing-bliss-a-3kb-library-for-happier-vanilla-js/
  3. hash_url: 5d5b6602ee225a8262987bded2b6ac2b
  4. <p><a href="http://blissfuljs.com"><img class="alignleft wp-image-2574 size-medium" src="http://lea.verou.me/wp-content/uploads/2015/12/Screen-Shot-2015-12-04-at-16.59.39-300x127.png" alt="Screen Shot 2015-12-04 at 16.59.39"/></a>Anyone who follows this blog, my twitter, or my work probably is aware that <a href="http://lea.verou.me/2015/04/jquery-considered-harmful/">I’m not a huge fan of big libraries</a>. I think wrapper objects are messy, and big libraries are overkill for smaller projects. On large projects, one uses frameworks like React or Angular anyway, not libraries.</p>
  5. <p>Anyone who writes <a href="http://vanilla-js.com/" target="_blank">Vanilla JS</a> on a daily basis probably is aware that it can sometimes be, ahem, somewhat unpleasant to work with. Sure, the situation is orders of magnitude better than it was when I started. Back then, IE6 was the dominant browser and you needed a helper function to even add event listeners to an element (remember element.attachEvent?) or to get elements by a class!</p>
  6. <p><a href="http://lea.verou.me/wp-content/uploads/2015/12/jasset-datepicker.png"><img class="alignright wp-image-2571" title="Ah, the memories!" src="http://lea.verou.me/wp-content/uploads/2015/12/jasset-datepicker-300x211.png" alt="jasset-datepicker"/></a></p>
  7. <p><strong>Fun fact:</strong> I learned JavaScript back then by writing my own library, called <em>jAsset</em>. I had not heard of jQuery when I started it in 2007, so I had even coded my own selector engine! (Anyone remember <em>slickspeed</em>?) jAssset had plenty of nice helper functions, its own UI library and a cool logo. I had even started to make a website for its UI components, seen on the right.</p>
  8. <p><a href="https://shipitsquirrel.github.io"><img class="alignleft" src="http://shipitsquirrel.github.io/images/ship%20it%20squirrel.png" alt=""/></a>Sadly, <em>jAsset</em> died the sad inevitable death of all unreleased projects: Without external feedback, I had nobody to hold me back from adding to its API every time I personally needed a helper function. And adding, and adding, and adding… Until it became 5000+ loc long and its benefit of being lightweight or comprehensible had completely vanished. It collapsed under its own weight before it even saw the light of day. I abandoned it and went through a few years of using jQuery as my preferred helper library. Eventually, my <a href="http://lea.verou.me/2015/04/jquery-considered-harmful/">distaste for wrapper objects</a>, the constantly improving browser support for new APIs that made Vanilla JS more palatable, and the decline of overly conspicuous browser bugs led me to give it up.</p>
  9. <p>It was refreshing, and educational, but soon I came to realize that while Vanilla JS is orders of magnitude better than it was when I started, certain APIs are still quite unwieldy, which can be annoying if you use them often. For example, the Vanilla JS for creating an element, with other elements inside it, events and inline styles is so commonly needed, but also so verbose and <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">WET</a>, it can make one suicidal.</p>
  10. <p>However, Vanilla JS does not mean “use no abstractions”. Programming is all about abstractions! The Vanilla JS movement, is about favoring speed, smaller abstractions and understanding of the Web Platform, over big libraries that we treat as a black box. It’s about using libraries to save time, not to skip learning.</p>
  11. <p>So, I used my own tiny helpers, on every project. They were small and easy to understand, instead of several KB of code aiming to fix browser bugs I will likely never encounter and let me create complex nested DOM structures with a single JSON-like object. Over time, their API solidified and improved. On larger projects it was a separate file which I had tentatively codenamed <em>Utopia</em> (due to the lack of browser bug fixes and optimistic use of modern APIs). On smaller ones just a few helper methods (I could not live without at least my tiny 2 sloc $() and $$() helpers!). Here is a sample from my open source repos:</p>
  12. <ul>
  13. <li><a href="https://github.com/LeaVerou/dabblet/blob/master/code/utopia.js">dabblet.com/utopia.js</a></li>
  14. <li><a href="https://github.com/LeaVerou/regexplained/blob/gh-pages/utopia.js">regexplained/utopia.js</a></li>
  15. <li><a href="https://github.com/LeaVerou/dpi/blob/gh-pages/utopia.js">dpi.lv/utopia.js</a></li>
  16. <li><a href="https://github.com/LeaVerou/css3test/blob/gh-pages/utopia.js">css3test.com/utopia.js</a></li>
  17. <li><a href="https://github.com/LeaVerou/awesomplete/blob/gh-pages/awesomplete.js#L296-L351">awesomplete.js</a></li>
  18. <li>$() and/or $$() helpers in:
  19. <ul>
  20. <li><a href="https://github.com/LeaVerou/prefixfree/blob/gh-pages/prefixfree.js#L167-L169">prefixfree</a></li>
  21. <li><a href="https://github.com/LeaVerou/csss/blob/gh-pages/slideshow.js#L10-L11">CSSS</a></li>
  22. <li><a href="https://github.com/LeaVerou/animatable/blob/gh-pages/index.js#L1-L2">animatable</a></li>
  23. <li><a href="https://github.com/LeaVerou/contrast-ratio/blob/gh-pages/contrast-ratio.js#L1-L7">contrast-ratio</a></li>
  24. <li><a href="https://github.com/LeaVerou/cubic-bezier/blob/gh-pages/environment.js#L27-L28">cubic-bezier.com</a></li>
  25. <li><a href="https://github.com/LeaVerou/whathecolor/blob/gh-pages/whathecolor.js#L1-L7">whathecolor</a></li>
  26. <li><a href="https://github.com/LeaVerou/corner-shape/blob/gh-pages/corner-shape.js#L1-L2">corner-shape</a></li>
  27. <li><a href="https://github.com/LeaVerou/css-colors/blob/gh-pages/colors.js#L1-L2">css-colors</a></li>
  28. </ul>
  29. </li>
  30. </ul>
  31. <p>Notice any recurring themes there? <img src="http://lea.verou.me/wp-includes/images/smilies/simple-smile.png" alt=":)" class="wp-smiley"/></p>
  32. <p>I never mentioned <em>Utopia.js</em> anywhere, besides silently including it in my projects, so it went largely unnoticed. Sometimes people would look at it, ask me to release it, I’d promise them I would and then nothing. A few years ago, someone noticed it, liked it and <a href="http://davidhiggins.me/utopia/">documented it</a> a bit (site is down now it seems). However, it was largely my little secret, hidden in public view.</p>
  33. <p>For the past half year, I’ve been working hard on my research project at MIT. It’s pretty awesome and is aimed at helping people who know HTML/ CSS but <strong>not</strong> JS, achieve more with Web technologies (and that’s all I can say for now). It’s also written in JS, so I used <em>Utopia</em> as a helper library, naturally. <em>Utopia</em> evolved even more with this project, got renamed to <em>Bliss</em> and got chainability via <a href="http://lea.verou.me/2015/04/idea-extending-native-dom-prototypes-without-collisions/">my idea about extending DOM prototypes without collisions</a> (can be disabled and the property name is customizable).</p>
  34. <p>All this worked fine while I was the only person working on the project. Thankfully, I might get some help soon, and it might be rather inexperienced (the academia equivalent of interns). Help is very welcome, but it did raise the question: How will these people, who likely only know jQuery, work on the project? [1]</p>
  35. <p>The answer was that the time has come to polish, document and release <a href="http://blissfuljs.com">Bliss</a> to the world. My plan was to spend a weekend documenting it, but it ended up being a little over a week on and off, when procrastinating from other tasks I had to do. However, I’m very proud of the resulting docs, so much that I gifted myself <a href="http://blissfuljs.com">a domain</a> for it. They are fairly extensive (though some functions still need work) and has two things I always missed in other API docs:</p>
  36. <ul>
  37. <li>Recommendations about what Vanilla JS to use instead when appropriate, instead of guiding people into using library methods even when Vanilla JS would have been perfectly sufficient.</li>
  38. <li>A “Show Implementation” button showing the implementation, so you can both learn, and judge whether it’s needed or not, instead of assuming that you should use it over Vanilla JS because it has magic pixie dust. This way, the docs also serve as a source viewer!</li>
  39. </ul>
  40. <p>So, enjoy <a href="http://blissfuljs.com">Bliss</a>. The helper library for people who don’t like helper libraries. <img src="http://lea.verou.me/wp-includes/images/smilies/simple-smile.png" alt=":)" class="wp-smiley"/> In a way, it feels that a journey of 8 years, finally ends today. I hope the result makes you blissful too.</p>
  41. <p><a class="view-demo" href="http://blissfuljs.com" target="_blank">blissfuljs.com</a></p>
  42. <p>Oh, and don’t forget to follow <a href="http://twitter.com/blissfuljs">@blissfuljs</a> on twitter!</p>
  43. <p>[1]: Academia is often a little behind tech-wise, so <strong>everyone</strong> uses jQuery here — hardly any exceptions. Even though browser support doesn’t usually even matter to research projects!</p>