Skip to content

Commit

Permalink
fix(gesture): unable to move text cursor and tap away on mobile
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 angular#10301.
Fixes angular#5365.
  • Loading branch information
crisbeto committed Jul 27, 2017
1 parent 72f930b commit 7432cd7
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/core/services/gesture/gesture.js
Original file line number Diff line number Diff line change
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 7432cd7

Please sign in to comment.