From 3b8e815340e3098ab5b438128e8ed1a07906ae8c Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 4 May 2020 12:57:23 +0300 Subject: [PATCH] Add supportsTouchEvents --- snippets/supportsTouchEvents.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 snippets/supportsTouchEvents.md diff --git a/snippets/supportsTouchEvents.md b/snippets/supportsTouchEvents.md new file mode 100644 index 000000000..3d704b2ec --- /dev/null +++ b/snippets/supportsTouchEvents.md @@ -0,0 +1,18 @@ +--- +title: supportsTouchEvents +tags: browser,intermediate +--- + +Returns `true` if touch events are supported, `false` otherwise. + +Check if `ontouchstart` exists in `window` or `window.DocumentTouch` is true and the current `document` is an instance of it. + +```js +const supportsTouchEvents = () => + window && + ('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch); +``` + +```js +supportsTouchEvents(); // true +```