Frontend frameworks
Within this post, I will show you one, of many ways to use components without a frontend framework. Do not get me wr...
For further actions, you may consider blocking this person and/or reporting abuse
I read 3 times and unless I’m blind, I don’t see where you applied BEM in javascript.
Also the $ sign here has nothing to do with jQuery I guess.. just a naming convention to point out that it holds an element.
Thanks for your comment. It made me smile. You are not blind. You are right. I did not go into details with applying BEM to JavaScript, although I said so: »I am going to show you how to use it for JavaScript, too«.
The JavaScript part in my post does show how to create an isolated environment for each instance of a block. To go further, maybe a small teaser for a gallery block can help.
Thanks for this example (extended) and kudos to this loop counting backward 😆
for (var i = $cards.length - 1; i >= 0; i--){...}
Another practice that may be worth considering is using JavaScript Hooks (though it isn't universally accepted; see also Decoupling Your HTML, CSS, and JavaScript).
so
Now superficially this may come across as redundant but as stated by Harry Roberts:
It could also be seen as helpful that the
js-*
classes within the HTML clearly identify those blocks (or elements) that have behavioural enhancements (i.e. are "JavaScript components").It isn't always necessary to use class-based JavaScript hooks on elements inside a block. Given that a block/component instance is already scoped on a particular fragment of the DOM it is possible to more easily select on non-class features or the fragment's internal structure to bind element behaviour.
See also:
Thanks for the insights. I once tried the
js-
prefix, but did not like it that much. I like to use same classes for JavaScript and CSS.And also thanks for showing me how to use syntax highlighting at DEV. I will update my post as soon as possible.
Regarding the $ sign. Yes, I use it to point out which variables hold DOM elements.
It's my personal opinion but I think these days, unless you're web app doesn't require any JS, manipulating DOM manually is a very risky approach. It's very easy to get into a spaghetti call stack if you e.g. need to fetch and interpolate some data into DOM (especially when constructing lists). Having said that, I see why shifting towards roots using pure JS + HTML + CSS might look attractive. And I see how CV-driven development became so popular, and that over-engineering and using fancy tech-stack takes precedence over the product/feature being implemented. Luckily there are still minimalistic rendring libraries, micro frameworks, starter boilerplates or even entire self-contained tools which purpose is to make you productive.
Thanks for your opinion on this. I think it is safe to do simple DOM manipulating without a framework.
Just be careful: going frameworkless is a one-way trip. =)
Here is my small attempt: github.com/Honatas/frameworkless-j...
"Frameworkless" doesn't necessarily imply going "full vanilla". It's possible to use libraries like µhtml (and related) to take care of some of the tedious details while having your own code supply the plumbing of the client side page/application.
Rasmus Lerdorf - original creator of PHP
Nonetheless having knowledge of the more common Web APIs on a detailed level (first) is invaluable.
Thanks for commenting.
I like your approach, but isn't it a bit of a framework? ;)
A very small one, nonetheless. Anything you do that tries to avoid code duplicity will end up looking like a framework anyway. The point of frameworkless is not to avoid frameworks at all, but to make things simple and keep the code under your control, in order to be able to eliminate as much technical debt as possible. Which I believe is exactly what you want to do with your approach: keep things simple, stick to the basics.
But yeah, I guess I could strip some stuff from there and go even simpler, but then I wouldn't have a Router anymore. Sad. ;)
I am not sure, where is that BEM in JS, too, but in CSS, BEM is overpriced, like you fix a problem that doesn't exists just to think something is much more organized and at the end, the class names are longer that the content inside the element.
Thanks for sharing your opinion.
I have made other experiences. With BEM the frontend (HTML, CSS and JavaScript) gets more organized and it works really well for some of our teams.
Not everything works well for everyone. Some of our teams use TailwindCSS, which is a completely different approach.
What works for your team?
Thanks for this nice article! Vanilla HTML, CSS and JS are truly underrated.
Btw I like seeing fellow devs from Germany here on DEV
<= located near Chemnitz 😀
Thank you :)
<= located in Flensburg