material icon indicating copy to clipboard operation
material copied to clipboard

Unexpected behavior of md-dialog if back button is used

Open saulotoledo opened this issue 11 years ago • 20 comments

You can easily note this at mobile devices, but you can try at normal browsers too. Use the back button with a single page app back the page states, but the dialog still opened. The problems are (1) that some page states scroll the dialog outside the visible area when you press the back button, and (2) if you want to update some element at page who triggered the dialog, he is not there anymore, since the back button changed the angular's loaded page.

With mobile devices you can press the back button even by accident, leading to problems at pages.

The solution should be to close the dialog if the user press the back button.

saulotoledo avatar Mar 20 '15 12:03 saulotoledo

+1

alanpurple avatar Mar 24 '15 04:03 alanpurple

+1 If you hit the back button on a desktop the dialog stays open but the state changes. This is of course if your using ui-router.

epelc avatar Apr 22 '15 20:04 epelc

This should be implemented in the application. The frameworks/libraries you use should not do magic.

gustavohenke avatar Apr 22 '15 20:04 gustavohenke

It's quite easy to fix if you don't use special states to show your dialogs or have multi state dialogs.

 $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
  //Make sure you inject $mdDialog wherever you stick this
  $mdDialog.cancel()
})

epelc avatar Apr 22 '15 21:04 epelc

@epelc this will close the dialog but the state will still change. Any idea on how to check if there is an open dialog? If there is, one could prevent the state change and only close the dialog.

kugler avatar May 28 '15 11:05 kugler

This is application-level logic with dependency issues upon Routing

ThomasBurleson avatar Aug 10 '15 22:08 ThomasBurleson

@ThomasBurleson Maybe an addition of current state of $mdDialog / $mdSidenav / $mdBottomSheet would help. This way - on state change we could check the state and see if there is a dialog to close (and then prevent the state change). Something like $mdDialog.isOpen()

kugler avatar Sep 16 '15 14:09 kugler

@kugler - My comment on 8/10 was in error. Navigation may generate $destroy events and components that use interElements (eg bottomsheet, dialog, toast, menu, and select) need to properly cleanup during $destory cycles. This has been fixed in master with SHA 77a34bd8

ThomasBurleson avatar Sep 16 '15 19:09 ThomasBurleson

Thanks @ThomasBurleson didn't know this was fixed - now we can remove another patch :+1: Let me explain our use case for the comment above: We use angular material for a responsive web app (desktop and mobile web) in addition to a hybrid mobile app. On android devices, users expect different behaviour when clicking the device's back button on a web page vs true mobile application. One of the things that would help emulate a real app is to enable closing dialogs on the device's back button (same as escape on a desktop keyboard) without navigating. We have the ability to catch the button click but in order to decide whether to navigate back in history or closing the dialog - we need to know the service's state (dialog open/close etc.). I think this option will be useful in the future when multiple dialogs feature will be available.

kugler avatar Sep 16 '15 20:09 kugler

I'm still having this issue; When a md-dialog is opened, and I navigate with the back-button, the dialog stays open. It cannot be closed after that. But instead of changing the route, I just want to close the dialog

This can be fixed with a listener, but not sure if this is the best way

$rootScope.$on('$locationChangeStart', function(event) {
    // Check if there is a dialog active
    if(angular.element(document).find('md-dialog').length > 0) {
        event.preventDefault(); // Prevent route from changing
        $mdDialog.cancel();  // Cancel the active dialog
    }
})

barryvdh avatar Dec 10 '15 08:12 barryvdh

Ideally it would probably remember the stack of dialogs, perhaps even change the url state, and close the dialogs in order.

barryvdh avatar Dec 10 '15 08:12 barryvdh

Same here, still a bug.

rvarbanov avatar Feb 26 '16 20:02 rvarbanov

+1

smurugavel avatar Mar 07 '16 22:03 smurugavel

@barryvdh The solution you provided solved my problem. Thanks!!!

kssaw avatar May 09 '16 05:05 kssaw

@barryvdh thanks for your answer , it is very helpful

BeSaRa avatar May 17 '16 20:05 BeSaRa

we can simplify @barryvdh suggested answer to:

$rootScope.$on('$locationChangeStart', function($event) {
  // Check if there is a dialog active
  if ($mdDialog.isShowing()) {
    $event.preventDefault(); // Prevent route from changing
    $mdDialog.cancel();  // Cancel the active dialog
  }
});

grrrian avatar Feb 22 '17 19:02 grrrian

How to handle close dialog on outsideclick and escape along with state change ?

Back button click to close dialog along with url change result in UI router state change. But when users click outside dialog it has to be closed along with bring same similar behavior of back button press.

What is best way to handle above two scenarios?

sthan001 avatar Apr 11 '17 03:04 sthan001

@grrrian the function isShowing doesn't exists for me. I'm use the latest version of Ang Material.

I put your example code on module.run method where I inject the $mdDialog. It's Correct?

rafaeldev avatar Apr 12 '17 14:04 rafaeldev

@rafaeldev Looking at the code, there is no isShowing. And logically I think they would go with isOpen - this is discussed in this issue https://github.com/angular/material/issues/5071 , which also has an alternative method for checking for an open dialog:

angular.element(document.body).hasClass('md-dialog-is-showing')

which will presumably be quicker than doing a find for an md-dialog.

colmben avatar Apr 19 '17 10:04 colmben

Thanks @colmben for your answer!

rafaeldev avatar Apr 19 '17 17:04 rafaeldev