Blog

Ideas and insights from our team

Our styled-components learning experience


Here at Vinta, we’ve been using React for quite some time. In our learning React adventure, some of us fell in love with styled-component and recently, we have decided to introduce it in one of our projects.

Before using styled-components in our projects, we studied it and compared its usage with other styling methods. After making some experiments, we have decided that it would be great to try it in an actual project.

This post includes a collection of style-components material that has helped us to understand the package better and gave us more confidence when using it. If you already are using styled-components, you may find here some useful information about the package.

What is styled-components?

First of all, let’s understand what styled-components is. It is a CSS in JS styling lib that makes it easy to add styles to your components without the need of an extra CSS file.

For example, let’s say we want to create a custom button that works as follow:

  1. It is 44x44 pixels
  2. It has round borders
  3. It has a green background
  4. Its content is white and has a font size of 23 pixels
  5. If disabled the cursor is not allowed and it has an opacity of 50%

Using pure CSS we would need to create a .css file and add classes with the properties that we want. Then, we would need to import the .css file from our React component file and use the className prop in our button tag. Something like this:

// style.css
.custom-button {
  width: 44px;
  height: 44px;
  background-color: green;
  border: 1px solid green;
  border-radius: 50%;
  font-size: 23px;
  color: #fff;
}

.custom-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
// CssButton.js
import React from 'react';
import PropTypes from 'prop-types';

import './style.scss';

const CssButton = ({ children, ...rest }) => (
  <button
    className="custom-button"
    {...rest}
  >
    {children}
  </button>
);

CssButton.propTypes = {
  children: PropTypes.oneOfType([
    PropTypes.arrayOf(PropTypes.node),
    PropTypes.node,
  ]),
};

export default CssButton;

With Styled Components, there is no need for extra files. Here is an example of how we could do this custom button using the lib:

import styled from 'styled-components';

const StyledButton = styled.button`
  width: 44px;
  height: 44px;
  background-color: green;
  border: 1px solid green;
  border-radius: 50%;
  font-size: 23px;
  color: #fff;

  &:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }
`;

export default StyledButton;

Now that we understand what is Styled Components let's get into more details about the lib.

Why styled-components?

First of all, we need to understand why styled-components. In this talk you can understand Max Stoiber’s motivations to create this package. He also gives a quick introduction on its usage and on how it works.

Another great thing about styled-components is that you don’t need a whole lot of setting up to start using it. As you can see here you are able to install and start using it really quickly.

Another interesting point is that, with its last update, styled-component got a lot faster, as you can check here.

What can we do with styled-components?

When using styled-components you have basically all the features you have when you use a stylesheet compiler like SASS or LESS, with one big difference, you don’t need to learn a single new thing. You’ll be able to do all the magic using just JavaScript and pure CSS.

You can check a more in depth guide with examples to styled-components here and here. But if you want the go even deeper into the details you can check the styled-components docs.

How does it work?

Style-components makes use of JavaScript Template Literals to style the components. It is a littler bit complicated to understand its foundations, but if you want, here is a post by Max explaining how the magic happens.

Some extra material

Just to conclude, here you can find some extra info that may help you with styled-components.

  • A quick comparative guide on the 4 ways to style your react components: here
  • A great React styling tutorial: here
  • Some tips for production development: Here and here.
  • A package to help you test your styled-componenets: here

That's it, this should be enough material to help you understanding and start working with styled-components. If you have any thoughts or some material you’d like to share with us, please leave a comment.

About Arimatea Neto

I'm a passionate software developer and a Python enthusiast who loves challenges and to explore new technologies.

Comments