Categories
Vue Ionic

Mobile Development with Ionic and Vue — Segment Display, Dropdowns, and Toasts

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.

Segment Display

We can use the ion-segment component to add a segment display.

For example, we can write:

<template>
  <ion-segment @ionChange="segmentChanged($event)">
    <ion-segment-button value="friends">
      <ion-label>Friends</ion-label>
    </ion-segment-button>
    <ion-segment-button value="enemies">
      <ion-label>Enemies</ion-label>
    </ion-segment-button>
  </ion-segment>
</template>

<script lang="ts">
import { IonSegment, IonSegmentButton, IonToolbar } from "@ionic/vue";
import { defineComponent } from "vue";

export default defineComponent({
  components: { IonSegment, IonSegmentButton, IonToolbar },
  methods: {
    segmentChanged(ev: CustomEvent) {
      console.log("Segment changed", ev);
    },
  },
});
</script>

We add the ion-segment component to add the segment.

The ionChange event is emitted when we click on the segment.

ion-segment-button has the buttons for the segment display.

We can also change the color with the color prop:

<template>
  <ion-segment @ionChange="segmentChanged($event)" color="secondary">
    <ion-segment-button value="standard">
      <ion-label>Standard</ion-label>
    </ion-segment-button>
    <ion-segment-button value="hybrid">
      <ion-label>Hybrid</ion-label>
    </ion-segment-button>
    <ion-segment-button value="sat">
      <ion-label>Satellite</ion-label>
    </ion-segment-button>
  </ion-segment>
</template>

<script lang="ts">
import { IonSegment, IonSegmentButton, IonToolbar } from "@ionic/vue";
import { defineComponent } from "vue";

export default defineComponent({
  components: { IonSegment, IonSegmentButton, IonToolbar },
  methods: {
    segmentChanged(ev: CustomEvent) {
      console.log("Segment changed", ev);
    },
  },
});
</script>

Select Dropdown

We can add a select dropdown with the ion-select and ion-select-option components.

For example, we can write:

<template>
  <ion-list>
    <ion-item>
      <ion-label>Hair Color</ion-label>
      <ion-select value="brown" ok-text="Okay" cancel-text="Dismiss">
        <ion-select-option value="brown">Brown</ion-select-option>
        <ion-select-option value="blonde">Blonde</ion-select-option>
        <ion-select-option value="black">Black</ion-select-option>
        <ion-select-option value="red">Red</ion-select-option>
      </ion-select>
    </ion-item>
  </ion-list>
</template>

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

export default defineComponent({
  components: {
    IonItem,
    IonLabel,
    IonList,
    IonListHeader,
    IonSelect,
    IonSelectOption,
  },
});
</script>

We add the ion-label for the label.

ion-select is the select dropdown.

ok-text has the button for the OK text. cancel-text has the Cancel text.

ion-select-option has the option.

Toast

We can add a toast popup with Ionic Vue.

For example, we can write:

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

<script>
import { IonButton, IonContent, IonPage, toastController } from "@ionic/vue";

export default {
  components: { IonButton, IonContent, IonPage },
  methods: {
    async openToast() {
      const toast = await toastController.create({
        header: "Toast header",
        message: "Click to Close",
        position: "top",
        buttons: [
          {
            side: "start",
            icon: "star",
            text: "Favorite",
            handler: () => {
              console.log("Favorite clicked");
            },
          },
          {
            text: "Done",
            role: "cancel",
            handler: () => {
              console.log("Cancel clicked");
            },
          },
        ],
      });
      return toast.present();
    },
  },
};
</script>

We add a button that calls openToast when it’s clicked.

The toastController.create method creates the toast.

header has the toast header.

message has the toast message.

position has the position.

buttons have the buttons. It’s an array of objects that some options.

side has the side to put the button. start puts it on the left.

icon has the button icon.

text has the button text.

handler is a function that’s called when it’s clicked.

Conclusion

We can add segment display, select dropdowns, and toasts with Ionic Vue.

Categories
Vue Ionic

Mobile Development with Ionic and Vue — Pull to Refresh, Reorder, and Search Bars

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.

Pull to Refresh

We can add a pull to refresh component with the ion-refresher component.

For example, we can write:

<template>
  <ion-content>
    <ion-refresher slot="fixed" pull-factor="0.5" pull-min="100" pull-max="200">
      <ion-refresher-content></ion-refresher-content>
    </ion-refresher>
  </ion-content>
  <ion-content>
    <ion-refresher slot="fixed" @ionRefresh="doRefresh($event)">
      <ion-refresher-content
        :pulling-icon="chevronDownCircleOutline"
        pulling-text="Pull to refresh"
        refreshing-spinner="circles"
        refreshing-text="Refreshing..."
      >
      </ion-refresher-content>
    </ion-refresher>
  </ion-content>
