Giter Club home page Giter Club logo

vue-use-variant's Introduction

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.

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

Buy Me a Coffee

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.