Skip to content

Commit b5c736f

Browse files
authored
fix(scroll-assist): improve input scroll accuracy with native resizing (#28169)
Issue number: Part of #22940 --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> 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 | | - | - | - | - | | <video src="https://github.com/ionic-team/ionic-framework/assets/2721089/06d1cd20-0349-4a59-ad85-c1c8a8a03caa"></video> | <video src="https://github.com/ionic-team/ionic-framework/assets/2721089/1d4e8363-a69b-45c4-931c-d6227e548ec9"></video> | <video src="https://github.com/ionic-team/ionic-framework/assets/2721089/7e4304c1-7d56-48c8-aed8-16fc7e51641a"></video> | <video src="https://github.com/ionic-team/ionic-framework/assets/2721089/7869c5e0-b202-46e1-af82-49e41b3b067e"></video> | ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - 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 | | - | - | - | - | | <video src="https://github.com/ionic-team/ionic-framework/assets/2721089/c5a66287-0cad-42db-bece-da16edad60e3"></video> | <video src="https://github.com/ionic-team/ionic-framework/assets/2721089/372a45c8-e8bd-43d2-bf50-d87b7250e9b3"></video> | <video src="https://github.com/ionic-team/ionic-framework/assets/2721089/3d656467-8e2e-48cc-8d72-dc89a67ef8b1"></video> | <video src="https://github.com/ionic-team/ionic-framework/assets/2721089/19969535-7d06-404c-98e4-ae49957e0ffe"></video> | ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change, please describe the impact and migration path for existing applications below. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> Dev build: `7.3.4-dev.11694548895.1578981b`
1 parent 81714d4 commit b5c736f

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

core/src/utils/input-shims/hacks/scroll-assist.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { KeyboardResizeOptions } from '@capacitor/keyboard';
2+
import { win } from '@utils/browser';
23

34
import { getScrollElement, scrollByPoint } from '../../content';
45
import { raf } from '../../helpers';
@@ -34,6 +35,21 @@ export const enableScrollAssist = (
3435
const addScrollPadding =
3536
enableScrollPadding && (keyboardResize === undefined || keyboardResize.mode === KeyboardResize.None);
3637

38+
/**
39+
* When adding scroll padding we need to know
40+
* how much of the viewport the keyboard obscures.
41+
* We do this by subtracting the keyboard height
42+
* from the platform height.
43+
*
44+
* If we compute this value when switching between
45+
* inputs then the webview may already be resized.
46+
* At this point, `win.innerHeight` has already accounted
47+
* for the keyboard meaning we would then subtract
48+
* the keyboard height again. This will result in the input
49+
* being scrolled more than it needs to.
50+
*/
51+
const platformHeight = win !== undefined ? win.innerHeight : 0;
52+
3753
/**
3854
* When the input is about to receive
3955
* focus, we need to move it to prevent
@@ -50,7 +66,16 @@ export const enableScrollAssist = (
5066
inputEl.removeAttribute(SKIP_SCROLL_ASSIST);
5167
return;
5268
}
53-
jsSetFocus(componentEl, inputEl, contentEl, footerEl, keyboardHeight, addScrollPadding, disableClonedInput);
69+
jsSetFocus(
70+
componentEl,
71+
inputEl,
72+
contentEl,
73+
footerEl,
74+
keyboardHeight,
75+
addScrollPadding,
76+
disableClonedInput,
77+
platformHeight
78+
);
5479
};
5580
componentEl.addEventListener('focusin', focusIn, true);
5681

@@ -84,12 +109,13 @@ const jsSetFocus = async (
84109
footerEl: HTMLIonFooterElement | null,
85110
keyboardHeight: number,
86111
enableScrollPadding: boolean,
87-
disableClonedInput = false
112+
disableClonedInput = false,
113+
platformHeight = 0
88114
) => {
89115
if (!contentEl && !footerEl) {
90116
return;
91117
}
92-
const scrollData = getScrollData(componentEl, (contentEl || footerEl)!, keyboardHeight);
118+
const scrollData = getScrollData(componentEl, (contentEl || footerEl)!, keyboardHeight, platformHeight);
93119

94120
if (contentEl && Math.abs(scrollData.scrollAmount) < 4) {
95121
// the text input is in a safe position that doesn't

core/src/utils/input-shims/hacks/scroll-data.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ export interface ScrollData {
99
inputSafeY: number;
1010
}
1111

12-
export const getScrollData = (componentEl: HTMLElement, contentEl: HTMLElement, keyboardHeight: number): ScrollData => {
12+
export const getScrollData = (
13+
componentEl: HTMLElement,
14+
contentEl: HTMLElement,
15+
keyboardHeight: number,
16+
platformHeight: number
17+
): ScrollData => {
1318
const itemEl = (componentEl.closest('ion-item,[ion-item]') as HTMLElement) ?? componentEl;
1419
return calcScrollData(
1520
itemEl.getBoundingClientRect(),
1621
contentEl.getBoundingClientRect(),
1722
keyboardHeight,
18-
(componentEl as any).ownerDocument.defaultView.innerHeight // TODO(FW-2832): type
23+
platformHeight
1924
);
2025
};
2126

0 commit comments

Comments
 (0)