Steps deducted from official example repository:
-
install:
npm install styled-components
-
install types:
npm install --save-dev @types/styled-components
-
create
.babelrc
in project root with following content:{ "presets": ["next/babel"], "plugins": [["styled-components", { "ssr": true }]] }
-
create
./pages/_document.tsx
and copy & pastedocument.tsx
from the version from repo. This enables proper handling of dynamically createdCSS-Classnames
forstyled-components
even server-side.
- import
ThemeProvider
:import styled, { ThemeProvider } from 'styled-components';
- Wrap content with
<ThemeProvider>
your react stuff here</ThemeProvider>
-
Simple usage:
background-color: ${(props) => props.theme.backgroundColor};
-
Complex example with
css
:import styled, { css } from 'styled-components'; export const Button = styled.div<{ role: keyof typeof Role }>` font-weight: bold; ${(props) => props.theme && css` font-size: ${props.theme.typography.h1}; color: ${props.theme.warningColor}; `} `;