ion-modal
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.
Presenting#
There are two ways to use ion-modal: inline or via the modalController. Each method comes with different considerations, so be sure to use the approach that best fits your use case.
Inline Modals#
ion-modal can be used by writing the component directly in your template. This reduces the number of handlers you need to wire up in order to present the modal. See Usage for an example of how to write a modal inline.
When using ion-modal with Angular, React, or Vue, the component you pass in will be destroyed when the modal is dismissed. As this functionality is provided by the JavaScript framework, using ion-modal without a JavaScript framework will not destroy the component you passed in. If this is a needed functionality, we recommend using the modalController instead.
Angular#
Since the component you passed in needs to be created when the modal is presented and destroyed when the modal is dismissed, we are unable to project the content using <ng-content> internally. Instead, we use <ng-container> which expects an <ng-template> to be passed in. As a result, when passing in your component you will need to wrap it in an <ng-template>:
When to use#
Using a modal inline is useful when you do not want to explicitly wire up click events to open the modal. For example, you can use the is-open property to easily present or dismiss a modal based on some state in your application.
If you need fine grained control over when the modal is presented and dismissed, we recommend you use the modalController.
Controller Modals#
ion-modal can also be presented programmatically by using the modalController imported from Ionic Framework. This allows you to have complete control over when a modal is presented above and beyond the customization that inline modals give you. See Usage for an example of how to use the modalController.
When to use#
We typically recommend that you write your modals inline as it streamlines the amount of code in your application. You should only use the modalController for complex use cases where writing a modal inline is impractical.
Interfaces#
Below you will find all of the options available to you when using the modalController. These options should be supplied when calling modalController.create().
Dismissing#
The modal can be dismissed after creation by calling the dismiss() method on the modal controller. The onDidDismiss function can be called to perform an action after the modal is dismissed.
Customization#
Modal uses scoped encapsulation, which means it will automatically scope its CSS by appending each of the styles with an additional class at runtime. Overriding scoped selectors in CSS requires a higher specificity selector.
We recommend passing a custom class to cssClass in the create method and using that to add custom styles to the host and inner elements. This property can also accept multiple classes separated by spaces. View the Usage section for an example of how to pass a class using cssClass.
Any of the defined CSS Custom Properties can be used to style the Modal without needing to target individual elements:
If you are building an Ionic Angular app, the styles need to be added to a global stylesheet file. Read Style Placement in the Angular section below for more information.
ion-modalworks under the assumption that stacked modals are the same size. As a result, each subsequent modal will have no box shadow and a backdrop opacity of0. This is to avoid the effect of shadows and backdrops getting darker with each added modal. This can be changed by setting the--box-shadowand--backdrop-opacityCSS variables:
Usage#
- ANGULAR
- JAVASCRIPT
- REACT
- STENCIL
- VUE
If you need a wrapper element inside of your modal component, we recommend using a
<div class="ion-page">so that the component dimensions are still computed properly.
Passing Data#
During creation of a modal, data can be passed in through the componentProps.
The previous example can be written to include data:
To get the data passed into the componentProps, set it as an @Input:
Dismissing a Modal#
A modal can be dismissed by calling the dismiss method on the modal controller and optionally passing any data from the modal.
After being dismissed, the data can be read in through the onWillDismiss or onDidDismiss attached to the modal after creation:
Lazy Loading#
When lazy loading a modal, it's important to note that the modal will not be loaded when it is opened, but rather when the module that imports the modal's module is loaded.
For example, say there exists a CalendarComponent and an EventModal. The modal is presented by clicking a button in the CalendarComponent. In Angular, the EventModalModule would need to be included in the CalendarComponentModule since the modal is created in the CalendarComponent:
Swipeable Modals#
Modals in iOS mode have the ability to be presented in a card-style and swiped to close. The card-style presentation and swipe to close gesture are not mutually exclusive, meaning you can pick and choose which features you want to use. For example, you can have a card-style modal that cannot be swiped or a full sized modal that can be swiped.
Card style modals when running on iPhone-sized devices do not have backdrops. As a result, the
--backdrop-opacityvariable will not have any effect.
If you are creating an application that uses ion-tabs, it is recommended that you get the parent ion-router-outlet using this.routerOutlet.parentOutlet.nativeEl, otherwise the tabbar will not scale down when the modal opens.
In most scenarios, using the ion-router-outlet element as the presentingElement is fine. In cases where you are presenting a card-style modal from within another modal, you should pass in the top-most ion-modal element as the presentingElement.
Style Placement#
In Angular, the CSS of a specific page is scoped only to elements of that page. Even though the Modal can be presented from within a page, the ion-modal element is appended outside of the current page. This means that any custom styles need to go in a global stylesheet file. In an Ionic Angular starter this can be the src/global.scss file or you can register a new global style file by adding to the styles build option in angular.json.
If you need a wrapper element inside of your modal component, we recommend using a
<div class="ion-page">so that the component dimensions are still computed properly.
Passing Data#
During creation of a modal, data can be passed in through the componentProps. The previous example can be written to include data:
To get the data passed into the componentProps, query for the modal in the modal-page:
Dismissing a Modal#
A modal can be dismissed by calling the dismiss method and optionally passing any data from the modal.
After being dismissed, the data can be read in through the onWillDismiss or onDidDismiss attached to the modal after creation:
Swipeable Modals#
Modals in iOS mode have the ability to be presented in a card-style and swiped to close. The card-style presentation and swipe to close gesture are not mutually exclusive, meaning you can pick and choose which features you want to use. For example, you can have a card-style modal that cannot be swiped or a full sized modal that can be swiped.
Card style modals when running on iPhone-sized devices do not have backdrops. As a result, the
--backdrop-opacityvariable will not have any effect.
In most scenarios, using the ion-nav element as the presentingElement is fine. In cases where you are presenting a card-style modal from within a modal, you should pass in the top-most ion-modal element as the presentingElement.
Swipeable Modals#
Modals in iOS mode have the ability to be presented in a card-style and swiped to close. The card-style presentation and swipe to close gesture are not mutually exclusive, meaning you can pick and choose which features you want to use. For example, you can have a card-style modal that cannot be swiped or a full sized modal that can be swiped.
Card style modals when running on iPhone-sized devices do not have backdrops. As a result, the
--backdrop-opacityvariable will not have any effect.
In most scenarios, setting a ref on IonRouterOutlet and passing that ref's current value to presentingElement is fine. In cases where you are presenting a card-style modal from within another modal, you should pass in the top-most ion-modal ref as the presentingElement.
If you need a wrapper element inside of your modal component, we recommend using a
<div class="ion-page">so that the component dimensions are still computed properly.
Passing Data#
During creation of a modal, data can be passed in through the componentProps.
The previous example can be written to include data:
To get the data passed into the componentProps, set each one as a @Prop:
Dismissing a Modal#
A modal can be dismissed by calling the dismiss method on the modal controller and optionally passing any data from the modal.
After being dismissed, the data can be read in through the onWillDismiss or onDidDismiss attached to the modal after creation:
Swipeable Modals#
Modals in iOS mode have the ability to be presented in a card-style and swiped to close. The card-style presentation and swipe to close gesture are not mutually exclusive, meaning you can pick and choose which features you want to use. For example, you can have a card-style modal that cannot be swiped or a full sized modal that can be swiped.
Card style modals when running on iPhone-sized devices do not have backdrops. As a result, the
--backdrop-opacityvariable will not have any effect.
In most scenarios, using the ion-router-outlet element as the presentingElement is fine. In cases where you are presenting a card-style modal from within another modal, you should pass in the top-most ion-modal element as the presentingElement.
Developers can also use this component directly in their template:
If you need a wrapper element inside of your modal component, we recommend using an
<ion-page>so that the component dimensions are still computed properly.
Properties#
animated#
| Description | If true, the modal will animate. |
| Attribute | animated |
| Type | boolean |
| Default | true |
backdropDismiss#
| Description | If true, the modal will be dismissed when the backdrop is clicked. |
| Attribute | backdrop-dismiss |
| Type | boolean |
| Default | true |
enterAnimation#
| Description | Animation to use when the modal is presented. |
| Attribute | undefined |
| Type | ((baseEl: any, opts?: any) => Animation) \| undefined |
| Default | undefined |
isOpen#
| Description | If true, the modal will open. If false, the modal will close.Use this if you need finer grained control over presentation, otherwise just use the modalController or the trigger property.Note: isOpen will not automatically be set back to false whenthe modal dismisses. You will need to do that in your code. |
| Attribute | is-open |
| Type | boolean |
| Default | false |
keyboardClose#
| Description | If true, the keyboard will be automatically dismissed when the overlay is presented. |
| Attribute | keyboard-close |
| Type | boolean |
| Default | true |
leaveAnimation#
| Description | Animation to use when the modal is dismissed. |
| Attribute | undefined |
| Type | ((baseEl: any, opts?: any) => Animation) \| undefined |
| Default | undefined |
mode#
| Description | The mode determines which platform styles to use. |
| Attribute | mode |
| Type | "ios" \| "md" |
| Default | undefined |
presentingElement#
| Description | The element that presented the modal. This is used for card presentation effects and for stacking multiple modals on top of each other. Only applies in iOS mode. |
| Attribute | undefined |
| Type | HTMLElement \| undefined |
| Default | undefined |
showBackdrop#
| Description | If true, a backdrop will be displayed behind the modal. |
| Attribute | show-backdrop |
| Type | boolean |
| Default | true |
swipeToClose#
| Description | If true, the modal can be swiped to dismiss. Only applies in iOS mode. |
| Attribute | swipe-to-close |
| Type | boolean |
| Default | false |
trigger#
| Description | An ID corresponding to the trigger element that causes the modal to open when clicked. |
| Attribute | trigger |
| Type | string \| undefined |
| Default | undefined |
Events#
| Name | Description |
|---|---|
didDismiss | Emitted after the modal has dismissed. |
| Shorthand for ionModalDidDismiss. | |
didPresent | Emitted after the modal has presented. |
| Shorthand for ionModalWillDismiss. | |
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. |
willDismiss | Emitted before the modal has dismissed. |
| Shorthand for ionModalWillDismiss. | |
willPresent | Emitted before the modal has presented. |
| Shorthand for ionModalWillPresent. |
Methods#
dismiss#
| Description | Dismiss the modal overlay after it has been presented. |
| Signature | dismiss(data?: any, role?: string \| undefined) => Promise<boolean> |
onDidDismiss#
| Description | Returns a promise that resolves when the modal did dismiss. |
| Signature | onDidDismiss<T = any>() => Promise<OverlayEventDetail<T>> |
onWillDismiss#
| Description | Returns a promise that resolves when the modal will dismiss. |
| Signature | onWillDismiss<T = any>() => Promise<OverlayEventDetail<T>> |
present#
| Description | Present the modal overlay after it has been created. |
| Signature | present() => Promise<void> |
CSS Shadow Parts#
| Name | Description |
|---|---|
backdrop | The ion-backdrop element. |
content | The wrapper element for the default slot. |
CSS Custom Properties#
| Name | Description |
|---|---|
--backdrop-opacity | Opacity of the backdrop |
--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 |
Slots#
| Name | Description |
|---|---|
= Content is placed inside of the .modal-content element. |