Skip to content

Commit 6cc559c

Browse files
committed
fix(keyboard): check if input is in scroll view
Closes #3586.
1 parent 08956b2 commit 6cc559c

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

js/utils/keyboard.js

+35-24
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ function keyboardFocusIn(e) {
297297
e.target.readOnly ||
298298
!ionic.tap.isKeyboardElement(e.target) ||
299299
!(scrollView = inputScrollView(e.target))) {
300+
keyboardActiveElement = null;
300301
return;
301302
}
302303

@@ -527,9 +528,11 @@ function keyboardHide() {
527528
ionic.keyboard.isOpen = false;
528529
ionic.keyboard.isClosing = false;
529530

530-
ionic.trigger('resetScrollView', {
531-
target: keyboardActiveElement
532-
}, true);
531+
if (keyboardActiveElement) {
532+
ionic.trigger('resetScrollView', {
533+
target: keyboardActiveElement
534+
}, true);
535+
}
533536

534537
ionic.requestAnimationFrame(function(){
535538
document.body.classList.remove(KEYBOARD_OPEN_CSS);
@@ -549,6 +552,8 @@ function keyboardHide() {
549552
if (keyboardHasPlugin()) cordova.plugins.Keyboard.close();
550553
keyboardActiveElement && keyboardActiveElement.blur();
551554
}
555+
556+
keyboardActiveElement = null;
552557
}
553558

554559
/**
@@ -557,36 +562,40 @@ function keyboardHide() {
557562
* the currently focused input into view if necessary.
558563
*/
559564
function keyboardShow() {
560-
var elementBounds = keyboardActiveElement.getBoundingClientRect();
565+
566+
ionic.keyboard.isOpen = true;
567+
ionic.keyboard.isOpening = false;
568+
561569
var details = {
562-
target: keyboardActiveElement,
563-
elementTop: Math.round(elementBounds.top),
564-
elementBottom: Math.round(elementBounds.bottom),
565570
keyboardHeight: keyboardGetHeight(),
566571
viewportHeight: keyboardCurrentViewportHeight
567572
};
568573

569-
details.windowHeight = details.viewportHeight - details.keyboardHeight;
570-
//console.log("keyboardShow viewportHeight: " + details.viewportHeight +
571-
//", windowHeight: " + details.windowHeight +
572-
//", keyboardHeight: " + details.keyboardHeight);
574+
if (keyboardActiveElement) {
575+
details.target = keyboardActiveElement;
573576

574-
// figure out if the element is under the keyboard
575-
details.isElementUnderKeyboard = (details.elementBottom > details.windowHeight);
576-
//console.log("isUnderKeyboard: " + details.isElementUnderKeyboard);
577-
//console.log("elementBottom: " + details.elementBottom);
577+
var elementBounds = keyboardActiveElement.getBoundingClientRect();
578578

579-
ionic.keyboard.isOpen = true;
580-
ionic.keyboard.isOpening = false;
579+
details.elementTop = Math.round(elementBounds.top);
580+
details.elementBottom = Math.round(elementBounds.bottom);
581+
582+
details.windowHeight = details.viewportHeight - details.keyboardHeight;
583+
//console.log("keyboardShow viewportHeight: " + details.viewportHeight +
584+
//", windowHeight: " + details.windowHeight +
585+
//", keyboardHeight: " + details.keyboardHeight);
586+
587+
// figure out if the element is under the keyboard
588+
details.isElementUnderKeyboard = (details.elementBottom > details.windowHeight);
589+
//console.log("isUnderKeyboard: " + details.isElementUnderKeyboard);
590+
//console.log("elementBottom: " + details.elementBottom);
581591

582-
// send event so the scroll view adjusts
583-
ionic.trigger('scrollChildIntoView', details, true);
592+
// send event so the scroll view adjusts
593+
ionic.trigger('scrollChildIntoView', details, true);
594+
}
584595

585596
setTimeout(function(){
586597
document.body.classList.add(KEYBOARD_OPEN_CSS);
587598
}, 400);
588-
589-
return details;
590599
}
591600

592601
/* eslint no-unused-vars:0 */
@@ -654,9 +663,11 @@ function keyboardUpdateViewportHeight() {
654663
keyboardPortraitViewportHeight = keyboardCurrentViewportHeight;
655664
}
656665

657-
ionic.trigger('resetScrollView', {
658-
target: keyboardActiveElement
659-
}, true);
666+
if (keyboardActiveElement) {
667+
ionic.trigger('resetScrollView', {
668+
target: keyboardActiveElement
669+
}, true);
670+
}
660671

661672
if (ionic.keyboard.isOpen && ionic.tap.isTextInput(keyboardActiveElement)) {
662673
keyboardShow();

0 commit comments

Comments
 (0)