Categories
Top Vue Packages

Top Vue Packages for Adding Async Computed Properties, and Carousel

Vue.js is an easy to use web app framework that we can use to develop interactive front end apps.

In this article, we’ll look at how the best packages for adding pagination buttons, async computed properties, and carousels.

vue-async-computed

Vue components can’t have computed properties that are async.

However, with the addition of the vue-async-computed package, we can.

To install it, we run:

npm i vue-async-computed

Then we can use it by writing:

main.js

import Vue from "vue";
import App from "./App.vue";
import AsyncComputed from "vue-async-computed";

Vue.use(AsyncComputed);
Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

App.vue

`<template>
  <div>
    <div>{{product}}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      x: 2,
      y: 3
    };
  },
  asyncComputed: {
    product() {
      return new Promise(resolve =>
        setTimeout(() => resolve(this.x * this.y), 1000)
      );
    }
  }
};
</script>

We registered the plugin that came with the package,.

Then in our component, we return a promise that resolves to values derived from other states.

In our example, we just take the product of them and set that as the resolved value of the promise. We can set which properties to watch explicitly.

To do that, we write:

<template>
  <div>
    <div>{{product}}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      x: 2,
      y: 3
    };
  },
  asyncComputed: {
    product: {
      get() {
        return new Promise(resolve =>
          setTimeout(() => resolve(this.x * this.y), 1000)
        );
      },
      watch: ["y"]
    }
  }
};
</script>

Then this,y ‘s value is watched and its new value recomputed if it changed.

vue-flicking

The vue-flicking library lets us add a carousel to our Vue app easily.

To use it, first we install it by running:

npm i @egjs/vue-flicking

Then we can use it by writing:

main.js

import Vue from "vue";
import App from "./App.vue";
import VueFlicking from "@egjs/vue-flicking";

Vue.use(VueFlicking);
Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

App.vue

<template>
  <div>
    <flicking
      :options="{ gap: 10, moveType: 'freeScroll' }"
      :tag="'div'"
      :viewportTag="'div'"
      @need-panel="e => {}"
      @move-end="e => {}"
    >
      <div v-for="n in 10" :key="n">slide {{n}}</div>
    </flicking>
  </div>
</template>

<script>
import { Flicking } from "@egjs/vue-flicking";

export default {
  components: {
    Flicking: Flicking
  }
};
</script>

We set the gap between slides in pixels.

moveType is how the slides move.

tag is the tag to render for the slider.

viewportTag is the tag for the viewport.

We can also add plugins that are separate from the package.

To do that, we write:

<template>
  <div>
    <flicking
      :options="{ gap: 10, moveType: 'freeScroll' }"
      :tag="'div'"
      :viewportTag="'div'"
      :plugins="plugins"
      @need-panel="e => {}"
      @move-end="e => {}"
    >
      <div v-for="n in 10" :key="n">slide {{n}}</div>
    </flicking>
  </div>
</template>

<script>
import { Flicking } from "@egjs/vue-flicking";
import { Fade, AutoPlay } from "@egjs/flicking-plugins";

export default {
  components: {
    Flicking: Flicking
  },
  data() {
    return {
      plugins: [new Fade(), new AutoPlay(2000, "NEXT")]
    };
  }
};
</script>

We installed the plugin package with:

npm i @egjs/flicking-plugins

Then we added the Fade and AutoPlay plugins to add a fade effect and autoplay respectively.

2000 is the number of milliseconds before we move to the next slide.

vue-cookie-accept-decline

The vue-cookie-accept-decline package lets us add a banner for displaying a cookies message that’s seen frequently on websites.

To install it, we run:

npm i vue-cookie-accept-decline

Then we add the CSS to index.html between the head tags:

<link
  rel="stylesheet"
  type="text/css"
  href="https://unpkg.com/vue-cookie-accept-decline@5.2.3/dist/vue-cookie-accept-decline.css"
/>

Then we register the component by writing:

import Vue from "vue";
import App from "./App.vue";
import VueCookieAcceptDecline from "vue-cookie-accept-decline";
Vue.component("vue-cookie-accept-decline", VueCookieAcceptDecline);
Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

in main.js

Next, we use it by writing:

<template>
  <div>
    <vue-cookie-accept-decline
      :ref="'myPanel1'"
      :elementId="'myPanel1'"
      :debug="false"
      :position="'bottom-left'"
      :type="'floating'"
      :disableDecline="false"
      :transitionName="'slideFromBottom'"
      :showPostponeButton="false"
      @status="cookieStatus"
      @clicked-accept="cookieClickedAccept"
      @clicked-decline="cookieClickedDecline"
    >
      <div slot="postponeContent">&times;</div>

      <div slot="message">
        We use cookies.
        <a href="https://example.com/" target="_blank">Learn More...</a>
      </div>

      <div slot="declineContent">OPT OUT</div>

      <div slot="acceptContent">GOT IT!</div>
    </vue-cookie-accept-decline>
  </div>
</template>

<script>
export default {
  methods: {
    cookieStatus() {},
    cookieClickedAccept() {},
    cookieClickedDecline() {}
  }
};
</script>

The setting of the button will be stored in local stored in local storage so it won’t show up again.

We’ll see the message in the message slot displayed.

The acceptContent slot has the accept button.

The declineContent slot has the decline button.

There are also events emitting for each button clicked.

clicked-accept is emitted when the accept button is clicked.

clicked-decline is emitted when the decline button is clicked.

status is emitted when the status is changed.

Conclusion

The vue-async-computed package us add async computed properties.

vue-cookie-accept-decline lets us add an accept cookie message.

vue-flicking lets us add a slideshow.

Categories
Top Vue Packages

Top Vue Packages for Adding a Kanban Board and Pagination Buttons

Vue.js is an easy to use web app framework that we can use to develop interactive front end apps.

In this article, we’ll look at how the best packages for adding a kanban board and adding pagination buttons.

vue-kanban

We can add a kanban board to our Vue app with the vue-kanban component.

The items in the kanban board can be dragged and dropped to different columns according to their status.

To install it, we run:

npm i vue-kanban

Then we can use it by writing:

main.js

import Vue from "vue";
import App from "./App.vue";
import vueKanban from "vue-kanban";

Vue.use(vueKanban);
Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

styles.css

ul.drag-list,
ul.drag-inner-list {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

.drag-container {
  max-width: 1000px;
  margin: 20px auto;
}

.drag-list {
  display: flex;
  align-items: flex-start;
}
[@media](http://twitter.com/media "Twitter profile for @media") (max-width: 500px) {
  .drag-list {
    display: block;
  }
}

.drag-column {
  flex: 1;
  margin: 0 10px;
  position: relative;
  background: rgba(0, 0, 0, 0.2);
  overflow: hidden;
}
@media (max-width: 500px) {
  .drag-column {
    margin-bottom: 30px;
  }
}
.drag-column h2 {
  font-size: 0.8rem;
  margin: 0;
  text-transform: uppercase;
  font-weight: 600;
}

.drag-column-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px;
}

.drag-inner-list {
  min-height: 50px;
  color: white;
}

.drag-item {
  padding: 10px;
  margin: 10px;
  height: 100px;
  background: gray;
}
.drag-item.is-moving {
  transform: scale(1.5);
  background: lightgray;
}

.drag-header-more {
  cursor: pointer;
}

.drag-options {
  position: absolute;
  top: 44px;
  left: 0;
  width: 100%;
  height: 100%;
  padding: 10px;
  transform: translateX(100%);
  opacity: 0;
}
.drag-options.active {
  transform: translateX(0);
  opacity: 1;
}
.drag-options-label {
  display: block;
  margin: 0 0 5px 0;
}
.drag-options-label input {
  opacity: 0.6;
}
.drag-options-label span {
  display: inline-block;
  font-weight: 400;
  margin-left: 5px;
}

.gu-mirror {
  position: fixed !important;
  margin: 0 !important;
  z-index: 9999 !important;
  opacity: 0.8;
  list-style-type: none;
}

.gu-hide {
  display: none !important;
}

.gu-unselectable {
  -webkit-user-select: none !important;
  -moz-user-select: none !important;
  -ms-user-select: none !important;
  user-select: none !important;
}

.gu-transit {
  opacity: 0.2;
}

The styles are derived from the starter styles at https://github.com/BrockReece/vue-kanban/blob/master/src/assets/kanban.css.

App.vue

<template>
  <div>
    <kanban-board :stages="stages" :blocks="blocks"></kanban-board>
  </div>
</template>

<script>
export default {
  data() {
    return {
      stages: ["not-started", "in-progress", "needs-review", "approved"],
      blocks: [
        {
          id: 1,
          status: "not-started",
          title: "Test"
        }
      ]
    };
  }
};
</script>

<style src='./styles.css'>
</style>

We need the CSS to style the kanban board since it comes with no styles.

In App.vue , we pass in the stages which is an array of strings.

blocks is an array of objects with the id , status , and title .

vuejs-paginate

vuejs-paginate is a pagination buttons component.

To install it, we run:

npm i vuejs-paginate

Then we can use it by writing:

main.js

import Vue from "vue";
import App from "./App.vue";
import Paginate from "vuejs-paginate";
Vue.component("paginate", Paginate);
Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

App.vue

<template>
  <div>
    <div>page {{pageNum}}</div>
    <paginate
      :page-count="20"
      :click-handler="onClick"
      :prev-text="'Prev'"
      :next-text="'Next'"
      :container-class="'className'"
    ></paginate>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pageNum: 0
    };
  },
  methods: {
    onClick(pageNum) {
      this.pageNum = pageNum;
    }
  }
};
</script>

We register the plugin in main.js .

Then we add the paginate component by passing a few props to enable navigation.

page-count has the page count.

click-handler is a function that’s run when a pagination button is clicked.

prev-text has the text for the previous button.

next-text has the text for the next button.

container-class has the class name for the pagination button container.

Now we’ll see a list of buttons that we can click to go to the page we want.

Conclusion

We can use vue-kanban to add a kanban board to our app.

The vuejs-paginate plugin lets us add pagination buttons for anything.

Categories
Top Vue Packages

Top Vue Packages for Adding Walkthroughs, Making HTTP Requests, and Validating Forms

Vue.js is an easy to use web app framework that we can use to develop interactive front end apps.

In this article, we’ll look at how the best packages for adding walkthroughs, making HTTP requests, and validating forms.

vue-tour

We can use vue-tour to create walkthroughs for our app.

To use it, we write:

npm i vue-tour

Then we can register the components by writing:

import Vue from "vue";
import App from "./App.vue";
import VueTour from "vue-tour";

require("vue-tour/dist/vue-tour.css");
Vue.use(VueTour);

Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

We also imported the styles.

Then we can use it by writing:

<template>
  <div>
    <div id="v-step-0">step 0.</div>
    <div class="v-step-1">step 1</div>
    <div data-v-step="2">step 2</div>
    <v-tour name="myTour" :steps="steps"></v-tour>
  </div>
</template>

<script>
export default {
  data() {
    return {
      steps: [
        {
          target: "#v-step-0",
          header: {
            title: "get started"
          },
          content: `hello <strong>world</strong>!`
        },
        {
          target: ".v-step-1",
          content: "step 1"
        },
        {
          target: '[data-v-step="2"]',
          content: "Step 2",
          params: {
            placement: "top"
          }
        }
      ]
    };
  },
  mounted() {
    this.$tours["myTour"].start();
  }
};
</script>

We have the list of steps in the template.

Then we have the steps array with the steps that target the elements that are on the page.

This way, the steps are displayed near the target element.

In each step, we can set the header, content, and placement of the steps.

Then to start the tour, we use the start method at the bottom of the page as we did.

vue-resource

vue-resource is an HTTP client that’s made as a Vue plugin.

To install it, we run:

npm i vue-resource

Then we can use it by writing:

import Vue from "vue";
import App from "./App.vue";
const VueResource = require("vue-resource");

Vue.use(VueResource);

Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

to register the plugin.

Now we have access to the $http property in our component:

<template>
  <div>{{data.name}}</div>
</template>

<script>
export default {
  data() {
    return {
      data: {}
    };
  },
  async mounted() {
    const { body } = await this.$http.get("https://api.agify.io/?name=michael");
    this.data = body;
  }
};
</script>

It’s promised based so we can use async and await.

We can set common options that are used throughout the app by writing:

Vue.http.options.root = '/root';
Vue.http.headers.common['Authorization'] = 'Basic YXBpOnBhc3N3b3Jk';

We set the root URL for all requests and the Authorization header that’s used everywhere.

vue-form

vue-form is a form validation library for Vue apps.

We can use it by installing the package:

npm i vue-form

Then we can use it by writing:

main.js

import Vue from "vue";
import App from "./App.vue";
import VueForm from "vue-form";

Vue.use(VueForm);

Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

App.vue

<template>
  <div>
    <vue-form :state="formState" [@submit](http://twitter.com/submit "Twitter profile for @submit").prevent="onSubmit">
      <validate tag="label">
        <span>Name *</span>
        <input v-model="model.name" required name="name">

<field-messages name="name">
          <div>Success!</div>
          <div slot="required">name is required</div>
        </field-messages>
      </validate>
    </vue-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      model: {
        name: ""
      },
      formState: {}
    };
  },
  methods: {
    onSubmit() {
      if (this.formState.$invalid) {
        return;
      }
    }
  }
};
</script>

We registered the component in main.js .

Then in the component, we used the vue-form component.

The state prop is set to the formState object, which will be updated when the form state change.

submit.prevent has the submit handler with e.preventDefault called on it.

v-model binds to the model.

The field-messages component displays the form validation messages.

required indicates that it’s required.

In the onSubmit method, we check the formState.$invalid to check whether the form is invalid.

If it’s invalid, we don’t proceed with submission.

Other form state properties include $name with the name attribute value of the field.

$valid to see if the form or field is valid.

$focused to check that the form or field is focused.

$dirty to see if the form or field has been manipulated.

It also comes with other validators like email, URL, number, min and max length, and more.

Conclusion

We can add a walkthrough tour to our Vue app with the vue-tour package.

vue-resource is a promise-based HTTP client that’s made for Vue apps.

vue-form is a useful form validation plugin for Vue apps.

Categories
Top Vue Packages

Top Vue Packages for Adding Avatar, Slides, and Tooltips

Vue.js is an easy to use web app framework that we can use to develop interactive front end apps.

In this article, we’ll look at how the best packages for adding avatars, swiping, and tooltips.

vue-avatar

vue-avatar lets us add an avatar to our Vue app easily.

To use it, we run:

npm i vue-avatar

Then we can use it by importing the Avatar component:

<template>
  <div class="demo">
    <avatar username="Jane Smith"></avatar>
  </div>
</template>

<script>
import Avatar from "vue-avatar";

export default {
  name: "app",
  components: {
    Avatar
  }
};
</script>

We just set the name as the value of the username property to display a circle with the initials.

To add an image, we can use the src prop:

<template>
  <div class="demo">
    <avatar src="http://placekitten.com/200/200"></avatar>
  </div>
</template>

<script>
import Avatar from "vue-avatar";

export default {
  name: "app",
  components: {
    Avatar
  }
};
</script>

It also supports changing many other options like color, background, size, roundedness, custom styling, and more.

vue-awesome-swiper

The vue-awesome-swiper component lets us add a slider to add that we can swipe to our Vue app.

It’s great for creating carousels and slide shows.

To install it, we run:

npm i vue-awesome-swiper

Then we can use it by registering the plugin and importing the styles:

main.js

import Vue from "vue";
import App from "./App.vue";
import "swiper/css/swiper.css";
import VueAwesomeSwiper from "vue-awesome-swiper";
Vue.use(VueAwesomeSwiper);

Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

Then we can use it in our component by writing:

<template>
  <swiper ref="mySwiper" :options="swiperOptions">
    <swiper-slide v-for="n in 10" :key="n">Slide {{n}}</swiper-slide>
    <div class="swiper-pagination" slot="pagination"></div>
  </swiper>
</template>

<script>
export default {
  name: "carrousel",
  data() {
    return {
      swiperOptions: {
        pagination: {
          el: ".swiper-pagination"
        }
      }
    };
  },
  computed: {
    swiper() {
      return this.$refs.mySwiper.$swiper;
    }
  },
  mounted() {
    this.swiper.slideTo(3, 1000, false);
  }
};
</script>

swiper-slide has the slides.

swiperOptions has the options. We set the button for changing slide to have the class swiper-pagination .

The swiper object is available in the this.$refs.mySwiper.$swiper property.

Then we can call methods on it like the slideTo method to slide to the slide with the given index.

1000 is the delay.

The 3rd argument indicates that we don’t want to run callbacks.

Alternatively, we can use it as a directive.

For instance, we can write:

<template>
  <div v-swiper:mySwiper="swiperOptions">
    <div class="swiper-wrapper">
      <div class="swiper-slide" :key="n" v-for="n in 10">
        <img src="http://placekitten.com/200/200">
        <p>slide {{n}}</p>
      </div>
    </div>
    <div class="swiper-pagination"></div>
  </div>
</template>

<script>
export default {
  name: "carrousel",
  data() {
    return {
      swiperOptions: {
        pagination: {
          el: ".swiper-pagination"
        }
      }
    };
  }
};
</script>

We used the v-swiper:mySwiper directive to create our swiper with the swiperOptions object.

Now we get the same slider, but without sliding automatically to the given slide.

The classes must be set so that we get the right items on the slide.

It can handle many events like clicking sides, transitions, and more.

v-tooltip

To add a tooltip to our Vue app, we can use the v-tooltip component.

To install it, we run:

npm i v-tooltip

Then we can use it by writing:

import Vue from "vue";
import App from "./App.vue";
import VTooltip from "v-tooltip";

Vue.use(VTooltip);

Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

to register the components and directives.

Then in our component, we can write:

<template>
  <div>
    <button v-tooltip="`You have ${count} messages`">click me</button>
  </div>
</template>

<script>
export default {
  data() {
    return { count: 100 };
  }
};
</script>

to see a tooltip when we hover over the button.

The v-tooltip component lets us add the tooltip.

We can also change the position with some modifiers:

<template>
  <div>
    <button v-tooltip.right="`You have ${count} messages`">click me</button>
  </div>
</template>

<script>
export default {
  data() {
    return { count: 100 };
  }
};
</script>

We can also add classes so that we can style the tooltip:

<template>
  <div>
    <button
      v-tooltip.right="{ content: `You have ${count} messages`, classes: ['a', 'b'] }"
    >click me</button>
  </div>
</template>

<script>
export default {
  data() {
    return { count: 100 };
  }
};
</script>

We have the classes property in the object. content has the content.

Conclusion

The vue-avatar package lets us add an avatar easily.

The v-tooltip package is a great package for adding tooltips into our app.

vue-awesome-swiper lets us add slides to our app.

Categories
Top Vue Packages

Top Vue Packages for Adding Draggable Lists and Text Editing Capabilities

Vue.js is an easy to use web app framework that we can use to develop interactive front end apps.

In this article, we’ll look at how the best packages for adding draggable lists into a Vue app.

vuedraggable

The vuedraggable package is a package that we can use to add draggable lists to our app.

To install it, we run:

npm i vuedraggable

Then we can use it by writing:

<template>
  <div class="demo">
    <draggable v-model="arr" group="people" @start="drag=true" @end="drag=false">
      <div v-for="element in arr" :key="element.id">{{element.name}}</div>
    </draggable>
  </div>
</template>

<script>
import draggable from "vuedraggable";

export default {
  components: {
    draggable
  },
  data() {
    return {
      arr: [
        {
          id: 1,
          name: "foo"
        },
        {
          id: 2,
          name: "bar"
        },
        {
          id: 3,
          name: "baz"
        }
      ]
    };
  }
};
</script>

We registered the draggable component.

Then we nest out list inside it.

We also set our start and end handlers, which are run when we start and stop dragging respectively.

v-model binds to the latest value of arr .

Now we can drag and drop the names.

We can also use it with transition-group :

<template>
  <div class="demo">
    <draggable v-model="arr" group="people" @start="drag = true" @end="drag = false">
      <transition-group name="list">
        <div v-for="element in arr" :key="element.id">{{element.name}}</div>
      </transition-group>
    </draggable>
  </div>
</template>

<script>
import draggable from "vuedraggable";

export default {
  components: {
    draggable
  },
  data() {
    return {
      arr: [
        {
          id: 1,
          name: "foo"
        },
        {
          id: 2,
          name: "bar"
        },
        {
          id: 3,
          name: "baz"
        }
      ]
    };
  }
};
</script>

<style>
.list-enter-active,
.list-leave-active {
  transition: all 1s;
}
.list-enter, .list-leave-to {
  opacity: 0;
  transform: translateY(30px);
}
</style>

We just put it inside the draggable component to add transition effects when dragging and dropping.

We can add a slot for the footer or header.

For instance, we can write:

<template>
  <div class="demo">
    <draggable v-model="arr" group="people" @start="drag = true" @end="drag = false">
      <p slot="header">header</p>
      <div v-for="element in arr" :key="element.id">{{element.name}}</div>
      <p slot="footer">footer</p>
    </draggable>
  </div>
</template>

<script>
import draggable from "vuedraggable";

export default {
  components: {
    draggable
  },
  data() {
    return {
      arr: [
        {
          id: 1,
          name: "foo"
        },
        {
          id: 2,
          name: "bar"
        },
        {
          id: 3,
          name: "baz"
        }
      ]
    };
  }
};
</script>

There are many other options that we can use to configure it, including changing the tag of the rendered elements, setting the list, watching move events, and more.

TinyMCE Vue

TinyMCE has a Vue version, so we can use the TinyMCE rich text editor without hassle.

To install it, we run:

npm install --save @tinymce/tinymce-vue

Then we can use it by writing:

<template>
  <div class="demo">
     <editor
       api-key="no-api-key"
       :init="{
         height: 500,
         menubar: false,
         plugins: [
           'advlist autolink lists link image charmap print preview anchor',
           'searchreplace visualblocks code fullscreen',
           'insertdatetime media table paste code help wordcount'
         ],
         toolbar:
           'undo redo | formatselect | bold italic backcolor |
           alignleft aligncenter alignright alignjustify |
           bullist numlist outdent indent | removeformat | help'
       }"
     />
  </div>
</template>

<script>
import Editor from '@tinymce/tinymce-vue'

export default {
   name: 'app',
   components: {
     'editor': Editor
   }
 }
</script>

We just import the component and register it.

Then we specify our plugins and other options.

We can add v-model to bind the inputted value to a state variable:

<template>
  <div class="demo">
    <editor
      api-key="no-api-key"
      :init="{
         height: 200,
         menubar: false,
         plugins: [
           'advlist autolink lists link image charmap print preview anchor',
           'searchreplace visualblocks code fullscreen',
           'insertdatetime media table paste code help wordcount'
         ],
         toolbar:
           'undo redo | formatselect | bold italic backcolor |
           alignleft aligncenter alignright alignjustify |
           bullist numlist outdent indent | removeformat | help'
       }"
      v-model="content"
    />
    <p v-html="content"></p>
  </div>
</template>

<script>
import Editor from "@tinymce/tinymce-vue";

export default {
  name: "app",
  components: {
    editor: Editor
  },
  data() {
    return {
      content: ""
    };
  }
};
</script>

We have the v-model prop bound to content. And we added the p element with the v-html directive set to content to see what we typed.

Now we get a text editor in our app without much hassle.

Conclusion

We can use the vuedraggable library to lets us make list items draggable and droppable.

The Vue version of the TinyMCE is full of features and it’s easy to add.