Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

emmnull/tailwind-variant-selectors

Repository files navigation

Tailwind Variant Selectors

Maybe cursed 🤮 maybe blessed 🤩, easily use your custom Tailwind CSS @variants in CSS. Useful to maintain consistency across inline class variants and custom @components or global styles without relying on @apply-ridden stylesheets.

@variant hocus (&:hover, &:focus);

.btn {
  color: var(--color-input);

  &:--hocus {
    color: var(--color-input-accent);
  }
}

Note

Previous version of this package used a custom syntax (:variant()) which has since been abandonned to instead follow the CSS Extensions specification for custom selectors.

Usage

pnpm add -D tailwind-variant-selectors

Then either use the PostCSS plugin:

// postcss.config.js

export default {
  plugins: {
    'tailwind-variant-selectors/postcss': {
      files: ['./src/styles/variants.css'],
    },
    //...
  },
};

Or the Vite plugin:

// vite.config.ts

import variants from 'tailwind-variant-selectors/vite';

export default defineConfig({
  plugins: [
    variants(), // Place before tailwind
    tailwindcss(),
  ],
});

Caveats

Tailwind config

This package relies on @variant declarations, i.e. you need to use Tailwind v4's CSS-based config approach. Variants added using addVariant or matchVariant js plugins currently are not supported.

Pseudo-elements

Since variants containing multiple selectors are compounded into :is() selectors, any variants based on non-single pseudo-elements will result in unmatchable CSS selectors.

Ex.:

@variant foo (&::after);
@variant bar (&::after, .hi-mom);

/* This will be transformed into a matchable selector. */
.btn:--foo /* .btn::after. */ {
  /* ... */
}

/* This will be transformed into an unmatchable selector. */
.btn:--bar /* .btn:is(::after, .hi-mom) */ {
  /* ... */
}

Dynamic variants

Not supported.

About

Enabling custom css selectors based on your custom tailwind variants.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy