Skip to content

Commit

Permalink
refactor(util): Avoid touch detection by userAgent (#2533)
Browse files Browse the repository at this point in the history
Touch detection based on navigator.userAgent isn't trustable.
Add some conditionals based on feature detection.

Fix #2526
  • Loading branch information
netil committed Feb 3, 2022
1 parent 69b0d09 commit 0ae640a
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/module/util.ts
Expand Up @@ -730,21 +730,28 @@ function isTabVisible(): boolean {
* @private
*/
function convertInputType(mouse: boolean, touch: boolean): "mouse" | "touch" | null {
let isMobile = false;
let hasTouch = false;

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent#Mobile_Tablet_or_Desktop
if (/Mobi/.test(window.navigator.userAgent) && touch) {
if (touch) {
// Some Edge desktop return true: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/20417074/
const hasTouchPoints = window.navigator && "maxTouchPoints" in window.navigator && window.navigator.maxTouchPoints > 0;
if (window.navigator && "maxTouchPoints" in window.navigator) {
hasTouch = window.navigator.maxTouchPoints > 0;

// Ref: https://github.com/Modernizr/Modernizr/blob/master/feature-detects/touchevents.js
// On IE11 with IE9 emulation mode, ('ontouchstart' in window) is returning true
const hasTouch = ("ontouchmove" in window || (window.DocumentTouch && document instanceof window.DocumentTouch));

isMobile = hasTouchPoints || hasTouch;
} else if ("ontouchmove" in window || (window.DocumentTouch && document instanceof window.DocumentTouch)) {
hasTouch = true;
} else {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent#avoiding_user_agent_detection
const mQ = window.matchMedia && matchMedia("(pointer:coarse)");

if (mQ && mQ.media === "(pointer:coarse)") {
hasTouch = !!mQ.matches;
}
}
}

const hasMouse = mouse && !isMobile ? ("onmouseover" in window) : false;
const hasMouse = mouse && !hasTouch ? ("onmouseover" in window) : false;

return (hasMouse && "mouse") || (isMobile && "touch") || null;
return (hasMouse && "mouse") || (hasTouch && "touch") || null;
}

0 comments on commit 0ae640a

Please sign in to comment.