🔥 View the Next + Styled Components + Tailwind Twin starter for usage examples
After creating your next app:
npm install --save twin.macro styled-components
Yarn instructions
yarn add twin.macro styled-components
// In .babelrc
{
"presets": [
"next/babel"
],
"plugins": [
"babel-plugin-macros",
[
"styled-components",
{
"ssr": true
}
]
]
}
Projects using Twin also use the Tailwind preflight base styles to smooth over cross-browser inconsistencies.
Twin adds the preflight base styles with the GlobalStyles
import which you can add in pages/_app.js
:
// page/_app.js
import React from 'react'
import { GlobalStyles } from 'twin.macro'
const App = ({ Component, pageProps }) => (
<>
<GlobalStyles />
<Component {...pageProps} />
</>
)
export default App
GlobalStyles
also includes some @keyframes so the animate-xxx
classes have animations. But if you’re not using the animate classes then you can avoid adding the extra keyframes.
Twin’s recommended config can be added in a couple of different places.
a) In your package.json
:
// package.json
"babelMacros": {
"twin": {
"config": "tailwind.config.js",
"preset": "styled-components",
"debugProp": true,
"debugPlugins": false,
"debug": false,
}
},
b) Or in a new file named babel-plugin-macros.config.js
placed in your project root:
// babel-plugin-macros.config.js
module.exports = {
twin: {
config: 'tailwind.config.js',
preset: 'styled-components',
debugProp: true,
debugPlugins: false,
debug: false,
},
}
Twin comes with types for every import except the css
and styled
imports.
To avoid the ugly Flash Of Unstyled Content (FOUC), add a server stylesheet in pages/_document.js
that gets read by Next.js:
// 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()
}
}
}
Note: Adding styles within this file won’t work like you would expect, take a look at this issue for a work around.
Name | Type | Default | Description |
---|---|---|---|
config | string |
"tailwind.config.js" |
The path to your Tailwind config |
preset | string |
"emotion" |
The css-in-js library behind the scenes - also supports 'styled-components' and 'goober' |
hasSuggestions | boolean |
true |
Display class suggestions when a class can't be found |
debugPlugins | boolean |
false |
Display generated class information in your terminal from your plugins |
debugProp | boolean |
false |
Add a prop to your elements in development so you can see the original tailwind classes, eg: <div data-tw="bg-black" /> |
debug | boolean |
false |
Display information in your terminal about the Tailwind class conversions |
If twin’s default styled
and css
imports need to be adjusted, you can do so with the following config:
{
styled: { import: "default", from: "styled-components" },
css: { import: "css", from: "styled-components/macro" }
}
Note: Make sure you remove the preset
option as that value disables the styled + css options.
- See how to customize your classes →
- Learn how to use the styled-components library
The css prop / css import / styled import
- "Vanilla" React + Styled Components
- Create React App + Styled Components
- Gatsby + Styled Components
- Next.js + Styled Components (current)