</template>

<script lang="ts">
import { IonContent, IonRefresher, IonRefresherContent } from "@ionic/vue";
import { chevronDownCircleOutline } from "ionicons/icons";
import { defineComponent } from "vue";

interface CustomEvent {
  target: {
    complete: Function;
  };
}

export default defineComponent({
  components: { IonContent, IonRefresher, IonRefresherContent },
  setup() {
    const doRefresh = (event: CustomEvent) => {
      console.log("Begin");
      setTimeout(() => {
        console.log("End");
        event.target.complete();
      }, 2000);
    };
    return { chevronDownCircleOutline, doRefresh };
  },
});
</script>

The pulling-icon shows the icon.

pulling-text has the refresh indicator text.

refreshing-spinner sets the spinner type.

refreshing-text sets the text for the spinner.

ion-refresher-content has the content that we want to refresh with the pull to refresh feature.

Reorder

We can add reorderable items with the ion-reorder component.

For example, we can write:

<template>
  <!-- The reorder gesture is disabled by default, enable it to drag and drop items -->
  <ion-reorder-group :disabled="false">
    <ion-item>
      <ion-label> Item 1 </ion-label>
      <ion-reorder slot="end">
        <ion-icon name="pizza"></ion-icon>
      </ion-reorder>
    </ion-item>
    <ion-item>
      <ion-label> Item 2 </ion-label>
      <ion-reorder slot="end">
        <ion-icon name="pizza"></ion-icon>
      </ion-reorder>
    </ion-item>
    <ion-reorder>
      <ion-item>
        <ion-label> Item 3 </ion-label>
      </ion-item>
    </ion-reorder>
  </ion-reorder-group>
</template>

<script>
import {
  IonIcon,
  IonItem,
  IonLabel,
  IonReorder,
  IonReorderGroup,
} from "@ionic/vue";
import { pizza } from "ionicons/icons";
import { defineComponent } from "vue";

export default defineComponent({
  components: {
    IonIcon,
    IonItem,
    IonLabel,
    IonReorder,
    IonReorderGroup,
  },
  setup() {
    return { pizza };
  },
});
</script>

to add it.

ion-reorder is wrapped around the item we want to make draggable.

Search Bar

We can add a search bar with the ion-searchbar component.

For example, we can write:

<template>
  <ion-searchbar
    show-cancel-button="focus"
    cancel-button-text="Custom Cancel"
  ></ion-searchbar>
  <ion-searchbar debounce="500"></ion-searchbar>
  <ion-searchbar animated></ion-searchbar>
  <ion-searchbar placeholder="Filter Schedules"></ion-searchbar>
  <ion-toolbar>
    <ion-searchbar></ion-searchbar>
  </ion-toolbar>
</template>

<script>
import { IonSearchbar, IonToolbar } from "@ionic/vue";
import { defineComponent } from "vue";

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

The debounce prop sets the debounce.

show-cancel-button adds a cancel button to the search bar.

animated makes the search bar animated.

placeholder has the search bar placeholder.

We can also put search bars in a toolbar.

Also, we can set the type of the search bar:

<template>
  <ion-searchbar type="tel"></ion-searchbar>
</template>

<script>
import { IonSearchbar, IonToolbar } from "@ionic/vue";
import { defineComponent } from "vue";

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

We set type to tel so that we let users enter phone numbers.

Conclusion

We can add pull to refresh, reorderable items and search bars with Ionic Vue.

Categories
Vue Ionic

Mobile Development with Ionic and Vue — Loading Spinner and Range Input

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.

Loading Spinner

We can add a loading spinner with the ion-spinner component.

For example, we can write:

<template>
  <ion-spinner name="bubbles"></ion-spinner>
</template>

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

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

to add a loading spinner.

Radio

We can add radio buttons with the ion-radio component.

For example, we can write:

<template>
  <ion-list>
    <ion-radio-group value="biff">
      <ion-list-header>
        <ion-label>Name</ion-label>
      </ion-list-header>
      <ion-item>
        <ion-label>Biff</ion-label>
        <ion-radio slot="start" value="biff"></ion-radio>
      </ion-item>
      <ion-item>
        <ion-label>Cliff</ion-label>
        <ion-radio slot="start" value="cliff"></ion-radio>
      </ion-item>
    </ion-radio-group>
  </ion-list>
</template>

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

export default defineComponent({
  components: {
    IonItem,
    IonLabel,
    IonList,
    IonListHeader,
    IonRadio,
    IonRadioGroup,
  },
});
</script>
CopyCopied
Properties

to add an ion-list component and ion-item component to hold a series of radio buttons.

