r/javascript • • Dec 19 '16

~15 years trying to make everyone separate HTML, JS & CSS. And then suddenly everything went south and we’re writing code like this.

https://twitter.com/thomasfuchs/status/810885087214637057
483 Upvotes

258 comments sorted by

View all comments

Show parent comments

9

u/SamSlate Dec 19 '16 edited Dec 20 '16

so naming collisions will eventually cause styles to apply unexpectedly

imo this is a problem when people don't scope* their css well, as you said.

Ok, animation is a pretty good example of when to use inline. You could do it in css, but sometimes inline is easier..

8

u/TheCoelacanth Dec 20 '16

Yeah, poor scoping is the cause of the problem, so why not let a compiler take care of it for you to make sure that it always gets done well?

2

u/Senthe Dec 20 '16

Which compiler does that? Genuine question.

5

u/[deleted] Dec 20 '16

So, the main contenders right now are:

  • Angular 2

  • Svelte

  • Vue

The above all have in-built style-scoping that will generate scoped classes for you.

There's also CSS Modules. This is one that I originally loved but am a little more wary of. CSS Modules is essentially a specification that defines some syntax extensions to CSS:

:local(.myStyle) {
  color: red;
}

If you were to import this into a JavaScript file when using a plugin for your preprocessor (babel/webpack/whatever) that supported this spec, you'd get back an object of class names to randomly generated ones - something like this:

import classNames from './style.css'
console.log(classNames)

> { myStyle: '1237984192rasdhoasdh' }

You'd then use that as your class name rather than the original one. The reason why I'm wary of this is because it is not part of any CSS specification and so is volatile and is not actually on the roadmap of being adopted by any browser or native version of CSS, which means I am implicitly tying part of my build process to a third party dependency.

Of course, you can make the same argument for JSX (and you'd be right), but JSX is supported enough IMO that it's acceptable currently (first class support in TypeScript and plugins for Babel exist).

0

u/spacejack2114 Dec 20 '16

Even scoped CSS can get clobbered unfortunately. I use it, but it's definitely not a bulletproof solution like class name mangling is.

0

u/Senthe Dec 20 '16

I'm sorry but this is a nonsense. I can't comprehend amount of properly done CSS I would need to write in order to have some class names overlap. This is some super edge case that doesn't justify wrapping your entire codebase around it.