Splitting the CSS into two distinct pieces (or better: files, but I realize that's a limitation of jsfiddle) would go a very long way to making this technique maintainable. Having documentation that then shows the required HTML structure and the necessary classes to add would help immensely.
Essentially, build it like a UI library that just happens to be written in CSS.
Right now, it's just a demo of a technique that's not built in a reusable way, and you need to be pretty decent with CSS in order to understand and use it (since you pretty much have to re-implement it every time you want to use it). To be fair, the HN submission and site don't make any claims it's anything but this.
Try this in Chrome: https://jsfiddle.net/timdavila/f5rcarmn/
"Yeah, yeah, but your scientists were so preoccupied with whether or not they could that they didn't stop to think if they should."
(when having lots of tabs open)
With a sane limit to number of characters on each line as well.
JS:
var panels = [...document.querySelectorAll(".accordion li")];
panels.forEach(x => x.addEventListener('click', e =>
panels.forEach(y => y.setAttribute('class',
y == e.target && !y.classList.contains('open') ? 'open' : ''))
)); y.classList.toggle('open', y == e.target && !y.classList.contains('open'))
As the behavior of what to do with the "open" class is only dependent on whether `y` is the clicked element, i find this version more readable: y == e.target ? y.classList.toggle('open') : y.classList.remove('open'))
Aaaand, it can be made less readable again by selecting the classList method to call conditionally :) y.classList[y == e.target ? 'toggle' : 'remove']('open')All an accordion does is add an event listener that removes a class from all elements and adds it back to one of them. Then there's a 1 line transition for that class in css.
function eachItem(arrayLike, cb) {
return [].slice.call(arrayLike).forEach(cb);
}
So 3 more lines. If I said you can write a very verbose accordion in under 20 lines would that be good enough for you?Since that's more realistic, yes that would do nicely.
Two others completed the accordion in 3 lines task without jQuery.
Yeah, it's incredibly crude but it is 3 lines and it took approximately 60 seconds.
And I know the trends around jQuery, that was not the issue at all.
Ignoring the fact that it clearly wasn't intended to be taken literally, "in 3 lines of JS" when speaking in the context of a team effort on a web application can be understood as "adding 3 additional lines to the source code." Context.
There's no need to be nasty and argumentative for no reason.
jQuery is JavaScript, so...
I've written a couple of small jQuery plugins and I don't claim I wrote this awesome Javascript code that does this cool thing. I say I made a jQuery plugin that does this cool thing.
But that's changing once web animations (https://w3c.github.io/web-animations/) hits mainstream anyway - both CSS & JS get access to the same engine.
If you use Wordpress, like millions of people do, you will have a fuck ton of JS plugins running, so I can see where this would be advantageous.
If you only write it once and put it somewhere, among other advantages, you can document it easily.