Skip to content

Instantly share code, notes, and snippets.

@charleslouis
Created September 22, 2018 04:02
Show Gist options
  • Save charleslouis/e1785538f0bc87060ff9f8893c32cf8f to your computer and use it in GitHub Desktop.
Save charleslouis/e1785538f0bc87060ff9f8893c32cf8f to your computer and use it in GitHub Desktop.
export function detectTouch() {
// source https://codeburst.io/the-only-way-to-detect-touch-with-javascript-7791a3346685
window.addEventListener('touchstart', function onFirstTouch() {
// we could use a class
document.body.classList.add('user-is-touching');
// or set some global variable
// window.USER_IS_TOUCHING = true;
// or set your app's state however you normally would
// frameworkOfChoice.dispatchEvent('USER_IS_TOUCHING', true);
// we only need to know once that a human touched the screen, so we can stop listening now
window.removeEventListener('touchstart', onFirstTouch, false);
}, false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment