Categories
Quasar

Developing Vue Apps with the Quasar Library — Button Toggles

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.

Toggles in Option Groups

We can add a group of toggles with the q-option-group component.

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">
      <q-layout
        view="lHh Lpr lFf"
        container
        style="height: 100vh;"
        class="shadow-2 rounded-borders"
      >
        <div class="q-pa-md">
          <q-option-group
            :options="options"
            label="Notifications"
            type="toggle"
            v-model="group"
          >
          </q-option-group>
        </div>
      </q-layout>
    </div>
    <script>
      new Vue({
        el: "#q-app",
        data: {
          group: [],
          options: [
            { label: "Battery low", value: "bat" },
            { label: "Friend request", value: "friend", color: "green" },
            { label: "Picture uploaded", value: "upload", color: "red" }
          ]
        }
      });
    </script>
  </body>
</html>

Button Toggle

Quasar comes with the q-btn-toggle component that renders a button toggle.

To use it, 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-toggle
            v-model="model"
            push
            glossy
            toggle-color="primary"
            :options="options"
          >
          </q-btn-toggle>
        </div>
      </q-layout>
    </div>
    <script>
      new Vue({
        el: "#q-app",
        data: {
          model: "",
          options: [
            { label: "Battery low", value: "bat" },
            { label: "Friend request", value: "friend", color: "green" },
            { label: "Picture uploaded", value: "upload", color: "red" }
          ]
        }
      });
    </script>
  </body>
</html>

We add the push prop to let users push the buttons.

glossy adds a glossy effect.

And the toggle-color prop sets the color of the toggled element.

options sets the options for the button toggle.

We can replace glossy with flat , rounded and unelevated to change the styles.

no-caps changes the button text to be sentence case.

We can spread the button toggle across the screen with the spread 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-toggle
            v-model="model"
            spread
            no-caps
            toggle-color="purple"
            color="white"
            text-color="black"
            toggle-color="primary"
            :options="options"
          >
          </q-btn-toggle>
        </div>
      </q-layout>
    </div>
    <script>
      new Vue({
        el: "#q-app",
        data: {
          model: "",
          options: [
            { label: "Battery low", value: "bat" },
            { label: "Friend request", value: "friend", color: "green" },
            { label: "Picture uploaded", value: "upload", color: "red" }
          ]
        }
      });
    </script>
  </body>
</html>

Conclusion

We can add a button toggle into our Vue app with Quasar’sq-btn-toggle component.

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 *