Create tailwind css react components like styled components with classes name on multiple lines
npm install tailwind-styled-components
[![NPM version][npm-image]][npm-url]
[npm-image]: http://img.shields.io/npm/v/tailwind-styled-components.svg?style=flat-square[npm-url]: http://npmjs.org/package/tailwind-styled-components
#### Before 😬
``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 🥳`jsconst 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 functionInstallUsing npm`bashnpm i -D tailwind-styled-components`Using yarn`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 VSCodeFirst, install Tailwind CSS IntelliSense VSCode extensionhttps://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcssThen add these user settings (How to edit VSCode settings?)`js"tailwindCSS.includeLanguages": { "typescript": "javascript", // if you are using typescript "typescriptreact": "javascript" // if you are using typescript with react},"editor.quickSuggestions": { "strings": true // forces VS Code to trigger completions when editing "string" content},"tailwindCSS.experimental.classRegex": [ "tw([^]*)", // tw... "tw\\.[^]+([^]*)", // tw.xxx... "tw\\(.?\\).?([^]*)" // tw(Component)...]`Usage$3`jsimport tw from "tailwind-styled-components"`$3Create a Tailwind Styled Component with Tailwind rules that you can render directly`jsconst Container = tw.div flex items-center justify-center flex-col w-full bg-indigo-600``jsrender( Use the Container as any other React Component )`Will be rendered as`html Use the Container as any other React Component`$3Set tailwind class conditionally with the same syntax as styled components`tsinterface ButtonProps { $primary: boolean}const Button = tw.button flex ${(p) => (p.$primary ? "bg-indigo-600" : "bg-indigo-300")}`Tailwind Styled Components supports Transient PropsPrefix the props name with a dollar sign ($) to prevent forwarding them to the DOM element`jsx`Will be rendered as`html `and`jsx`Will be rendered as`html `---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"}---$3`jsconst DefaultContainer = tw.div flex items-center``jsconst RedContainer = tw(DefaultContainer) bg-red-300`Will be rendered as`html `$3Extend styled components`jsconst StyledComponentWithCustomCss = styled.div filter: blur(1px);const = tw(StyledComponentWithCustomCss) flex`Css rule filter is not supported by default on TailwindCSSWill be rendered as`html `$3If you want to keep all the styling you've applied to a component but just switch out what's being ultimately rendered (be it a different HTML tag or a different custom component), you can use the $as prop to do this at runtime.`jsconst Button = tw.buttoninline-flex items-center p-2Link`Will render as`htmlLink`Example`tsximport React from "react"import tw from "tailwind-styled-components"import styled from "styled-components"// Create a react component that renders an which is// indigo and sized at 1.125reminterface TitleProps { $large: boolean;}const Title = tw.h1 ${(p) => (p.$large ? "text-lg" : "text-base")} text-indigo-500// Create a react component that renders a with// a special blue background colorconst SpecialBlueContainer = styled.section background-color: #0366d6;// Create a react component that extends the SpecialBlueContainer to render// a tailwind with the special blue background and adds the flex classesconst Container = tw(SpecialBlueContainer) flex items-center justify-center w-full// Use them like any other React component – except they're styled!render( Hello World, this is my first tailwind styled component! )``Show more
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 🥳`jsconst 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 functionInstallUsing npm`bashnpm i -D tailwind-styled-components`Using yarn`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 VSCodeFirst, install Tailwind CSS IntelliSense VSCode extensionhttps://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcssThen add these user settings (How to edit VSCode settings?)`js"tailwindCSS.includeLanguages": { "typescript": "javascript", // if you are using typescript "typescriptreact": "javascript" // if you are using typescript with react},"editor.quickSuggestions": { "strings": true // forces VS Code to trigger completions when editing "string" content},"tailwindCSS.experimental.classRegex": [ "tw([^]*)", // tw... "tw\\.[^]+([^]*)", // tw.xxx... "tw\\(.?\\).?([^]*)" // tw(Component)...]`Usage$3`jsimport tw from "tailwind-styled-components"`$3Create a Tailwind Styled Component with Tailwind rules that you can render directly`jsconst Container = tw.div flex items-center justify-center flex-col w-full bg-indigo-600``jsrender( Use the Container as any other React Component )`Will be rendered as`html Use the Container as any other React Component`$3Set tailwind class conditionally with the same syntax as styled components`tsinterface ButtonProps { $primary: boolean}const Button = tw.button flex ${(p) => (p.$primary ? "bg-indigo-600" : "bg-indigo-300")}`Tailwind Styled Components supports Transient PropsPrefix the props name with a dollar sign ($) to prevent forwarding them to the DOM element`jsx`Will be rendered as`html `and`jsx`Will be rendered as`html `---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"}---$3`jsconst DefaultContainer = tw.div flex items-center``jsconst RedContainer = tw(DefaultContainer) bg-red-300`Will be rendered as`html `$3Extend styled components`jsconst StyledComponentWithCustomCss = styled.div filter: blur(1px);const = tw(StyledComponentWithCustomCss) flex`Css rule filter is not supported by default on TailwindCSSWill be rendered as`html `$3If you want to keep all the styling you've applied to a component but just switch out what's being ultimately rendered (be it a different HTML tag or a different custom component), you can use the $as prop to do this at runtime.`jsconst Button = tw.buttoninline-flex items-center p-2Link`Will render as`htmlLink`Example`tsximport React from "react"import tw from "tailwind-styled-components"import styled from "styled-components"// Create a react component that renders an which is// indigo and sized at 1.125reminterface TitleProps { $large: boolean;}const Title = tw.h1 ${(p) => (p.$large ? "text-lg" : "text-base")} text-indigo-500// Create a react component that renders a with// a special blue background colorconst SpecialBlueContainer = styled.section background-color: #0366d6;// Create a react component that extends the SpecialBlueContainer to render// a tailwind with the special blue background and adds the flex classesconst Container = tw(SpecialBlueContainer) flex items-center justify-center w-full// Use them like any other React component – except they're styled!render( Hello World, this is my first tailwind styled component! )``Show more
>
#### After 🥳
jsconst Button = tw.div
flex inline-flex items-center border border-transparent text-xs font-medium rounded shadow-sm text-white
hover:bg-indigo-700 focus:outline-none`
♻️ Reusable
🧩 Extendable
💅 Compatible with Styled Components
⚡️ Use props like every React Component
🤯 Stop editing 400+ characters lines
🧘 Cleaner code in the render function
Using npm`bashnpm i -D tailwind-styled-components`
bashnpm i -D tailwind-styled-components
Using yarn`yarn add -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?)
`js"tailwindCSS.includeLanguages": { "typescript": "javascript", // if you are using typescript "typescriptreact": "javascript" // if you are using typescript with react},"editor.quickSuggestions": { "strings": true // forces VS Code to trigger completions when editing "string" content},"tailwindCSS.experimental.classRegex": [ "tw([^]*)", // tw... "tw\\.[^]+([^]*)", // tw.xxx... "tw\\(.?\\).?([^]*)" // tw(Component)...]`
js"tailwindCSS.includeLanguages": { "typescript": "javascript", // if you are using typescript "typescriptreact": "javascript" // if you are using typescript with react},"editor.quickSuggestions": { "strings": true // forces VS Code to trigger completions when editing "string" content},"tailwindCSS.experimental.classRegex": [ "tw
]*)", // tw
"tw\\.[^
([^
", // tw.xxx
"tw\\(.?\\).?
]*)" // tw(Component)
]
`jsimport tw from "tailwind-styled-components"`
jsimport tw from "tailwind-styled-components"
Create a Tailwind Styled Component with Tailwind rules that you can render directly
`jsconst Container = tw.div flex items-center justify-center flex-col w-full bg-indigo-600`
jsconst Container = tw.div
`jsrender( Use the Container as any other React Component )`
jsrender( Use the Container as any other React Component )
Will be rendered as
`html Use the Container as any other React Component`
html Use the Container as any other React Component
Set tailwind class conditionally with the same syntax as styled components
`tsinterface ButtonProps { $primary: boolean}
tsinterface ButtonProps { $primary: boolean}
const Button = tw.button 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
`jsx`
jsx
`html `
html
and
---
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"}
`jsconst DefaultContainer = tw.div flex items-center`
jsconst DefaultContainer = tw.div
`jsconst RedContainer = tw(DefaultContainer) bg-red-300`
jsconst RedContainer = tw(DefaultContainer)
Extend styled components
`jsconst StyledComponentWithCustomCss = styled.div filter: blur(1px);
jsconst StyledComponentWithCustomCss = styled.div
const = tw(StyledComponentWithCustomCss) flex`
Css rule filter is not supported by default on TailwindCSS
is not supported by default on TailwindCSS
If you want to keep all the styling you've applied to a component but just switch out what's being ultimately rendered (be it a different HTML tag or a different custom component), you can use the $as prop to do this at runtime.
prop to do this at runtime.
`jsconst Button = tw.buttoninline-flex items-center p-2
jsconst Button = tw.button
Link`
Will render as
`htmlLink`
htmlLink
`tsximport React from "react"import tw from "tailwind-styled-components"import styled from "styled-components"
tsximport React from "react"import tw from "tailwind-styled-components"import styled from "styled-components"
// Create a
const Title = tw.h1 ${(p) => (p.$large ? "text-lg" : "text-base")} text-indigo-500
// Create a react component that renders a with// a special blue background colorconst SpecialBlueContainer = styled.section background-color: #0366d6;
// Create a react component that extends the SpecialBlueContainer to render// a tailwind with the special blue background and adds the flex classesconst Container = tw(SpecialBlueContainer) flex items-center justify-center w-full
// Use them like any other React component – except they're styled!render( Hello World, this is my first tailwind styled component! )``