Last active
March 30, 2024 06:51
-
-
Save ryanmorr/c3d4a1513f09fb35ac1fff853b533b3d to your computer and use it in GitHub Desktop.
Determine if the user has a touch-enabled device
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
// An aggressive means to detect if the user's device is touch only | |
// Probably bad practice: https://css-tricks.com/interaction-media-features-and-their-potential-for-incorrect-assumptions/ | |
function isTouch() { | |
return matchMedia('(hover: none) and (pointer: coarse)').matches; | |
} | |
// A more passive approach is to detect if the user's device is merely touch-enabled | |
function hasTouch() { | |
return matchMedia('(any-pointer:coarse)').matches; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment