From b5c736f5ac829efebedf3256ddf77ab3daa7a5f6 Mon Sep 17 00:00:00 2001 From: Liam DeBeasi Date: Tue, 19 Sep 2023 12:11:15 -0400 Subject: [PATCH] fix(scroll-assist): improve input scroll accuracy with native resizing (#28169) Issue number: Part of #22940 --------- ## What is the current behavior? While working on a fix for #22940, I discovered another bug that impacted the reliability of my proposed fix for #22940. When we compute the scroll data (i.e. how much the input needs to be scrolled by), we subtract the `keyboardHeight` from the `platformHeight` (i.e. the viewport height): https://github.com/ionic-team/ionic-framework/blob/1015c06cbef4ceb10d43e722157c04844d984509/core/src/utils/input-shims/hacks/scroll-data.ts#L34 Every time we tap between inputs (even if the keyboard is already open) we re-run scroll assist because the newly focused input could be partially obscured by the keyboard. However, in this case we scroll by too much because we effectively subtract the keyboard height twice. This is because by the time we compute `platformHeight`, the platform dimensions have already shrunk to account for the keyboard (when the webview resizes). As a result, when we subtract `keyboardHeight` we get a visible area that is much smaller than the actual visible area. Examples below with different resize modes. Notice that with the "Native" resize mode (entire webview resizes when keyboard is open) tapping into other inputs scrolls the content by much more than it needs to. | Body | Native | Ionic | None | | - | - | - | - | | | | | | ## What is the new behavior? - We now compute the viewport height on load rather than on focus. This ensures that we always use the full viewport height when computing the visible area. | Body | Native | Ionic | None | | - | - | - | - | | | | | | ## Does this introduce a breaking change? - [ ] Yes - [x] No ## Other information Dev build: `7.3.4-dev.11694548895.1578981b` --- .../utils/input-shims/hacks/scroll-assist.ts | 32 +++++++++++++++++-- .../utils/input-shims/hacks/scroll-data.ts | 9 ++++-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/core/src/utils/input-shims/hacks/scroll-assist.ts b/core/src/utils/input-shims/hacks/scroll-assist.ts index 97928d608e..400b201f09 100644 --- a/core/src/utils/input-shims/hacks/scroll-assist.ts +++ b/core/src/utils/input-shims/hacks/scroll-assist.ts @@ -1,4 +1,5 @@ import type { KeyboardResizeOptions } from '@capacitor/keyboard'; +import { win } from '@utils/browser'; import { getScrollElement, scrollByPoint } from '../../content'; import { raf } from '../../helpers'; @@ -34,6 +35,21 @@ export const enableScrollAssist = ( const addScrollPadding = enableScrollPadding && (keyboardResize === undefined || keyboardResize.mode === KeyboardResize.None); + /** + * When adding scroll padding we need to know + * how much of the viewport the keyboard obscures. + * We do this by subtracting the keyboard height + * from the platform height. + * + * If we compute this value when switching between + * inputs then the webview may already be resized. + * At this point, `win.innerHeight` has already accounted + * for the keyboard meaning we would then subtract + * the keyboard height again. This will result in the input + * being scrolled more than it needs to. + */ + const platformHeight = win !== undefined ? win.innerHeight : 0; + /** * When the input is about to receive * focus, we need to move it to prevent @@ -50,7 +66,16 @@ export const enableScrollAssist = ( inputEl.removeAttribute(SKIP_SCROLL_ASSIST); return; } - jsSetFocus(componentEl, inputEl, contentEl, footerEl, keyboardHeight, addScrollPadding, disableClonedInput); + jsSetFocus( + componentEl, + inputEl, + contentEl, + footerEl, + keyboardHeight, + addScrollPadding, + disableClonedInput, + platformHeight + ); }; componentEl.addEventListener('focusin', focusIn, true); @@ -84,12 +109,13 @@ const jsSetFocus = async ( footerEl: HTMLIonFooterElement | null, keyboardHeight: number, enableScrollPadding: boolean, - disableClonedInput = false + disableClonedInput = false, + platformHeight = 0 ) => { if (!contentEl && !footerEl) { return; } - const scrollData = getScrollData(componentEl, (contentEl || footerEl)!, keyboardHeight); + const scrollData = getScrollData(componentEl, (contentEl || footerEl)!, keyboardHeight, platformHeight); if (contentEl && Math.abs(scrollData.scrollAmount) < 4) { // the text input is in a safe position that doesn't diff --git a/core/src/utils/input-shims/hacks/scroll-data.ts b/core/src/utils/input-shims/hacks/scroll-data.ts index 48b4a12007..cc82553b55 100644 --- a/core/src/utils/input-shims/hacks/scroll-data.ts +++ b/core/src/utils/input-shims/hacks/scroll-data.ts @@ -9,13 +9,18 @@ export interface ScrollData { inputSafeY: number; } -export const getScrollData = (componentEl: HTMLElement, contentEl: HTMLElement, keyboardHeight: number): ScrollData => { +export const getScrollData = ( + componentEl: HTMLElement, + contentEl: HTMLElement, + keyboardHeight: number, + platformHeight: number +): ScrollData => { const itemEl = (componentEl.closest('ion-item,[ion-item]') as HTMLElement) ?? componentEl; return calcScrollData( itemEl.getBoundingClientRect(), contentEl.getBoundingClientRect(), keyboardHeight, - (componentEl as any).ownerDocument.defaultView.innerHeight // TODO(FW-2832): type + platformHeight ); };