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

tailwind-merge-vue-directive

v2.0.5

Published

A Vue directive to automatically merge tailwind classes on custom components. This plugin builds on the awesome work done by the "tailwind-merge" package.

Downloads

367

Readme

tailwind-merge-vue-directive

A Vue.js plugin that adds a "v-tw-merge" directive that you can use in your components to automatically merge tailwind classes that were passed from the parent with classes that exist on the component level (example below).

If you find this package helpful please consider starring it on github, Thank you ❤️.

To achieve the tailwind class merging behavior, this package uses tailwind-merge under the hood.

Get started

What is this for?
How to install
How to use
Example

How to install:

npm install tailwind-merge-vue-directive

How to use in your Vue app:

Register the plugin:

// main.ts (or main.js)
import  {  createApp  }  from  "vue";
import App from  "./App.vue";

// import the tailwind-merge-vue-directive package
import twMergeDirective from  "tailwind-merge-vue-directive";

const app =  createApp(App);
app.use(twMergeDirective);// register the plugin
app.mount("#app");

Use the directive on your Vue components:

// SomeComponent.vue
<template>
   <div class="h-20 w-20 bg-red-500" v-tw-merge/>
</template>

Or:

// SomeComponent.vue
<template>
   <div class="h-20 w-20 bg-red-500" v-twMerge/>
</template>

Example

// ParentComponent.vue
<template>
   <div>
      ...
      <ChildComponent class="text-amber-500" />
      ...
   </div>
</template>
// ChildComponent.vue
<template>
   <div  class="text-2xl text-red-500" v-tw-merge>
      Lorem ipsum dolor sit amet consectetur adipisicing elit
   </div>
</template>

ChildComponent.vue will be rendered as:

<div  class="text-2xl text-amber-500">
   Lorem ipsum dolor sit amet consectetur adipisicing elit
</div>

The classes that were passed from ParentComponent.vue will override the classes that were specified in ChildComponent.vue

What is this for?

When using Tailwind CSS to style Vue components, you might encounter a situation where you want to change some styles of a component, but only for a specific instance of the component.

For demonstration purposes let's assume we have this component:

// MyButton.vue
<template>
   <button class="bg-red-600 text-white px-6 py-2 rounded">
      <slot/>
   </button>
</template>

Now let's assume we want to use this MyButton component in our App.vue, but just this time we want the button to be blue instead of red.

Seeminglty, the solution to this problem is quite obvious.
We can just to pass a class attribute when using the component.

// App.vue
<template>
   ...
   <MyButton class="bg-blue-600">My blue button</MyButton>
   ...
</template>

The class attribute that was defined on the component instance in App.vue will be appended to the existing class attribute in MyButton.vue.

The final rendered HTML will look like this:

<button class="bg-red-600 text-white px-6 py-2 rounded bg-blue-600">My blue button</button> 

It is important to point out that the order of the classes in the class attribute does not matter at all.

CSS rules are applied by specificity, when two selectors have the same specificity score the selector that was declared last "wins".
You can read more about how the CSS Cascade works here.

In the CSS file that Tailwind generates, both of the selectors .bg-red-600 and .bg-blue-600 have the exact same specificity score.
The .bg-red-600 selector is defined after the .bg-blue-600 selector simply because tailwind generates the color class names based on alphabetical order.
This means our rendered button will be have a red background, which is not what we wanted.

In order to solve this problem a package called "tailwind-merge" was created by Dany Castillo.

The tailwind-merge package gives you a function that you can pass all the class names to.
The function will "merge" conflicting classes while ignoring the specificity score of the selectors and instead giving classes that where defined later precedence.

import {twMerge} from 'tailwind-merge'
const classes = twMerge('bg-red-600 bg-blue-600')
console.log(classes) // will output: "bg-blue-600"

I have created this package (tailwind-merge-vue-directive) to simplify the process of merging conflicting tailwind classes on Vue components.

After installing the package you can simply use a v-tw-merge directive on your components' markup to automatically merge confilicting classes using the twMerge function provided by "tailwind-merge".