Categories
Vuetify

Vuetify — Paddings and Margins

Vuetify is a popular UI framework for Vue apps.

In this article, we’ll look at how to work with the Vuetify framework.

Flex align-content

We can add the align-content classes to change the alignment of flexbox content.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <v-card
          class="d-flex align-content-start flex-wrap"
          color="grey lighten-2"
          flat
          tile
          min-height="200"
        >
          <v-card v-for="n in 10" :key="n" class="pa-2" outlined tile>item</v-card>
        </v-card>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

We have the align-content-start class to align the content to the left.

There are other classes for other alignments.

Flex Grow and Shrink

Vuetify has the flex-{condition}-{value} classes to grow and shrink the items.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="2" class="flex-grow-0 flex-shrink-0">
        <v-card class="pa-2" outlined tile>2 column wide</v-card>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

to create a column 2 columns wide and use the flex-grow-0 and flex-shrink-0 classes to fill the container.

Float

We can apply a custom float with Vuetify classes.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <div>
          <div class="float-left">Float left</div>
          <br />
          <div class="float-right">Float right</div>
          <br />
          <div class="float-none">Don't float</div>
        </div>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

We have float-left class to float left.

float-right floats right.

float-none disables float.

Responsive

Floats can be applied with some breakpoints.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <div>
          <div class="float-sm-left">sm</div>
          <br />
          <div class="float-md-left">md</div>
          <br />
          <div class="float-lg-left">lg</div>
          <br />
          <div class="float-xl-left">xl</div>
          <br />
        </div>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

We have the breakpoint in the class name to only apply the float if the breakpoint is the given one or wider.

Spacing

Spacing can be changed with classes in the following format:

{property}{direction}-{size}

property can be:

  • m – applies margin
  • p – applies padding

direction can be:

  • t – applies the spacing for margin-top and padding-top
  • b – applies the spacing for margin-bottom and padding-bottom
  • l – applies the spacing for margin-left and padding-left
  • r – applies the spacing for margin-right and padding-right
  • s – applies the spacing for margin-left/padding-left (in LTR mode) and margin-right/padding-right (in RTL mode)
  • e – applies the spacing for margin-right/padding-right (in LTR mode) and margin-left/padding-left (in RTL mode)
  • x – applies the spacing for both *-left and *-right
  • y – applies the spacing for both *-top and *-bottom
  • a – applies the spacing for the property in all directions

size is 0 to 16 to set the size from 0 to 64px.

n1 to n16 set the margin to -4px to -64px.

We can use mx-auto to set the margin size automatically:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <v-card class="mx-auto" color="white" width="200px">
          <v-card-text>Centered</v-card-text>
        </v-card>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

We can change the padding by writing:

<template>
  <v-container>
    <v-row class="text-center">
      <v-card class="pa-md-4 mx-lg-auto" color="white" width="250px">
        <v-card-text>text</v-card-text>
      </v-card>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

We have the pa-md-4 class to change the padding for md breakpoint and up to 16px.

Conclusion

Vuetify provides us with many classes for changing margins and padding.

Categories
Vuetify

Vuetify — Flexbox

Vuetify is a popular UI framework for Vue apps.

In this article, we’ll look at how to work with the Vuetify framework.

Flex Justify

We can justify-content in a flexbox container with the classes provided by Vuetify.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <v-card
          v-for="j in justify"
          :key="j"
          :class="`d-flex justify-${j} mb-2`"
          color="grey lighten-2"
          flat
          tile
        >
          <v-card v-for="n in 3" :key="n" class="pa-2" outlined tile>item</v-card>
        </v-card>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({
    justify: ["start", "end", "center", "space-between", "space-around"],
  }),
};
</script>

to set the justification of our content to what we want.

There are also responsive variants of the classes.

Flex Align

Vuetify also provides us with flex align classes to align content.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <v-card
          v-for="a in align"
          :key="a"
          :class="`d-flex align-${a} mb-6`"
          flat
          height="100"
          tile
        >
          <v-card v-for="n in 3" :key="n" class="pa-2" outlined tile>item</v-card>
        </v-card>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({
    align: ["start", "end", "center", "baseline", "stretch"],
  }),
};
</script>

to align our content our way.

We have the d-flex and align classes to align the containers in various ways.

Flex Align Self

