modal-dialogionic4ionic5

Ionic 5 Modal over modal is missing ion-backdrop


Why is my ion-backdrop + modal shadow styling not working when I open a modal on top of another modal?

PREFACE: This was working fine with V4, but broken on the upgrade to V5. I don’t want to change my page approach, just fix the CSS/whatever is actually causing the issue below.

Step1 Fine enter image description here

Step 2 - broken ion-backdrop: enter image description here

Showing my custom modal:

async showClipboard() {
    const modal = await this._ModalController.create({
      component: ClipboardPage,
      cssClass: 'custom-round-modal',
      componentProps: {
        user: this.user
      },
      showBackdrop: true
    });
    await modal.present(); 
  }

The CSS:

@media only screen and (min-width: 768px) {
  .custom-round-modal {
    .modal-wrapper {
      border-radius: 15px !important;
      -moz-border-radius: 15px !important;
      -webkit-border-radius: 15px !important;
      .ion-page {
        border-radius: 15px !important;
        -moz-border-radius: 15px !important;
        -webkit-border-radius: 15px !important;
      }
    }
  }
}

Solution

  • First off, I think you pasted the same screenshot twice by mistake. But I'm having the same issue, so I know what you mean.

    It looks like Ionic 5 introduced this css for the modals:

    .sc-ion-modal-ios-h:first-of-type {
        --backdrop-opacity: var(--ion-backdrop-opacity, 0.4);
    }
    

    Which means when you show multiple modals at the same time, only the first one will get the backdrop.

    A possible workaround is to add the backdrop yourself to your global css using something like this:

    ion-modal {
        --backdrop-opacity: var(--ion-backdrop-opacity, 0.4);
    }
    

    Or use the css class Ionic is using (but note that this one is iOS specific, so you'd likely need to do the same with the Android-equivalent class):

    .sc-ion-modal-ios-h {
        --backdrop-opacity: var(--ion-backdrop-opacity, 0.4);
    }
    

    NOTE: This will likely not look good if you are showing multiple modals on top of each other that are not fullscreen, since you'll be getting multiple backdrops on top of each other (so they'll get increasingly darker). But since your issue is a non-fullscreen modal on top of a fullscreen one, I think it will work in your case.

    Hopefully the Ionic team will come up with a more elegant solution to this issue.