DEV Community

Suhag Lapani
Suhag Lapani

Posted on

Simplifying TailwindCSS with Tailwind Variants in React

TailwindCSS is a powerful utility-first CSS framework that allows you to build modern websites quickly. However, managing complex styles can sometimes be a challenge, especially when dealing with responsive design, multiple component states, and variant configurations. This is where Tailwind Variants comes in handy. Tailwind Variants extends TailwindCSS with a first-class variant API, making it easier to manage your styles efficiently.

In this blog post, we'll walk you through setting up and using Tailwind Variants in a React project to reduce complexity and improve maintainability. We'll also discuss the advantages of using Tailwind Variants based on the documentation.

Getting Started

1. Setting Up TailwindCSS

First, ensure you have TailwindCSS installed in your project. If not, follow the TailwindCSS installation guide.

2. Installing Tailwind Variants

Next, install Tailwind Variants as a dependency:

npm install tailwind-variants
Enter fullscreen mode Exit fullscreen mode

3. Configuring TailwindCSS

Add the Tailwind Variants wrapper to your TailwindCSS config file (tailwind.config.js):

const { withTV } = require('tailwind-variants/transformer');

/** @type {import('tailwindcss').Config} */
module.exports = withTV({
  content: ['./index.html', './src/**/*.{js,ts,jsx,tsx}'],
  theme: {
    extend: {},
  },
  plugins: [],
});
Enter fullscreen mode Exit fullscreen mode

Using Tailwind Variants

1. Basic Example

Let’s create a simple button component using Tailwind Variants.

import { tv } from 'tailwind-variants';

const button = tv({
  base: 'font-medium bg-blue-500 text-white rounded-full active:opacity-80',
  variants: {
    color: {
      primary: 'bg-blue-500 text-white',
      secondary: 'bg-purple-500 text-white',
    },
    size: {
      sm: 'text-sm',
      md: 'text-base',
      lg: 'px-4 py-3 text-lg',
    },
  },
  compoundVariants: [
    {
      size: ['sm', 'md'],
      class: 'px-3 py-1',
    },
  ],
  defaultVariants: {
    size: 'md',
    color: 'primary',
  },
});

const Button = ({ size, color, children }) => (
  <button className={button({ size, color })}>{children}</button>
);

export default Button;
Enter fullscreen mode Exit fullscreen mode

With this setup, you can easily create buttons with different sizes and colors by passing the appropriate props:

<Button size="sm" color="secondary">Click me</Button>
<Button size="lg" color="primary">Click me</Button>
Enter fullscreen mode Exit fullscreen mode

2. Responsive Variants

Tailwind Variants also supports responsive variants. To use them, add the responsiveVariants option to your Tailwind Variants configuration:

const button = tv(
  {
    base: 'font-semibold text-white py-1 px-3 rounded-full active:opacity-80',
    variants: {
      color: {
        primary: 'bg-blue-500 hover:bg-blue-700',
        secondary: 'bg-purple-500 hover:bg-purple-700',
        success: 'bg-green-500 hover:bg-green-700',
        error: 'bg-red-500 hover:bg-red-700',
      },
    },
  },
  {
    responsiveVariants: ['xs', 'sm', 'md'], // `true` to apply to all screen sizes
  }
);

const ResponsiveButton = () => (
  <button
    className={button({
      color: {
        initial: 'primary',
        xs: 'secondary',
        sm: 'success',
        md: 'error',
      },
    })}
  >
    Responsive Button
  </button>
);
Enter fullscreen mode Exit fullscreen mode

3. IntelliSense Setup

For better development experience, you can enable autocompletion for Tailwind Variants in VSCode:

Add the following to your settings.json:

{
  "tailwindCSS.experimental.classRegex": [
    ["tv\\((([^()]*|\\([^()]*\\))*)\\)", "[\"'`]([^\"'`]*).*?[\"'`]"]
  ]
}
Enter fullscreen mode Exit fullscreen mode

4. Overriding Styles

You can override styles for individual components or slots. Here’s an example of overriding a single component’s style:

button({
  color: 'secondary',
  class: 'bg-pink-500 hover:bg-pink-500', // overrides the color variant
});
Enter fullscreen mode Exit fullscreen mode

Advantages of Using Tailwind Variants

Based on the documentation, here are some key advantages of using Tailwind Variants:

  1. Variants API: Tailwind Variants provides a robust and flexible API for managing variants, making it easy to define different styles based on component states.

  2. Framework Agnostic: Tailwind Variants works independently of any specific JavaScript framework, making it versatile and adaptable to various projects.

  3. Responsive Variants: Tailwind Variants allows you to define responsive variants, ensuring that your components look great on all screen sizes without duplicating code.

  4. Split Components (Slots): With slots, you can divide components into multiple parts, making it easier to manage and style individual sections of a component.

  5. Compound Slots: Tailwind Variants supports compound slots, enabling you to apply styles to multiple slots simultaneously, reducing redundancy.

  6. Overrides Components: You can easily override component styles, providing flexibility to customize and adjust styles as needed.

  7. Components Composition (Extend): Tailwind Variants allows you to extend and compose components, promoting reuse and consistency across your project.

  8. Great Developer Experience (DX): Tailwind Variants enhances the development experience with features like autocompletion and better type safety, improving productivity and reducing errors.

  9. Conflict Resolution: Tailwind Variants handles conflicts gracefully, ensuring that your styles are applied consistently without unexpected behavior.

Conclusion

Tailwind Variants offers a powerful way to manage complex styles in TailwindCSS by providing a first-class variant API. It simplifies the process of creating responsive, maintainable, and scalable components in your React projects. By using Tailwind Variants, you can reduce repeated code and make your project more readable, ultimately speeding up your development process.

Feel free to experiment with different configurations and see how Tailwind Variants can help you streamline your styling workflow in React. Happy coding!

Top comments (0)