Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(menu): hardware back button now dismisses side menu if open #20558

Merged
merged 1 commit into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/src/utils/hardware-back-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ const executeAction = async (handler: Handler | undefined) => {
console.error(e);
}
};

export const OVERLAY_BACK_BUTTON_PRIORITY = 100;
export const MENU_BACK_BUTTON_PRIORITY = 99; // 1 less than overlay priority since menu is displayed behind overlays
13 changes: 12 additions & 1 deletion core/src/utils/menu-controller/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AnimationBuilder, MenuI } from '../../interface';
import { AnimationBuilder, BackButtonEvent, MenuI } from '../../interface';
import { MENU_BACK_BUTTON_PRIORITY } from '../hardware-back-button';

import { menuOverlayAnimation } from './animations/overlay';
import { menuPushAnimation } from './animations/push';
Expand Down Expand Up @@ -206,6 +207,16 @@ const createMenuController = () => {
registerAnimation('push', menuPushAnimation);
registerAnimation('overlay', menuOverlayAnimation);

const doc: Document = document;
doc.addEventListener('ionBackButton', (ev: any) => {
const openMenu = _getOpenSync();
if (openMenu) {
(ev as BackButtonEvent).detail.register(MENU_BACK_BUTTON_PRIORITY, () => {
return openMenu.close();
});
}
});

return {
registerAnimation,
get,
Expand Down
4 changes: 3 additions & 1 deletion core/src/utils/overlays.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { config } from '../global/config';
import { ActionSheetOptions, AlertOptions, Animation, AnimationBuilder, BackButtonEvent, HTMLIonOverlayElement, IonicConfig, LoadingOptions, ModalOptions, OverlayInterface, PickerOptions, PopoverOptions, ToastOptions } from '../interface';

import { OVERLAY_BACK_BUTTON_PRIORITY } from './hardware-back-button';

let lastId = 0;

export const activeAnimations = new WeakMap<OverlayInterface, Animation[]>();
Expand Down Expand Up @@ -72,7 +74,7 @@ export const connectListeners = (doc: Document) => {
doc.addEventListener('ionBackButton', ev => {
const lastOverlay = getOverlay(doc);
if (lastOverlay && lastOverlay.backdropDismiss) {
(ev as BackButtonEvent).detail.register(100, () => {
(ev as BackButtonEvent).detail.register(OVERLAY_BACK_BUTTON_PRIORITY, () => {
return lastOverlay.dismiss(undefined, BACKDROP);
});
}
Expand Down