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.

Categories
Top Vue Packages

Top Vue Packages for Adding Drag and Drop and Watching Resizing

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 drag and drop and watching elements resizing to our Vue app.

VueDraggableResizable

VueDraggableResizable is an easy to use the package to let us make comments draggable and resizable.

To use it, we install it by running:

npm i vue-draggable-resizable

Then we can use it by writing:

import Vue from "vue";
import App from "./App.vue";
import VueDraggableResizable from "vue-draggable-resizable";

import "vue-draggable-resizable/dist/VueDraggableResizable.css";

Vue.component("vue-draggable-resizable", VueDraggableResizable);
Vue.config.productionTip = false;

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

to register the component and add the styles.

Then in our component, we write:

<template>
  <div id="app">
    <div id="draggable">
      <vue-draggable-resizable
        :w="100"
        :h="100"
        [@dragging](http://twitter.com/dragging "Twitter profile for @dragging")="onDrag"
        [@resizing](http://twitter.com/resizing "Twitter profile for @resizing")="onResize"
        :parent="true"
      >
        <p>drag me ({{x}},{{y}})</p>
      </vue-draggable-resizable>
    </div>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      width: 0,
      height: 0,
      x: 0,
      y: 0
    };
  },
  methods: {
    onResize(x, y, width, height) {
      this.x = x;
      this.y = y;
      this.width = width;
      this.height = height;
    },
    onDrag(x, y) {
      this.x = x;
      this.y = y;
    }
  }
};
</script>

<style>
#draggable {
  height: 500px;
  width: 500px;
  position: relative;
}
</style>

We have a 500px by 500px div that we can drag and resize thanks to the vue-draggable-resizable component.

The x and y coordinates change and can be listened to with the onDrag handler, which has the latest coordinates in the parameters.

Likewise, we can do the same for resize:

<template>
  <div id="app">
    <div id="draggable">
      <vue-draggable-resizable
        :w="100"
        :h="100"
        [@dragging](http://twitter.com/dragging "Twitter profile for @dragging")="onDrag"
        [@resizing](http://twitter.com/resizing "Twitter profile for @resizing")="onResize"
        :parent="true"
      >
        <p>drag me ({{x}},{{y}})</p>
        <p>size ({{width}}x{{height}})</p>
      </vue-draggable-resizable>
    </div>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      width: 0,
      height: 0,
      x: 0,
      y: 0
    };
  },
  methods: {
    onResize(x, y, width, height) {
      this.x = x;
      this.y = y;
      this.width = width;
      this.height = height;
    },
    onDrag(x, y) {
      this.x = x;
      this.y = y;
    }
  }
};
</script>

<style>
#draggable {
  height: 500px;
  width: 500px;
  position: relative;
}
</style>

The onResize method for the coordinates and the size.

Props

It comes with other props like class-name to set the class name.

class-name-draggable to add styles with draggable is enabled.

class-name-resizable lets us style the element when it’s resized.

class-name-resizing lets us add styles when it’s resizing.

class-name-handle styles the handles.

disable-user-select lets us disable user to select.

There are many other props that we can use to style and handle events.

The initial x and y coordinates can also be set with the props of the same name.

vue-resize

We can watch elements being resized with the vue-resize package.

To use it, we install it by running:

npm i vue-resize

Then we register the component by adding:

import Vue from "vue";
import App from "./App.vue";
import "vue-resize/dist/vue-resize.css";
import VueResize from "vue-resize";

Vue.use(VueResize);

Vue.config.productionTip = false;

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

It comes with styles and the resize-observer component.

Then in our component, we add:

<template>
  <div class="demo">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eu facilisis lorem. In arcu nisl, vulputate id diam eget, ultricies aliquet neque. Phasellus sapien lacus, consectetur ut nisi quis, mattis tincidunt ante. Aliquam ultrices nisl ornare augue laoreet, a vehicula libero consequat. Sed aliquam aliquet turpis, ut sodales elit sodales sit amet. Donec ullamcorper velit neque, in maximus odio tempus eu. Praesent ullamcorper, nibh sodales maximus feugiat, erat tellus condimentum sem, ac convallis tellus diam suscipit tellus. Proin egestas tellus neque. Vestibulum porttitor tempus tellus sit amet volutpat. Sed urna nibh, molestie non pulvinar in, accumsan nec nisl.</p>
    <resize-observer @notify="handleResize"/>
  </div>
</template>

<script>
export default {
  methods: {
    handleResize({ width, height }) {
      console.log("resized", width, height);
    }
  }
};
</script>

<style scoped>
.demo {
  position: relative;
}
</style>

We set the notify handler to handleResize to watch for size changes.

The width and height are in the handler for us to use.

Conclusion

We can use the vue-draggable-resizable package to let us create elements that are draggable and resizable.

The vue-resize package lets us watch for elements being resized.