Tailwind Variants

Tailwindcss first-class variant API

README

tailwind-variants


The power of Tailwind combined with a first-class variant API.

Features


- First-class variant API
- Responsive variants
- Slots support
- Composition support
- Fully typed
- Framework agnostic
- Automatic conflict resolution

Documentation


For full documentation, visit tailwind-variants.org

Quick Start


1. Installation:
To use Tailwind Variants in your project, you can install it as a dependency:

  1. ```bash
  2. yarn add tailwind-variants
  3. # or
  4. npm i tailwind-variants
  5. ```

2. Usage:

  1. ```js
  2. import { tv } from 'tailwind-variants';
  3. const button = tv({
  4.   base: "font-medium bg-blue-500 text-white rounded-full active:opacity-80",
  5.   variants: {
  6.     color: {
  7.       primary: "bg-blue-500 text-white",
  8.       secondary: "bg-purple-500 text-white",
  9.     },
  10.     size: {
  11.       sm: "text-sm",
  12.       md: "text-base",
  13.       lg: "px-4 py-3 text-lg",
  14.     },
  15.   },
  16.   compoundVariants: [
  17.     {
  18.       size: ["sm", "md"],
  19.       class: "px-3 py-1",
  20.     },
  21.   ],
  22.   defaultVariants: {
  23.     size: "md",
  24.     color: "primary",
  25.   }
  26. });
  27. return (
  28.   <button className={button({ size: 'sm', color: 'secondary' })}>Click me</button>
  29. )
  30. ```

3. Responsive variants configuration (optional): If you want to use responsive variants
you need to add the Tailwind Variants wrapper to your TailwindCSS config file tailwind.config.js.

  1. ```js
  2. // tailwind.config.js
  3. const { withTV } = require('tailwind-variants/transformer')
  4. /** @type {import('tailwindcss').Config} */
  5. module.exports = withTV({
  6.   content:  ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"],
  7.   theme: {
  8.     extend: {},
  9.   },
  10.   plugins: [],
  11. })
  12. ```


Acknowledgements


- [cva](https://github.com/joe-bell/cva) (Joe Bell)
  This project as started as an extension of Joe's work on cva – a great tool for generating variants for a single element with Tailwind CSS. Big shoutout to Joe Bell and contributors you guys rock! 🤘 - we recommend to usecva if don't need any of the Tailwind Variants features listed here.

- [Stitches](https://stitches.dev/) (Modulz)
  The pioneers of the variants API movement. Inmense thanks to Modulz for their work on Stitches and the community around it. 🙏


Community


We're excited to see the community adopt NextUI, raise issues, and provide feedback. Whether it's a feature request, bug report, or a project to showcase, please get involved!


Contributing


Contributions are always welcome!

Please follow our contributing guidelines.

Please adhere to this project's CODE_OF_CONDUCT.

Authors


- Junior garcia (@jrgarciadev)
- Tianen Pang (@tianenpang)

License


Licensed under the MIT License.

See LICENSE for more information.