Socket
Socket
Sign inDemoInstall

detect-it

Package Overview
Dependencies
0
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 4.0.0-next.0 to 4.0.0-next.1

dist/detect-it.cjs.development.js

18

dist/detect-it.esm.js

@@ -27,3 +27,3 @@ // so it doesn't throw if no window or matchMedia

var noop = function noop() {};
var noop = function () {};

@@ -45,3 +45,3 @@ w.addEventListener && w.addEventListener('p', noop, options);

var supportsTouchEvents = onTouchStartInWindow || touchEventInWindow && matchMedia('(any-pointer: coarse)').matches;
var supportsTouchEvents = onTouchStartInWindow || touchEventInWindow && /*#__PURE__*/matchMedia('(any-pointer: coarse)').matches;
var hasTouch = (w.navigator.maxTouchPoints || 0) > 0 || supportsTouchEvents; // userAgent is used as a backup to correct for known device/browser bugs

@@ -58,14 +58,16 @@ // and when the browser doesn't support interaction media queries (pointer and hover)

var isIPad = matchMedia('(pointer: coarse)').matches && // both iPad and iPhone can "request desktop site", which sets the userAgent to Macintosh
var isIPad = /*#__PURE__*/matchMedia('(pointer: coarse)').matches &&
/*#__PURE__*/
// both iPad and iPhone can "request desktop site", which sets the userAgent to Macintosh
// so need to check both userAgents to determine if it is an iOS device
// and screen size to separate iPad from iPhone
/iPad|Macintosh/.test(userAgent) && Math.min(w.screen.width || 0, w.screen.height || 0) >= 768;
var hasCoarsePrimaryPointer = (matchMedia('(pointer: coarse)').matches || // if the pointer is not coarse and not fine then the browser doesn't support
/iPad|Macintosh/.test(userAgent) && /*#__PURE__*/Math.min(w.screen.width || 0, w.screen.height || 0) >= 768;
var hasCoarsePrimaryPointer = ( /*#__PURE__*/matchMedia('(pointer: coarse)').matches || // if the pointer is not coarse and not fine then the browser doesn't support
// interaction media queries (see https://caniuse.com/css-media-interaction)
// so if it has onTouchStartInWindow assume it has a coarse primary pointer
!matchMedia('(pointer: fine)').matches && onTouchStartInWindow) && // bug in firefox (as of v81) on hybrid windows devices where the interaction media queries
! /*#__PURE__*/matchMedia('(pointer: fine)').matches && onTouchStartInWindow) && // bug in firefox (as of v81) on hybrid windows devices where the interaction media queries
// always indicate a touch only device (only has a coarse pointer that can't hover)
// so assume that the primary pointer is not coarse for firefox windows
!/Windows.*Firefox/.test(userAgent);
var hasAnyHoverOrAnyFinePointer = matchMedia('(any-pointer: fine)').matches || matchMedia('(any-hover: hover)').matches || // iPads might have an input device that can hover, so assume it has anyHover
! /*#__PURE__*/ /Windows.*Firefox/.test(userAgent);
var hasAnyHoverOrAnyFinePointer = /*#__PURE__*/matchMedia('(any-pointer: fine)').matches || /*#__PURE__*/matchMedia('(any-hover: hover)').matches || // iPads might have an input device that can hover, so assume it has anyHover
isIPad || // if no onTouchStartInWindow then the browser is indicating that it is not a touch only device

@@ -72,0 +74,0 @@ // see above note for supportsTouchEvents

{
"name": "detect-it",
"version": "4.0.0-next.0",
"version": "4.0.0-next.1",
"description": "Detect if a device is mouse only, touch only, or hybrid",
"source": "src/index.ts",
"main": "dist/detect-it.js",
"umd:main": "dist/detect-it.umd.min.js",
"main": "dist/detect-it.cjs.js",
"module": "dist/detect-it.esm.js",

@@ -12,14 +10,14 @@ "types": "dist/index.d.ts",

"scripts": {
"dev": "npm link && npm run watch && npm unlink",
"watch": "rm -rf dist && trap 'exit 0' SIGINT; microbundle watch --format esm,cjs --no-compress --strict --tsconfig tsconfig.build.json & microbundle watch --format umd --compress --strict --tsconfig tsconfig.build.json --external peerDependencies",
"build": "rm -rf dist && microbundle build --format esm,cjs --no-compress --strict --tsconfig tsconfig.build.json && microbundle build --format umd --compress --strict --tsconfig tsconfig.build.json --external peerDependencies",
"prepublishOnly": "npm run build",
"tsc": "tsc --watch",
"dev": "npm link && npm run watch && npm unlink -g",
"build": "rollpkg build",
"watch": "rollpkg watch",
"prepublishOnly": "npm run lint && npm test && npm run build",
"lint": "eslint src",
"test": "jest",
"test:watch": "jest --watchAll",
"coverage": "npx live-server coverage/lcov-report"
"coverage": "npx live-server coverage/lcov-report",
"lintStaged": "lint-staged"
},
"files": [
"dist",
"src"
"dist"
],

@@ -36,3 +34,4 @@ "repository": {

"hybrid",
"passive events"
"passive events",
"pointer events"
],

@@ -46,58 +45,26 @@ "author": "Rafael Pedicini <rafael@rafgraph.dev>",

"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.4.1",
"@typescript-eslint/parser": "^4.4.1",
"eslint": "^7.11.0",
"eslint-config-prettier": "^6.13.0",
"eslint-plugin-import": "^2.22.1",
"husky": "^4.3.0",
"jest": "^26.5.3",
"lint-staged": "^10.4.0",
"microbundle": "^0.12.4",
"prettier": "^2.1.2",
"ts-jest": "^26.4.1",
"typescript": "^4.0.3"
"lint-staged": "^10.5.2",
"pre-commit": "^1.2.2",
"rollpkg": "^0.1.1",
"typescript": "^4.1.2"
},
"pre-commit": "lintStaged",
"lint-staged": {
"src/**/*": [
"eslint",
"prettier --write --ignore-unknown"
]
},
"prettier": "rollpkg/configs/prettier.json",
"eslintConfig": {
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"sourceType": "module",
"project": "./tsconfig.json",
"createDefaultProgram": true
},
"env": {
"browser": true
},
"extends": [
"eslint:recommended",
"plugin:import/errors",
"plugin:import/warnings",
"plugin:import/typescript",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"prettier",
"prettier/@typescript-eslint"
"./node_modules/rollpkg/configs/eslint"
],
"rules": {
"import/no-default-export": "error"
},
"ignorePatterns": [
"dist",
"node_modules",
"jest.config.js"
]
},
"prettier": {
"trailingComma": "all",
"singleQuote": true,
"printWidth": 100
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
"jest/no-mocks-import": "off"
}
},
"lint-staged": {
"src/**/*": "prettier --write --ignore-unknown"
"jest": {
"preset": "rollpkg"
}
}
# Detect It
Detect if a device is `mouseOnly`, `touchOnly`, or `hybrid`, and if the primary input is `mouse` or `touch`. Also detects if the browser supports the Pointer Events API, the Touch Events API, and passive event listeners. Detect It is tree-shakable and side-effect free.
- Detect if a device is `mouseOnly`, `touchOnly`, or `hybrid`
- Detect if the primary input is `mouse` or `touch`
- Detect if the browser supports Pointer Events, Touch Events, and passive event listeners
This is useful for creating device responsive UX and responding to user interactions. When creating apps with device responsive UX it is important to know what the user can do. Can they hover? Can they swipe? Etc. Once it's known what the user can do, the next question is how does the app listen for user input. For example, it's not enough to know that the user is on a touch device, the app also needs to know how to listen for touch input, should the app set PointerEvent listeners or TouchEvent listeners? For more on this see the [Recommended usage](#recommended-usage) section.
[Live detection test](https://detect-it.rafgraph.dev) (code in the [demo repo](https://github.com/rafgraph/detect-it-demo))
---
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. 😁
[Live detection test](https://detect-it.rafgraph.dev) (code in the [demo repo](https://github.com/rafgraph/detect-it-demo))
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, but it should be noted that the detection is reliant on how the browser presents the capabilities of the device as it is not possible to access the device hardware directly.

@@ -15,2 +17,6 @@ [![npm](https://img.shields.io/npm/dm/detect-it?label=npm)](https://www.npmjs.com/package/detect-it) [![npm bundle size (version)](https://img.shields.io/bundlephobia/minzip/detect-it@next?color=purple)](https://bundlephobia.com/result?p=detect-it@next) ![npm type definitions](https://img.shields.io/npm/types/detect-it?color=blue)

[CDN option](#pre-built-cdn-option) ⚡️ [Recommended usage](#recommended-usage) ⚡️ [Device responsive UX](#device-responsive-ux) ⚡️ [Setting event listeners](#setting-event-listeners) ⚡️ [Detection details](#detection-details)
---
> `detect-it` v4 is currently in pre-release, use the `@next` tag to install it, [v3 is available here](https://github.com/rafgraph/detect-it/tree/v3.0.7)

@@ -49,3 +55,3 @@

Indicates if the the device is `mouseOnly`, `touchOnly` or `hybrid`. For info on how the detection works and how specific devices are classified see [Notes on detecting `deviceType`](#notes-on-detecting-devicetype).
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](#detection-details) section.

@@ -132,5 +138,5 @@ ```js

## Pre-built option served from Unpkg CDN
## Pre-built CDN option
Optionally, instead of using `npm install` you can load Detect It directly in the browser. A minified UMD version is available from Unpkg for this purpose.
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.

@@ -155,6 +161,6 @@ ```html

- Use `primaryInput` for creating device responsive UX that optimizes the user experience for either `mouse` or `touch` 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.
- Use `primaryInput` to optimize the user experience for either `mouse` or `touch` 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 PointerEvent listeners and use [`pointerType`](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/pointerType) to determine if the interaction was from `mouse` or `touch`.
- Otherwise always set both MouseEvent and TouchEvent listeners and use [`event-from`](https://github.com/rafgraph/event-from) to ignore MouseEvents generated from touch input.
- If the browser `supportsPointerEvents` then only set Pointer Event listeners and use [`pointerType`](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/pointerType) to determine if the interaction was from `mouse` or `touch`.
- Otherwise always set both Mouse Event and Touch Event listeners and use [`event-from`](https://github.com/rafgraph/event-from) to ignore Mouse Events generated from touch input.

@@ -165,9 +171,9 @@ ### Device responsive UX

There are 3 parts of device responsive UX: **Size** (size of screen/window), **Can** (what the user can do/capabilities of the device), and **Is** (is the user hovering, touching, etc). **Size** and **Can** need to be known at render time (when the UI is rendered), and **Is** needs to be known at interaction time (when the user is interacting with the app).
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`](https://github.com/ReactTraining/react-media).
- **Can**
- This is what **Detect It** is built for - knowing at render time what the capabilities of the device are (what can the user do). There are a number of ways that you could use `deviceType` or `primaryInput` to optimize the UX for the capabilities of the device, however, in most cases I've found it makes sense to just use `primaryInput` and optimize the UX for `mouse` or `touch`, while ensuring that the app is still usable by both inputs.
- Putting **Size** and **Can** together, my preferred approach is to optimize the UX for 4 scenarios:
- **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` or `primaryInput` to optimize the UX for the capabilities of the device, however, in most cases I've found it makes sense to just use `primaryInput` and optimize the UX for `mouse` or `touch`, while ensuring that the app is still usable by both inputs.
- 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

@@ -177,12 +183,12 @@ - Narrow screen and `primaryInput` `mouse`: desktop/laptop with a narrow window

- Narrow screen with `primaryInput` `touch`: phone
- **Is**
- 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 ways that are not possible with CSS pseudo selectors.
- **Interaction**
- Is the user hovering, touching, etc. To help with this I created [React Interactive](https://github.com/rafgraph/react-interactive) which provides a callback for interactive state changes (`hover`, `mouseActive`, `touchActive`, `keyActive`) and allows you to style touch interactions in ways that feel native and are not possible with CSS pseudo classes.
### Setting event listeners
Setting event listeners can be thought of as either setting PointerEvent listeners **_or_** setting MouseEvent and TouchEvent listeners. PointerEvents can do everything that MouseEvents and TouchEvents can do (and more), without having to worry about if a MouseEvent was caused by touch input and so should be ignored. It is recommended to use PointerEvents if they are supported.
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 recommended to use Pointer Events if they are supported.
#### PointerEvent listeners
#### Pointer Event listeners
If the browser `supportsPointerEvents` then only set PointerEvent listeners and use [`pointerType`](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/pointerType) to determine if the interaction was from `mouse` or `touch`.
If the browser `supportsPointerEvents` then only set Pointer Event listeners and use [`pointerType`](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/pointerType) to determine if the interaction was from `mouse` or `touch`.

@@ -209,13 +215,13 @@ ```js

#### MouseEvent and TouchEvent listeners
#### Mouse Event and Touch Event listeners
If the browser doesn't support PointerEvents, then there are a couple of ways to approach setting mouse 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 TouchEvents as the interaction is in progress (finger on the screen), and then will fire MouseEvents after the touch interaction has finished (after the finger is removed from the screen) to support sites that only listen for MouseEvents.
> Note that a touch interaction will fire Touch Events as the interaction is in progress (finger on the screen), and then will fire Mouse Events after the touch interaction has finished (after the finger 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 MouseEvents caused by touch input (you can use [`event-from`](https://github.com/rafgraph/event-from) for this).
**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`](https://github.com/rafgraph/event-from) for this).
**Option 2**: Always set both mouse and touch event listeners and use [`event-from`](https://github.com/rafgraph/event-from) to ignore MouseEvents from touch input.
**Option 2**: Always set both mouse and touch event listeners and use [`event-from`](https://github.com/rafgraph/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 TouchEvent listeners on a browser that doesn't support TouchEvents is fine, the browser will just ignore the event listeners).
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).

@@ -238,3 +244,3 @@ ```js

} else {
// PointerEvents are not supported so set both MouseEvent and TouchEvent listeners
// Pointer Events are not supported so set both Mouse Event and Touch Event listeners
element.addEventListener('mouseenter', handleMouseEnter, false);

@@ -247,6 +253,18 @@ element.addEventListener('touchstart', handleTouchStart, false);

## Notes on detecting `deviceType`
## Detection details
To determine the `deviceType` and `primaryInput` Detect It uses multiple API detections and media query results 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).
#### 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](https://github.com/rafgraph/detect-it/blob/main/src/index.ts) 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](https://github.com/rafgraph/detect-it/tree/main/src/__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`:

@@ -272,5 +290,1 @@

- 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` with `primaryInput` `touch`)
#### Detection limitations
These detections are limited by how the browser presents itself (the APIs it exposes and how it responds to media queries) so there is the potential for errors. As anyone who has done web development knows, browsers are quirky and they don't always behave in expected ways, so **Detect It uses real world browser behavior to triangulate the `deviceType` and `primaryInput`.** For more on this see the comments in the [source code](https://github.com/rafgraph/detect-it/blob/main/src/index.ts) for notes about detecting the device type. Also, [tests](https://github.com/rafgraph/detect-it/tree/main/src/__tests__) have been written to mock a number of different browser behaviors and edge cases. Cloning this repo and running `npm test` will provide insight into how `detect-it` will preform on different devices.

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc