Framework v4 BETA

ion-modal

Open Preview

A Modal is a dialog that appears on top of the app's content, and must be dismissed by the app before interaction can resume. It is useful as a select component when there are a lot of options to choose from, or when filtering items in a list, as well as many other use cases.

Creating

Modals can be created using a Modal Controller. They can be customized by passing modal options in the modal controller's create method.

Passing paramaters

When a modal is created, paramaters might be passed to the newly created modal:

ts
// Create a modal using MyModalComponent with some initial data
const modal = await modalController.create({
  component: MyModalComponent,
  componentProps: {
    'prop1': value,
    'prop2': value2
  }
});

Under the hood, the controller creates a new ion-modal and attaches the specified component to it. It also assigns the specified componentProps to the component's instance:

js
// pseudo-code
const instance = create(MyModalComponent);
instance.prop1 = value;
instance.prop2 = value2;

This way, your component can access the passed params, check the "Usage" section for further code example for each frameworks.

Retuning data

Modals can also return data back to the controller when they are dismissed.

js
const modal = await modalController.create({...});
const { data } = await modal.onDidDismiss();
console.log(data);
js
// Dismiss the top modal returning some data object
modalController.dismiss({
  'result': value
})

Usage

angular javascript
typescript
import { Component } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { ModalPage } from '../modal/modal.page';
@Component({
  selector: 'modal-example',
  templateUrl: 'modal-example.html',
  styleUrls: ['./modal-example.css']
})
export class ModalExample {
  constructor(public modalController: ModalController) {}

  async presentModal() {
    const modal = await this.modalController.create({
      component: ModalPage,
      componentProps: { value: 123 }
    });
    return await modal.present();
  }
}
typescript
import { Component } from '@angular/core';
import { NavParams } from '@ionic/angular';

@Component({
  selector: 'modal-page',
})
export class ModalExample {

  // "value" passed in componentProps
  @Input() value: number;

  constructor(navParams: NavParams) {
    // componentProps can also be accessed at construction time using NavParams
  }

}
html
<body>
  <ion-modal-controller></ion-modal-controller>
</body>
javascript
customElements.define('modal-page', class extends HTMLElement {
  connectedCallback() {
    this.innerHTML = `
<ion-header>
  <ion-toolbar>
    <ion-title>Super Modal</ion-title>
  </ion-toolbar>
</ion-header>
<ion-content>
  Content
</ion-content>`;
  }
});

async function presentModal() {
  // initialize controller
  const modalController = document.querySelector('ion-modal-controller');
  await modalController.componentOnReady();

  // present the modal
  const modalElement = await modalController.create({
    component: 'modal-page'
  });
  await modalElement.present();
}

Properties

animated

Attribute: animated Type: boolean
If true, the modal will animate.

backdropDismiss

Attribute: backdrop-dismiss Type: boolean
If true, the modal will be dismissed when the backdrop is clicked.

component

Attribute: component Type: Function | HTMLElement | null | string
The component to display inside of the modal.

componentProps

Type: undefined | { [key: string]: any; }
The data to pass to the modal component.

cssClass

Attribute: css-class Type: string | string[] | undefined
Additional classes to apply for custom CSS. If multiple classes are provided they should be separated by spaces.

enterAnimation

Type: ((Animation: Animation, baseEl: any, opts?: any) => Promise) | undefined
Animation to use when the modal is presented.

keyboardClose

Attribute: keyboard-close Type: boolean
If true, the keyboard will be automatically dismissed when the overlay is presented.

leaveAnimation

Type: ((Animation: Animation, baseEl: any, opts?: any) => Promise) | undefined
Animation to use when the modal is dismissed.

mode

Attribute: mode Type: "ios" | "md"
The mode determines which platform styles to use.

showBackdrop

Attribute: show-backdrop Type: boolean
If true, a backdrop will be displayed behind the modal.

Events

ionModalDidDismiss

Emitted after the modal has dismissed.

ionModalDidPresent

Emitted after the modal has presented.

ionModalWillDismiss

Emitted before the modal has dismissed.

ionModalWillPresent

Emitted before the modal has presented.

Methods

dismiss()

Dismiss the modal overlay after it has been presented.

onDidDismiss()

Returns a promise that resolves when the modal did dismiss.

onWillDismiss()

Returns a promise that resolves when the modal will dismiss.

present()

Present the modal overlay after it has been created.

CSS Custom Properties

Name Description
--background Background of the modal content
--border-color Border color of the modal content
--border-radius Border radius of the modal content
--border-style Border style of the modal content
--border-width Border width of the modal content
--height Height of the modal
--max-height Maximum height of the modal
--max-width Maximum width of the modal
--min-height Minimum height of the modal
--min-width Minimum width of the modal
--width Width of the modal