Photo by Lee Campbell on Unsplash

Styled-Components and Styled-System

  Published:

Styled-Components and Styled-System are two popular npm packages that are commonly used in building user interfaces with React. They are both designed to make it easier to create and manage styles in a React application, and they can be used together to create a powerful and flexible styling system.

Styled-Components is a library that allows you to write actual CSS code within your JavaScript files, using a special syntax. It allows you to write component-based CSS, where styles are scoped to a specific component. With Styled-Components, you can create a component and style it in the same file, making it easy to see the component's structure and styles together.

Styled-Components

Here's an example of how you can use Styled-Components to create a button component:

import styled from 'styled-components' 

const Button = styled.button`
background-color: blue;
color: white;
padding: 10px 20px;
border-radius: 5px;
font-size: 16px;
cursor: pointer;

&:hover {
background-color: darkblue;
}


function MyComponent() { return (<Button>Click me!</Button> ) }

Styled-System

Styled-System is a library that allows you to design and build consistent user interfaces using a set of design constraints and a set of utility functions. It provides a set of utility functions for creating styles, such as margin, padding, color, fontSize, and so on. With Styled-System, you can easily create consistent spacing, typography, and color schemes across your entire application.

Here's an example of how you can use Styled-System to create a responsive button component:

import styled from 'styled-components' 
import { space, fontSize } from 'styled-system'

const Button = styled.button`
background-color: blue;
color: white;
border-radius: 5px;
cursor: pointer; 
${space} 
${fontSize} 


Button.defaultProps = { padding: [2, 4], fontSize: [1, 2], } 

function MyComponent() { return ( <Button>Click me!</Button> ) }

Potential Next.js Bug for Styled-Components


If you end up running into the issue where your Styled-Components are not loading correctly in your Next.js app, then that is because the Next.js server-side rendering cannot fetch the styles before rendering the page. The documentation forStyled-Components mentions this issue but it is very easy to miss. You will have to inject server-side rendered styles to the head manually so that it can render your pages and their styles correctly. The Next.js GitHub Repo has the exact file you need to accomplish this. It is located in the `deprecated-main` branch here, but I'll provide it below for easy copy-paste access.

// Replace the code in pages/_document.js with the code provided below
// From the vercel/nextjs GitHub Repo, deprecated-main branch.
// https://github.com/vercel/next.js/blob/deprecated-main/examples/with-styled-components/pages/_document.js

import Document from 'next/document'
import { ServerStyleSheet } from 'styled-components'

export default class MyDocument extends Document {
static async getInitialProps(ctx) {
const sheet = new ServerStyleSheet()
const originalRenderPage = ctx.renderPage

try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) =>
sheet.collectStyles(<App {...props} />),
})

const initialProps = await Document.getInitialProps(ctx)
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
),
}
} finally {
sheet.seal()
}
}
}

By combining the power of Styled-Components and Styled-System, you can create a powerful and flexible styling system for your React application. With these two libraries, you can easily create consistent and reusable styles, and you can also take advantage of the powerful features of CSS to add more advanced styling to your React projects:

  1. Consistent design: By using Styled-System to create a set of design constraints and values, you can create a consistent design system across your entire application. You can then use Styled-Components to create specific styles for each component, which can help make your design more cohesive and consistent.
  2. Reusability and flexibility: Styled-Components allows you to create reusable components with specific styles. By using Styled-System to create utility functions, you can also create flexible styles that can be easily adapted to different screen sizes and devices.
  3. Dynamic styling: Styled-Components allows you to create dynamic styles based on props or user interactions. By using Styled-System to create utility functions, you can also create dynamic styles based on the values of your design system.
  4. Server-side rendering: Styled-Components supports server-side rendering, which can be useful if your application needs to support this feature.
  5. Customizability: With Styled-System, you have full control over your design and you can style your components as you wish. Combining with Styled-Components gives you the ability to write component-based CSS.
  6. Easy to use: Both libraries provide an easy-to-use API that allows you to add styles to your components by passing props, which can be more intuitive and less verbose than writing CSS styles manually.

Overall, using both Styled-Components and Styled-System in the same project can provide a powerful and flexible styling system that can help make your design more consistent, reusable, and responsive.