Skip to content

Instantly share code, notes, and snippets.

@leodutra
Last active May 5, 2023 17:57
Show Gist options
  • Save leodutra/64d90b988535f9e15464165718a49064 to your computer and use it in GitHub Desktop.
Save leodutra/64d90b988535f9e15464165718a49064 to your computer and use it in GitHub Desktop.
Detect if browser is mobile - JavaScript - using feature detection
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