Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V3 #5

Merged
merged 6 commits into from
May 4, 2017
Merged

V3 #5

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"root": true,
"extends": "airbnb-base"
"parser": "babel-eslint",
"extends": "airbnb-base",
"env": {
"browser": true
}
}
72 changes: 22 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ Detect if a device is mouse only, touch only, or hybrid, and if it supports pass

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 four micro state machines that it contains ([`detect-hover`][detectHoverRepo], [`detect-pointer`][detectPointerRepo], [`detect-touch-events`][detectTouchEventsRepo], and [`detect-passive-events`][detectPassiveEventsRepo]). `detect-it`'s `update()` function first runs the `update()` function on each micro state machine that it contains, and then updates it own state.
`detect-it`'s state is based on the state of the four micro state machines that it contains ([`detect-hover`][detectHoverRepo], [`detect-pointer`][detectPointerRepo], [`detect-touch-events`][detectTouchEventsRepo], and [`detect-passive-events`][detectPassiveEventsRepo]). `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 v2 removed support for Pointer Events detection because they're just not relevant enough (only used by IE and Edge and not supported by React). If you need Pointer Events detection, use [Detect It v1.1][detectItv1.1].
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][canIUsePointerEvents], and not supported by React). If you need Pointer Events detection, use [Detect It v1.1][detectItv1.1].

### `detectIt` micro state machine
```javascript
const detectIt = {
deviceType: 'mouseOnly' / 'touchOnly' / 'hybrid',
passiveEvents: boolean,
hasMouse: boolean,
hasTouch: boolean,
maxTouchPoints: whole number,
primaryHover: 'hover' / 'none',
primaryPointer: 'fine' / 'coarse' / 'none',
passiveEvents: true / false,
hasMouse: true / false,
hasTouch: true / false,
primaryInput: 'mouse' / 'touch',

// access to the four micro state machines that it contains
state: {
Expand Down Expand Up @@ -54,10 +52,7 @@ detectIt.hasMouse === true; // the deviceType is mouseOnly or hybrid
// the browser supports the touch events api, and the deviceType is touchOnly or hybrid
detectIt.hasTouch === true;

detectIt.maxTouchPoints; // max number of touch points supported

detectIt.primaryHover === 'hover' / 'none'; // can the primary pointing system easily hover
detectIt.primaryPointer === 'fine' / `coarse` / 'none'; // how accurate is the primary pointing system
detectIt.primaryInput === 'mouse' / 'touch'; // the primary input type


// accessing the state of the micro state machines that detectIt contains
Expand All @@ -72,32 +67,24 @@ detectIt.update();
```

```javascript
/*
* note that in the case of a legacy computer and browser, one that
* doesn't support any of detect-it's detection tests, the default state will be:
*/
// note that in the case of a legacy computer and browser, one that
// doesn't support any of detect-it's detection tests, the default state will be:
const detectIt = {
deviceType: 'mouseOnly',
passiveEvents: false,
hasMouse: true,
hasTouch: false,
maxTouchPoints: undefined,
primaryHover: 'hover',
primaryPointer: 'fine',
primaryInput: 'mouse',
}

/*
* note that in the case of a legacy touch device, one that supports the touch events api,
* but not any of the other detection tests, the default state will be:
*/
// note that in the case of a legacy touch device, one that supports the touch events api,
// but not any of the other detection tests, the default state will be:
const detectIt = {
deviceType: 'touchOnly',
passiveEvents: false,
hasMouse: false,
hasTouch: true,
maxTouchPoints: undefined,
primaryHover: 'none',
primaryPointer: 'coarse',
primaryInput: 'touch',
}
```

Expand Down Expand Up @@ -132,19 +119,8 @@ if (detectIt.deviceType === 'hybrid') {
}
```

#### Using `detect-it` to adjust the user interface
```javascript
if (detectIt.primaryPointer === 'coarse') {
// make clickable elements bigger
}

