Last active
May 5, 2023 17:57
-
-
Save leodutra/64d90b988535f9e15464165718a49064 to your computer and use it in GitHub Desktop.
Detect if browser is mobile - JavaScript - using feature detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const hasCoarsePointer = () => window.matchMedia("(pointer: coarse)").matches | |
const hasMobileWidth = (maxWidth = 639) => | |
window.matchMedia(`(max-width: ${maxWidth}px)`).matches | |
const hasMultipleTouchPoints = () => navigator.maxTouchPoints > 1 | |
const hasTouchEvents = () => "ontouchstart" in document.documentElement | |
export const isMobile = ({ maxWidth } = {}) => { | |
return ( | |
hasCoarsePointer() && | |
hasMultipleTouchPoints() && | |
hasMobileWidth() && | |
hasTouchEvents() | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment