State of Modern Component Styling

As new user interface component frameworks are created and old frameworks are replaced with emerging technologies, methods for styling those components must change with them. Long gone are the days of creating a simple HTML component and importing a simple CSS file with corresponding class names. Concerns such as style leaking, local DOM manipulation and theming are driving changes to the approaches we take. With developer IDEs and the JavaScript language itself and extensions such as TypeScript becoming more intelligent and allowing auto-completion, should our styling approaches not give us the same? In this world of intelligent auto-completion, why should we expect developers to remember and replicate matching class names across CSS and HTML? Modern frameworks are adapting to this shift and there are many different solutions to the problem of making styling and theming more intuitive. Here we will explore some of these solutions.

The olden days

In simple forms, a component is just a block of HTML with some corresponding JavaScript and CSS.

There are many problems with this approach in this form. The first main issue is that any other styles for a label class will likely affect our button label. The class names are brittle as they are manually entered in both the HTML and CSS and the colours are hardcoded, making any changes or theming more difficult to maintain. Sure, you could tidy this up by using something like a BEM technique, but this is really treating the symptoms of the issue rather than changing the underlying approach.

More recently, styling and theming an app was often achieved using a framework such as the highly popular Bootstrap and adding extra classes / structure to your components to bring them inline with the framework’s guidelines. Other popular approaches such as Material and Semantic have since been released and largely have implementations available for use within React, Polymer, and other frameworks.

Improvements could be made to the approach of base CSS using CSS preprocessors such as Less, Stylus, and Sass. These introduced a proprietary format of CSS including popular features such as mixins, variables, and vendor prefixing. The downside of using preprocessors is that developers must learn this format and the end result is still CSS, without solving many of the same problems including leaking styles across components.

What we are trying to solve?

So what problems are modern component styling approaches really trying to solve?

Style leakage

A common problem with using class names in global CSS is that you can have style clashes. If you have more than one node with the same class name, these nodes will receive the same CSS rules. You can strive to avoid this problem by nesting your CSS selectors and by following the principles of BEM, but this can often become very complicated.

Changes to designs

Changes to your designs when using vanilla CSS can be a challenge as you will likely end up using find and replace to change color, padding, margin and other properties across your CSS files. Preprocessors provide variables to make this easier, but then you are introducing a new proprietary CSS language into your codebase.

Theming

Vanilla or preprocessed CSS can be challenging to theme. I’ve often found myself going down the path of writing overly complicated CSS rule definitions with highly specific selectors to override the base styles of a component and this quickly gets messy. Especially when the toolkit you are theming changes their style definitions in the next release, and you have to use the browser debugging tools to inspect the CSS to identify any changes impacting your UI components

Class name mistakes

Keeping track of how class names are spelt, and which class names applying particular styles, can be frustrating and inefficient. One small spelling mistake somewhere in your codebase can stop styles from being applied and tracking down the cause can time consuming as this is not reported as an error.

The new way

There are many approaches used in modern component frameworks. These can largely be split into CSS, CSS-in-JS and inline-styles. We’ll stay clear of the latter option and concentrate on those that generate actual CSS stylesheets.

So lets dive in.

Polymer Styling

Polymer uses the cutting edge of available browser functionality largely using polyfills to cover the gaps in browser support. As such, the framework uses the same approach for its styling. Polymer styles are written using proposed future CSS syntax and make heavy use of css-custom-properties.

css-custom-properties allow you to create DOM level scoped variables that can be used to alter the CSS that is applied to a given element.

Polymer utilises this approach for both its component styling and its theming. A theme file will typically include a theme class wrapping a number of css-custom-properties that are then used to manipulate the visible styles at run time. The challenge with this approach is that css-custom-properties cannot be fully polyfilled at run time as any polyfill must account for the structure of the DOM when applying the correct CSS properties and variables to each node.

To get around this issue, Polymer has a comprehensive build time polyfill for css-custom-properties which has knowledge of the application’s DOM structure and applies calculated variables to the generated output. At this time, Polymer has deprecated the use of external stylesheets, so all styles must be written within the polymer element template.

Styled components

Styled components for React uses tagged template literals to create styles. They can be used to create predefined components to represent button, a and other HTML elements, or to add styles to any standard React component. A CSS stylesheet is created with class names which are passed to the component via the classname property.

These stye tags can return functions that receive component properties. For example, you could pass a property to your component to indicate if it was a primary button, this would be used by the styles.button to adapt the styles accordingly.

jsxstyle

jsxstyle aims to create display components that can wrap components. These display components can take the form of block, inlineBlock, flex and other typical values for the CSS display property. Each display component accepts properties that represent CSS attributes, which are in turn used to style their contents. The built code inserts stylesheets into the DOM with unique CSS class names. This avoids style leakage and enables rules to be reused between components that share styles.

Glamorous

PayPal’s Glamorous aims to build upon the ideas of both styled-components and jsxstyle. Glamorous provides a function that receives styles as a JavaScript object as well as providing a collection of components that can be used with property styles. The latter approach allows you to create styled components without having to give them a name.

