From 7871210e9e4ecc09353b821b60f977498a01ee8d Mon Sep 17 00:00:00 2001 From: Liam DeBeasi Date: Wed, 31 May 2023 14:54:38 -0400 Subject: [PATCH] fix(keyboard): listen on correct events for keyboard lifecycle (#27569) Issue number: resolves #27558 --------- ## What is the current behavior? The keyboard lifecycle util was always listening on visual viewport events even when in native environments. The visual viewport API is not reliable due to how the Ionic webview customizes how it resizes. We should only be listening on the native keyboard events in native environments. https://github.com/ionic-team/capacitor/issues/3730 https://github.com/ionic-team/capacitor-plugins/blob/2e883f39329626062bcf876fe7d424432617229d/keyboard/ios/Plugin/Keyboard.m#L90-L98 ## What is the new behavior? - Keyboard lifecycle util listens on native keyboard events only when in native environment ## Does this introduce a breaking change? - [ ] Yes - [x] No ## Other information --- core/src/utils/keyboard/keyboard.ts | 44 +++++++++++++++++++---------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/core/src/utils/keyboard/keyboard.ts b/core/src/utils/keyboard/keyboard.ts index 730e221294..2b43d3cacb 100644 --- a/core/src/utils/keyboard/keyboard.ts +++ b/core/src/utils/keyboard/keyboard.ts @@ -1,3 +1,5 @@ +import { Keyboard } from '../native/keyboard'; + export const KEYBOARD_DID_OPEN = 'ionKeyboardDidShow'; export const KEYBOARD_DID_CLOSE = 'ionKeyboardDidHide'; const KEYBOARD_THRESHOLD = 150; @@ -19,23 +21,35 @@ export const resetKeyboardAssist = () => { }; export const startKeyboardAssist = (win: Window) => { - startNativeListeners(win); + const nativeEngine = Keyboard.getEngine(); - if (!(win as any).visualViewport) { - return; - } - - currentVisualViewport = copyVisualViewport((win as any).visualViewport); - - (win as any).visualViewport.onresize = () => { - trackViewportChanges(win); - - if (keyboardDidOpen() || keyboardDidResize(win)) { - setKeyboardOpen(win); - } else if (keyboardDidClose(win)) { - setKeyboardClose(win); + /** + * If the native keyboard plugin is available + * then we are running in a native environment. As a result + * we should only listen on the native events instead of + * using the Visual Viewport as the Ionic webview manipulates + * how it resizes such that the Visual Viewport API is not + * reliable here. + */ + if (nativeEngine !== undefined) { + startNativeListeners(win); + } else { + if (!(win as any).visualViewport) { + return; } - }; + + currentVisualViewport = copyVisualViewport((win as any).visualViewport); + + (win as any).visualViewport.onresize = () => { + trackViewportChanges(win); + + if (keyboardDidOpen() || keyboardDidResize(win)) { + setKeyboardOpen(win); + } else if (keyboardDidClose(win)) { + setKeyboardClose(win); + } + }; + } }; /**