Categories
Ant Design Vue

Ant Design Vue — Grids and Layouts

Ant Design Vue or AntD Vue, is a useful UI framework made for Vue.js.

In this article, we’ll look at how to use it in our Vue apps.

Grid Order

We can change the grid order by setting the order prop:

<template>
  <div id="app">
    <a-row type="flex">
      <a-col :span="6" :order="4">1 order-4</a-col>
      <a-col :span="6" :order="3">2 order-3</a-col>
      <a-col :span="6" :order="2">3 order-2</a-col>
      <a-col :span="6" :order="1">4 order-1</a-col>
    </a-row>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We set the order to a number.

Grid Column Offset

We can add the offset prop to add gaps between grid columns. For example, we can write:

<template>
  <div id="app">
    <a-row>
      <a-col :span="8">col-8</a-col>
      <a-col :span="8" :offset="8">col-8</a-col>
    </a-row>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Responsive Grid

We can set the sizes of the columns at different breakpoints.

For example, we can write:

<template>
  <div id="app">
    <a-row>
      <a-col :xs="2" :sm="4" :md="6" :lg="8" :xl="10">Col</a-col>
      <a-col :xs="20" :sm="16" :md="12" :lg="8" :xl="4">Col</a-col>
      <a-col :xs="2" :sm="4" :md="6" :lg="8" :xl="10">Col</a-col>
    </a-row>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

to set the column size for the xs , sm , md and lg breakpoints.

Push and Pull

We can also set the column order with the push and pull props:

<template>
  <div id="app">
    <a-row>
      <a-col :span="18" :push="6">col-18 col-push-6</a-col>
      <a-col :span="6" :pull="18">col-6 col-pull-18</a-col>
    </a-row>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Layout

Ant Design Vue comes with various components for creating layouts.

We can use the a-layout , a-layout-header , a-layout-content , and a-layout-footer components:

<template>
  <div id="app">
    <a-layout>
      <a-layout-header>Header</a-layout-header>
      <a-layout-content>Content</a-layout-content>
      <a-layout-footer>Footer</a-layout-footer>
    </a-layout>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

The header is black.

We can trigger content to be displayed when we click on a link on a menu:

<template>
  <div id="app">
    <a-layout id="components-layout-demo-custom-trigger">
      <a-layout-sider v-model="collapsed" :trigger="null" collapsible>
        <div class="logo"/>
        <a-menu theme="dark" mode="inline" :default-selected-keys="['1']">
          <a-menu-item key="1">
            <a-icon type="user"/>
            <span>nav 1</span>
          </a-menu-item>
          <a-menu-item key="2">
            <a-icon type="video-camera"/>
            <span>nav 2</span>
          </a-menu-item>
          <a-menu-item key="3">
            <a-icon type="upload"/>
            <span>nav 3</span>
          </a-menu-item>
        </a-menu>
      </a-layout-sider>
      <a-layout>
        <a-layout-header style="background: #fff; padding: 0">
          <a-icon
            class="trigger"
            :type="collapsed ? 'menu-unfold' : 'menu-fold'"
            @click="() => (collapsed = !collapsed)"
          />
        </a-layout-header>
        <a-layout-content
          :style="{ margin: '24px 16px', padding: '24px', background: '#fff', minHeight: '280px' }"
        >Content</a-layout-content>
      </a-layout>
    </a-layout>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      collapsed: false
    };
  }
};
</script>

We created a sidebar menu with the a-menu component. How it’s displaying us controlled by the collapsed component.

a-layout displays beside the menu.

Conclusion

We can add layouts and grids with Ant Design Vue.

Categories
Ant Design Vue

Ant Design Vue — Dropdowns and Top Menus

Ant Design Vue or AntD Vue, is a useful UI framework made for Vue.js.

In this article, we’ll look at how to use it in our Vue apps.

Dropdown

We can add dropdowns with the a-dropdown component.

For example, we can write:

<template>
  <div id="app">
    <a-dropdown>
      <a class="ant-dropdown-link" @click="e => e.preventDefault()">Hover me
        <a-icon type="down"/>
      </a>
      <a-menu slot="overlay">
        <a-menu-item>
          <a href="javascript:;">1st menu item</a>
        </a-menu-item>
        <a-menu-item>
          <a href="javascript:;">2nd menu item</a>
        </a-menu-item>
      </a-menu>
    </a-dropdown>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We add the a-dropdown with the a element being its trigger.

