Skip to content

Instantly share code, notes, and snippets.

@yangshun
Last active November 20, 2018 21:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yangshun/d64a1b9537c5a8d87ff893f51f9a344e to your computer and use it in GitHub Desktop.
Save yangshun/d64a1b9537c5a8d87ff893f51f9a344e to your computer and use it in GitHub Desktop.

Problems faced by Grab UI

  1. Not possible to have project specific overrides of variables
  2. CSS modules not specific enough to override Grab UI (Semantic UI) classes as CSS modules only apply one class where Semantic UI applies two classes (.ui.) and has higher specificity.
  3. Import theme variables from Grab UI theme configs to use in project-specific components

Requirements

  • No build requirements
  • Small and lightweight
  • Supports global CSS
  • Supports entirety of CSS
  • Colocated
  • Isolated
  • Easy to override
  • Theming
  • SSR
  • No wrapper components

Case for CSS in JS:

React inline styles solve the following problem:

  • Global namespace
  • Dependencies
  • Dead code elimination
  • Minification
  • Sharing constants

2. React Inline Styles are Fundamentally Flawed: https://byjoeybaker.com/react-inline-styles

Problems with (React) inline styles:

  • No property fallbacks such as when vendor prefixes are needed. Can't declare two display styles in a JS object.
  • No media queries or element queries.
    • If you render on server, you can't find out window/element size and initial styling sent to clients is likely to be wrong.
  • No cascading styles
    • Debuggin is a pain. Impossible to see changes made to one component affect all other similar components.
    • Inline styles have the highest specificity and you can't override styles on those elements.
  • Huge payload for server-side rendered markup with inline-styles for repeated components.
  • Performance
    • Browsers need to parse all the inline styles which is slower as compared to parsing a stylesheet.
    • Loading CSS in can be parallelized and doesn't necessitate downloading duplicate markup for (potentially many) similarly styled elements.
  • Scoped selectors:
    • Unique class names are generated and ensures no collision.
  • Rules isolation:
    • Prevents elements from inheriting properties from parent elements.
  • Fast selectors
    • Non-nested selectors are faster than deeply nested ones.
  • Rules Sharing
    • CSS rules can be shared for a list of items.
  • All CSS features included.
  • Faster performance than inline styles.
  • CSS is, as Mark Dalgleish points out, “a model clearly designed in the age of documents, now struggling to offer a sane working environment for today’s modern web applications.”
  • If your stylesheets are well organized and written with best practices, there is no bi-directional dependency between them and the HTML. So we do not need to solve the same problem with our CSS that we had to solve with our markup. — Keith J. Grant
  • The foundational issue with CSS is fear of change. "You must have an intimate knowledge of both in order to safely make any substantive changes to either."
  • That said, I like to minimize cognitive effort whenever possible. Things like naming strategies require a lot of discipline and “are hard to enforce and easy to break.”
  • Smart people on great teams cede to the fact that they are afraid of their own CSS. You can’t just delete things as it’s so hard to know if it’s absolutely safe to do that. So, they don’t, they only add. I’ve seen a graph charting the size of production CSS over five years show that size grow steadily, despite the company’s focus on performance. ~ Chris Coyier
  • You only include CSS classes that are actually used because your build system / module bundler can statically analyze your style dependencies.
  • You don’t worry about the global namespace. The class “default” in the example above is turned into something guaranteed to be globally unique. It treats CSS files as isolated namespaces.
  • You are more confident of exactly where given styles apply.
  • Any time we make a change to a CSS file, we need to carefully consider the global environment in which our styles will sit. No other front end technology requires so much discipline just to keep the code at a minimum level of maintainability.
  • We’ve worked around the issues of global scope with a series of naming conventions like OOCSS, SMACSS, BEM and SUIT, each providing a way for us to avoid naming collisions and emulate sane scoping rules.
  • Writing maintainable CSS is now encouraged, not by careful adherence to a naming convention, but by style encapsulation during development.
  • The relationship between CSS and JavaScript is not like the relationship between HTML and JavaScript. With HTML, a true separation of concerns between the markup and the corresponding component code is impossible. With CSS, this separation is possible and is in fact vital to clean code organization.
  • If your stylesheets are well organized and written with best practices, there is no bi-directional dependency between them and the HTML. So we do not need to solve the same problem with our CSS that we had to solve with our markup.
  • We had better have a damn good reason before we go down that road. Christopher Chedeau, in the original slidedeck that introduced this idea, outlines seven “problems” with CSS. The thing is, they are solvable problems. If we as web developers just understand CSS better, and take the time to learn modern best-practices like SMACSS and BEM, these issues almost entirely dissolve, especially at the scale most of us work at.
  • CSS hasn’t been designed to fit into components based architecture.
  • CSS in JS represents a fundamental change in how web apps get written — it throws away CSS in favor of a complex Javascript-based build chain that you’d be forced to use to implement CSS in JS.

8. CSS in JS is like replacing a broken screwdriver with your favorite hammer. :https://zendev.com/2017/09/11/css-in-js.html

The reason JSX provides an improvement in productivity is that structure and behavior are tightly linked - a large amount of JavaScript logic has to do with manipulating HTML structure, so separating the two creates more cognitive overhead, not less. Contrast this to graphical properties, which are a very large percentage of what is being manipulated by CSS. Rather than being tightly linked to structure, these are very loosely coupled - this is why one can often wireframe an entire application out before styling it! Moving your CSS into your logic introduces unnecessary coupling, leading to more cognitive overhead not less.

Contrast this to your visual output - if each and every component is completely isolated visually from one another, your site or application will be a disastrous mess! Certainly every component has unique properties, and those properties should be isolated so as not to leak, but there is also a tremendous need for visual consistency across your site. This is one of the core reasons for the CSS cascade, and while a misunderstood and uncontrolled cascade can be disastrous, used properly it is an incredibly powerful tool.

This means that if every component is itself responsible for its own styling, you will end up with tremendous amounts of duplication!

Comparisons

Solutions

@devinrhode2
Copy link

Publish 1 month ago: "A Unified Styling Language" - deep dive into CSS in JS from the creator of CSS Modules
https://medium.com/seek-blog/a-unified-styling-language-d0c208de2660
Mark Dalgleish's top stories on medium are:

  1. A Unified Styling Language
  2. The End of Global CSS (article 5 in this list)
  3. Block, Element, Modifying Your JavaScript Components

@devinrhode2
Copy link

devinrhode2 commented Jun 21, 2017

Also worth cross linking https://www.reddit.com/r/javascript/comments/6cqxdb/a_unified_styling_language_a_deep_dive_into_css/
(comments are pretty good, link to some good articles)

@devinrhode2
Copy link

Your requirements are ridiculous (not necessarily in a bad way)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment