Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lukasborawski/vue-use-variant

Vue.js CSS class variant resolver. Presented as a handy composable.
https://github.com/lukasborawski/vue-use-variant

composable css styles tailwind vue vue3 vuejs

Last synced: about 2 months ago
JSON representation

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

Awesome Lists containing this project

README

        

## Vue Use Variant

npm version
npm version
npm version
npm version

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

Read the story behind this package [here](https://itnext.io/tailwind-class-madness-never-again-75ec6ebfd3a0).

**you can use it with any other framework as well*

### Install

---

Install the package:

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

### Motivation

---

We all know that systems like [Tailwind](https://tailwindcss.com) 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:

```vue
Submit
```

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

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

```vue

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,
),
}
},
})

```

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

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

---

You can also use it with **props**.

```vue

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),
}
},
})

```

Use as an Array.

```vue

import { buttonVariantsRef } from './variants.ts'

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

return {
buttonVariant: defineVariant(['button', 'buttonPrimary'], buttonVariantsRef),
}
},
})

```

Use straight without variant definitions.

```vue

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

return {
buttonVariant: defineVariant({ shadow: 'shadow' }),
}
},
})

```

Finally, you can define your variants as composable argument.

```vue

import { buttonVariantsRef } from './variants.ts'

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

return {
buttonVariant: defineVariant({ buttonPrimary: true }),
}
},
})

```

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 o**ther 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](src/types.ts) for API definitions.

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

**Support**: Want to support? [Buy me a coffee](https://www.buymeacoffee.com/lukas.borawski).

Buy Me a Coffee