Categories
Quasar

Developing Vue Apps with the Quasar Library — Repeated Touch and Swipes

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.

Touch Repeat

We can watch the repeated touch events with the v-touch-repeat directive.

For instance, we can 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">
      <div class="q-pa-md">
        <q-card
          v-touch-repeat.mouse="handleRepeat"
          class="custom-area cursor-pointer bg-primary text-white shadow-2 relative-position row flex-center"
        >
          <div v-if="info" class="custom-info">
            <pre>{{ info }}</pre>
          </div>
          <div v-else class="text-center">
            Click/touch and hold.
          </div>
        </q-card>
      </div>
    </div>
    <script>
      new Vue({
        el: "#q-app",
        data: {
          info: null
        },
        methods: {
          handleRepeat({ evt, ...info }) {
            this.info = info;
          }
        }
      });
    </script>
  </body>
</html>

We can watch key presses by adding some modifiers:

<!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">
      <div class="q-pa-md">
        <q-card
          v-touch-repeat:0:300:200.mouse.enter.space.72.104="handleRepeat"
          class="custom-area cursor-pointer bg-primary text-white shadow-2 relative-position row flex-center"
        >
          <div v-if="info" class="custom-info">
            <pre>{{ info }}</pre>
          </div>
          <div v-else class="text-center">
            Click/touch and hold.
          </div>
        </q-card>
      </div>
    </div>
    <script>
      new Vue({
        el: "#q-app",
        data: {
          info: null
        },
        methods: {
          handleRepeat({ evt, ...info }) {
            this.info = info;
          }
        }
      });
    </script>
  </body>
</html>

The 72, 104, enter and space modifiers are the keys we are watching presses for.

Touch Swipe

We can watch swipes with the v-touch-swipe directive.

For instance, we can 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">
    <style>
      .custom-area {
        width: 90%;
        height: 220px;
        border-radius: 3px;
        padding: 8px;
      }

      .custom-info pre {
        width: 180px;
        font-size: 12px;
      }
    </style>
    <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">
      <div class="q-pa-md">
        <q-card
          v-touch-swipe.mouse="handleSwipe"
          class="custom-area cursor-pointer bg-primary text-white shadow-2 relative-position row flex-center"
        >
          <div v-if="info" class="custom-info">
            <pre>{{ info }}</pre>
          </div>
          <div v-else class="text-center">
            <q-icon name="arrow_upward"></q-icon>
            <div class="row items-center">
              <q-icon name="arrow_back"></q-icon>
              <div>Swipe in any direction</div>
              <q-icon name="arrow_forward"></q-icon>
            </div>
            <q-icon name="arrow_downward"></q-icon>
          </div>
        </q-card>
      </div>
    </div>
    <script>
      new Vue({
        el: "#q-app",
        data: {
          info: null
        },
        methods: {
          handleSwipe({ evt, ...info }) {
            this.info = info;
          }
        }
      });
    </script>
  </body>
</html>

We watch for mouse drags with the mouse modifier.

Also, we can restrict the direction we’re watching with the given modifier:

<!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">
    <style>
      .custom-area {
        width: 90%;
        height: 220px;
        border-radius: 3px;
        padding: 8px;
      }

      .custom-info pre {
        width: 180px;
        font-size: 12px;
      }
    </style>
    <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">
      <div class="q-pa-md">
        <q-card
          v-touch-swipe.mouse.right="handleSwipe"
          class="custom-area cursor-pointer bg-primary text-white shadow-2 relative-position row flex-center"
        >
          <div v-if="info" class="custom-info">
            <pre>{{ info }}</pre>
          </div>
          <div v-else class="text-center">
            <div class="row items-center">
              swipe right
            </div>
          </div>
        </q-card>
      </div>
    </div>
    <script>
      new Vue({
        el: "#q-app",
        data: {
          info: null
        },
        methods: {
          handleSwipe({ evt, ...info }) {
            this.info = info;
          }
        }
      });
    </script>
  </body>
</html>

Conclusion

We can watch for swipes and repeated touches with the directives that are provided by 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 *