A styled-components-like wrapper around tailwind-variants
npm install styled-tailwind-variants

A styled-components-like wrapper around tailwind-variants that provides a familiar API for creating styled React components with Tailwind CSS.



- šØ Styled-components-like API - Familiar syntax for React developers
- šÆ Full TypeScript support - Complete type safety and IntelliSense
- š Multiple syntax styles - String, object, and template literal support
- š§ Variant system - Full support for tailwind-variants features (except slots)
- š¦ Tree-shakeable - Optimized bundle size
- ā” Zero runtime overhead - Compile-time class generation
``bash`
npm install styled-tailwind-variants tailwind-variants tailwind-mergeor
pnpm add styled-tailwind-variants tailwind-variants tailwind-mergeor
yarn add styled-tailwind-variants tailwind-variants tailwind-merge
`tsx
import styled from 'styled-tailwind-variants';
// Template literal syntax
const UICard = styled.divbg-white shadow-lg p-6 rounded-xl;
// String syntax
const UIBox = styled.div("p-4 bg-white rounded-lg shadow-md");
// Object syntax with variants
const UIButton = styled.button({
base: "px-4 py-2 rounded font-medium transition-colors",
variants: {
color: {
primary: "bg-blue-500 text-white hover:bg-blue-600",
secondary: "bg-gray-500 text-white hover:bg-gray-600",
success: "bg-green-500 text-white hover:bg-green-600"
},
size: {
sm: "text-sm px-3 py-1.5",
md: "text-base px-4 py-2",
lg: "text-lg px-6 py-3"
}
},
defaultVariants: {
color: "primary",
size: "md"
}
});
// Usage
function App() {
return (
API Reference
$3
#### Template Literal Syntax
`tsx
const UIComponent = styled.divbase-classes;
`#### String Syntax
`tsx
const UIComponent = styled.div("base-classes");
`#### Object Syntax
`tsx
const UIComponent = styled.div({
base: "base-classes",
variants: {
variantName: {
option1: "classes-for-option1",
option2: "classes-for-option2"
}
},
defaultVariants: {
variantName: "option1"
}
});
`$3
`tsx
import { CustomComponent } from './CustomComponent';const StyledCustom = styled(CustomComponent)({
base: "additional-classes"
});
`$3
All styled components accept the following props:
-
className - Additional CSS classes to merge
- children - React children
- as - Render as a different element or component
- Variant props based on your configuration`tsx
const UIButton = styled.button({
base: "px-4 py-2 rounded",
variants: {
color: {
primary: "bg-blue-500",
secondary: "bg-gray-500"
}
}
});// Usage with variant props
Click me
`$3
#### Compound Variants
`tsx
const UIButton = styled.button({
base: "px-4 py-2 rounded",
variants: {
color: {
primary: "bg-blue-500",
secondary: "bg-gray-500"
},
disabled: {
true: "opacity-50 cursor-not-allowed"
}
},
compoundVariants: [
{
color: "primary",
disabled: true,
class: "bg-blue-200 text-blue-800"
}
]
});
`#### Boolean Variants
`tsx
const UICard = styled.div({
base: "p-4 rounded-lg",
variants: {
elevated: {
true: "shadow-lg border"
},
interactive: {
true: "hover:shadow-xl cursor-pointer transition-shadow"
}
}
});// Usage
Content
`TypeScript Support
The library provides full TypeScript support with proper type inference:
`tsx
const UIButton = styled.button({
base: "px-4 py-2",
variants: {
color: {
primary: "bg-blue-500",
secondary: "bg-gray-500"
}
}
});// TypeScript will infer the correct props
// ā
Valid
// ā TypeScript error
`Development Setup
$3
To enable autocompletion for Tailwind CSS classes in your styled components, you can configure your editor:
#### VSCode
If you're using VSCode with the TailwindCSS IntelliSense Extension, add the following to your
settings.json:`json
{
"tailwindCSS.experimental.classRegex": [
[
"styled\\.[a-zA-Z]+([^]*)",]*)"
],
[
"styled\\.[a-zA-Z]+\\(]*" class="text-primary hover:underline" target="_blank" rel="noopener noreferrer">\"'[\"']\\)",]*)"
],
[
"styled\\.[a-zA-Z]+\\(\\{[^}]base:\\s]*" class="text-primary hover:underline" target="_blank" rel="noopener noreferrer">\"'[\"']",]*)"
]
]
}
`This will enable autocompletion for:
- String syntax:
styled.div("bg-blue-500 hover:bg-blue-600")
- Template literal syntax: styled.divbg-blue-500 hover:bg-blue-600
- Object syntax: styled.div({ base: "bg-blue-500 hover:bg-blue-600" })$3
If you're using
prettier-plugin-tailwindcss to sort your class names, add styled to the list of functions that should be sorted:`javascript
// prettier.config.js
module.exports = {
plugins: [require('prettier-plugin-tailwindcss')],
tailwindFunctions: ['styled']
}
`This will automatically sort Tailwind classes in:
-
styled.div("px-4 py-2 bg-blue-500 text-white")
- styled.divpx-4 py-2 bg-blue-500 text-white
- styled.div({ base: "px-4 py-2 bg-blue-500 text-white" })Performance
This library adds minimal performance overhead compared to native
tailwind-variants`:| Library | Bundle Size | Runtime Overhead | DX Score |
|---------|-------------|------------------|----------|
| styled-tailwind-variants | 4-5 KB | ~2-5% | āāāāā |
| styled-components | 15-20 KB | ~50-100% | āāāāā |
| emotion | 10-15 KB | ~30-50% | āāāā |
ā
Great for:
- Most web applications
- Components with infrequent re-renders
- Projects where developer experience is important
ā ļø Consider alternatives for:
- High-frequency animations (60fps)
- Rendering thousands of components in lists
- Mobile devices with limited performance
The minimal overhead is usually worth the significant improvement in developer experience and code maintainability.
Contributions are welcome! Please feel free to submit a Pull Request.
MIT Ā© Alexey Elizarov