Giter VIP home page Giter VIP logo

tw-variants's Introduction

tw variants

Tailwind variant grouping without increasing CSS size

Introduction

Tailwind doesn't support variant-grouping out of the box because it leads to tons of duplicate CSS being generated as compared to using individual classes (as we can see in this tweet)

This is because with the grouped syntax like focus:(font-bold underline) each group leads to a new CSS selector being generated, and the CSS generated for focus:font-bold and focus:underline can't be reused.

tw-variants overcomes this by expanding this grouped syntax at build time into individual classes. So now when tailwind scans the files, it sees focus:(font-bold underline) as focus:font-bold focus:underline since it has been expanded before tailwind sees it.

This way you can use grouped-syntax while not having to worry about the css size.

Installation

npm install tw-variants

Add the babel plugin to your babel config (.babelrc) :-

{
  "plugins": ["tw-variants/babel"]
}

Add .tw-variants to your .gitignore. This is where tw-variants stores the expanded CSS for tailwind to scan

.tw-variants

And add the same to the content section of your tailwind.config.js:-

module.exports = {
  content: [
    // ...
    './.tw-variants',
  ],
  // ...
};

Usage

Just import tw from tw-variants and use it like a tagged template literal :-

import { tw } from 'tw-variants';

const button = tw`font-semibold border rounded focus:(font-bold underline)`;

You can't use ${variable} inside tw, so instead of doing:-

import { tw } from 'tw-variants';

const button = tw`font-semibold border rounded focus:(font-bold underline)`;
const redButton = tw`${button} bg-red-500`;

Instead do:-

import { tw } from 'tw-variants';

const button = tw`font-semibold border rounded focus:(font-bold underline)`;
const redButton = `${button} ${tw`bg-red-500`}`;

The grouping-syntax tw-variants uses is the same as the one used by twind. You can view the supported syntax here

Tailwind CSS IntelliSense

If you're using the "Tailwind CSS IntelliSense" Visual Studio Code extension, you can enable autocompletion inside tw by adding the following to your settings.json:

{
  "tailwindCSS.experimental.classRegex": ["tw`([^`]*)"]
}

tw-variants's People

Contributors

mokshit06 avatar

Stargazers

Serge avatar Salvatore avatar Huy Giang avatar Alexei Accio avatar Thomas Harr avatar Francisco Bongiovanni avatar Nikita avatar Arnav Gosain avatar Brandon McConnell avatar Ayan Yenbekbay avatar Leo Friedrichs avatar Kavin Valli avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.