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 🙏

© 2025 – Pkg Stats / Ryan Hefner

vue-class-clsx

v0.1.3

Published

> A plugin that supports writing class modules in vuejs quickly and easily similar to classname and clsx for reactjs

Downloads

90

Readme

vue-class-clsx

A plugin that supports writing class modules in vuejs quickly and easily similar to classname and clsx for reactjs

Note that the plugin does not support vue2

Demo

Install

npm install vue-class-clsx -D
yarn add vue-class-clsx -D

Setup

  1. Import with global

    import { createApp } from 'vue';
    import { createVClsx } from 'vue-class-clsx'; // import vclsx
    import App from './App.vue';
    
    const app = createApp(App);
    const vclsx = createVClsx();
    
    app.use(vclsx); // use vclsx
    app.mount('#app');
  2. Import with component

    import { vclsxComponent as vclsx } from 'vue-class-clsx'
  3. Use with typescript

    Add to env.d.ts

    import type { createVClsx } from 'vue-class-clsx';
    
    declare module '@vue/runtime-core' {
      interface ComponentCustomProperties {
        vclsx: createVClsx;
      }
    }

Usage

Use with function vclsx

<template>
  <h2 :class="vclsx('title')">Hello</h2>
</template>
<style module>
  .title {
    color: green;
  }
</style>

Use with v-binding

<template>
  <h2 v-class-module="'title'">Hello</h2>
  <h2 v-class-module="['title', 'primay']">Hello</h2>
</template>
<style module>
  .title {
    color: green;
  }

  .primay {
    color: tomato;
  }
</style>

Options (new)

1. Custom css module name

if you set custom module name for class name, then use below option. Note the option only works when used with global

<style module="custom-name"></style>
const vclsx = createVclsx({ cssModuleName: 'custom-name' });

2. Custom function name

const vclsx = createVclsx({ functionName: 'vx' });
<template>
  <h2 :class="vx('title')">Hello</h2>
</template>

Warning Attention: If you customize the function name, you need to update the type declaration to avoid construction errors

// Add to env.d.ts
import type { createVClsx } from 'vue-class-clsx';

declare module '@vue/runtime-core' {
  interface ComponentCustomProperties {
    vx: createVClsx; // vclsx => vx
  }
}

3. Custom v-binding name

const vclsx = createVclsx({ directiveName: 'v-vx' });
<template>
  <h2 v-vx="'title'">Hello</h2>
  <h2 v-vx="['title', 'primay']">Hello</h2>
</template>

| options | Description | | ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | functionName | Name of the function called in :class | | cssModuleName | Name css module, default is $style, if using css module default of vuejs then call like this module="css" => :class="cs.<classname>" . However, when attaching conditional operators, it is quite verbose. | | directiveName | The name of the props binding when using the css class module call via v-binding |

Example

Note: plugin only works with components that use css module

<style module></style>
<script setup lang="ts">
  import { ref } from 'vue';

  const bg = ref(false);
  const italic = ref(true);
  const green = ref(true);
</script>

<template>
  <!-- use with v-binding -->
  <h1>Use with v-binding</h1>

  <h2 v-class-module="{ t1: green, t2: bg, t3: italic }">Hello world! (button change)</h2>
  <h2 v-class-module="'t1'">Hello world!</h2>
  <h2 v-class-module="['t1', 't2']">Hello world!</h2>
  <h2 v-class-module="['t1', 't2', { t3: true }]">Hello world!</h2>
  <h2 v-class-module="['t1', 't2', ['fs', { t3: true }]]">Hello world!</h2>

  <hr />
  <!-- use with function -->
  <h1>Use with :class function</h1>
  <h2 :class="vclsx({ t1: green, t2: bg, t3: italic })">Hello world! (button change)</h2>
  <h2 :class="vclsx('t1', 't2')">Hello world!</h2>
  <h2 :class="vclsx(['t1', 't2'])">Hello world!</h2>
  <h2 :class="vclsx(['t1', 't2', { t3: true }])">Hello world!</h2>
  <h2 :class="vclsx(['t1', 't2', ['fs', { t3: true }]])">Hello world!</h2>
  <hr />
  <button v-class-module="{ active: bg }" @click="bg = !bg">background</button>
  <button v-class-module="{ active: italic }" @click="italic = !italic">Italic</button>
  <button v-class-module="{ active: green }" @click="green = !green">Green</button>
</template>
<style module>
  * {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell,
      'Open Sans', 'Helvetica Neue', sans-serif;
  }

  button {
    color: #333;
    margin-right: 4px;
  }

  .active {
    background: #333;
    color: #fff;
  }

  .fs {
    font-size: 3rem;
  }

  .t1 {
    color: green;
  }

  .t2 {
    background: yellow;
  }

  .t3 {
    font-style: italic;
  }
</style>
// string
vclsx('class1', 'class2', 'class3');
//=> class1 class2 class3

// array
vclsx(['class1', 'class2', 'class3']);
//=> class1 class2 class3

// object
vclsx({ class1: true, class2: false });
//=> class1

// string, object, array
vclsx('class1', { class2: true, class3: false }, ['class4', { class5: true }]);
//=> class1 class2 class4 class5

Input

The vclsx function can take multiple arguments of type arrays, strings, and objects

Priority

When using v-binding and function at the same time to call the css class module, the class generated from: class (function) is always called last

Example

<template>
  <h2 :class="vclsx('primary')" v-class-module="'title'">Hello</h2>
</template>
<style module>
.title {
  color: green;
}

.primary {
  color: tomato;
}
<!-- result -->
<h2 class="_title_bbnon_3 _primary_bbnon_11">Hello</h2>

License MIT