The benefit of not having to provide a name is that this avoids having to add a placeholder HTML element just to provide a reference for a group of items. Inline styles allow you to apply styles directly to a node without naming it in that manner. Glamorous’s approach to allowing similar inline styles (that turn into CSS) to be applied directly to an anonymous node / element provides the same benefit.

Theming in Glamorous is provided via a <ThemeProvider>, much like styled-components, but it also allows a theme to be directly injected into a component. Themes can provide both variables and styles to components.

CSS in JS (JSS)

CSS in JS uses CSS directly within your JavaScript code. CSS Class names form the top level object keys and each nested object contains the CSS rules. This pattern is used within React and React Native and allows styles to be combined using an array.

React Themeable

React themeable is an attempt to normalise the styling and theming of React components. The idea is that all third-party components should adopt this approach to level out the inconsistencies of styling and theming. It is highly versatile and allows styles to be written using css-modules, react style, radium and plain CSS.

React Components such as react-autosugest make use of react-themeable and ships with zero styles of its own.

React-themeable provides a component with a single function, themeable, which accepts a theme property and returns a function that may be uses to decorate the relevant nodes. The returned function deals with whether the theme provided is class or style based, and automatically sets up the appropriate attributes.

The theme for this component must now provide classes or styles for button and label. This approach is highly effective because it gives the component author control over which nodes can receive styles and which cannot, whilst leaving the component user free to use whatever styling approach and technology they see fit.

CSS Modules

CSS modules allows you to write locally scoped class names and animation names using plain CSS. The CSS files are then imported into your JavaScript modules and the classes provided are then used to decorate your DOM nodes. When importing a CSS module, it exports a JSON object with mappings from the specified class names to the localised class names it has generated.

This means that the developer does not need to nest CSS class names in order to achieve style encapsulation. When using CSS modules, you should refrain from id or tag selectors as these cannot be localised by the build process.

When you pair CSS modules with typed-css-modules within a TypeScript project, you are able to get the benefit of Intellisense/auto-completion of CSS class names.

PostCSS and PostCSS-cssnext

PostCSS enables you to write cutting edge CSS and down emit your code to a format that the browser can use, much the same way as Babel allows you to write ES6+ code for a wide range of browsers.

PostCSS has a vast library of plugins available to enable various features. PostCSS-cssnext is one of the most useful plugins available, as it allows you to use modern CSS specification features without having to worry about browser support. These include color functions, rule nesting, css-custom-properties and more. PostCSS-cssnext also comes bundled with autoprefixer which alleviates the need to add vendor prefixes to your CSS code and maximises browser support.

Dojo 2

Dojo 2 aims to provide a typesafe styling and theming approach using the most recent technology available. With a robust widget authoring framework, its very important that the theming system is baked into the widget creation system. Widget theme files are authored as css-modules and compiled using PostCSS and PostCSS-cssnext. This allows widget authors to write CSS files without having to worry about cross-browser compatibility, style leakage and without using a preprocessor language. Each widget has its own style file with a .m.css file extension to mark it as a css-module to the build system. Common module files for icons and app level styles can be imported into your widget file and the classes provided are used to decorate widget nodes. css-custom-properties are used to provide CSS variables to the widgets, and these values are computed at build time so that browser compatibility is maximised. A variables.css file is included with the @dojo/widgets package and can be imported into client CSS files to allow third-party developers to use and extend the Dojo 2 look and feel in their own widgets.

Dojo 2 provides a class level function, classes, which is used to keep track of CSS classes being added and removed from the rendered Virtual DOM and to allow for themes to be used.

In order to theme a Dojo 2 widget you must pass it a theme object. The theme object can provide the theme for multiple widgets keyed by their widget name. Dojo 2 widgets from @dojo/widgets are keyed with a dojo- prefix to avoid naming clashes. Themes can target the class names passed to this.classes. For example, to change the span element’s appearance from the above example, a theme should provide an alternative label class.

Summary

JavaScript has come a long way and it is great to see that styling, theming and uses of CSS are also progressing quickly. Frameworks like like Dojo 2 and Polymer are making the most of the advances in CSS specifications for css-custom-properties and localisation of class names, which is great for developers who like to write CSS. CSS in JavaScript approaches such as Glamourous and react-styles are proving very popular in the React world due to the auto-completion/Intellisense that they provide within the IDE, not to mention the options they provide for frameworks like react-native. css-modules and TypeScript are a great combination for developers who like their styles to be in CSS files (where they should be!), but still want the Intellisense that CSS in JavaScript approaches provide. There are many options available, to help your providing better styling for your applications, and most importantly, stay away from the !important declaration!

Need a few style tips?

Let’s talk about how we can get your web app dressed in the latest styles of the season.

Comments

What wonders me why jsx has become possible? I have refused to consume dom manipulation libs a-la react.

W.S. Hager

XML is what made it possible, but I don’t think the React creators want to acknowledge that ancestry. Look at XQuery, where XML is mixed with functional programming. Nothing wrong with that by the way, the only problem is that React is not a complete tool and jsx is not a standard (and never will be methinks).