Thoughts on scalable UI in the world of JSX, SASS, React, Styled Components and the likes.
2 min read
Those of you who read my blogs regularly, already know that I have been coding for the better part of the past decade.
A fair bit of that time has gone into building beautiful and usable UI components for projects built for scale. (e.g. Fab, Hem, Tavisca)
So it was not a surprise for me when I saw that RoR community came up with SASS a preprocessor for CSS which allowed you to dynamically create your style-sheets. Eventually, developers could use functions (mixins, extend, import), variables, nesting and create UI components in a much elegant fashion.
All was well, the UI developers were still writing their styles in a separate file which would compile down to CSS and be included into the HTML as required.
But eventually, with Angular and React, users started writing their styles in the HTML directly. It felt natural to new developers who liked keeping their HTML/JS for a component in a single JSX file. They had no qualms about using CSS in the style attributes of their DOM nodes.
Even though in my opinion (please note the term ‘Opinion’) inline-styles pollute the DOM and get in the way of development.
Also, keeping styles for each component in its own JSX makes it difficult to use the power of SASS and visualize the entire hierarchy that we get by looking at a SASS file (if done properly.)
Enter, styled-components which lets you write CSS as JSON objects and auto assigns a unique class name to every component. It also auto links the class to every instance of the component.
What I dislike about this approach is the fact that we are adding inline-style nodes and the browser has to generate the same class during every execution of the app/page. This does not play well with the fact that browsers can easily create the class, pre-process its properties and cache it for future use across reloads.
What are your takes?
Did you find this article valuable?
Support Sunny Gupta by becoming a sponsor. Any amount is appreciated!