a-menu shows the menu.

Context Menu

Also, we can add a context menu by setting the trigger prop to the ['contextmenu'] array:

<template>
  <div id="app">
    <a-dropdown :trigger="['contextmenu']">
      <div
        :style="{
        textAlign: 'center',
        background: '#f7f7f7',
        height: '200px',
        lineHeight: '200px',
        color: '#777',
      }"
      >Right Click on here</div>
      <a-menu slot="overlay">
        <a-menu-item key="1">1st menu item</a-menu-item>
        <a-menu-item key="2">2nd menu item</a-menu-item>
        <a-menu-item key="3">3rd menu item</a-menu-item>
      </a-menu>
    </a-dropdown>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Also, we can add a divider and disable menu items:

<template>
  <div id="app">
    <a-dropdown>
      <a class="ant-dropdown-link" @click="e => e.preventDefault()">Hover me
        <a-icon type="down"/>
      </a>
      <a-menu slot="overlay">
        <a-menu-item key="0">
          <a target="_blank" rel="noopener noreferrer" href="https://www.google.com/">1st menu item</a>
        </a-menu-item>
        <a-menu-item key="1">
          <a target="_blank" rel="noopener noreferrer" href="https://www.ebay.com/">2nd menu item</a>
        </a-menu-item>
        <a-menu-divider/>
        <a-menu-item key="3" disabled>3rd menu item(disabled)</a-menu-item>
      </a-menu>
    </a-dropdown>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

a-menu-divider adds the divider and the disabled prop disables the item.

The placement of the dropdown menu can be changed:

<template>
  <div id="app">
    <a-dropdown placement="bottomRight">
      <a-button>bottom right</a-button>
      <a-menu slot="overlay">
        <a-menu-item key="0">
          <a target="_blank" rel="noopener noreferrer" href="https://www.google.com/">1st menu item</a>
        </a-menu-item>
        <a-menu-item key="1">
          <a target="_blank" rel="noopener noreferrer" href="https://www.ebay.com/">2nd menu item</a>
        </a-menu-item>
      </a-menu>
    </a-dropdown>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We set the placement prop to change the placement.

Menu

We can add a menu with the a-menu component as the container and a-menu-item for the menu items:

<template>
  <div id="app">
    <a-menu v-model="current" mode="horizontal">
      <a-menu-item key="mail">
        <a-icon type="mail"/>Navigation One
      </a-menu-item>
      <a-menu-item key="app" disabled>
        <a-icon type="appstore"/>Navigation Two
      </a-menu-item>
      <a-sub-menu>
        <span slot="title" class="submenu-title-wrapper">
          <a-icon type="setting"/>Navigation Three - Submenu
        </span>
        <a-menu-item-group title="Item 1">
          <a-menu-item key="setting:1">Option 1</a-menu-item>
          <a-menu-item key="setting:2">Option 2</a-menu-item>
        </a-menu-item-group>
        <a-menu-item-group title="Item 2">
          <a-menu-item key="setting:3">Option 3</a-menu-item>
          <a-menu-item key="setting:4">Option 4</a-menu-item>
        </a-menu-item-group>
      </a-sub-menu>
      <a-menu-item key="google">
        <a
          href="https://google.com"
          target="_blank"
          rel="noopener noreferrer"
        >Navigation Four - Link</a>
      </a-menu-item>
    </a-menu>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Conclusion

We can add a dropdown and top menu with Ant Design Vue.

Categories
Vue

Storing Vue App Data in Session Storage with vue-sessionstorage

We can use the vue-sessionstorage library to store Vue app data in session storage of our browser.

In this article, we’ll look at how to use it to save data.

Installation

We can install the library by running:

npm i vue-sessionstorage

Save Data in Local Storage

After installing the library, we can save data in local storage with by writing:

main.js

import Vue from "vue";
import App from "./App.vue";
import VueSessionStorage from "vue-sessionstorage";
Vue.use(VueSessionStorage);
Vue.config.productionTip = false;

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

App.vue

<template>
  <div id="app"></div>
</template>

<script>
export default {
  name: "App",
  mounted() {
    this.$session.set("username", "user123");
  }
};
</script>

We just register the plugin in main.js .

Then we have the this.$session object available for us to use anywhere.

We call set with the key and value of the data we want to save to save it.

