Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styled-Components vs SASS (SCSS) or LESS [closed]

I came across a ReactJS Boilerplate which had good reps and is community driven. Styling section emphasized more on styled component CSS but never stopped from switching to conventional CSS styling methodologies. Although this pulled my interests what makes the Styled-Component CSS stand out and why need to adopt it.

My Understanding of Styled component CSS:

  1. Component Driven idealogy. Your CSS also is now a component. - This is pretty cool!
  2. Load what you need and when you needed, kinda lazy CSS
  3. Theme provider, skins, modularity and dynamic - This can be achieved by other libs too
  4. Server side construction of your component DOM and its style.

My questions are:

  1. Browsers are evolved to parse CSS separately from Javascript parsing, why are we trying to deviate from this and fit all in Javascript?

  2. Styled-component CSS ships its javascript library to the client end, which actually parses styles at the runtime and put inside <style /> tag when each component loads on demand. This means extra load and logic eventually contributing to execution cycles on browser. Why need this?

    (By the above question i mean for every component loaded, corresponding CSS is computed, created and inserted in head via style tag / Multiple style tags - Re-inventing CSS interpreters)

  3. Does continuous computed style text banging via <style /> in the head tag cause browser reflow/repaint ?

  4. What are the performance advantages i get from this?

  5. With add-on libraries / options like Post-CSS & SCSS classname hashing for dynamic classnames which pretty much solves the problem that everyone states. Why SC still ?

Community, please clear the air for me or correct me if i am wrong.


Some good articles that talks about repaint or DOM re-flow how it is performance expensive for browser when CSS styles are modified.

  • https://developers.google.com/speed/articles/reflow
  • http://www.stubbornella.org/content/2009/03/27/reflows-repaints-css-performance-making-your-javascript-slow/
  • https://www.sitepoint.com/10-ways-minimize-reflows-improve-performance/
  • https://www.phpied.com/rendering-repaint-reflowrelayout-restyle/
  • https://developer.mozilla.org/en-US/docs/Web/API/CSS_Object_Model/Using_dynamic_styling_information
like image 927
NiRUS Avatar asked Oct 14 '17 16:10

NiRUS


People also ask

Which is better Sass or styled-components?

According to the StackShare community, Sass has a broader approval, being mentioned in 2101 company stacks & 1486 developers stacks; compared to styled-components, which is listed in 63 company stacks and 48 developer stacks.

Does styled-components use SCSS?

In SCSS, you can use variables and formulas for expressing colors, font sizes, widths and spacing variants. However, when you switch from SCSS to styled components, the syntax isn't valid anymore. Styled component files are CSS-in-JS, so they are actually JavaScript files.

Are styled-components good 2022?

It may be a great solution if you want to build your own UI components library. The cool feature of styled-components is that you can set up the styling condition through the props and also it is very much customizable.

Why Sass is better than less?

SASS is a CSS preprocessor that assists with reducing redundancies in CSS and in the end saves time. They are extensions of CSS, which helps in saving time. It gives a few features which can be utilized to make stylesheets and assist with keeping up with the code.


2 Answers

I've been using SCSS (SASS) for many years now, and also have been using Styled-Components for a few projects, some large.

I love both, and Styled-Components feels, for me, like a step forward:

Styled-Components - Pros

  1. Total styling isolation; prevents potential bugs when working in teams, when one team member can never override a style of another. (unless multiple places share the same styled component)
  2. Remove the need to manually handle class names as they get auto-generated (name-picking components is somewhat easier IMHO than class names)
  3. I've found it easier to work with CSS within the JSX file itself (Opposing my judgment for many years before)

  4. Easily use javascript variables within styles (eliminate the need for 2-sets of theme variable)

Styled-Components - Cons

  1. Each style component is yet another wrapper function, and many styled-components means more functions, which means less efficient since all that code needs to be "compiled" and so on.
  2. Biggest drawback: changes to styles require bundle re-compilation and the app's state might reset.

The cons can be only viewed as such on some scenarios but not necessarily all.


SCSS/LESS pros can be viewed as opposite to the cons listed above, while having some more cons such as mixings and faster development when working with variables (IMHO). it can get "ugly" defining a local selector variable:

Compare these simplified examples:

SCSS example:

.icon{   $size: '20px';   width: $size;   height: $size;   margin-left: $size/2; } 

Styled-Components local scope example:

const Icon = styled.i(props => {   const size = 20; // easier to use Number instead of String for calculations    return `     width: ${size}px;     height: ${size}px;     margin-left: ${size/2}px; `}); 

Obviously the variable could have been defined outside of the Icon styled wrapper and then internally used, but that won't make it isolated, because styled-component CSS might be composed of sub-components styled and look more like CSS:

const Header = styled.header`    > ul{      ...    }     li{      ...    }     img{...}     navigation{...} ` 

Not always it is desirable to extract each individual HTML element to its own styled component. it is done mostly for components that are or might be repeated across the app.

Regarding SASS mixings, they can be converted to javascript functions, so there's not much advantage to SASS here.

Overall, working with Styled-Components is fun & easy, but has a side-effect of a tighter coupling between the styles and the framework/component and it obviously has some performance penalty (nothing that will actually slow you down, but still).

like image 116
vsync Avatar answered Oct 13 '22 06:10

vsync


Browsers are evolved to parse CSS separately from Javascript parsing, why are we trying to deviate from this and fit all in Javascript?

When you mix both Javascript and HTML (namely JSX) and then also CSS (JSS or another), you make your component a solid module that fits into a single file. You don't need to keep styles in a separate file anymore.

Then, the functional magic happens: as JSX is a pure function of raw data that returns "HTML" (not really), the same way CSS-in-JS is a pure function or raw data that returns "CSS" (also not really). From this point, I think it's worth reading about JSX and also about CSS-in-JS.

Styled-component CSS ships its javascript library to the client end, which actually parses styles at the runtime and put inside <style /> tag when each component loads on demand. This means extra load and logic eventually contributing to execution cycles on browser.

Not only on the run-time. Because CSS-in-JSS is just a function of data that returns CSS, you can use it on any platform. Take Node, add SSR, and you have style elements delivered to the browser in response body, therefore parsed just like it would happen in the original case of getting CSS delivered.

Why need this?

Because it is convenient in development, just like React or Redux, just like jQuery, and just like any other lib, it comes at network load and browser performance cost.

You take a library because it solves a problem. If there seems to be no problem, why use library at all, right?

Does continuous computed style text banging via <style /> in the head tag cause browser reflow/repaint ?

There are so many things that force reflow.

Browsers are smart. They don't even make an attempt to repaint if the styles haven't changed. Which doesn't mean that they don't calculate the difference, which costs CPU time.

There's a good intro into the scope and complexity of style (re)calculations, it's really worth reading to understand the subject deeper.

like image 42
rishat Avatar answered Oct 13 '22 05:10

rishat