Simple composable for Vue.js* to handle long and ugly CSS class chaining
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
.
Now let’s see how we can use it with some real component example.
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.
Use straight without variant definitions.
Finally, you can define your variants as composable argument.
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.