DEV Community

Cover image for Part 2: Configuring Styled Components
Ary Barros
Ary Barros

Posted on

2 1

Part 2: Configuring Styled Components

So far, our template already has:

  • Compatibility between multiple editors
  • Automated Code Standardization
  • Automated code beautify

Now, we will configure extra features for our project. One of them is the so-called ** Styled Components **, which changed the way we write CSS / SCSS in React projects. Come on?

What is Styled Components?

This is a package that appeared around 2016 and came from the need for CSS to adapt to the componentization proposed by React in its creation. The idea of ​​Styled Components is to create React components with their individual CSS and that can be easily replicated on other pages and other projects.

In the example below, we see the construction of a component made in Styled Components with structuring of themes and application in a JSX.

const Button = styled.button`
  background: transparent;
  border-radius: 3px;
  border: 2px solid palevioletred;
  color: palevioletred;
  margin: 0.5em 1em;
  padding: 0.25em 1em;
  ${props => props.primary && css`
    background: palevioletred;
    color: white;
  `}
`;
const Container = styled.div`
  text-align: center;
`
render(
  <Container>
    <Button>Normal Button</Button>
    <Button primary>Primary Button</Button>
  </Container>
);
Enter fullscreen mode Exit fullscreen mode

Cool, huh? We will configure it in our template.

Installing Styled Components

To install the package, simply execute the command below:

yarn add styled-components
# Right after
yarn add @types/styled-components -D
Enter fullscreen mode Exit fullscreen mode

There, it's already installed. Now, we will use a very special configuration that Styled Components makes available to us, the configuration of global styles.

Configuring Global Style

The Global Style configuration helps us to configure a CSS common to all pages. This is a common practice among developers to override certain default browser settings, in addition to configuring default fonts and colors. Global Style is also a React component that will be imported into our App.tsx.

Let's create, inside the folder src a folder called styles and inside it the file global.ts.

To configure the global style, we just import this configuration of the styled components and export the functional component created:

import { createGlobalStyle } from 'styled-components';

export default createGlobalStyle`
`;
Enter fullscreen mode Exit fullscreen mode

Here, the configuration is up to you, but I show below the common configuration I use in my projects.

import {createGlobalStyle} from 'styled-components';

export default createGlobalStyle`
  * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    outline: 0;
  }
  body {
    background: # 312e38;
    color: #fff;
    -webkit-font-smoothing: antialiased;
  }
  body, input, button {
    font-family: 'Roboto Slab', serif;
    font-size: 16px;
  }
  h1, h2, h3, h4, h5, h6, strong {
    font-weight: 500;
  }
  button {
    cursor: pointer;
  }
`;
Enter fullscreen mode Exit fullscreen mode

Activating Global Style

In our App.tsx, just import our component and insert it in the return of the App.

import React from 'react';
import GlobalStyle from './styles/global';

function App () {
  return (
    <>
      <GlobalStyle />
    </>
  );
}

export default App;

Enter fullscreen mode Exit fullscreen mode

Ready! Another configuration made in our template. It is available on github.

In the next article, we will configure react-app-rewired which overrides the webpack settings.

I'll wait for you there!
Thanks for reading!

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay