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.
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 a deterministic function of the state of the five micro state machines that it contains (detect-hover
, detect-pointer
, detect-touch-events
, detect-pointer-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.
detectIt
micro state machine
const detectIt = {
deviceType: 'mouseOnly' / 'touchOnly' / 'hybrid',
passiveEvents: boolean,
hasTouchEventsApi: boolean,
hasPointerEventsApi: boolean,
hasTouch: boolean,
maxTouchPoints: whole number,
primaryHover: 'hover' / 'none',
primaryPointer: 'fine' / 'coarse' / 'none',
state: {
detectHover,
detectPointer,
detectTouchEvents,
detectPointerEvents,
detectPassiveEvents,
},
update() {...},
pointerEventsPrefix(value) {...},
}
Installing detect-it
$ npm install detect-it
Using detect-it
import detectIt from 'detect-it';
detectIt.deviceType === 'mouseOnly' / 'touchOnly' / 'hybrid';
detectIt.passiveEvents === true
detectIt.hasTouchEventsApi === true;
detectIt.hasPointerEventsApi === true;
detectIt.hasTouch === true;
detectIt.maxTouchPoints;
detectIt.primaryHover === 'hover' / 'none';
detectIt.primaryPointer === 'fine' / `coarse` / 'none';
detectIt.state.detectHover;
detectIt.state.detectPointer;
detectIt.state.detectTouchEvents;
detectIt.state.detectPointerEvents;
detectIt.update();
detectIt.pointerEventsPrefix(value)
element.addEventListener(detectIt.pointerEventsPrefix('pointerdown'), function...)
const detectIt = {
deviceType: 'mouseOnly',
passiveEvents: false,
hasTouchEventsApi: false,
hasPointerEventsApi: false,
hasTouch: false,
maxTouchPoints: undefined,
primaryHover: 'hover',
primaryPointer: 'fine',
}
const detectIt = {
deviceType: 'touchOnly',
passiveEvents: false,
hasTouchEventsApi: true,
hasPointerEventsApi: false,
hasTouch: true,
maxTouchPoints: undefined,
primaryHover: 'none',
primaryPointer: 'coarse',
}
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 (or just use the-listener
)
const dIt = detectIt;
if (dIt.passiveEvents === true) {
document.addEventListener('scroll', handleScroll, { capture: false, passive: true });
} else {
document.addEventListener('scroll', handleScroll, false);
}
if (dIt.deviceType === 'mouseOnly') {
}
if (dIt.deviceType === 'touchOnly' && dIt.hasTouchEventsApi) {
}
if (dIt.deviceType === 'hybrid' && dIt.hasTouchEventsApi) {
}
if (dIt.hasTouch && dIt.hasPointerEventsApi && !dIt.hasTouchEventsApi) {
}
if (dIt.hasPointerEventsApi) {
}
if (dIt.deviceType === 'mouseOnly' && dIt.hasPointerEventsApi) {
}
if (dIt.deviceType === 'touchOnly' && dIt.hasPointerEventsApi) {
}
if (dIt.deviceType === 'hybrid' && dIt.hasPointerEventsApi) {
}
Using detect-it
to adjust the user interface
const dIt = detectIt;
if (dIt.primaryPointer === 'coarse') {
}
if (dIt.primaryHover === 'hover') {
}
Real world examples using detect-it
react-interactive
- a better interactive state machine than cssthe-listener
- easily set complex mouse, touch and pointer listeners without conflictscurrent-input
- detect the current input (mouse or touch) and fix the sticky hover problem on touch devices
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 (and that hasTouchEventsApi
is true). - The Galaxy Note with stylus running the Chrome mobile browser registers that
hasTouch
and anyFine
are true, but that anyHover
is false (and that hasTouchEventsApi
is true) - as a side note I think that since the stylus hovers effectively, the Note should register as anyHover
true, but for some reason it doesn't. - The Microsoft Surface (and other Windows 10 touchscreen computers) register that
hasTouch
, anyHover
and anyFine
are all true for both Microsoft Edge and Chrome browsers (note that for the Edge browser hasPointerEventsApi
is true, but hasTouchEventsApi
is false, and for the Chrome browser hasTouchEventsApi
is true, but hasPointerEventsApi
is false).