The slot prop set to start put the radio button on the left side.

Also, we can put radio buttons in ion-radio-group component:

<template>
  <ion-list>
    <ion-radio-group>
      <ion-list-header>
        <ion-label> Auto Manufacturers </ion-label>
      </ion-list-header>
      <ion-item>
        <ion-label>Cord</ion-label>
        <ion-radio value="cord"></ion-radio>
      </ion-item>
      <ion-item>
        <ion-label>Duesenberg</ion-label>
        <ion-radio value="duesenberg"></ion-radio>
      </ion-item>
    </ion-radio-group>
  </ion-list>
</template>

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

export default defineComponent({
  components: {
    IonItem,
    IonLabel,
    IonList,
    IonListHeader,
    IonRadio,
    IonRadioGroup,
  },
});
</script>

We add the ion-radio-group component to group the radio buttons together.

Range Input

We can add a numeric input with the ion-range component.

For example, we can write:

<template>
  <ion-list>
    <ion-item>
      <ion-range
        :min="1000"
        :max="2000"
        :step="100"
        :snaps="true"
        color="secondary"
      ></ion-range>
    </ion-item>
    <ion-item>
      <ion-range
        ref="rangeDualKnobs"
        dual-knobs="true"
        :min="21"
        :max="72"
        :step="3"
        :snaps="true"
      ></ion-range>
    </ion-item>
  </ion-list>
</template>

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

export default defineComponent({
  components: { IonItem, IonLabel, IonList, IonRange },
  mounted() {
    this.$refs.rangeDualKnobs.value = { lower: 24, upper: 42 };
  },
});
</script>

We add the ion-range component with a few props.

min sets the min allowed value.

max sets the max allowed value.

step sets step interval.

snaps set to true makes the knob snaps to a step.

We can also assign a ref so that we can set the value property with it to set the value.

Conclusion

We can add a loading spinner and a range input with Ionic Vue.

Categories
Vue Ionic

Mobile Development with Ionic and Vue — Loading Indicators

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.

Loading Indicator

We can add a loading indicator with Ionic Vue with the ion-loading component.

For example, 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: 'Please wait...',
          duration: this.timeout,
        });

        await loading.present();

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

to use it.

The loadingController.create method creates the loading indicator.

The object has the cssClass property to set the CSS class.

message has the loading indicator message.

duration has the duration to show the loading indicator.

loading.present shows the loading indicator.

loading.dismiss closes the loading indicator.

Also, we can make the loading indicator dismissible:

<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({
        spinner: null,
        duration: this.timeout,
        message: "Click the backdrop to dismiss early...",
        translucent: true,
        cssClass: "custom-class custom-loading",
        backdropDismiss: true,
      });
      await loading.present();

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

The spinner sets to null removes the spinner.

translucent makes the loading indicator container translucent.

backdropDismiss sets to true lets us dismiss the backdrop.

Progress Bar

We can add the progress bar with the ion-progress component.

For instance, we can use it by writing:

<template>
  <ion-progress-bar color="primary" value="0.5"></ion-progress-bar>
  <ion-progress-bar type="indeterminate" reversed="true"></ion-progress-bar>
</template>

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

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

We set the color with the color prop.

value has the progress value between 0 and 1.

type has the type of progress bar.

reversed changes the bar movement direction from right to left.

Skeleton Text

We can add the skeleton text loading indicator.

For example, we can write:

<template>
  <div>
    <ion-item>
      <ion-skeleton-text
        animated
        style="width: 27px; height: 27px"
        slot="start"
      ></ion-skeleton-text>
      <ion-label>
        <h3>
          <ion-skeleton-text animated style="width: 50%"></ion-skeleton-text>
        </h3>
        <p>
          <ion-skeleton-text animated style="width: 80%"></ion-skeleton-text>
        </p>
        <p>
          <ion-skeleton-text animated style="width: 60%"></ion-skeleton-text>
        </p>
      </ion-label>
    </ion-item>
  </div>
</template>

<script>
import {
  IonAvatar,
  IonIcon,
  IonItem,
  IonLabel,
  IonList,
  IonListheader,
  IonSkeletonText,
  IonThumbnail,
} from "@ionic/vue";
import { call } from "ionicons/icons";
import { defineComponent } from "vue";

export default defineComponent({
  components: {
    IonAvatar,
    IonIcon,
    IonItem,
    IonLabel,
    IonList,
    IonListheader,
    IonSkeletonText,
    IonThumbnail,
  },
});
</script>

The ion-skeleton-text to add the bars for the skeleton text display.

animated makes the bars animated.

The width style sets the width of the bar.

Conclusion

We can add various loading indicators with Ionic Vue.

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.