Understanding Tailwind CSS
If you’re new to Tailwind CSS, we highly recommend exploring the official Tailwind documentation to learn about utility classes, responsive design, and customization options.Working with Dynamic classNames
Uniwind uses the Tailwind parser to process your className
strings at build time. This means that dynamically constructed class names cannot be detected by the Tailwind compiler, as explained in the Tailwind guide on detecting classes.
Always use complete class names in your source code. Never construct class names using string interpolation or concatenation.
❌ Incorrect Usage
The following examples show common mistakes that prevent Tailwind from detecting your classes:Example 1: String interpolation in class names
bg-red-600
and bg-green-600
because they’re constructed dynamically. Uniwind won’t be able to precompile these classes, resulting in no styling.
Example 2: Template literals with variables
text-${props.color}
will be at runtime, so it won’t generate the necessary styles.
✅ Correct Usage
Here are the recommended patterns for conditionally applying Tailwind classes:Solution 1: Use complete class names with conditionals
bg-red-600
and bg-green-600
because you’ve written out the complete class names.
Solution 2: Create a mapping object with complete class names
colorVariants
object, allowing Tailwind to detect and generate them at build time.
You can use this pattern to create reusable style variants based on props, making your components more maintainable.
Advanced Pattern: Variants and Compound Variants
For more complex component styling with multiple variants and conditions, we recommend using tailwind-variants, a popular open-source library that Uniwind fully supports. Tailwind Variants allows you to:- Define multiple style variants for your components
- Create compound variants (styles that apply when multiple conditions are met)
- Manage complex conditional styling in a clean, maintainable way
Example with tailwind-variants
Learn more about tailwind-variants in their official documentation.