if (detectIt.primaryHover === 'hover') {
// can add hover features
}
```

#### Real world example using `detect-it`
- [`react-interactive`][reactInteractive] - a better interactive state machine than CSS
- [React Interactive][reactInteractive] - a better interactive state machine than CSS

### Part of the `detect-it` family
- **`detect-it`**
Expand All @@ -166,25 +142,21 @@ I chose a wide definition for what constitutes a `hybrid` device, or rather a st
```javascript
// this is the function used by detect-it to determine the device type
function determineDeviceType(hasTouch, anyHover, anyFine) {
/*
* A hybrid device is one that both hasTouch and any input device can hover
* or has a fine pointer.
*/
// A hybrid device is one that both hasTouch and any input device can hover
// or has a fine pointer.
if (hasTouch && (anyHover || anyFine)) return 'hybrid';

/*
* In almost all cases a device that doesn’t support touch will have a mouse,
* but there may be rare exceptions. Note that it doesn’t work to do additional tests
* based on hover and pointer media queries as older browsers don’t support these.
* Essentially, 'mouseOnly' is the default.
*/
// In almost all cases a device that doesn’t support touch will have a mouse,
// but there may be rare exceptions. Note that it doesn’t work to do additional tests
// based on hover and pointer media queries as older browsers don’t support these.
// Essentially, 'mouseOnly' is the default.
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 - 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 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][detectItv1.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.
Expand All @@ -202,11 +174,11 @@ Some `hybrid` examples:
[theListener]: https://github.com/rafrex/the-listener
[currentInput]: https://github.com/rafrex/current-input

[canIUsePointerEvents]: http://caniuse.com/#feat=pointer
[w3cMediaQueriesSpecLatestHover]: https://www.w3.org/TR/mediaqueries-4/#hover
[w3cMediaQueriesSpecLatestPointer]: https://www.w3.org/TR/mediaqueries-4/#pointer
[mdnTouchEvents]: https://developer.mozilla.org/en-US/docs/Web/API/Touch_events
[w3cTouchEventsSpecLatest]: https://w3c.github.io/touch-events/
[mdnPointerEvents]: https://developer.mozilla.org/en-US/docs/Web/API/Pointer_events

[touchTests]: https://patrickhlauke.github.io/touch/
[passiveExplainer]: https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "detect-it",
"version": "2.1.1",
"version": "3.0.0",
"description": "Detect if a device is mouse only, touch only, or hybrid",
"main": "lib/index.js",
"scripts": {
Expand Down Expand Up @@ -34,7 +34,7 @@
"detect-hover": "^1.0.1",
"detect-passive-events": "^1.0.2",
"detect-pointer": "^1.0.1",
"detect-touch-events": "^1.0.1"
"detect-touch-events": "^2.0.0"
},
"devDependencies": {
"babel-cli": "^6.24.1",
Expand Down
65 changes: 32 additions & 33 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,29 @@ import detectPassiveEvents from 'detect-passive-events';
* }
*/

function determineDeviceType(hasTouch, anyHover, anyFine) {
/*
* A hybrid device is one that both hasTouch and any input device can hover
* or has a fine pointer.
*/
function determineDeviceType(hasTouch, anyHover, anyFine, state) {
// A hybrid device is one that both hasTouch and any input device can hover
// or has a fine pointer.
if (hasTouch && (anyHover || anyFine)) return 'hybrid';

/*
* In almost all cases a device that doesn’t support touch will have a mouse,
* but there may be rare exceptions. Note that it doesn’t work to do additional tests
* based on hover and pointer media queries as older browsers don’t support these.
* Essentially, 'mouseOnly' is the default.
*/
// workaround for browsers that have the touch events api,
// and have implemented Level 4 media queries but not the
// hover and pointer media queries, so the tests are all false (notable Firefox)
// if it hasTouch, no pointer and hover support, and on an android assume it's touchOnly
// if it hasTouch, no pointer and hover support, and not on an android assume it's a hybrid
if (hasTouch &&
Object.keys(state.detectHover).filter(key => key !== 'update').every(key => state.detectHover[key] === false) &&
Object.keys(state.detectPointer).filter(key => key !== 'update').every(key => state.detectPointer[key] === false)) {
if (window.navigator && /android/.test(window.navigator.userAgent.toLowerCase())) {
return 'touchOnly';
}
return 'hybrid';
}

// In almost all cases a device that doesn’t support touch will have a mouse,
// but there may be rare exceptions. Note that it doesn’t work to do additional tests
// based on hover and pointer media queries as older browsers don’t support these.
// Essentially, 'mouseOnly' is the default.
return hasTouch ? 'touchOnly' : 'mouseOnly';
}

Expand All @@ -57,37 +67,26 @@ const detectIt = {
if (typeof window !== 'undefined') {
detectIt.passiveEvents = detectIt.state.detectPassiveEvents.hasSupport || false;

detectIt.hasTouch = detectIt.state.detectTouchEvents.hasApi || false;
detectIt.hasTouch = detectIt.state.detectTouchEvents.hasSupport || false;

detectIt.deviceType = determineDeviceType(
detectIt.hasTouch,
detectIt.state.detectHover.anyHover,
detectIt.state.detectPointer.anyFine,
detectIt.state,
);

detectIt.hasMouse = detectIt.deviceType !== 'touchOnly';
detectIt.maxTouchPoints = detectIt.state.detectTouchEvents.maxTouchPoints;

detectIt.primaryHover =
(detectIt.state.detectHover.hover && 'hover') ||
(detectIt.state.detectHover.none && 'none') ||
// if it's a mouseOnly device that doesn't support level 4 media queries,
// then assume it hovers
(detectIt.deviceType === 'mouseOnly' && 'hover') ||
// if it's a touchOnly device that doesn't support level 4 media queries,
// then assume it doesn't hover, otherwise it's undefined
(detectIt.deviceType === 'touchOnly' && 'none') || undefined;

detectIt.primaryPointer =
(detectIt.state.detectPointer.fine && 'fine') ||
(detectIt.state.detectPointer.coarse && 'coarse') ||
(detectIt.state.detectPointer.none && 'none') ||
// if it's a mouseOnly device that doesn't support level 4 media queries,
// then assume it has a fine pointer
(detectIt.deviceType === 'mouseOnly' && 'fine') ||
// if it's a touchOnly device that doesn't support level 4 media queries,
// then assume it has a coarse pointer, otherwise it's undefined
(detectIt.deviceType === 'touchOnly' && 'coarse') || undefined;
detectIt.primaryInput =
(detectIt.deviceType === 'mouseOnly' && 'mouse') ||
(detectIt.deviceType === 'touchOnly' && 'touch') ||
// deviceType is hybrid:
(detectIt.state.detectHover.hover && 'mouse') ||
(detectIt.state.detectHover.none && 'touch') ||
// if there's no support for hover media queries but detectIt determined it's
// a hybrid device, then assume it's a mouse first device
'mouse';
}
},
};
Expand Down