Styling in React with styled-components

styled-components is one of the most commonly used CSS-in-JS libraries for React.

9 min read ⋅ 4233 views

Introduction

The styling of your website or web application is an important part of creating a good user experience. You cannot just ship HTML and JavaScript and expect the app to look decent. When it comes to styling a React app, there are plenty of options to choose from.

  1. the CSS way:
    a. good old fashioned CSS (inline, <style> tag or linked stylesheets)
    b. using modern build tools for the adventurous - CSS Modules
  2. the JavaScript way:
    a. inline styling using the Object-based syntax provided out of the box with React
    b. choose from a plethora (yes, that many) of CSS-in-JS libraries.

The CSS-in-JS scenario has been a controversial one at times but no serious developer can deny that they are worth exploring, that's why we will explore the basic APIs and usage of styled-components (one of the most widely used of the available options) by creating a trivial app that displays a list of "Random People" and some info about them. This is just enough to get you started with styled-components by highlighting some of the common real-world use cases.

We will not be going through every bit of the app in detail but you can view the live version of it on Codesandbox and fork the repo on Github. We will be focusing solely on the implementation related to styled-components throughout the app.

Note - The reader is expected to have prior experience of working with React. You can go through Introduction to React for a quick refresher.

Using styled-components

We will be using Codesandbox for bootstrapping the React app which uses the create-react-app under the hood. Once the setup is done we can get started by installing the npm package for styled-components.

$ npm i styled-components

No extra build steps or configurations are needed for styled-components to work. This makes getting started with the library very easy.

Styling in styled-components follows a syntax identical to CSS. It varies slightly for things like keyframes and media queries. We will explore them in details separately. The CSS like syntax uses ES6 Tagged Template Literals. The style rules we write as such are used to generate a CSS stylesheet with unique class names for each set of rules, and attaches those classes to their respective DOM nodes of the components. The generated stylesheet is injected at the end of the <head> section of the HTML document during runtime by default.

styled

styled is the default export of the styled-components. All the DOM elements are present as methods on the styled object. In the snippet above, we are creating the Main component which acts as the parent container for all the components other than our Header. Here, we use styled.div to create a styled component that renders a <div>. The CSS syntax inside the backticks utilizes common flexbox-based layout code.  This is the simplest way to create a styled component. If you go through the source code in the repo, you will come across multiple usages of this API like styled.img, styled.button, etc.

styled can also be invoked as a function (factory call) itself to wrap an existing React component as a styled component. In the above example, we create the Loader as a simple stateless React component and then call the styled factory function with Loader as an argument to wrap it with the necessary styles. Actually, styled.div is also just an alias for styled('div') so it can be used with HTML tags directly as well.

One important thing to note when using the styled as a factory function to wrap a React component is to manually set the className attribute on the DOM elements to this.props.className or else the style rules will not take effect. 

Keyframes, pseudo-elements and pseudo-selectors

We are creating a simple animated Loader component using keyframes and pseudo-element. styled-components export a keyframes helper function as a named export that is used to create animation keyframes. It also generates keyframes rules with unique names, just like the class names so that we don't have any global name clashes on the generated stylesheet. The API is similar to that of styled.tagname and the generated keyframes object is then used in the animation property for the style rules, as you can see on line 29 in the gist. 

The getColor utility function is just used to extract color values from the theme. We will explore the details of that shortly when we talk about theming in styled-components.

For the pseudo-elements, the syntax is more like what is used in SCSS. & refers to the current selector scope and can also be used for nested rules just like in SCSS. The rules under ::after defines the style for the after pseudo-element of the Loader <div>. Remember, just like in CSS, content property needs to set (even if to an empty string '') for the pseudo-element to be valid.

Similarly, pseudo-selectors like :hover, :active, etc. can also be used in styled-components. In the User component style rules, you can see the transition rules are similar to their CSS equivalents.

Using props and the css helper

Interpolated functions can be used inside in the tagged template literals. This function is passed all the props available to the component. styled-components pass on all their props and hence the props can be used to customize or alter style rules dynamically. In this Button component, we use the css helper function to create an absolutely positioned button but only if it has a floating prop. The css helper function allows the generation of valid CSS using template literals within interpolations, meaning it is used to generate CSS using `` when inside ${} wrapped inside another  `` (so meta).

In the Avatar styled-component, props.gender is used to customize the border of the <img> tag. For female users there will be border on the left and bottom side but not on the top and right side and vice versa for the male users. The props.theme.color.primary is the primary color of our theme, the code gist in theming section sheds more light on it.

extending styles

Sometimes, changing styles based on props is not enough and we may need to generate multiple variants of a single component. We can easily do that by calling the extend method on that component.

Here, we need two <span> elements with similar styles, so we extend Value (an existing styled.span) to create Label which has a bolder font and different font color.

Media queries

Modern sites need to be responsive and for that we need media queries. We will use media queries to adapt our UserProfile and its constituent components for different screen sizes.

When the screen width is lower than 600px, we change the flex-direction and other alignments rules of the UserProfile component and lower the width of the Details component from 350px to 280px.

Global styles

Sometimes, we need to add global styles for use cases like negating the default link anchor <a> styles or styling the <body> element. The styled-components library provides a helper function as named export for that too - injectGlobal can be used to add global styles using the now familiar tagged template literal syntax.

Theming

The styled-components have theming as a first-class feature because it is very common for web apps to have a uniform theme (like colors, typography, etc.). It exports a ThemeProvider wrapper component that accepts a theme props. It accepts an object holding values for theming purposes, which in our case here are colors and font sizes. It injects this theme props to all the styled components using the context API under the hood. If you need the theme props outside of the context of any styled components, there is also a higher-order component withTheme (just like withRouter in react-router-dom) available for that.

In our theme.js file, we create a theme object with various color and fontSize values. We also create a function invertTheme. The ThemeProvider can also be passed a function for something like contextual theming. The function receives the theme object as a parameter from a parent ThemeProvider i.e. any other ThemeProvider that is higher up the tree. We also have two curried functions to help us extract the color and font size values from the theme props. These two are the functions that have been popping up in the style rules of other components. These are simple functions that accept the props argument and return the desired value extracted from it.

In the index.js file we wrap the App component with ThemeProvider and pass it the theme object. This serves as our default theme. As ThemeProvider components can be nested and be used for contextual theming, we rerender a ThemeProvider wrapping the AppPresentional (the App component is divided into two components, container and presentational) if the this.state.themeInverted is true. This time we pass the invertTheme function to it which receives the default theme object as props from the ThemeProvider higher up in the tree and returns a new object with color values swapped around.

Conclusion

styled-components is one of the best solutions available for styling a React application. We have only discussed the most common of the possible use cases but there is much more to the library which you can explore in their documentation. styled-components have great server-rendering support as well. Now you know how to get going, give it a try.



CSS React