We can use the align-self classes to apply the align-self property.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <v-card
          v-for="j in justify"
          :key="j"
          class="d-flex mb-6"
          height="100"
        >
          <v-card
            v-for="n in 3"
            :key="n"
            class="pa-2"
            :class="[n === 1 && `align-self-${j}`]"
          >{{ n === 1 ? 'Aligned flex item' : 'Flex item' }}</v-card>
        </v-card>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({
    justify: ["start", "end", "center", "baseline", "auto", "stretch"],
  }),
};
</script>

We have the align-self classes to align the classes our way.

Flex Wrap

Vuetify also provides the flex wrap classes.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <v-card class="d-flex flex-nowrap py-3" color="grey lighten-2" flat tile>
          <v-card v-for="n in 5" :key="n" class="pa-2" outlined tile>Flex item</v-card>
        </v-card>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({
    justify: ["start", "end", "center", "baseline", "auto", "stretch"],
  }),
};
</script>

We have the flex-nowrap to prevent wrapping.

The general format of the classes is flex-{breakpoint}-{condition} .

Flex Order

We can switch the visual order of the flex items with the order classes.

So we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <v-card class="d-flex flex-wrap-reverse" color="grey lighten-2" flat tile>
          <v-card class="order-3 pa-2" outlined tile>First</v-card>
          <v-card class="order-2 pa-2" outlined tile>Second</v-card>
          <v-card class="order-1 pa-2" outlined tile>Third</v-card>
        </v-card>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({
  }),
};
</script>

We flipped the order with the flex-wrap-reverse class.

Conclusion

Vuetify provides us with various flexbox utilities to lay out our items.

Categories
Vuetify

Getting Started with Vuetify

Vuetify is a popular UI framework for Vue apps.

In this article, we’ll look at how to get started with the Vuetify framework.

Vue CLI Install

The easiest way to create a Vuetify Vue app is to use the Vue CLI.

We create a project directory, go into it, and run:

npx vue create .

We choose the options we want when prompted.

Once we did that, we run:

vue add vuetify

to add the Vuetify files.

We choose Default when prompted.

This is the recommended configuration.

It comes with all the components and assets we need to build an app with it.

Colors

Once we did that, we can set the colors by using the classes.

So we can write:

<div class="red">

or

<span class="red--text">

and to set the colors to red.

We can also change the colors in vuetify.js :

import Vue from 'vue';
import Vuetify from 'vuetify/lib';
import colors from 'vuetify/lib/util/colors'

Vue.use(Vuetify);

export default new Vuetify({
  theme: {
    themes: {
      light: {
        primary: colors.red.darken1,
        secondary: colors.red.lighten4,
        accent: colors.indigo.base
      },
    },
  },
});

We import the colors object and set the colors to what we want.

SASS Colors

We can also import SASS colors by writing:

@import '~vuetify/src/styles/main.sass';

They can also be imported in the src/index.js file writing;

import './src/sass/main.scss'
// OR
require('./src/sass/main.scss')

This works if we configured our Vue CLI to use SASS.

Content

We can create content with various elements.

Vuetify provides styles for them.

For instance, we can create a blockquote by writing:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <blockquote class="blockquote">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</blockquote>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

We just put them inside a component within the v-container to have the styling applied.

We can do the same for code:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
         <code>&lt;code&gt;</code>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

Also, we can apply styles to the kbd element:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <kbd>npm install vuetify</kbd>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

Display Helpers

We can add various classes to add padding, colors, set the display CSS property and more.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <div class="d-inline pa-3 deep-purple accent-4 white--text">foo</div>
        <div class="d-inline pa-3 black white--text">bar</div>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

We create a purple and black divs with some classes.

pa-3 created padding.

d-inline make the divs display inline.

deep-purple , accent-4 , white--text , and black are the color classes.

Conclusion

We can create a Vuetify app with a few commands.

It adds styles to elements and we can also add our own style with the classes it provides.

Categories
Top Vue Packages

Top Vue Packages for Adding YouTube Videos, Autofill Box, and More

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 the best packages for adding drag and drop items, YouTube videos, an input box with autofill, and a custom scrollbar.

Vue Smooth DnD

Vue Smooth DnD is an easy to use Vue library for making drag and drop lists.

To use it, we run:

npm i vue-smooth-dnd

Then we can use it by writing:

<template>
  <div>
    <div>
      <Container>
        <Draggable v-for="item in items" :key="item.id">
          <div>{{item.data}}</div>
        </Draggable>
      </Container>
    </div>
  </div>
</template>

<script>
import { Container, Draggable } from "vue-smooth-dnd";
export default {
  name: "Simple",
  components: { Container, Draggable },
  data() {
    return {
      items: Array(50)
        .fill()
        .map((_, i) => ({ id: i, data: `Draggable ${i}` }))
    };
  }
};
</script>

