Tailwind-Styled-Component
Create tailwind css react components like styled components with classes name on multiple lines
Before 😬
<div className=`flex ${primary ? "bg-indigo-600" : "bg-indigo-300"} inline-flex items-center border border-transparent text-xs font-medium rounded shadow-sm text-white hover:bg-indigo-700 focus:outline-none`>
After 🥳
<Button $primary={false}>
const Button = tw.div`
${(p) => (p.$primary ? "bg-indigo-600" : "bg-indigo-300")}
flex
inline-flex
items-center
border
border-transparent
text-xs
font-medium
rounded
shadow-sm
text-white
hover:bg-indigo-700
focus:outline-none
`
Features
♻️ Reusable
🧩 Extendable
💅 Compatible with Styled Components
⚡️ Use props like every React Component
🤯 Stop editing 400+ characters lines
🧘 Cleaner code in the render function
Install
npm i -D tailwind-styled-components
yarn add -D tailwind-styled-components
This extension requires TailwindCSS to be installed and configured on your project too. Install TailwindCSS
[Optional] Configure IntelliSense autocomplete on VSCode
First, install Tailwind CSS IntelliSense VSCode extension
https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss
Then add these user settings (How to edit VSCode settings?)
"tailwindCSS.includeLanguages": {
"typescript": "javascript",
"typescriptreact": "javascript"
},
"editor.quickSuggestions": {
"strings": true
},
"tailwindCSS.experimental.classRegex": [
"tw`([^`]*)",
"tw=\"([^\"]*)",
"tw={\"([^\"}]*)",
"tw\\.\\w+`([^`]*)",
"tw\\(.*?\\)`([^`]*)"
]
Usage
Import
import tw from "tailwind-styled-components"
Basic
Create a Tailwind Styled Component with Tailwind rules that you can render directly
const Container = tw.div`
flex
items-center
justify-center
flex-col
w-full
bg-indigo-600
`
render(
<Container>
<div>Use the Container as any other React Component</div>
</Container>
)
Will be rendered as
<div class="flex items-center justify-center flex-col w-full bg-indigo-600">
<div>Use the Container as any other React Component</div>
</div>
Conditional class names
Set tailwind class conditionally with the same syntax as styled components
interface ButtonProps {
$primary: boolean
}
const Button = tw.button<ButtonProps>`
flex
${(p) => (p.$primary ? "bg-indigo-600" : "bg-indigo-300")}
`
Tailwind Styled Components supports Transient Props)
Prefix the props name with a dollar sign ($) to prevent forwarding them to the DOM element
<Button $primary={true} />
Will be rendered as
<button class="flex bg-indigo-600">
</button>
and
<Button $primary={false} />
Will be rendered as
<button class="flex bg-indigo-300">
</button>
Be sure to set the entire class name
✅ Do ${p => p.$primary ? "bg-indigo-600" : "bg-indigo-300"}
❌ Don't bg-indigo-${p => p.$primary ? "600" : "300"}
Extends
const DefaultContainer = tw.div`
flex
items-center
bg-blue-600
`
const RedContainer = tw(DefaultContainer)`
bg-red-300
`
Will be rendered as
<div class="flex items-center bg-red-300">
</div>
Overrides the parent background color class
Extends Styled Component
Extend styled components
const StyledComponentWithCustomCss = styled.div`
filter: blur(1px);
`
const = tw(StyledComponentWithCustomCss)`
flex
`
Css rule filter
is not supported by default on TailwindCSS
Will be rendered as
<div class="flex" style="filter: blur(1px);">
</div>
Example
import React from "react"
import tw from "tailwind-styled-components"
import styled from "styled-components"
interface TitleProps {
$large: boolean;
}
const Title = tw.h1<TitleProps>`
${(p) => (p.$large ? "text-lg" : "text-base")}
text-indigo-500
`
const SpecialBlueContainer = styled.section`
background-color: #0366d6;
`
const Container = tw(SpecialBlueContainer)`
flex
items-center
justify-center
w-full
`
render(
<Container>
<Title $large={true}>Hello World, this is my first tailwind styled component!</Title>
</Container>
)