Javascript Vue

Simple composable for Vue.js* to handle long and ugly CSS class chaining3 min read

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 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.

GitHub

View Github

Pin It on Pinterest

Generated by Feedzy