To get the data, we can use the this.$session.get method:

<template>
  <div id="app"></div>
</template>

<script>
export default {
  name: "App",
  mounted() {
    this.$session.set("username", "user123");
    console.log(this.$session.get("username"));
  }
};
</script>

The this.$session.exists method checks if a key exists:

<template>
  <div id="app"></div>
</template>

<script>
export default {
  name: "App",
  mounted() {
    this.$session.set("username", "user123");
    console.log(this.$session.exists("username"));
  }
};
</script>

The this.$session.remove method removes the item with the given key:

<template>
  <div id="app"></div>
</template>

<script>
export default {
  name: "App",
  mounted() {
    this.$session.set("username", "user123");
    console.log(this.$session.remove("username"));
  }
};
</script>

The this.$session.clear method clears session storage data with the given key:

<template>
  <div id="app"></div>
</template>

<script>
export default {
  name: "App",
  mounted() {
    this.$session.set("username", "user123");
    console.log(this.$session.clear("username"));
  }
};
</script>

With session storage, different instances of the same app can store different data in their own session.

The data can be set both per-origin and per-instance, which is per-window or per-tab.

So the clear method will clear all data with the given key.

Conclusion

We can use the vue-sessionstorage library to save data to session storage.

Categories
Vue

Storing Data with Local Storage and Vuex with the vuex-persistedstate Library

The vuex-persistedstate library lets us add persistence to local storage of our data in our Vuex store.

In this article, we’ll look at how to use it to save data.

Installation

We can install it by running:

npm install --save vuex-persistedstate

Also, we can install it with a script tag:

<script src="https://unpkg.com/vuex-persistedstate/dist/vuex-persistedstate.umd.js"></script>

Then the window.createPersistedState is available for us to use.

Saving Vuex State in Local Storage

We can save our Vuex state to local storage with the createPersistedState function.

To use it, we write:

main.js

import Vue from "vue";
import App from "./App.vue";
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate";

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

const store = new Vuex.Store({
  state: {
    count: 0
  },
  plugins: [createPersistedState()],
  mutations: {
    increment: (state) => state.count++,
    decrement: (state) => state.count--
  }
});
new Vue({
  store,
  render: (h) => h(App)
}).$mount("#app");

App.vue

