Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
fix(gesture): unable to move text cursor and tap away on mobile (#10821)
Browse files Browse the repository at this point in the history
* Fixes not being able to move the text cursor within and input on mobile.
* Fixes not being able to blur an input by tapping away on mobile.

Both of the issues were due to the click hijacking from the gesture service being too aggressive and preventing focus from shifting.

Fixes #10301.
Fixes #5365.
  • Loading branch information
crisbeto authored and tinayuangao committed Aug 3, 2017
1 parent fa997b9 commit baa869a
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/core/services/gesture/gesture.js
Expand Up @@ -104,20 +104,9 @@ function MdGesture($$MdGestureHandler, $$rAF, $timeout) {
maxDistance: maxClickDistance
},
onEnd: function(ev, pointer) {
if (pointer.distance < this.state.options.maxDistance) {
if (canFocus(ev.target)) {
this.dispatchEvent(ev, 'focus', pointer);
ev.target.focus();
}
}

function canFocus(element) {
var focusableElements = ['INPUT', 'SELECT', 'BUTTON', 'TEXTAREA', 'VIDEO', 'AUDIO'];

return (element.getAttribute('tabindex') != '-1') &&
!element.hasAttribute('DISABLED') &&
(element.hasAttribute('tabindex') || element.hasAttribute('href') || element.isContentEditable ||
(focusableElements.indexOf(element.nodeName) != -1));
if (pointer.distance < this.state.options.maxDistance && canFocus(ev.target)) {
this.dispatchEvent(ev, 'focus', pointer);
ev.target.focus();
}
}
});
Expand Down Expand Up @@ -550,8 +539,14 @@ function attachToDocument( $mdGesture, $$MdGestureHandler ) {

function mouseInputHijacker(ev) {
var isKeyClick = !ev.clientX && !ev.clientY;
if (!isKeyClick && !ev.$material && !ev.isIonicTap
&& !isInputEventFromLabelClick(ev)) {

if (
!isKeyClick &&
!ev.$material &&
!ev.isIonicTap &&
!isInputEventFromLabelClick(ev) &&
(ev.type !== 'mousedown' || (!canFocus(ev.target) && !canFocus(document.activeElement)))
) {
ev.preventDefault();
ev.stopPropagation();
}
Expand Down Expand Up @@ -745,3 +740,18 @@ function getEventPoint(ev) {
(ev.changedTouches && ev.changedTouches[0]) ||
ev;
}

/** Checks whether an element can be focused. */
function canFocus(element) {
return (
!!element &&
element.getAttribute('tabindex') != '-1' &&
!element.hasAttribute('disabled') &&
(
element.hasAttribute('tabindex') ||
element.hasAttribute('href') ||
element.isContentEditable ||
['INPUT', 'SELECT', 'BUTTON', 'TEXTAREA', 'VIDEO', 'AUDIO'].indexOf(element.nodeName) != -1
)
);
}

0 comments on commit baa869a

Please sign in to comment.