Skip to content

Instantly share code, notes, and snippets.

@ryanmorr
Last active March 30, 2024 06:51
Show Gist options
  • Save ryanmorr/c3d4a1513f09fb35ac1fff853b533b3d to your computer and use it in GitHub Desktop.
Save ryanmorr/c3d4a1513f09fb35ac1fff853b533b3d to your computer and use it in GitHub Desktop.
Determine if the user has a touch-enabled device
// 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