From b4bcba353386b4d5d8d396e61ece421a15d42ff0 Mon Sep 17 00:00:00 2001 From: Liam DeBeasi Date: Mon, 27 Feb 2023 11:51:56 -0500 Subject: [PATCH] fix(modal): keyboard listener removed on dismiss (#26856) --- core/src/components/modal/modal.tsx | 61 +++++++++++++++++------------ 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/core/src/components/modal/modal.tsx b/core/src/components/modal/modal.tsx index 6802d3959f..06bca33b73 100644 --- a/core/src/components/modal/modal.tsx +++ b/core/src/components/modal/modal.tsx @@ -333,7 +333,7 @@ export class Modal implements ComponentInterface, OverlayInterface { if (this.gesture) { this.gesture.enable(enable); } else if (enable) { - await this.initSwipeToClose(); + this.initSwipeToClose(); } } @@ -532,6 +532,38 @@ export class Modal implements ComponentInterface, OverlayInterface { backdropBreakpoint: this.backdropBreakpoint, }); + /* tslint:disable-next-line */ + if (typeof window !== 'undefined') { + /** + * This needs to be setup before any + * non-transition async work so it can be dereferenced + * in the dismiss method. The dismiss method + * only waits for the entering transition + * to finish. It does not wait for all of the `present` + * method to resolve. + */ + this.keyboardOpenCallback = () => { + if (this.gesture) { + /** + * When the native keyboard is opened and the webview + * is resized, the gesture implementation will become unresponsive + * and enter a free-scroll mode. + * + * When the keyboard is opened, we disable the gesture for + * a single frame and re-enable once the contents have repositioned + * from the keyboard placement. + */ + this.gesture.enable(false); + raf(() => { + if (this.gesture) { + this.gesture.enable(true); + } + }); + } + }; + window.addEventListener(KEYBOARD_DID_OPEN, this.keyboardOpenCallback); + } + /** * TODO (FW-937) - In the next major release of Ionic, all card modals * will be swipeable by default. canDismiss will be used to determine if the @@ -559,31 +591,7 @@ export class Modal implements ComponentInterface, OverlayInterface { if (this.isSheetModal) { this.initSheetGesture(); } else if (hasCardModal) { - await this.initSwipeToClose(); - } - - /* tslint:disable-next-line */ - if (typeof window !== 'undefined') { - this.keyboardOpenCallback = () => { - if (this.gesture) { - /** - * When the native keyboard is opened and the webview - * is resized, the gesture implementation will become unresponsive - * and enter a free-scroll mode. - * - * When the keyboard is opened, we disable the gesture for - * a single frame and re-enable once the contents have repositioned - * from the keyboard placement. - */ - this.gesture.enable(false); - raf(() => { - if (this.gesture) { - this.gesture.enable(true); - } - }); - } - }; - window.addEventListener(KEYBOARD_DID_OPEN, this.keyboardOpenCallback); + this.initSwipeToClose(); } this.currentTransition = undefined; @@ -725,6 +733,7 @@ export class Modal implements ComponentInterface, OverlayInterface { /* tslint:disable-next-line */ if (typeof window !== 'undefined' && this.keyboardOpenCallback) { window.removeEventListener(KEYBOARD_DID_OPEN, this.keyboardOpenCallback); + this.keyboardOpenCallback = undefined; } /**