Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress ๐
npm install --save styled-components
Utilising tagged template literals (a recent addition to JavaScript) and the power of CSS, styled-components
allows you to write actual CSS code to style your components. It also removes the mapping between components and styles โ using components as a low-level styling construct could not be easier!
styled-components
is compatible with both React (for web) and ReactNative โย meaning it's the perfect choice even for truly universal apps! See the ReactNative section for more information
Note: If you're not using npm
as your package manager, aren't using a module bundler or aren't sure about either of those jump to Alternative Installation Methods.
Usage
Basic
This creates two react components, <Title>
and <Wrapper>
:
import React from 'react';
import styled from 'styled-components';
const Title = styled.h1`
font-size: 1.5em;
text-align: center;
color: palevioletred;
`;
const Wrapper = styled.section`
padding: 4em;
background: papayawhip;
`;
(The CSS rules are automatically vendor prefixed, so you don't have to think about it!)
You render them like so:
<Wrapper>
<Title>Hello World, this is my first styled component!</Title>
</Wrapper>
Passed props
Styled components pass on all their props. This is a styled <input>
:
import React from 'react';
import styled from 'styled-components';
const Input = styled.input`
font-size: 1.25em;
padding: 0.5em;
margin: 0.5em;
color: palevioletred;
background: papayawhip;
border: none;
border-radius: 3px;
&:hover {
box-shadow: inset 1px 1px 2px rgba(0,0,0,0.1);
}
`;
You can just pass a placeholder
prop into the styled-component
. It will pass it on to the DOM node like any other react component:
<Input placeholder="@mxstbr" type="text" />
Here is one input without any content showing the placeholder, and one with some content:
Adapting based on props
This is a button component that has a primary
state. By setting primary
to true
when rendering it we adjust the background and text color. (see tips and tricks for more examples of this pattern!)
import styled from 'styled-components';
const Button = styled.button`
/* Adapt the colors based on primary prop */
background: ${props => props.primary ? 'palevioletred' : 'white'};
color: ${props => props.primary ? 'white' : 'palevioletred'};
font-size: 1em;
margin: 1em;
padding: 0.25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
`;
export default Button;
<Button>Normal</Button>
<Button primary>Primary</Button>
Overriding component styles
Taking the Button
component from above and removing the primary rules, this is what we're left with โ just a normal button:
import styled from 'styled-components';
const Button = styled.button`
background: white;
color: palevioletred;
font-size: 1em;
margin: 1em;
padding: 0.25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
`;
export default Button;
Let's say someplace else you want to use your button component, but just in this one case you want the color and border color to be tomato
instead of palevioletred
. Now you could pass in an interpolated function and change them based on some props, but that's quite a lot of effort for overriding the styles once.
To do this in an easier way you can call styled
as a function and pass in the previous component. You style that like any other styled-component. It overrides duplicate styles from the initial component and keeps the others around:
import React from 'react';
import styled from 'styled-components';
import Button from './Button';
const TomatoButton = styled(Button)`
color: tomato;
border-color: tomato;
`;
export default TomatoButton;
This is what our TomatoButton
looks like, even though we have only specified the color
and the border-color
. Instead of copy and pasting or factoring out the styles into a separate function we've now reused them.
Note: You can also pass tag names into the styled()
call, like so: styled('div')
. In fact, the styled.tagname helpers are just aliases of styled('tagname')
!
Third-party components
The above also works perfectly for styling third-party components, like a react-router
<Link />
!
import styled from 'styled-components';
import { Link } from 'react-router';
const StyledLink = styled(Link)`
color: palevioletred;
display: block;
margin: 0.5em 0;
font-family: Helvetica, Arial, sans-serif;
&:hover {
text-decoration: underline;
}
`;
<Link to="/">Standard, unstyled Link</Link>
<StyledLink to="/">This Link is styled!</StyledLink>
Animations
CSS animations with @keyframes
aren't scoped to a single component but you still don't want them to be global. This is why we export a keyframes
helper which will generate a unique name for your keyframes. You can then use that unique name throughout your app.
This way, you get all the benefits of using JavaScript, are avoiding name clashes and get your keyframes like always:
import styled, { keyframes } from 'styled-components';
const rotate360 = keyframes`
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
`;
const Rotate = styled.div`
display: inline-block;
animation: ${rotate360} 2s linear infinite;
`;
This will now rotate it's children over and over again, for example our logo:
<Rotate>< ๐
></Rotate>
React Native
styled-components
has a ReactNative mode that works exactly the same, except you import the things from styled-components/native
:
import styled from 'styled-components/native';
const StyledView = styled.View`
background-color: papayawhip;
`;
const StyledText = styled.Text`
color: palevioletred;
`;
class MyReactNativeComponent extends React.Component {
render() {
<StyledView>
<StyledText>Hello World!</StyledText>
</StyledView>
}
}
You cannot use the keyframes
and injectGlobal
helpers since ReactNative doesn't support keyframes or global styles. We will also log a warning if you use media queries or nesting in your CSS.
Theming
styled-components
has full theming support by exporting a wrapper <ThemeProvider>
component. This component provides a theme to all react components underneath itself in the render tree, even multiple levels deep.
To illustrate this, let's create a component that renders its children with a theme. We do so by wrapping all its children in a ThemeProvider
that has a theme
:
import { ThemeProvider } from 'styled-components';
const theme = {
main: 'mediumseagreen',
};
const GreenSection = (props) => {
return (
<ThemeProvider theme={theme}>
{props.children}
</ThemeProvider>
);
}
Second, let's create a styled component that adapts to the theme.
styled-components
injects the current theme via props.theme
into the components, which means you can adapt your component to the theme with interpolated functions.
We'll create a button
that adapts based on the main
property of the theme:
import styled from 'styled-components';
const Button = styled.button`
/* Color the background and border with theme.main, otherwise 'palevioletred' */
background: ${props => props.theme.main || 'palevioletred'};
border: 2px solid ${props => props.theme.main || 'palevioletred'};
/* โฆmore styles hereโฆ */
`;
Now, when we render the Button
inside a GreenSection
, it'll be green! If not, it'll be red.
<Button>Normal Button</Button>
<GreenSection>
{/* Notice how there's no code changes for the button, it just
adapts to the theme passed from GreenSection! */}
<Button>Green Button!</Button>
<div>
<div>
<div>
{/* This works unlimited levels deep within the component
tree since we use React's context to pass the theme down. */}
<Button>Another green button!</Button>
</div>
</div>
</div>
</GreenSection>
See the theming doc for more detailed instructions.
Docs
See the documentation for more information about using styled-components
.
Table of Contents
Syntax highlighting
The one thing you lose when writing CSS in template literals is syntax highlighting. We're working hard on making proper syntax highlighting happening in all editors. We currently have support for Atom.
This is what it looks like when properly highlighted:
Atom
@gandm, the creator of language-babel
, has added support for styled-components
in Atom!
To get proper syntax highlighting, all you have to do is install and use the language-babel
package for your JavaScript files!
Other Editors
We could use your help to get syntax highlighting support to other editors! If you want to start working on syntax highlighting for your editor, open an issue to let us know.
Further Reading
These are some great articles and talks about related topics in case you're hungry for more:
Alternative Installation Methods
If you're not using a module bundler or not using npm
as your package manager, we also have a global ("UMD") build!
You can use that via the unpkg
CDN to get styled-components
, the URL is https://unpkg.com/styled-components/dist/styled-components.min.js
.
To install styled-components
with bower you'd do:
bower install styled-components=https://unpkg.com/styled-components/dist/styled-components.min.js
To use it from your HTML, add this at the bottom of your index.html
, and you'll have access to the global window.styled
variable:
<script src="https://unpkg.com/styled-components/dist/styled-components.min.js" type="text/javascript"></script>
License
Licensed under the MIT License, Copyright ยฉ 2016 Glen Maddern and Maximilian Stoiber.
See LICENSE for more information.
Acknowledgements
This project builds on a long line of earlier work by clever folks all around the world. We'd like to thank Charlie Somerville, Nik Graf, Sunil Pai, Michael Chan, Andrey Popp, Jed Watson & Andrey Sitnik who contributed ideas, code or inspiration.
Special thanks to @okonet for the fantastic logo.
Development supported by Front End Center and Thinkmill