Steps deducted from official example repository:
-
install:
npm install styled-components -
install types:
npm install --save-dev @types/styled-components -
create
.babelrcin project root with following content:{ "presets": ["next/babel"], "plugins": [["styled-components", { "ssr": true }]] } -
create
./pages/_document.tsxand copy & pastedocument.tsxfrom the version from repo. This enables proper handling of dynamically createdCSS-Classnamesforstyled-componentseven 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}; `} `;