Detect if a device is mouseOnly, touchOnly, or hybrid, and if the primary input is mouse or touch.
Installations
npm install detect-it
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
14.15.4
NPM Version
7.5.3
Statistics
419 Stars
143 Commits
13 Forks
11 Watching
2 Branches
1 Contributors
Updated on 13 Nov 2024
Bundle Size
1.23 kB
Minified
603.00 B
Minified + Gzipped
Languages
TypeScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
28,316,910
Last day
5.4%
41,085
Compared to previous day
Last week
4.6%
198,350
Compared to previous week
Last month
9.3%
828,390
Compared to previous month
Last year
18.7%
9,862,488
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
4
Detect It
- Live detection demo
- Detect if a device is
mouseOnly
,touchOnly
, orhybrid
- Detect if the primary input is
mouse
ortouch
- Detect if the browser supports Pointer Events, Touch Events, and passive event listeners
- You may also be interested in Event From, which determines if a browser event was caused by
mouse
,touch
, orkey
input
Detect It's state is determined using multiple media query and API detections. Detect It uses the hover
and pointer
media queries, the Pointer Events API and max touch points detections, and two Touch Events API detections (browsers respond differently to each Touch Events API detection depending on the device 😩 welcome to WebDev). But now you don't have to worry about any of this, just let Detect It handle the details while you optimize your app for the type of device that's being used. 😁
Detect It has been tested on numerous real world devices (since 2016), and the tests mock multiple devices and edge cases to ensure accurate results. The detection relies on how the browser presents the capabilities of the device as it is not possible to access the device hardware directly.
CDN option ⚡️ Recommended usage ⚡️ Device responsive UX ⚡️ Setting event listeners ⚡️ Detection details
npm install --save detect-it
1import * as detectIt from 'detect-it'; 2// OR 3import { 4 deviceType, 5 primaryInput, 6 supportsPointerEvents, 7 supportsTouchEvents, 8 supportsPassiveEvents, 9} from 'detect-it';
1// types 2deviceType: 'mouseOnly' | 'touchOnly' | 'hybrid'; 3primaryInput: 'mouse' | 'touch'; 4supportsPointerEvents: boolean; 5supportsTouchEvents: boolean; 6supportsPassiveEvents: boolean;
deviceType
mouseOnly
| touchOnly
| hybrid
Indicates if the the device is mouseOnly
, touchOnly
or hybrid
. For info on how the detection works and how specific devices are classified see the Detection details section.
1import { deviceType } from 'detect-it'; 2 3if (deviceType === 'hybrid') { 4 // ensure the site is usable by both mouse and touch input 5}
primaryInput
mouse
| touch
Indicates if the primary input for the device is mouse
or touch
. For more info on how to use primaryInput
see the Recommended usage section.
1import { primaryInput } from 'detect-it'; 2 3if (primaryInput === 'touch') { 4 // tailor UX for touch input 5} else { 6 // tailor UX for mouse input 7}
supportsPointerEvents
boolean
Indicates if the browser supports the Pointer Events API. See MDN's Pointer Events and the W3C Pointer Events specification for more information on Pointer Events. See Can I use for current support.
1import { supportsPointerEvents } from 'detect-it'; 2 3if (supportsPointerEvents) { 4 element.addEventListener('pointerenter', handlePointerEnter, false); 5}
supportsTouchEvents
boolean
Indicates if the browser supports the Touch Events API. See MDN's Touch Events and the W3C Touch Events specification for more information on Touch Events.
1import { supportsTouchEvents } from 'detect-it'; 2 3if (supportsTouchEvents) { 4 element.addEventListener('touchstart', handleTouchStart, false); 5}
supportsPassiveEvents
boolean
Indicates if the browser supports passive event listeners. See this Passive Events Explainer for more information on passive events. See Can I use for current support.
1import { supportsPassiveEvents } from 'detect-it';
2
3if (supportsPassiveEvents) {
4 // passive events are supported by the browser
5 document.addEventListener('scroll', handleScroll, {
6 capture: false,
7 passive: true,
8 });
9} else {
10 // passive events are not supported by the browser
11 document.addEventListener('scroll', handleScroll, false);
12}
Pre-built CDN option
Optionally, instead of using npm install
you can load Detect It directly in the browser. A minified and production ready UMD version is available from the Unpkg CDN for this purpose.
1<!-- in index.html --> 2<script src="https://unpkg.com/detect-it@4/dist/detect-it.umd.production.js"></script>
1// it will be available on the window as DetectIt 2if (window.DetectIt.primaryInput === 'touch') { 3 // tailor UX for touch input 4}
Recommended usage
TL;DR:
- Use
primaryInput
to optimize the user experience for eithermouse
ortouch
input (note that the app should still be usable by both inputs). Use this along with classic responsive design that adapts to screen/window size to create a fully device responsive app. - Listening for user interactions:
- If the browser
supportsPointerEvents
then only set Pointer Event listeners and usepointerType
to determine if the interaction was frommouse
ortouch
. - Otherwise always set both Mouse Event and Touch Event listeners and use Event From to ignore Mouse Events generated from touch input.
- If the browser
Device responsive UX
Device responsive UX is about creating web apps that feel native on every device. This goes beyond classic responsive design, which only responds to the screen/window size, and includes how the user can interact with the app (the capabilities of the device). Can the user hover, swipe, long press, etc?
There are 3 parts of device responsive UX: Size (size of screen/window), Capabilities (what the user can do/capabilities of the device), and Interaction (is the user hovering, touching, etc). Size and Capabilities need to be known at render time (when the UI is rendered before the user interacts with it), and Interaction needs to be known at interaction time (when the user is interacting with the app).
- Size
- This can be determined using media queries, for example
(max-width: 600px)
, either applied via CSS or in JavaScript by using something like React Media.
- This can be determined using media queries, for example
- Capabilities
- This is what Detect It is for - knowing at render time what the capabilities of the device are. There are a number of ways that you can use
deviceType
orprimaryInput
to optimize the UX for the capabilities of the device, however, in most cases I've found it makes sense to just useprimaryInput
and optimize the UX formouse
ortouch
, while ensuring that the app is still usable by both inputs.
- This is what Detect It is for - knowing at render time what the capabilities of the device are. There are a number of ways that you can use
- Putting Size and Capabilities together, one approach is to optimize the UX for 4 scenarios:
- Wide screen with
primaryInput
mouse
: desktop/laptop with a normal window - Narrow screen and
primaryInput
mouse
: desktop/laptop with a narrow window - Wide screen with
primaryInput
touch
: tablet - Narrow screen with
primaryInput
touch
: phone
- Wide screen with
- Interaction
- Is the user hovering, touching, etc. To help with this I created React Interactive which provides a callback for interactive state changes (
hover
,mouseActive
,touchActive
,keyActive
) and allows you to style touch interactions in a way that feels native and is not possible with CSS pseudo classes.
- Is the user hovering, touching, etc. To help with this I created React Interactive which provides a callback for interactive state changes (
Setting event listeners
Setting event listeners can be thought of as either setting Pointer Event listeners or setting Mouse Event and Touch Event listeners. Pointer Events can do everything that Mouse Events and Touch Events can do (and more), without having to worry about if a Mouse Event was caused by touch input and so should be ignored. It is generally preferred to use Pointer Events if they are supported.
Pointer Event listeners
If the browser supportsPointerEvents
then only set Pointer Event listeners and use pointerType
to determine if the interaction was from mouse
or touch
.
1import { supportsPointerEvents } from 'detect-it'; 2 3const handlePointerEnter = (e) => { 4 if (e.pointerType === 'mouse') { 5 // event from mouse input 6 } else { 7 // event from touch input 8 // note that pointerType can be 'mouse', 'touch' or 'pen' 9 // but in most situations it makes it makes sense to treat 'touch' and 'pen' as the same 10 } 11}; 12 13if (supportsPointerEvents) { 14 element.addEventListener('pointerenter', handlePointerEnter, false); 15} else { 16 // set mouse and touch event listeners 17}
Mouse Event and Touch Event listeners
If the browser doesn't support Pointer Events, then there are a couple of ways to approach setting mouse and touch event listeners.
Note that a touch interaction will fire Touch Events as the interaction is in progress (touch on the screen), and will fire Mouse Events during a long press (extended touch on the screen), or after the touch interaction has finished (after the touch is removed from the screen) to support sites that only listen for Mouse Events.
Option 1: If the device is mouseOnly
or touchOnly
then only set mouse or touch listeners, and if the device is hybrid
set both mouse and touch event listeners and ignore Mouse Events caused by touch input (you can use Event From for this).
Option 2: Always set both mouse and touch event listeners and use Event From to ignore Mouse Events from touch input.
I prefer option 2 as it's simpler to code and I haven't noticed any performance impact from setting extra listeners (note that setting Touch Event listeners on a browser that doesn't support Touch Events is fine, the browser will just ignore the event listeners).
1import { supportsPointerEvents } from 'detect-it'; 2import { eventFrom } from 'event-from'; 3 4const handleMouseEnter = (e) => { 5 if (eventFrom(e) !== 'mouse') return; 6 // code for handling mouse enter event from mouse input 7}; 8 9const handleTouchStart = (e) => { 10 // code for handling touch start from touch input 11}; 12 13if (supportsPointerEvents) { 14 // set pointer event listeners 15} else { 16 // Pointer Events are not supported so set both Mouse Event and Touch Event listeners 17 element.addEventListener('mouseenter', handleMouseEnter, false); 18 element.addEventListener('touchstart', handleTouchStart, false); 19}
Detection details
Determining the deviceType
and primaryInput
To determine the deviceType
and primaryInput
Detect It uses several media query and API detections to triangulate what type of device is being used. The entire detection is done when the script is imported so the results are known at render time (Detect It doesn't set any event listeners).
Detect It uses the hover
and pointer
media queries, the Pointer Events API and max touch points detections, and two Touch Events API detections (browsers respond differently to each Touch Events API detection depending on the device). For more on this see the comments in the source code for notes about detecting the device type and edge cases.
Device tests and limitations
Detect It has been tested on numerous real world devices (since 2016), and the tests mock multiple devices and edge cases to ensure accurate results. However, these detections are limited by how the browser presents the capabilities of the device (the APIs it exposes and how it responds to media queries) so there are some limitations. For example, on an iPad it is impossible to tell if a mouse is connected, so Detect It always treats iPads as a hybrid
device with primaryInput
touch
.
In the case of a legacy browser or device that doesn't support the detections (e.g. no media query or Pointer Events support), Detect It will fall back to a default mouseOnly
or touchOnly
state.
Hybrid device definition
Detect It has a wide definition for what constitutes a hybrid
device, or rather a strict definition for what are mouseOnly
and touchOnly
devices, because if a device strays from only a fine point and hover with a mouse, or a coarse touch with a finger, then it should be treated uniquely when considering how the user will interact with it. Below is the source code for determining deviceType
:
1// a hybrid device is one that both hasTouch and 2// any input can hover or has a fine pointer, or the primary pointer is not coarse 3// if it's not a hybrid, then if it hasTouch it's touchOnly, otherwise it's mouseOnly 4export const deviceType = 5 hasTouch && (hasAnyHoverOrAnyFinePointer || !hasCoarsePrimaryPointer) 6 ? 'hybrid' 7 : hasTouch 8 ? 'touchOnly' 9 : 'mouseOnly';
Some hybrid
device examples
- A touch capable Chromebook
- A touch capable Windows computer (both when it's used as a regular computer, and when in tablet mode, e.g. Microsoft Surface without a keyboard)
- A Samsung Galaxy Note with stylus
- All iPads now that they support a mouse and keyboard (note that Apple makes it impossible to know if a mouse or keyboard is attached, so iPads are always treated as a
hybrid
withprimaryInput
touch
)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 1/25 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 8 are checked with a SAST tool
Reason
116 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-whgm-jr23-g3j9
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-fwr7-v2mv-hh25
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw
- Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-3wcq-x3mq-6r9p
- Warn: Project is vulnerable to: GHSA-phwq-j96m-2c2q
- Warn: Project is vulnerable to: GHSA-ghr5-ch3p-vcr6
- Warn: Project is vulnerable to: GHSA-434g-2637-qmqr
- Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m
- Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw
- Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
- Warn: Project is vulnerable to: GHSA-4gmj-3p3h-gm8h
- Warn: Project is vulnerable to: GHSA-6h5x-7c5m-7cr7
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-74fj-2j2h-c42q
- Warn: Project is vulnerable to: GHSA-pw2r-vq6v-hr8c
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-8mmm-9v2q-x3f9
- Warn: Project is vulnerable to: GHSA-c7qv-q95q-8v27
- Warn: Project is vulnerable to: GHSA-33f9-j839-rf8h
- Warn: Project is vulnerable to: GHSA-c36v-fmgq-m8hx
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-qrpm-p2h7-hrv2
- Warn: Project is vulnerable to: GHSA-5rrq-pxf6-6jx5
- Warn: Project is vulnerable to: GHSA-8fr3-hfg3-gpgp
- Warn: Project is vulnerable to: GHSA-gf8q-jrpm-jvxq
- Warn: Project is vulnerable to: GHSA-2r2c-g63r-vccr
- Warn: Project is vulnerable to: GHSA-cfm4-qjh2-4765
- Warn: Project is vulnerable to: GHSA-x4jg-mjrx-434g
- Warn: Project is vulnerable to: GHSA-rp65-9cf3-cjxr
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-566m-qj78-rww5
- Warn: Project is vulnerable to: GHSA-hwj9-h5mp-3pm3
- Warn: Project is vulnerable to: GHSA-7fh5-64p2-3v2j
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-g4rg-993r-mgx7
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-hh27-ffr2-f2jc
- Warn: Project is vulnerable to: GHSA-rqff-837h-mm52
- Warn: Project is vulnerable to: GHSA-8v38-pw62-9cw2
- Warn: Project is vulnerable to: GHSA-hgjh-723h-mx2j
- Warn: Project is vulnerable to: GHSA-jf5r-8hm2-f872
- Warn: Project is vulnerable to: GHSA-wr3j-pwj9-hqq6
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-j4f2-536g-r55m
- Warn: Project is vulnerable to: GHSA-r7qp-cfhv-p84w
- Warn: Project is vulnerable to: GHSA-pfrx-2q88-qq97
- Warn: Project is vulnerable to: GHSA-7f5c-rpf4-86p8
- Warn: Project is vulnerable to: GHSA-rc47-6667-2j5j
- Warn: Project is vulnerable to: GHSA-8hfj-j24r-96c4
- Warn: Project is vulnerable to: GHSA-wc69-rhjr-hc9g
- Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g
- Warn: Project is vulnerable to: GHSA-r8f7-9pfq-mjmv
- Warn: Project is vulnerable to: GHSA-px4h-xg32-q955
- Warn: Project is vulnerable to: GHSA-3965-hpx2-q597
- Warn: Project is vulnerable to: GHSA-35q2-47q7-3pc3
- Warn: Project is vulnerable to: GHSA-7mwh-4pqv-wmr8
- Warn: Project is vulnerable to: GHSA-25hc-qcg6-38wj
- Warn: Project is vulnerable to: GHSA-qm95-pgcg-qqfq
- Warn: Project is vulnerable to: GHSA-cqmj-92xf-r6r9
- Warn: Project is vulnerable to: GHSA-wv8q-r932-8hc7
- Warn: Project is vulnerable to: GHSA-8266-84wp-wv5c
- Warn: Project is vulnerable to: GHSA-7p7h-4mm5-852v
- Warn: Project is vulnerable to: GHSA-rjf2-j2r6-q8gr
- Warn: Project is vulnerable to: GHSA-6pw2-5hjv-9pf7
- Warn: Project is vulnerable to: GHSA-4w2j-2rg4-5mjw
- Warn: Project is vulnerable to: GHSA-mrgp-mrhc-5jrq
- Warn: Project is vulnerable to: GHSA-7jxr-cg7f-gpgv
- Warn: Project is vulnerable to: GHSA-xj72-wvfv-8985
- Warn: Project is vulnerable to: GHSA-ch3r-j5x3-6q2m
- Warn: Project is vulnerable to: GHSA-p5gc-c584-jj6v
- Warn: Project is vulnerable to: GHSA-whpj-8f3w-67p5
- Warn: Project is vulnerable to: GHSA-cchq-frgv-rjh5
- Warn: Project is vulnerable to: GHSA-g644-9gfx-q4q4
- Warn: Project is vulnerable to: GHSA-g3ch-rx76-35fx
- Warn: Project is vulnerable to: GHSA-hc6q-2mpp-qw7j
- Warn: Project is vulnerable to: GHSA-4vvj-4cpr-p986
- Warn: Project is vulnerable to: GHSA-72mh-269x-7mh5
- Warn: Project is vulnerable to: GHSA-h4j5-c7cj-74xg
Score
1.7
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to detect-it
detect-package-manager
Detect which package manager you're using (yarn or npm)
detect-node-es
Detect Node.JS (as opposite to browser environment). ESM modification
detect-newline
Detect the dominant newline character of a string
type-detect
Improved typeof detection for node.js and the browser.