We use the Container and Draggable components to make the draggable container and items.

Container is the container.

Draggable are the draggable items.

We can change the classes of the items, the animation duration, drop placeholder, and other options.

vue-simple-suggest

As its name suggests, vue-simple-suggest is a simple package for adding an input box with autofill suggestions,

To use it, we run:

npm i vue-simple-suggest

Then we use it by writing:

<template>
  <div>
    <vue-simple-suggest v-model="chosen" :list="simpleSuggestionList" :filter-by-query="true"></vue-simple-suggest>

<p>{{ chosen }}</p>
  </div>
</template>

<script>
import VueSimpleSuggest from "vue-simple-suggest";
import "vue-simple-suggest/dist/styles.css";

export default {
  components: {
    VueSimpleSuggest
  },
  data() {
    return {
      chosen: ""
    };
  },
  methods: {
    simpleSuggestionList() {
      return ["apple", "orange", "grape"];
    }
  }
};
</script>

We import the component and CSS to display the input box.

Then we specified the simpleSuggestionList to return the suggestions.

v-model binds the selected choice to the chosen state.

It also works with async data and others has many other options for changes.

We can return a promise in our suggestion function to use async data as our suggestion source:

<template>
  <div>
    <vue-simple-suggest v-model="chosen" :list="simpleSuggestionList" :filter-by-query="true"></vue-simple-suggest>
    <p>{{ chosen }}</p>
  </div>
</template>

<script>
import VueSimpleSuggest from "vue-simple-suggest";
import "vue-simple-suggest/dist/styles.css";

export default {
  components: {
    VueSimpleSuggest
  },
  data() {
    return {
      chosen: ""
    };
  },
  methods: {
    async simpleSuggestionList() {
      return Promise.resolve(["apple", "orange", "grape"]);
    }
  }
};
</script>

We return a promise instead of an array.

vue-youtube

vue-youtube is a Vue package that let us embed YouTube videos in our Vue app.

To use it, we run:

npm i vue-youtube

to install it.

Then we can use it by writing:

main.js

import Vue from "vue";
import App from "./App.vue";
import VueYoutube from "vue-youtube";

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

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

App.vue

<template>
  <div>
    <youtube ref="youtube" :video-id="videoId" :player-vars="playerVars" @playing="playing"></youtube>
  </div>
</template>

<script>
export default {
  data() {
    return {
      videoId: "NnsHhqAdOiM",
      playerVars: {
        autoplay: 1
      }
    };
  },
  computed: {
    player() {
      return this.$refs.youtube.player;
    }
  },
  methods: {
    async playVideo() {
      await this.player.playVideo();
    }
  }
};
</script>

We have the youtube component to embed the videos.

All we have to do is to specify the ID of the video and set a few options to play videos.

Vue 2 Scrollbar

Vue 2 Scrollbar is a custom scrollbar component for Vue apps.

To use it, we run:

npm i vue2-scrollbar

to install it.

Then we write:

<template>
  <div>
    <vue-scrollbar ref="Scrollbar" classes="my-scrollbar">
      <div class="scroll-me">
        <div v-for="n in 100" :key="n">{{n}}</div>
      </div>
    </vue-scrollbar>
  </div>
</template>

<script>
import VueScrollbar from "vue2-scrollbar";
import "vue2-scrollbar/dist/style/vue2-scrollbar.css";

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

<style>
.my-scrollbar {
  max-height: 300px;
}
</style>

We add the vue-scrollbar component with the styles.

Also, we set the height of the container to 300px max.

Then we should see the scrollbar from the package displayed.

Conclusion

Vue Smooth DnD is a drag and drop component for Vue apps.

vue-simple-suggest lets us add an input box with autofill suggestions.

vue-youtube is a YouTube component for Vue apps.

Vue 2 Scrollbar provides us with a custom scrollbar.

Categories
Top Vue Packages

Top Vue Packages for Adding File Uploaders, Draggable Dialog, Slides, and Lightbox

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 the best packages for adding a file uploader, draggable dialog, slides, and lightbox.

Vue-Transmit

Vue-Transmit is a handy file uploader widget for Vue apps.

To use it, we run:

npm i vue-transmit

Then we can use it by writing:

main.js

import Vue from "vue";
import App from "./App.vue";
import VueTransmit from "vue-transmit";
Vue.use(VueTransmit);

Vue.config.productionTip = false;

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

App.vue

