Detect It
Detect if a device is mouse only, touch only, or hybrid, and if it supports passive event listeners.
Live detection test
Exports a reference to a singleton object (a micro state machine with an update function) with its state set to if the device is mouse only, touch only, or hybrid (and other related info about the device), as well as an update()
function which updates the object's state.
detect-it
's state is based on the state of the four micro state machines that it contains (detect-hover
, detect-pointer
, detect-touch-events
, and detect-passive-events
). detect-it
's update()
function first runs the update()
function on each micro state machine that it contains, and then updates it own state.
Note that Detect It has removed support for Pointer Events detection because they're just not relevant enough (support for less than 60% of users, see Can I Use, and not supported by React). If you need Pointer Events detection, use Detect It v1.1.
detectIt
micro state machine
const detectIt = {
deviceType: 'mouseOnly' / 'touchOnly' / 'hybrid',
passiveEvents: true / false,
hasMouse: true / false,
hasTouch: true / false,
primaryInput: 'mouse' / 'touch',
state: {
detectHover,
detectPointer,
detectTouchEvents,
detectPassiveEvents,
},
update() {...},
}
Installing detect-it
$ yarn add detect-it
# OR
$ npm install --save detect-it
Using detect-it
import detectIt from 'detect-it';
detectIt.deviceType === 'mouseOnly' / 'touchOnly' / 'hybrid';
detectIt.passiveEvents === true;
detectIt.hasMouse === true;
detectIt.hasTouch === true;
detectIt.primaryInput === 'mouse' / 'touch';
detectIt.state.detectHover;
detectIt.state.detectPointer;
detectIt.state.detectTouchEvents;
detectIt.state.detectPassiveEvents;
detectIt.update();
const detectIt = {
deviceType: 'mouseOnly',
passiveEvents: false,
hasMouse: true,
hasTouch: false,
primaryInput: 'mouse',
}
const detectIt = {
deviceType: 'touchOnly',
passiveEvents: false,
hasMouse: false,
hasTouch: true,
primaryInput: 'touch',
}
Note that the update()
function is run once at the time of import to set the object's initial state, and generally doesn't need to be run again. If it doesn't have access to the window
, then the state will be undefined
(detect-it
will not throw an error), and you will have to call the update()
function manually at a later time to update its state.
Using detect-it
to set event listeners
if (detectIt.passiveEvents === true) {
document.addEventListener('scroll', handleScroll, { capture: false, passive: true });
} else {
document.addEventListener('scroll', handleScroll, false);
}
if (detectIt.hasMouse) {
}
if (detectIt.hasTouch) {
}
if (detectIt.deviceType === 'mouseOnly') {
}
if (detectIt.deviceType === 'touchOnly') {
}
if (detectIt.deviceType === 'hybrid') {
}
Real world example using detect-it
Part of the detect-it
family
For more information
Notes about detecting the deviceType
I chose 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 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, and so is placed in the broad hybrid
category.
function determineDeviceType(hasTouch, anyHover, anyFine) {
if (hasTouch && (anyHover || anyFine)) return 'hybrid';
return hasTouch ? 'touchOnly' : 'mouseOnly';
}
Some hybrid
examples:
- A touch capable Chromebook with Chrome browser registers that
hasTouch
, anyHover
, and anyFine
are all true. - The Galaxy Note with stylus running the Chrome mobile browser registers that
hasTouch
and anyFine
are true, but that anyHover
is false. - The Microsoft Surface (and other Windows 10 touchscreen computers)
- When using the Chrome browser,
hasTouch
, anyHover
and anyFine
are all true because Chrome supports the Touch Events API, so the device registers as a hybrid
. - When using Microsoft's Edge browser
hasTouch
is false because Edge doesn't support the Touch Events API, so it registers as a mouseOnly
device. To access the touch capabilities in Edge you have to use Pointer Events. If you want Edge to register as a hybrid
device then use Detect It v1.1 which supports Pointer Events. Note that touches will still fire mouse events, so if you don't set Pointer Event listeners, touch input will act like a mouse.