What is detect-it?
The detect-it npm package is designed to help developers detect various input methods and device capabilities. It can identify whether a device supports touch, mouse, or stylus inputs, and can also determine if the device is a mobile or desktop device.
What are detect-it's main functionalities?
Detecting Touch Support
This feature allows you to check if the device supports touch input. The `hasTouch` property will return `true` if touch is supported and `false` otherwise.
const detectIt = require('detect-it');
console.log(detectIt.hasTouch); // true or false
Detecting Mouse Support
This feature allows you to check if the device supports mouse input. The `hasMouse` property will return `true` if a mouse is supported and `false` otherwise.
const detectIt = require('detect-it');
console.log(detectIt.hasMouse); // true or false
Detecting Primary Input Method
This feature allows you to determine the primary input method of the device. The `primaryInput` property will return either 'mouse', 'touch', or 'stylus' based on the primary input method detected.
const detectIt = require('detect-it');
console.log(detectIt.primaryInput); // 'mouse', 'touch', or 'stylus'
Detecting Device Type
This feature allows you to determine the type of device. The `deviceType` property will return 'mouseOnly', 'touchOnly', 'hybrid', or 'unknown' based on the detected input methods.
const detectIt = require('detect-it');
console.log(detectIt.deviceType); // 'mouseOnly', 'touchOnly', 'hybrid', or 'unknown'
Other packages similar to detect-it
detect-touch
The detect-touch package is a lightweight library that focuses on detecting touch capabilities of a device. It is simpler and more focused compared to detect-it, which offers a broader range of input detection features.
bowser
Bowser is a browser detection library that can identify the browser, its version, and the operating system. While it does not specifically focus on input methods like detect-it, it provides comprehensive information about the user's environment.
mobile-detect
Mobile-detect is a user-agent parser that can detect mobile devices, tablets, and their operating systems. It offers broader device detection capabilities compared to detect-it, which is more focused on input methods.
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.