Skip to content

Instantly share code, notes, and snippets.

@stphn
Created November 19, 2019 00:29
Show Gist options
  • Save stphn/2fc17ad9dd7a2de089ef02c43d2783b1 to your computer and use it in GitHub Desktop.
Save stphn/2fc17ad9dd7a2de089ef02c43d2783b1 to your computer and use it in GitHub Desktop.
Detect Touch Devices
export const isTouchDevice = () => {
let prefixes = ' -webkit- -moz- -o- -ms- '.split(' ')
let mq = function(query) {
return window.matchMedia(query).matches
}
if (('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) {
return true
}
// include the 'heartz' as a way to have a non matching MQ to help terminate the join
// https://git.io/vznFH
let query = ['(', prefixes.join('touch-enabled),('), 'heartz', ')'].join('')
return mq(query)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment