What is styled-components?
The styled-components npm package is a library for styling React applications. It utilizes tagged template literals to style your components. It allows you to write actual CSS code to style your components without worrying about class name bugs due to its unique feature of scoped styles. It also supports theming, dynamic styling, and can work with server-side rendering.
What are styled-components's main functionalities?
Basic Styling
This feature allows you to create React components with styles attached to them. The example shows how to create a styled button component.
import styled from 'styled-components';
const Button = styled.button`
background: palevioletred;
border-radius: 3px;
border: none;
color: white;
`;
Theming
Theming support allows you to define a set of constants for styling that can be accessed throughout your application. The example shows how to use a theme to style a button component.
import styled, { ThemeProvider } from 'styled-components';
const ThemeButton = styled.button`
background: ${props => props.theme.main};
color: ${props => props.theme.secondary};
`;
const theme = {
main: 'mediumseagreen',
secondary: 'white'
};
<ThemeProvider theme={theme}>
<ThemeButton>Click me</ThemeButton>
</ThemeProvider>;
Dynamic Styling
Dynamic styling allows you to pass props to your styled component to change its appearance dynamically. The example shows a div that changes its background and text color based on props.
import styled from 'styled-components';
const DynamicDiv = styled.div`
background: ${props => props.bgColor};
color: ${props => props.textColor};
`;
<DynamicDiv bgColor='papayawhip' textColor='palevioletred'>Dynamic Content</DynamicDiv>;
Server-Side Rendering
styled-components can be rendered on the server, allowing you to generate the required styles along with your HTML for faster initial load times. The example shows how to use ServerStyleSheet to collect styles during server-side rendering.
import { ServerStyleSheet, StyleSheetManager } from 'styled-components';
const sheet = new ServerStyleSheet();
<StyleSheetManager sheet={sheet.instance}>
<YourApp />
</StyleSheetManager>;
const styleTags = sheet.getStyleTags(); // gets all the tags from the sheet
Other packages similar to styled-components
emotion
Emotion is a performant and flexible CSS-in-JS library. It is similar to styled-components but offers a different API and additional features like composition patterns and the ability to work with plain objects instead of template literals.
jss
JSS is an authoring tool for CSS which allows you to use JavaScript to describe styles in a declarative, conflict-free and reusable way. It differs from styled-components in its approach to styling, using JavaScript objects instead of tagged template literals.
aphrodite
Aphrodite is another CSS-in-JS library that allows you to write styles in JavaScript and attach them to your components. It focuses on performance by generating as little CSS as possible and supports server-side rendering, but it does not use tagged template literals.
linaria
Linaria is a zero-runtime CSS-in-JS library that extracts CSS to real CSS files during the build process. Unlike styled-components, which injects styles at runtime, Linaria aims to provide better performance by avoiding the runtime style injection.
Styled components
This is a work in progress based off of this demo.
Usage
** This may change at any point but it's pretty 💃 right now so... **
npm install -D styled-components
import styled from 'styled-components'
const Header = styled.header`
padding: 4rem;
display: flex;
flex-direction: column;
align-items: center;
background: papayawhip;
> * + * {
margin: 1rem 0 0 0;
}
> h1 {
font-size: 2rem;
}
> h2 {
font-size: 1.5rem;
}
`
const Main = styled.section`
max-width: 800px;
margin: 0 auto;
`
const P = styled.p`
line-height: 1.4;
margin-bottom: 1.5rem;
`
export default () => (
<div>
<Header>
<h1>Styled Components</h1>
<h2>They're so rad!</h2>
</Header>
<Main>
<P>
Separate those concerns! JSX is for structure, styled.tagName for styling!
</P>
<P>
Just gotta use component names that start with an Uppercase letter and you're good to get started!
</P>
</Main>
</div>
)
By Glen Maddern and Max Stoiber.
With thanks to Charlie Somerville & lots of others.