ionic-team / ionic-framework

A powerful cross-platform UI toolkit for building native-quality iOS, Android, and Progressive Web Apps with HTML, CSS, and JavaScript.
https://ionicframework.com
MIT License
51.13k stars 13.5k forks source link

bug: controller getTop() method returns overlays that are not presented #19111

Closed damirarh closed 2 years ago

damirarh commented 5 years ago

Bug Report

Ionic version:

[x] 4.7.4

Current behavior: The call to LoadingController.getTop() returns the most recently created loading overlay even if it hasn't been presented/opened yet.

Expected behavior: According to the docs,, the call to LoadingController.getTop() should only return a loading overlay if it has already been presented/opened.

Steps to reproduce:

  1. Create a loading overlay and present it.
  2. Create another loading overlay, but don't present it.
  3. LoadingController.getTop() returns the second overlay instead of the first one.
  4. LoadingController.dismiss() doesn't dismiss the open loading overlay and returns false.

Related code:

https://github.com/damirarh/ioni4-loading-ctrl

this.loadingCtrl.create({message: 'presented'}).then(loader => {
  loader.present().then(() => {
    this.loadingCtrl.create({message: 'created'}).then(() => {
      this.loadingCtrl.getTop().then(topLoader => {
        console.log(topLoader.message); // outputs 'created' instead of 'presented'
        this.loadingCtrl.dismiss().then(dismissed => {
          console.log(dismissed); // doesn't dismiss the open loading overlay and outputs 'false'
        });
      });
    });
  });
});

Other information: This behavior also makes it impossible to reliably close all opened loading overlays without having a reference to each opened instance (i.e. reimplement the behavior of dismissAll in Ionic 3) because there's no way to get to the previously opened loading overlay using the LoadingController if an opened loading overlay was created afterward. I blogged about my attempt at implementing dismissAll in Ionic 4.

Ionic info:

Ionic:

   Ionic CLI                     : 5.2.2 (C:\Users\damir\AppData\Roaming\npm\node_modules\ionic)
   Ionic Framework               : @ionic/angular 4.7.4
   @angular-devkit/build-angular : 0.801.3
   @angular-devkit/schematics    : 8.1.3
   @angular/cli                  : 8.1.3
   @ionic/angular-toolkit        : 2.0.0

Utility:

   cordova-res : not installed
   native-run  : 0.2.7

System:

   NodeJS : v10.16.0 (C:\Program Files\nodejs\node.exe)
   npm    : 6.9.0
   OS     : Windows 10
liamdebeasi commented 3 years ago

Thanks for the issue. I can reproduce this behavior. The problem here is that getOverlay, the function that getTop calls does not take into consideration whether or not an overlay is presented: https://github.com/ionic-team/ionic-framework/blob/main/core/src/utils/overlays.ts#L233-L236. This issue impacts all overlays.

This may have been intentional, but I think getTop implies getting the top overlay that is visible on the screen.

ionitron-bot[bot] commented 2 years ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.