npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tailwindcss-mixins

v0.0.11

Published

Mixins for Tailwind CSS provides a declarative API for creating reusable groups of utilities, reducing code duplication and improving maintainability while emphasizing a utility-first approach.

Downloads

153

Readme

minified size license version twitter

Mixins for Tailwind CSS is a plugin that introduces a new mixin variant/utility pair, allowing you to create reusable groups of Tailwind CSS utilities. This provides a more declarative and maintainable approach compared to traditional methods of targeting descendants with arbitrary variants or non-Tailwind CSS selectors.

Installation

You can install the plugin via npm:

npm install tailwindcss-mixins

Then, include it in your tailwind.config.js:

module.exports = {
  plugins: [
    require('tailwindcss-mixins'),
  ]
}

Usage

The mixin variant can be used to define a group of utilities that will be applied to multiple elements simultaneously. Here's an example:

<div class="mixin:inline-block mixin:font-bold mixin:underline">
  <div class="mixin">Test 1</div>
  <div class="mixin">Test 2</div>
  <div class="mixin">Test 3</div>
  <div class="mixin">Test 4</div>
</div>

View this example on Tailwind Play

In this example, the utilities inline-block, font-bold, and underline are applied to all elements with the mixin class.

Mixins help reduce repetitive class names, keeping your code DRY (Don't Repeat Yourself) and adhering to the utility-first approach of Tailwind CSS. Instead of using arbitrary variants or non-Tailwind CSS classes to target descendants, mixins provide a consistent and declarative pattern for creating reusable styles.

Naming Mixins

If you need to differentiate between multiple mixins, you can use the Tailwind CSS modifier syntax to name your mixins:

<div class="
  mixin/button:inline-block
  mixin/button:font-bold
  mixin/button:underline
  mixin/link:text-blue-500
  mixin/link:hover:underline
">
  <a class="mixin/link" href="#">Link</a>
  <button class="mixin/button">Button</button>
</div>

View this example on Tailwind Play

In this example, we've named our mixins mixin/{name}, allowing us to distinguish between different mixins and apply them accordingly.

Applying Variants to Mixins

When using mixins, you can apply Tailwind CSS variants to the declaration of the mixin itself. This will affect only the property it's applied to, allowing for more customization of mixins.

For example:

<div class="sm:mixin:inline-block">
  <div class="mixin"></div>
</div>

In this case, the inline-block utility will be applied to the elements with the mixin class only on small screens and above.

Note that the use of a variant on the consumer of the mixin, like in the below example, will not affect the mixin itself. Variants must be applied directly to the mixin declaration.

This will not work:

<div class="mixin:inline-block">
  <div class="sm:mixin"></div>
</div>

Why use Mixins for Tailwind CSS?

Mixins for Tailwind CSS provides several benefits:

  • Simplifies the application of styles to multiple elements
  • Reduces code duplication and improves maintainability
  • Provides a more declarative and consistent approach to styling
  • Adheres to the utility-first approach of Tailwind CSS
  • Improves developer experience by avoiding the need for arbitrary targeting and non-Tailwind CSS classes

Why NOT use Mixins for Tailwind CSS?

While Mixins for Tailwind CSS is a simple and powerful tool, it may not be suitable for every project. Some potential drawbacks include:

  • Learning Curve: Mixins introduce a new concept and syntax that developers will need to learn and understand.
  • Complexity: As the number of mixins and nested mixins increases, the complexity of your codebase may also increase, potentially leading to maintainability issues.
  • Performance: Mixins may have a slight impact on performance due to the additional CSS rules generated. However, this impact is likely to be negligible in most cases.

As with any tool or technique, it's important to weigh the pros and cons and choose the approach that best fits your project's needs and team's preferences.


I hope you find tailwindcss-mixins a valuable addition to your projects. If you have any issues or suggestions, don't hesitate to open an issue or pull request.

If you liked this, you might also like my other Tailwind CSS plugins: