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

vue-use-variant

v0.1.2

Published

Vue.js CSS class variant resolver. Presented as handy composable.

Downloads

241

Readme

Vue Use Variant

Simple composable for Vue.js* to handle long and ugly CSS class chaining.

Read the story behind this package here.

*you can use it with any other framework as well

Install


Install the package:

$ npm i vue-use-variant --save
# or
$ yarn add vue-use-variant

Motivation


We all know that systems like Tailwind are awesome, but we also know that defining styles by using utility classes can be hard ... Let's say you are using Tailwind and you want to style some button with provided classes. Probably the definition will look something like this:

<button
  class="
    p-2 
    bg-blue 
    rounded
    text-md 
    border-0
    font-bold 
    text-white 
    hover:bg-black 
    hover:text-gray
    hover:opacity-80
    hover:transition-all 
  "
  type="button">Submit</button>

... and this is just a button. Imagine whole markup for even tiny component. Readability of it will be - easy talking - not so great. So what problems we're facing here:

  • very long class definition
  • poor readability
  • lack of scalability
  • hard to maintain

Usage


To resolve these problems you can try useVariant composable.

First define some variants. You can crate regular JSON object for it or use Vue Ref.

import { ref } from 'vue'

export const buttonVariants = {
  button: 'font-bold rounded border-0 bg-blue hover:opacity-80',
  buttonPrimary: 'p-4 text-lg',
  buttonSecondary: 'p-2 text-md',
}

// or use with Vue Ref (Composition API)
export const buttonVariantsRef = ref(buttonVariants)

Now let's see how we can use it with some real component example.

<template>
  <button :class="buttonVariant" type="button" />
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { buttonVariants, buttonVariantsRef } from './variants.ts'
import { useVariant, UseVariant } from 'vue-use-variant'

export default defineComponent({
  name: 'Button',
  setup() {
    const { defineVariant } = useVariant() as UseVariant

    return {
      buttonVariant: defineVariant(
        ref({
          button: true,
          buttonPrimary: true,
        }),
        buttonVariantsRef,
      ),
    }
  },
})
</script>

As a result your button will get this set of classes:

font-bold rounded border-0 bg-blue hover:opacity-80 p-4 text-lg

You can also use it with props.

<script lang="ts">
import { buttonVariantsRef } from './variants.ts'

export default defineComponent({
  name: 'Button',
  props: {
    button: {
      type: Boolean,
      default: true,
    },
    buttonVariant: {
      type: String,
      default: 'buttonPrimary',
    },
  },
  setup() {
    const { defineVariant } = useVariant() as UseVariant

    return {
      buttonVariant: defineVariant({ ...props }, buttonVariantsRef),
    }
  },
})
</script>

Use as an Array.

<script lang="ts">
import { buttonVariantsRef } from './variants.ts'

export default defineComponent({
  name: 'Button',
  setup() {
    const { defineVariant } = useVariant() as UseVariant

    return {
      buttonVariant: defineVariant(['button', 'buttonPrimary'], buttonVariantsRef),
    }
  },
})
</script>

Use straight without variant definitions.

<script lang="ts">
export default defineComponent({
  name: 'Button',
  setup() {
    const { defineVariant } = useVariant() as UseVariant

    return {
      buttonVariant: defineVariant({ shadow: 'shadow' }),
    }
  },
})
</script>

Finally, you can define your variants as composable argument.

<script lang="ts">
import { buttonVariantsRef } from './variants.ts'

export default defineComponent({
  name: 'Button',
  setup() {
    const { defineVariant } = useVariant(buttonVariantsRef) as UseVariant

    return {
      buttonVariant: defineVariant({ buttonPrimary: true }),
    }
  },
})
</script>

Of course, you can combine and mix variants and use them as a global variations for your base, global and reusable components. It's super easy and convenient. You can of course use it with any other UI System like for example Boostrap or Vuetify. And maybe it was built for vue you can use it for any other frameworks like React.


Demo

Want to check or test it in action? Check out the simple app in the demo folder.


API Reference: Check out the types for API definitions.

Contribution: Please add Pull Request to introduce some changes or fixes.

Support: Want to support? Buy me a coffee.