<template>
  <div id="app">
    <vue-transmit tag="section" v-bind="options" ref="uploader">
      <div>
        <button class="btn btn-primary" @click="triggerBrowse">Upload Files</button>
      </div>
      <template slot="files" slot-scope="props">
        <div v-for="(file, i) in props.files" :key="file.id" :class="{'mt-5': i === 0}">
          <div class="media">
            <img :src="file.dataUrl" class="img-fluid d-flex mr-3">
          </div>
        </div>
      </template>
    </vue-transmit>
  </div>
</template>

<script>
export default {
  data() {
    return {
      options: {
        acceptedFileTypes: ["image/*"],
        url: "./upload",
        clickable: false
      }
    };
  },
  methods: {
    triggerBrowse() {
      this.$refs.uploader.triggerBrowseFiles();
    }
  },
  filters: {
    json(value) {
      return JSON.stringify(value, null, 2);
    }
  }
};
</script>

We use the vue-transmit component to add an upload button with a preview.

To display the preview, we populate the files slot to display the preview by using the dataUrl property.

We set the options for the modal by passing in the options with v-bind .

We set the acceptedFileTypes to set the file types the uploader accepts.

url is the URL to upload the data to.

It takes many other customization options that we can use to change how it behaves.

Drag and drop are also supported.

vue-dialog-drag

vue-dialog-drag is a draggable dialog for Vue apps.

To use it, we run:

npm i vue-dialog-drag

Then we write:

<template>
  <div id="app">
    <dialog-drag id="dialog-1">
      <p>Test dialog</p>
    </dialog-drag>
    <drop-area [@drop](http://twitter.com/drop "Twitter profile for @drop")="drop">
      <p>Drop Here</p>
    </drop-area>
  </div>
</template>

<script>
import DialogDrag from "vue-dialog-drag";
import DropArea from "vue-dialog-drag/dist/drop-area";

export default {
  name: "app",
  components: {
    DialogDrag,
    DropArea
  },
  methods: {
    drop(id) {
      console.log(id);
    }
  }
};
</script>

<style src="vue-dialog-drag/dist/vue-dialog-drag.css"></style>
<style src="vue-dialog-drag/dist/drop-area.css"></style>
<style src="vue-dialog-drag/dist/dialog-styles.css"></style>

to use it.

We use the built it dialog-drag and drop-area components with the built-in styles.

Now we can drag the dialog to the drop zone.

Vueper Slides

Vueper Slides is a slides library for Vue apps.

To install it, we run:

npm i vueperslides

Then we can use it by writing:

<template>
  <div id="app">
    <vueper-slides>
      <vueper-slide v-for="n in 10" :key="n" :title="`title ${n}`" :content="`slide ${n}`"/>
    </vueper-slides>
  </div>
</template>

<script>
import { VueperSlides, VueperSlide } from "vueperslides";
import "vueperslides/dist/vueperslides.css";

export default {
  components: { VueperSlides, VueperSlide }
};
</script>

We use the vueper-slides component to show the slides.

Also, we have to import the CSS to load the style of the slides.

vueper-slide renders the slides.

We pass in the title to display the title and content to display the content.

vue-easy-lightbox

vue-easy-lightbox is a simple lightbox component for Vue apps.

To use it, we run:

npm i vue-easy-lightbox

to install it.

Then we write:

<template>
  <div id="app">
    <div>
      <div v-for="(src, index) in imgs" :key="index" class="pic" @click="() => showImg(index)">
        <img :src="src">
      </div>
    </div>
    <vue-easy-lightbox :visible="visible" :imgs="imgs" :index="index" [@hide](http://twitter.com/hide "Twitter profile for @hide")="handleHide"></vue-easy-lightbox>
  </div>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      index: 0,
      imgs: [
        "https://placeimg.com/300/300/any",
        "https://placekitten.com/300/300"
      ]
    };
  },
  methods: {
    showImg(index) {
      this.index = index;
      this.visible = true;
    },
    handleHide() {
      this.visible = false;
    }
  }
};
</script>

to add the images and the lightbox.

vue-easy-lightbox has the lightbox component.

We click on the image and we’ll see the image.

To customize it, we can populate various slots to change the lightbox content.

The toolbar slot changes the toolbar.

next-btn slot changes the next button.

prev-btn slot changes the previous button.

close-btn slot changes the close button.

We can also listen to the events that are emitted by the lightbox.

Conclusion

Vue-Transmit is an easy to use uploader component.

vue-dialog-drag is a draggable dialog box.

Vueper Slides lets us add slides to our app.

vue-easy-lightbox is a handy lightbox component.