<template>
  <div id="app">
    <p>{{ count }}</p>
    <p>
      <button [@click](http://twitter.com/click "Twitter profile for @click")="increment">increment</button>
      <button [@click](http://twitter.com/click "Twitter profile for @click")="decrement">decrement</button>
    </p>
  </div>
</template>

<script>
export default {
  name: "App",
  computed: {
    count() {
      return this.$store.state.count;
    }
  },
  methods: {
    increment() {
      this.$store.commit("increment");
    },
    decrement() {
      this.$store.commit("decrement");
    }
  }
};
</script>

In main.js , we registered the Vuex plugin and created the store.

The store has the count state and mutations to change it,.

Also, we added the plugin created by the createPersistedState function from the vuex-persistedstate library.

This way, we can store the store’s state in local storage.

In App.vue , we get the count state in count computed property.

And we call commit to commit the mutations for changing the count state.

Storing Vuex State as Cookies

Alternatively, we can store Vuex states as cookies. To do that, we can use vuex-persistedstate with the js-cookie library.

We install js-cookie by running:

npm i js-cookie

In main.js , we change it to:

import Vue from "vue";
import App from "./App.vue";
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate";
import Cookies from "js-cookie";

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

const store = new Vuex.Store({
  state: {
    count: 0
  },
  plugins: [
    createPersistedState({
      storage: {
        getItem: (key) => Cookies.get(key),
        setItem: (key, value) =>
          Cookies.set(key, value, { expires: 3, secure: true }),
        removeItem: (key) => Cookies.remove(key)
      }
    })
  ],
  mutations: {
    increment: (state) => state.count++,
    decrement: (state) => state.count--
  }
});
new Vue({
  store,
  render: (h) => h(App)
}).$mount("#app");

App.vue stays the same.

We passed in an object so that we can get the data by its key with getItem .

setItem sets the data with the given key. expires is the expiry time in days. secure makes sure the cookie is only set over HTTPS.

removeItem removes an item by its key.

Secure Local Storage

We can scramble the data in local storage with the secure-ls library.

To use it, we can change main.js to:

import Vue from "vue";
import App from "./App.vue";
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate";
import SecureLS from "secure-ls";
const ls = new SecureLS({ isCompression: false });
Vue.use(Vuex);
Vue.config.productionTip = false;

const store = new Vuex.Store({
  state: {
    count: 0
  },
  plugins: [
    createPersistedState({
      storage: {
        getItem: (key) => ls.get(key),
        setItem: (key, value) => ls.set(key, value),
        removeItem: (key) => ls.remove(key)
      }
    })
  ],
  mutations: {
    increment: (state) => state.count++,
    decrement: (state) => state.count--
  }
});
new Vue({
  store,
  render: (h) => h(App)
}).$mount("#app");

We import the secure-ls library and create the ls object to let us get, set, and remove items from local storage.

Whatever is saved will be scrambled so no one can see it the browser dev console.

Conclusion

The vuex-persistedstate library is a useful library for storing Vuex state in local storage.

This way, we can keep the after we refresh the page.

Categories
Buefy

Buefy — Tags and Tooltips

Buefy is a UI framework that’s based on Bulma.

In this article, we’ll look at how to use Buefy in our Vue app.

Tag List

We add a list of tags with the b-taglist component.

To add it, we write:

<template>
  <div id="app">
    <b-taglist>
      <b-tag type="is-info">First</b-tag>
      <b-tag type="is-info">Second</b-tag>
      <b-tag type="is-info">Third</b-tag>
    </b-taglist>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We just put all the b-tag components inside.

The attached prop will attach 2 tags together:

<template>
  <div id="app">
    <b-taglist attached>
      <b-tag type="is-dark">npm</b-tag>
      <b-tag type="is-info">6</b-tag>
    </b-taglist>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Also, we can combine fields to group attached tags:

<template>
  <div id="app">
    <b-field grouped group-multiline>
      <div class="control">
        <b-tag type="is-primary" attached closable>Technology</b-tag>
      </div>
      <div class="control">
        <b-tag type="is-primary" attached closable>Vuejs</b-tag>
      </div>
      <div class="control">
        <b-tag type="is-primary" attached closable>CSS</b-tag>
      </div>
    </b-field>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We put them all in the b-field component with the grouped and grouped-multiline props to group them together.

Sizes and Types

We can change the color with the type prop:

<template>
  <div id="app">
    <b-tag type="is-info">Technology</b-tag>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

And we can change the size with the size prop:

<template>
  <div id="app">
    <b-tag size="is-large">Technology</b-tag>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Toast

We can add toasts to display notifications.

For example, we can write:

<template>
  <div id="app">
    <button class="button is-medium" @click="toast">Launch toast</button>
  </div>
</template>

<script>
export default {
  name: "App",
  methods: {
    toast() {
      this.$buefy.toast.open("toast");
    }
  }
};
</script>

We called the this.$buefy.toast.open method to display a toast with the text in the argument.

Also, we can set the background color with the type property. message has the message.

We can also change the duration that it’s displayed and its position with the duration and position properties:

<template>
  <div id="app">
    <button class="button is-medium" @click="toast">Launch toast</button>
  </div>
</template>

<script>
export default {
  name: "App",
  methods: {
    toast() {
      this.$buefy.toast.open({
        duration: 5000,
        message: `Something's <b>not good</b>`,
        position: "is-bottom",
        type: "is-danger"
      });
    }
  }
};
</script>

is-bottom places it at the bottom of the page.

Tooltip

Buefy comes with its own tooltip.

For example, we can write:

<template>
  <div id="app">
    <b-tooltip label="Tooltip right" position="is-right">
      <button class="button is-dark">Right</button>
    </b-tooltip>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

The b-tooltip component is wrapped around the trigger of the tooltip.

label has the tooltip text.

position has the position. It can be changed to other positions like is-left , is-bottom , etc.

We can add a delay with the delay prop:

<template>
  <div id="app">
    <b-tooltip label="Tooltip right" position="is-right" :delay="1000">
      <button class="button is-dark">Right</button>
    </b-tooltip>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Multilined Tooltip

The multilined prop makes it multilined:

<template>
  <div id="app">
    <b-tooltip label="Tooltip right" position="is-right" multilined>
      <button class="button is-dark">Right</button>
    </b-tooltip>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Conclusion

We can add tags and tooltips to our Vue app with Buefy.