Categories
Quasar

Developing Vue Apps with the Quasar Library — Context Menu and Menu Transitions

Spread the love

Quasar is a popular Vue UI library for developing good looking Vue apps.

In this article, we’ll take a look at how to create Vue apps with the Quasar UI library.

Context Menu

We can add a context menu with the q-menu component and the context-menu prop:

<!DOCTYPE html>  
<html>  
  <head>  
    <link  
      href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons"  
      rel="stylesheet"  
      type="text/css"  
    />  
    <link  
      href="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.min.css"  
      rel="stylesheet"  
      type="text/css"  
    />  
  </head>  
  <body class="body--dark">  
    <script src="https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js"></script>  
    <script src="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.umd.min.js"></script>  
    <div id="q-app">  
      <q-layout  
        view="lHh Lpr lFf"  
        container  
        style="height: 100vh;"  
        class="shadow-2 rounded-borders"  
      >  
        <div class="q-pa-md">  
          <q-img  
            src="https://cdn.quasar.dev/img/parallax1.jpg"  
            style="height: 100px;"  
          >  
            <q-menu touch-position context-menu>  
              <q-list dense style="min-width: 100px;">  
                <q-item clickable v-close-popup>  
                  <q-item-section>Open...</q-item-section>  
                </q-item>  
                <q-item clickable v-close-popup>  
                  <q-item-section>New</q-item-section>  
                </q-item>  
                <q-separator></q-separator>  
                <q-item clickable v-close-popup>  
                  <q-item-section>Quit</q-item-section>  
                </q-item>  
              </q-list>  
            </q-menu>  
          </q-img>  
        </div>  
      </q-layout>  
    </div>  
    <script>  
      new Vue({  
        el: "#q-app",  
        data: {}  
      });  
    </script>  
  </body>  
</html>

We add the q-menu component inside the q-img component so that when we right-click on the image, we see the menu displayed.

Persistent Menu

We can create a persistent menu with the persistent prop:

<!DOCTYPE html>  
<html>  
  <head>  
    <link  
      href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons"  
      rel="stylesheet"  
      type="text/css"  
    />  
    <link  
      href="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.min.css"  
      rel="stylesheet"  
      type="text/css"  
    />  
  </head>  
  <body class="body--dark">  
    <script src="https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js"></script>  
    <script src="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.umd.min.js"></script>  
    <div id="q-app">  
      <q-layout  
        view="lHh Lpr lFf"  
        container  
        style="height: 100vh;"  
        class="shadow-2 rounded-borders"  
      >  
        <div class="q-pa-md">  
          <q-btn color="primary" label="Persistent Menu">  
            <q-menu persistent auto-close>  
              <q-list style="min-width: 100px;">  
                <q-item clickable>  
                  <q-item-section>New tab</q-item-section>  
                </q-item>  
                <q-item clickable>  
                  <q-item-section>New incognito tab</q-item-section>  
                </q-item>  
                <q-separator></q-separator>  
                <q-item clickable>  
                  <q-item-section>Recent tabs</q-item-section>  
                </q-item>  
              </q-list>  
            </q-menu>  
          </q-btn>  
        </div>  
      </q-layout>  
    </div>  
    <script>  
      new Vue({  
        el: "#q-app",  
        data: {}  
      });  
    </script>  
  </body>  
</html>

Then the menu only closes when we click on the button.

Menu Transitions

We can add transition effects when we click on the menu by adding the transition-show and transition-hide props to add a transition when the menu is being shown or hidden respectively.

To do this, we write:

<!DOCTYPE html>  
<html>  
  <head>  
    <link  
      href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons"  
      rel="stylesheet"  
      type="text/css"  
    />  
    <link  
      href="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.min.css"  
      rel="stylesheet"  
      type="text/css"  
    />  
  </head>  
  <body class="body--dark">  
    <script src="https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js"></script>  
    <script src="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.umd.min.js"></script>  
    <div id="q-app">  
      <q-layout  
        view="lHh Lpr lFf"  
        container  
        style="height: 100vh;"  
        class="shadow-2 rounded-borders"  
      >  
        <div class="q-pa-md">  
          <q-btn color="primary" label="Menu">  
            <q-menu transition-show="flip-right" transition-hide="flip-left">  
              <q-list style="min-width: 100px;">  
                <q-item clickable>  
                  <q-item-section>New tab</q-item-section>  
                </q-item>  
                <q-item clickable>  
                  <q-item-section>New incognito tab</q-item-section>  
                </q-item>  
                <q-separator></q-separator>  
                <q-item clickable>  
                  <q-item-section>Recent tabs</q-item-section>  
                </q-item>  
              </q-list>  
            </q-menu>  
          </q-btn>  
        </div>  
      </q-layout>  
    </div>  
    <script>  
      new Vue({  
        el: "#q-app",  
        data: {}  
      });  
    </script>  
  </body>  
</html>

Other options for the 2 props include scale , jump-down , and jump-up .

Conclusion

We can add context menus and menus with transition effects into our Vue app with Quasar.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *