Categories
Vue Ionic

Mobile Development with Ionic and Vue — Backdrop, Loading Indicator, and Popovers

If we know how to create Vue web apps but want to develop mobile apps, we can use the Ionic framework.

In this article, we’ll look at how to get started with mobile development with the Ionic framework with Vue.

Backdrop

We can add a backdrop into our Ionic Vue app with the ion-backdrop component.

For example, we can write:

<template>
  <ion-backdrop
    :tappable="enableBackdropDismiss"
    :visible="showBackdrop"
    :stop-propagation="shouldPropagate"
  >
  </ion-backdrop>
</template>

<script>
import { IonBackdrop } from "@ionic/vue";
import { defineComponent } from "vue";

export default defineComponent({
  components: { IonBackdrop },
  setup() {
    return {
      enableBackdropDismiss: true,
      showBackdrop: true,
      shouldPropagate: true,
    };
  },
});
</script>

to add the ion-backdrop component into our app.

tappable makes the backdrop tappable.

visible lets us make the backdrop visible.

stop-propagation lets us stop the propagation of events if it’s true .

Popovers

We can show popovers by using the ion-popover component.

For example, we can write:

views/Popover.vue

<template>
  <ion-content class="ion-padding">
    Popover Content
  </ion-content>
</template>

<script>
import { IonContent } from '@ionic/vue';
import { defineComponent } from 'vue';

export default defineComponent({
  name: 'Popover',
  components: { IonContent }
});
</script>

views/Home.vue

<template>
  <ion-button @click="setOpen(true, $event)">Show Popover</ion-button>
  <ion-popover
    :is-open="isOpenRef"
    css-class="my-custom-class"
    :event="event"
    :translucent="true"
    @onDidDismiss="setOpen(false)"
  >
    <Popover></Popover>
  </ion-popover>
</template>

<script>
import { IonButton, IonPopover } from '@ionic/vue';
import { defineComponent, ref } from 'vue';
import Popover from './Popover'

export default defineComponent({
  components: { IonButton, IonPopover, Popover },
  setup() {
    const isOpenRef = ref(false);
    const event = ref();
    const setOpen = (state, event) => {
      event.value = event;
      isOpenRef.value = state;
    }
    return { isOpenRef, setOpen, event }
  }
});
</script>

The Popover component is used for the popover content.

We have a button to show the popover when clicked.

Also, we have the Popover displayed when we open the popover.

is-open has the open state as the value.

css-class has the CSS class for the modal.

event has the event object.

setOpen sets whether the popover is open by setting the isOpenRef.value property to the state .

The state has the open state.

Loading Indicator

We can add a loading indicator with the loadingController .

To use it, we can write:

<template>
  <ion-button @click="presentLoading">Show Loading</ion-button>
</template>

<script>
import { IonButton, loadingController } from "@ionic/vue";
import { defineComponent } from "vue";

export default defineComponent({
  props: {
    timeout: { type: Number, default: 1000 },
  },
  methods: {
    async presentLoading() {
      const loading = await loadingController.create({
        cssClass: "my-custom-class",
        message: "Loading...",
        duration: this.timeout,
      });

      await loading.present();

      setTimeout(function () {
        loading.dismiss();
      }, this.timeout);
    },
  },
  components: { IonButton },
});
</script>

We have the presentLoading method that calls the loadingController.create method to create the loading indicator object.

cssClass has the CSS class.

message has the message for the loading indicator.

duration has the duration for how long the indicator should show.

loading.present() shows the loading indicator.

loading.dismiss() closes the loading indicator.

Conclusion

We can show a backdrop, a popover, and a loading indicator with Ionic Vue.

Categories
Vue Ionic

Mobile Development with Ionic and Vue — Avatars, Modals, and Images

If we know how to create Vue web apps but want to develop mobile apps, we can use the Ionic framework.

In this article, we’ll look at how to get started with mobile development with the Ionic framework with Vue.

Avatars

We can add avatars with the ion-avatar component.

For example, we can write:

<template>
  <ion-page>
    <ion-content>
      <ion-avatar>
        <img
          src="https://gravatar.com/avatar/dba6bae8c566f9d4041fb9cd9ada7741?d=identicon&f=y"
        />
      </ion-avatar>
    </ion-content>
  </ion-page>
</template>
<script lang="ts">
import { IonContent, IonPage, IonAvatar } from "@ionic/vue";
import { defineComponent, ref } from "vue";

export default defineComponent({
  components: {
    IonContent,
    IonPage,
    IonAvatar,
  },
});
</script>

to add a simple avatar.

Avatars can also be embedded in chips:

<template>
  <ion-page>
    <ion-content>
      <ion-chip>
        <ion-avatar>
          <img
            src="https://gravatar.com/avatar/dba6bae8c566f9d4041fb9cd9ada7741?d=identicon&f=y"
          />
        </ion-avatar>
        <ion-label>Chip Avatar</ion-label>
      </ion-chip>
    </ion-content>
  </ion-page>
</template>
<script lang="ts">
import { IonContent, IonPage, IonAvatar, IonChip } from "@ionic/vue";
import { defineComponent, ref } from "vue";

export default defineComponent({
  components: {
    IonContent,
    IonPage,
    IonAvatar,
    IonChip,
  },
});
</script>

And we can put them in an ion-item component:

<template>
  <ion-page>
    <ion-content>
      <ion-item>
        <ion-avatar slot="start">
          <img
            src="https://gravatar.com/avatar/dba6bae8c566f9d4041fb9cd9ada7741?d=identicon&f=y"
          />
        </ion-avatar>
        <ion-label>Item Avatar</ion-label>
      </ion-item>
    </ion-content>
  </ion-page>
</template>
<script lang="ts">
import { IonContent, IonPage, IonAvatar, IonItem, IonLabel } from "@ionic/vue";
import { defineComponent, ref } from "vue";

export default defineComponent({
  components: {
    IonContent,
    IonPage,
    IonAvatar,
    IonItem,
    IonLabel,
  },
});
</script>

Image

We can add an image with the ion-img component.

For example, we can put it in a list item by writing:

<template>
  <ion-page>
    <ion-content>
      <ion-list>
        <ion-item v-for="item in items" :key="item.src">
          <ion-thumbnail slot="start">
            <ion-img :src="item.src"></ion-img>
          </ion-thumbnail>
          <ion-label>{{ item.text }}</ion-label>
        </ion-item>
      </ion-list>
    </ion-content>
  </ion-page>
</template>
<script lang="ts">
import {
  IonContent,
  IonPage,
  IonLabel,
  IonList,
  IonThumbnail,
} from "@ionic/vue";
import { defineComponent, ref } from "vue";

export default defineComponent({
  components: {
    IonContent,
    IonPage,
    IonLabel,
    IonList,
    IonThumbnail,
  },
  setup() {
    const items = [
      {
        text: "Cat 1",
        src: "http://placekitten.com/200/200",
      },
      {
        text: "Cat 2",
        src: "http://placekitten.com/201/201",
      },
      {
        text: "Cat 3",
        src: "http://placekitten.com/200/200",
      },
    ];
    return { items };
  },
});
</script>

We put ion-img in an img-thumbnail component so we can show the images as thumbnails.

Modal

We can add a modal with the modalController .

For example, we can write:

Modal.vue

<template>
  <div>
    <ion-header>
      <ion-toolbar>
        <ion-title>{{ title }}</ion-title>
      </ion-toolbar>
    </ion-header>
    <ion-content class="ion-padding">
      {{ content }}
    </ion-content>
  </div>
</template>

<script>
import { IonContent, IonHeader, IonTitle, IonToolbar } from '@ionic/vue';
import { defineComponent } from 'vue';

export default defineComponent({
  name: 'Modal',
  props: {
    title: { type: String, default: 'Super Modal' },
  },
  data() {
    return {
      content: 'Content',
    }
  },
  components: { IonContent, IonHeader, IonTitle, IonToolbar }
});
</script>

Home.vue

<template>
  <ion-page>
    <ion-content class="ion-padding">
      <ion-button @click="openModal">Open Modal</ion-button>
    </ion-content>
  </ion-page>
</template>

<script>
import { IonButton, IonContent, IonPage, modalController } from '@ionic/vue';
import Modal from './Modal'

export default {
  components: { IonButton, IonContent, IonPage },
  methods: {
    async openModal() {
      const modal = await modalController
        .create({
          component: Modal,
          cssClass: 'my-custom-class',
          componentProps: {
            data: {
              content: 'New Content',
            },
            propsData: {
              title: 'New title',
            },
          },
        })
      return modal.present();
    },
  },
}
</script>

We add the modalController object to let us use the Modal component as the modal.

Modal.vue is used for the modal content.

In Home.vue , we call modalController.create to create the modal.

component is the component for the modal.

cssClass has the CSS class.

componentProps lets us pass in props to the Modal component and use them.

Now when we click the Open Modal button, we should see the modal.

Conclusion

We can add avatars, images and modals with Ionic Vue.

Categories
Vue Ionic

Mobile Development with Ionic and Vue — Textareas and Lists

If we know how to create Vue web apps but want to develop mobile apps, we can use the Ionic framework.

In this article, we’ll look at how to get started with mobile development with the Ionic framework with Vue.

Textarea

We can add a text area with the ion-textarea component.

For example, we can write:

<template>
  <ion-page>
    <ion-content>
      <ion-item>
        <ion-textarea
          placeholder="Enter more information here"
          v-model="val"
        ></ion-textarea>
      </ion-item>
      <ion-item>
        <ion-text>{{ val }}</ion-text>
      </ion-item>
    </ion-content>
  </ion-page>
</template>
<script>
import { IonItem, IonTextarea, IonContent, IonText, IonPage } from "@ionic/vue";
import { defineComponent, ref } from "vue";
export default defineComponent({
  components: { IonItem, IonTextarea, IonContent, IonText, IonPage },
  setup() {
    const val = ref("hello world");
    return {
      val,
    };
  },
});
</script>

We add the ion-textarea into our template,

Then we can bind the input value to the val reactive property with the v-model directive.

placeholder has the placeholder for the text area.

List Items

We can add list items with the ion-item component.

For example, we can write:

<template>
  <ion-page>
    <ion-content>
      <ion-item>
        <ion-label> Item </ion-label>
      </ion-item>
    </ion-content>
  </ion-page>
</template>
<script lang="ts">
import { IonItem, IonContent, IonLabel, IonPage } from "@ionic/vue";
import { defineComponent, ref } from "vue";

export default defineComponent({
  components: { IonItem, IonContent, IonLabel, IonPage },
});
</script>

to add the component.

Also, we can set the color:

<template>
  <ion-page>
    <ion-content>
      <ion-item color="secondary">
        <ion-label> Secondary Color Item </ion-label>
      </ion-item>
    </ion-content>
  </ion-page>
</template>
<script lang="ts">
import { IonItem, IonContent, IonLabel, IonPage } from "@ionic/vue";
import { defineComponent, ref } from "vue";

export default defineComponent({
  components: { IonItem, IonContent, IonLabel, IonPage },
});
</script>

And we can make it a link:

<template>
  <ion-page>
    <ion-content>
      <ion-item href="https://www.ionicframework.com">
        <ion-label> Anchor Item </ion-label>
      </ion-item>
    </ion-content>
  </ion-page>
</template>
<script lang="ts">
import { IonItem, IonContent, IonLabel, IonPage } from "@ionic/vue";
import { defineComponent, ref } from "vue";

export default defineComponent({
  components: { IonItem, IonContent, IonLabel, IonPage },
});
</script>

We can also make it a button:

<template>
  <ion-page>
    <ion-content>
      <ion-item button @click="() => console.log('clicked')">
        <ion-label> Button Item </ion-label>
      </ion-item>
    </ion-content>
  </ion-page>
</template>
<script lang="ts">
import { IonItem, IonContent, IonLabel, IonPage } from "@ionic/vue";
import { defineComponent, ref } from "vue";

export default defineComponent({
  components: { IonItem, IonContent, IonLabel, IonPage },
});
</script>

Item Divider

We can add an item divider to add a heading to our list.

For example, we can write:

<template>
  <ion-page>
    <ion-content>
      <ion-list>
        <ion-item-divider>
          <ion-label> Section A </ion-label>
        </ion-item-divider>

        <ion-item><ion-label>A1</ion-label></ion-item>
        <ion-item><ion-label>A2</ion-label></ion-item>

        <ion-item-divider>
          <ion-label> Section B </ion-label>
        </ion-item-divider>

        <ion-item><ion-label>B1</ion-label></ion-item>
        <ion-item><ion-label>B2</ion-label></ion-item>
      </ion-list>
    </ion-content>
  </ion-page>
</template>
<script lang="ts">
import {
  IonItem,
  IonContent,
  IonItemDivider,
  IonPage,
  IonLabel,
} from "@ionic/vue";
import { defineComponent, ref } from "vue";

export default defineComponent({
  components: { IonItem, IonContent, IonItemDivider, IonPage, IonLabel },
});
</script>

to add 2 lists on top of each other.

The ion-item-divider lets us add list headings.

ion-item has the list items.

ion-label has the list item text.

Conclusion

We can add text areas and items with item dividers with Ionic Vue.

Categories
Vue Ionic

Mobile Development with Ionic and Vue — Grid and Inputs

If we know how to create Vue web apps but want to develop mobile apps, we can use the Ionic framework.

In this article, we’ll look at how to get started with mobile development with the Ionic framework with Vue.

Grid

We can add grids for layouts with Ionic.

To add it, we use the ion-grid , ion-row and ion-col components.

For example, we can write:

<template>
  <ion-grid>
    <ion-row>
      <ion-col> ion-col </ion-col>
      <ion-col> ion-col </ion-col>
      <ion-col> ion-col </ion-col>
      <ion-col> ion-col </ion-col>
    </ion-row>
  </ion-grid>
</template>

<script>
import { IonCol, IonGrid, IonRow } from "@ionic/vue";
import { defineComponent } from "vue";

export default defineComponent({
  components: { IonCol, IonGrid, IonRow },
});
</script>

We add the ion-grid to add the grid container.

ion-row has the rows, and ion-col has the columns.

The columns will be evenly distributed.

To add different size grids, we can write:

<template>
  <ion-grid>
    <ion-row>
      <ion-col size="6" size-lg offset="3">
        ion-col [size="6"] [size-lg] [offset="3"]
      </ion-col>
      <ion-col size="3" size-lg> ion-col [size="3"] [size-lg] </ion-col>
    </ion-row>
  </ion-grid>
</template>

<script>
import { IonCol, IonGrid, IonRow } from "@ionic/vue";
import { defineComponent } from "vue";

export default defineComponent({
  components: { IonCol, IonGrid, IonRow },
});
</script>

The size has the number of columns from 1 to 12.

size-lg makes the size large.

offset lets us set the offset.

Inputs

We can add a text input into our Ionic Vue app with the ion-input component.

For example, we can write:

<template>
  <ion-page>
    <ion-content>
      <ion-item>
        <ion-input placeholder="Enter Input" v-model="val"></ion-input>
      </ion-item>
      <ion-item>
        <ion-text>{{ val }}</ion-text>
      </ion-item>
    </ion-content>
  </ion-page>
</template>

<script>
import {
  IonLabel,
  IonInput,
  IonItem,
  IonContent,
  IonText,
  IonPage,
} from "@ionic/vue";
import { defineComponent, ref } from "vue";

export default defineComponent({
  components: { IonLabel, IonInput, IonItem, IonContent, IonText, IonPage },
  setup() {
    const val = ref("hello world");
    return {
      val,
    };
  },
});
</script>

to add a text input.

We use v-model to bind to the val reactive properties.

And we defined that in the setup method and returned it to do the binding.

Also, we can add a numeric input by setting the type to number :

<template>
  <ion-page>
    <ion-content>
      <ion-item>
        <ion-input type="number" v-model="val"></ion-input>
      </ion-item>
      <ion-item>
        <ion-text>{{ val }}</ion-text>
      </ion-item>
    </ion-content>
  </ion-page>
</template>

<script>
import {
  IonLabel,
  IonInput,
  IonItem,
  IonContent,
  IonText,
  IonPage,
} from "@ionic/vue";
import { defineComponent, ref } from "vue";

export default defineComponent({
  components: { IonLabel, IonInput, IonItem, IonContent, IonText, IonPage },
  setup() {
    const val = ref(0);
    return {
      val,
    };
  },
});
</script>

The disabled and readonly props disable interaction with the input.

readonly doesn’t change the styles.

Conclusion

We can add grids and inputs with Ionic Vue.

Categories
Vue Ionic

Mobile Development with Ionic and Vue — Checkbox, Chips, Floating Action Buttons, and Date and Time Pickers

If we know how to create Vue web apps but want to develop mobile apps, we can use the Ionic framework.

In this article, we’ll look at how to get started with mobile development with the Ionic framework with Vue.

Checkbox

We can add checkboxes with the ion-checkbox component.

For example, we can write:

<template>
  <ion-list>
    <ion-item v-for="entry in form" :key="entry.val">
      <ion-label>{{ entry.val }}</ion-label>
      <ion-checkbox
        slot="end"
        @input="entry.isChecked = $event.target.value"
        :value="entry.isChecked"
      >
      </ion-checkbox>
    </ion-item>
  </ion-list>
</template>

<script>
import { IonCheckbox, IonItem, IonLabel, IonList } from "@ionic/vue";
import { defineComponent } from "vue";

export default defineComponent({
  components: { IonCheckbox, IonItem, IonLabel, IonList },
  setup() {
    const form = [
      { val: "Apple", isChecked: true },
      { val: "Orange", isChecked: false },
      { val: "Grape", isChecked: false },
    ];
    return { form };
  },
});
</script>

to display multiple checkboxes in a list.

ion-item is the container for the checkbox.

ion-label is the label for the checkbox and it’s displayed on the left.

ion-checkbox is the checkbox itself and it’s displayed on the right.

We listen to the input event and set the isChecked property with the checkbox’s value.

The :value prop has the checked value.

Chips

We can add chips to display small bits of information.

To use it, we write:

<template>
  <ion-chip>
    <ion-icon :icon="heart" color="dark"></ion-icon>
    <ion-label>Default</ion-label>
  </ion-chip>
</template>

<script>
import { IonAvatar, IonChip, IonIcon, IonLabel } from "@ionic/vue";
import { close, closeCircle, heart, pin } from "ionicons/icons";

import { defineComponent } from "vue";

export default defineComponent({
  components: { IonAvatar, IonChip, IonIcon, IonLabel },
  setup() {
    return { close, closeCircle, heart, pin };
  },
});
</script>

to register the ion-chip component.

Then we use it in the template.

We can add icons and labels inside the chip.

Content Container

We can add a content container with the ion-content component.

For example, we can write:

<template>
  <ion-content
    :scroll-events="true"
    @ionScrollStart="(e) => console.log(e)"
    @ionScroll="(e) => console.log(e)"
    @ionScrollEnd="(e) => console.log(e)"
  >
    <div slot="fixed">
      <h1>Fixed Content</h1>
    </div>
  </ion-content>
</template>

<script>
import { IonContent } from "@ionic/vue";
import { defineComponent } from "vue";

export default defineComponent({
  components: { IonContent },
});
</script>

to add content into the ion-content container.

scroll-events lets us enable watching scroll events.

We add fixed position content by setting the slot prop to fixed .

ionScrollStart ‘s callback is run when we start scrolling.

ionScroll ‘s callback is run when we’re scrolling.

And ionScrollEnd ‘s callback is run when we finish scrolling.

Date and Time Pickers

We can add a date and time picker with the ion-datetime component.

For instance, we can write:

<template>
  <ion-content>
    <ion-item>
      <ion-label position="floating">MM/DD/YYYY</ion-label>
      <ion-datetime
        display-format="MM/DD/YYYY"
        min="1994-03-14"
        max="2020-12-09"
        value="2012-09-23T15:03:46.789"
      ></ion-datetime>
    </ion-item>
  </ion-content>
</template>

<script>
import { IonDatetime, IonItem, IonLabel } from "@ionic/vue";
import { defineComponent } from "vue";

export default defineComponent({
  components: { IonDatetime, IonItem, IonLabel },
});
</script>

to add the date and time picker.

display-format changes the way the selected date is displayed.

min has the min date value we can choose.

max has the max date value we can choose.

value has the value of the date we picked.

Also, we can customize the text that’s displayed.

For instance, we can write:

<template>
  <ion-content>
    <ion-item>
      <ion-label>DDD. MMM DD, YY (custom locale)</ion-label>
      <ion-datetime
        value="1995-04-15"
        min="1990-02"
        max="2000"
        :day-short-names="customDayShortNames"
        display-format="DDD. MMM DD, YY"
        month-short-names="jan, feb, mar, apr, mai, jun, jul, aug, sep, okt, nov, des"
      ></ion-datetime>
    </ion-item>
  </ion-content>
</template>

<script>
import { IonDatetime, IonItem, IonLabel } from "@ionic/vue";
import { defineComponent } from "vue";

export default defineComponent({
  components: { IonDatetime, IonItem, IonLabel },
  setup() {
    const customYearValues = [2020, 2016, 2008, 2004, 2000, 1996];
    const customDayShortNames = [
      "su00f8n",
      "man",
      "tir",
      "ons",
      "tor",
      "fre",
      "lu00f8r",
    ];
    const customPickerOptions = {
      buttons: [
        {
          text: "Save",
          handler: () => console.log("Clicked Save!"),
        },
        {
          text: "Log",
          handler: () => {
            console.log("Clicked Log. Do not Dismiss.");
            return false;
          },
        },
      ],
    };
    return {
      customYearValues,
      customDayShortNames,
      customPickerOptions,
    };
  },
});
</script>

The day-short-names prop has the short names for the days.

month-short-names has the months’ short names.

Conclusion

We can add checkboxes, chips, content containers and date and time pickers with Ionic Vue.