Adding modals to an Ionic app

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Modals are a big thing in apps. You see them almost everywhere for small detail transactions.
In this article, I'll show you how to add your own modal to an Ionic app.
The result will look like this.

This tutorial will pick up from our starting Ionic app, which you can download from GitHub.
We will be adding a modal to our first tab page.
Open up the tab1.page.ts file.
Start by creating a function which we can call through the HTML in a second.
This function will be an async function and call the modalController to create a certain modal.
async presentModal() {
const modal = await this.modalController.create({
component: DetailPage
});
return await modal.present();
}
We do have to register the modalController in our constructor.
constructor(public modalController: ModalController) {}
And you might have spotted we use a component called DetailPage so let's go ahead and create that one.
ng g page Detail
This will generate the DetailPage for us. (Make sure you import the detail page in the tab1.page.ts)
We can call the presentModal function from our tab1.page.html file to prompt the modal.
<ion-button (click)="presentModal()" expand="block">Show Modal</ion-button>
This will create a button, which on click, will open the detail modal.
However, when this happens, you might have spotted there is no way to close the modal now.
Luckily we can leverage a global modalController by injecting it in the detail.page.ts file.
constructor(public modalController: ModalController) {}
Then we can create a dismiss function, which will handle the dismissal of the modal.
dismiss() {
this.modalController.dismiss();
}
It's quite common to have a back button on the detail page that will dismiss the modal so let's add one of those in detail.page.html.
<ion-header>
<ion-toolbar>
<ion-buttons slot="start">
<ion-button color="white" (click)="dismiss()">
<ion-icon name="arrow-back"></ion-icon>
</ion-button>
</ion-buttons>
<ion-title>Detail</ion-title>
</ion-toolbar>
</ion-header>
We can even add a button on the page that will also dismiss the modal.
<ion-content fullscreen class="ion-padding">
<ion-button (click)="dismiss()" expand="block">Dismiss Modal</ion-button>
</ion-content>
This button will do the same, dismiss our modal.
And there you go, modals in Ionic are super easy and useful. They can even pass and return data, which we'll discuss in another topic.
You can find today's code on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter