Show HN: Umbrella JS, a 2.5kb jQuery alternative
umbrellajs.com
umbrellajs.com
It follows loosely a subset of jQuery methods, but it adds some others where relevant for common JS patterns (.handle() == .on() + e.preventDefault()) and the existing ones are generally more flexible.
[1] https://news.ycombinator.com/item?id=10954191
[2] https://npm-stat.com/charts.html?package=umbrellajs&from=201...
I did in fact try to improve the size of jQuery, but the tests are too coupled to the internal behavior of the methods, even if undocumented and (potentially) unintended initially. So it was very difficult even to change the most basic things, let alone change the internal structure of the whole method.
With Umbrella JS I'm trying to get to somewhere closer to native Javascript than jQuery. See for instance the arguments of any looping function (follows similar to the native arrays), or how things like .addClass(name) [1] are thin wrappers around .classList.add(name) (but quite more permissive/flexible). So it will follow closer to classList behavior than to whatever is defined in jQuery's tests, which is a win-win IMHO (since it also makes for a much smaller size, compare to jQuery's .addClass(name) [2]).
[1] https://github.com/franciscop/umbrella/blob/master/src/plugi...
[2] https://github.com/jquery/jquery/blob/master/src/attributes/...
Not sure how much sense it makes for Umbrella JS though, since traditionally it'd be used under a CDN but now with webpack it's treated as another dependency. Indeed, the CDN has remain constant-ish but the npm one has grown quite a bit in the last year.
Feel free to use Umbrella JS and ask me any question!
1. I went by the name and typed picniccss.com instead of picnicss.com and it didn't work. picnic-es-es does sound weird in one's head too.
2. On the website, you say '10kb' but should probably use '10kB'. The small-b is conventionally used to indicate bits and the capital-b is used to indicate bytes.
Agreed, but I want to point out that modern, large SPA-style app architecture and jQuery are not necessarily mutually exclusive. jQuery might not work with React and Vue, but it can work alongside other frameworks just fine (yep, without manual DOM manipulations).
jQuery offers utility beyond ensuring your code runs the same across different browsers. For instance, I've yet to see another `replaceWith` as concise as what jQuery offers or DocumentFragment generation as expressive as this: `$("<div>hello world <span>foo bar</span></div>");` For those two examples, the alternatives listed on http://youmightnotneedjquery.com/ are either very verbose or do not mimic the full functionality offered by jQuery.
Where I work we've built an in-house JS framework inspired by React and Vue but that "plays nicely" with our jQuery legacy codebase. The framework evens uses jQuery at it's core for UI state management and DocumentFragment generation. Re-factoring our jQuery legacy code is a breeze when compared with re-writing it to conform to React or Vue standards. We write modular and consistent components with `this.props`-style state management, one-way data binding, and zero manual DOM manipulations while still using jQuery.
TLDR: jQuery offers utility beyond manual DOM manipulations and smoothing out browser differences. When used appropriately, it still has a place in modern JS component architecture.
You wouldn’t use jQuery/Umbrella for that either, but there might then exist a middleground where you would. e.g. any time you’re adding JS to an existing page but don’t want a whole component abstraction.
Load speed:
Umbrella's 2.5kb loads well under the rule-of-thumb size of a single packet of 14kb [1], so you have to do a single roundtrip for that [2]. In this 3rd party test [3] it takes ~200ms to load the whole Umbrella JS. JQuery weights ~30x more kb, so there are several packets sent, which makes it load in ~2s in the same test.
Not to mention parsing and running all that extra JS.
Heck, I regularly bundle my whole JS in a single file and thanks to Umbrella JS tiny size the whole thing weights well under 14kb. It loads in a snap.
Edit: early send
[1] https://tylercipriani.com/blog/2016/09/25/the-14kb-in-the-tc...
[2] Excluding the TLS trip, which is common anyway.
[3] https://speakerdeck.com/mattclaffey/using-umbrella-js-for-mo...
Related: https://robots.thoughtbot.com/how-we-replaced-react-with-pho...
You have to be very, very careful with .html(...) and only for that reason sometimes React is worth it, specially with SPA. But for small-to-mid sizes projects, Umbrella JS FTW!
Sidenote: Anyway in my brain jquery/templates never made sense. I always wanted components, pass some arguments and get the component, self contained.
I suspect that if you refined it to "How many new websites in the last year use jQuery as their primary UI library?", the number would be much lower.
Something like this may work fine for that purpose. There are still thousands or millions of websites that don't act like an SPA/
It's as if those sites don't exist for the HN crowd, even though they make up the majority of the web.
Other than that, I don't see any reason to use jQuery (or something similar) in 2018.
IIRC, one of the main selling point of jQuery is compatibility with browsers like IE6.
Umbrella JS supports Internet Explorer 11+, with some of the known bugs and wont-fix here: https://github.com/franciscop/umbrella#support-ie11
There is a polyfill to help here: https://github.com/franciscop/umbrella/blob/master/polyfill....
PS: I admit that it's hard to support IE6 when it's no longer supperted by MS itself.
I wouldn’t want to build a new website for IE6. I guess you’ll have a very hard time setting up a test environment with IE6 installed.
Zepto: https://github.com/madrobby/zepto
Cash: https://github.com/kenwheeler/cash
How is yours better?
About Cash, first Umbrella is about 0.5x the size. The version 1.0 of both were released the same year (Umbrella a bit later), so back then it was not established. Then, there are these differences (opinionated ofc):
Better documentation in Umbrella. As an example, let's see the .addClass() in Cash [1] and in Umbrella [2]. Umbrella has many input examples, a lot more flexibility in the arguments (functions, comma-separated, etc), practical explained examples and related methods. This holds true for every single method.
Better testing in Umbrella. Compare Cash tests [3] with Umbrella tests [4]; Cash has 663 LOC of testing while Umbrella has 2234 LOC, a 3.3x improvement. Not only that, but Umbrella has a public testing page so you can test it in any browser you want: https://umbrellajs.com/tests
Cleaner code in Umbrella. If you want to modify any method, it will be self-contained in `src/plugins/NAME/`. In there you will find the relevant documentation displayed from the readme.md, the relevant tests and the actual code. To modify Cash's addClass you'd have to chase down the code [5], the tests [6] and the documentation [1] while in Umbrella you'd only need to go to the plugin folder [7]
More methods in Cash. This is the one I've seen that seems nicer, since it supports many more methods. Still, Umbrella's objectives is to be the minimum useful set. .insert() is enough and there's no need for .insertBefore() IMHO, but that's just different opinions. Some people prefer to have the choice, so this one is for Cash.
Extra useful methods in Umbrella. .handle() is the common pair of .on() + e.preventDefault(). .size() returns an object with the size of the element like { width: 20, height: 20, top: 10, ... }.
Better methods in Umbrella. Examples: .append(), .prepend(), etc will clone events and values (if any) from the DOM. More functions accepted as arguments.
[1] Cash docs: https://github.com/kenwheeler/cash#fnaddclass-
[2] Umbrella docs: https://umbrellajs.com/documentation#addclass
[3] Cash tests: https://github.com/kenwheeler/cash/blob/master/test/index.js
[4] Umbrella tests: https://github.com/franciscop/umbrella/blob/master/docs/test...
[5] Cash addClass code: https://github.com/kenwheeler/cash/tree/master/src/attribute...
[6] Cash addClass tests: https://github.com/kenwheeler/cash/blob/master/test/index.js...
[7] Umbrella addClass folder: https://github.com/franciscop/umbrella/tree/master/src/plugi...
When you do things like drag & drop and interactive resize you have to query DOM to get position and sizes, and perform hit detection, and you have to manipulate DOM directly. jQuery is very convenient for these kinds of things.
I'm starting to rewrite those to Vue and I've found the main limitation is not the DOM access (since you can use refs) but the model logic. In Vue all data is made of primitives or dumb objects, you can't really use classes for reactive data.
But like the parent said above, there's more to jQuery than just getting browser compatibility - it's an abstraction for UI concepts that would require writing a ton of low level JS otherwise.
The biggest issue is that "small JS interaction" end up in an app. In that case, I will re-write in React.
> It is strongly influenced by jquery...
Please use jQuery instead, thank you.
I agree it looks less professional, so I'll try fixing those. Is there any other bit that sounds off?
Edit: That should be fixed! Also a couple of missing `css` => `CSS`, `umbrella` => `Umbrella`, etc.
https://en.wikipedia.org/wiki/Obsessive%E2%80%93compulsive_d...
“Ugh, it’s ‘Lego’, not ‘legos’! ‘jQuery’, not ‘jquery’!”
IMO the world needs more people who don’t give a shit.
So not "scrollbar" but "scrolling" as the result of the click, on the documentation page:
https://umbrellajs.com/documentation
I'm one of those who have the problem with the animations in the UIs too. Believe or not some people get effectively a real sickness feeling just by looking at some specific movements, in my case that page manages to achieve that.
I'm pretty confused now, as it seems to be working.