Skip to content

Commit 77847f4

Browse files
committed
fix(keyboard): screen.height fallback for window.innerHeight
Closes #2168
1 parent ba3600d commit 77847f4

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

js/utils/keyboard.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
* It will also attempt to prevent the native overflow scrolling on focus,
1414
* which can cause layout issues such as pushing headers up and out of view.
1515
*
16-
* The keyboard fixes work best in conjunction with the
16+
* The keyboard fixes work best in conjunction with the
1717
* [Ionic Keyboard Plugin](https://github.com/driftyco/ionic-plugins-keyboard),
1818
* although it will perform reasonably well without. However, if you are using
1919
* Cordova there is no reason not to use the plugin.
2020
*
2121
* ### Hide when keyboard shows
22-
*
22+
*
2323
* To hide an element when the keyboard is open, add the class `hide-on-keyboard-open`.
2424
*
2525
* ```html
@@ -30,17 +30,17 @@
3030
* ----------
3131
*
3232
* ### Plugin Usage
33-
* Information on using the plugin can be found at
33+
* Information on using the plugin can be found at
3434
* [https://github.com/driftyco/ionic-plugins-keyboard](https://github.com/driftyco/ionic-plugins-keyboard).
3535
*
36-
* ----------
36+
* ----------
3737
*
3838
* ### Android Notes
39-
* - If your app is running in fullscreen, i.e. you have
39+
* - If your app is running in fullscreen, i.e. you have
4040
* `<preference name="Fullscreen" value="true" />` in your `config.xml` file
4141
* you will need to set `ionic.Platform.isFullScreen = true` manually.
4242
*
43-
* - You can configure the behavior of the web view when the keyboard shows by setting
43+
* - You can configure the behavior of the web view when the keyboard shows by setting
4444
* [android:windowSoftInputMode](http://developer.android.com/reference/android/R.attr.html#windowSoftInputMode)
4545
* to either `adjustPan`, `adjustResize` or `adjustNothing` in your app's
4646
* activity in `AndroidManifest.xml`. `adjustResize` is the recommended setting
@@ -57,11 +57,11 @@
5757
* out of view on input focus, try setting `cordova.plugins.Keyboard.disableScroll(true)`.
5858
* This does **not** disable scrolling in the Ionic scroll view, rather it
5959
* disables the native overflow scrolling that happens automatically as a
60-
* result of focusing on inputs below the keyboard.
61-
*
60+
* result of focusing on inputs below the keyboard.
61+
*
6262
*/
6363

64-
var keyboardViewportHeight = window.innerHeight;
64+
var keyboardViewportHeight = getViewportHeight();
6565
var keyboardIsOpen;
6666
var keyboardActiveElement;
6767
var keyboardFocusOutTimer;
@@ -205,8 +205,8 @@ function keyboardHide() {
205205
}
206206

207207
function keyboardUpdateViewportHeight() {
208-
if( window.innerHeight > keyboardViewportHeight ) {
209-
keyboardViewportHeight = window.innerHeight;
208+
if( getViewportHeight() > keyboardViewportHeight ) {
209+
keyboardViewportHeight = getViewportHeight();
210210
}
211211
}
212212

@@ -223,7 +223,7 @@ function keyboardPreventDefault(e) {
223223
}
224224

225225
function keyboardOrientationChange() {
226-
var updatedViewportHeight = window.innerHeight;
226+
var updatedViewportHeight = getViewportHeight();
227227

228228
//too slow, have to wait for updated height
229229
if (updatedViewportHeight === keyboardViewportHeight){
@@ -234,7 +234,7 @@ function keyboardOrientationChange() {
234234
clearInterval(pollViewportHeight);
235235
}
236236

237-
updatedViewportHeight = window.innerHeight;
237+
updatedViewportHeight = getViewportHeight();
238238

239239
if (updatedViewportHeight !== keyboardViewportHeight){
240240
if (updatedViewportHeight < keyboardViewportHeight){
@@ -267,10 +267,9 @@ function keyboardGetHeight() {
267267
return 275;
268268
}
269269
//otherwise, wait for the screen to resize
270-
if ( window.innerHeight < keyboardViewportHeight ){
271-
return keyboardViewportHeight - window.innerHeight;
272-
}
273-
else {
270+
if ( getViewportHeight() < keyboardViewportHeight ){
271+
return keyboardViewportHeight - getViewportHeight();
272+
} else {
274273
return 0;
275274
}
276275
}
@@ -293,6 +292,10 @@ function keyboardGetHeight() {
293292
return 275;
294293
}
295294

295+
function getViewportHeight() {
296+
return window.innerHeight || screen.height;
297+
}
298+
296299
function keyboardIsWithinScroll(ele) {
297300
while(ele) {
298301
if(ele.classList.contains(SCROLL_CONTAINER_CSS)) {

0 commit comments

Comments
 (0)