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

[Fix] isTouchDevice: improve detection logic #516

Merged
merged 1 commit into from
May 26, 2017
Merged

[Fix] isTouchDevice: improve detection logic #516

merged 1 commit into from
May 26, 2017

Conversation

tagoro9
Copy link
Contributor

@tagoro9 tagoro9 commented May 17, 2017

window.ontouchstart is a defiend property in Firefox with a value of null. This PR fixes the isTouchDevice function to check that ontouchstart != null.

This PR fixes #515.

@coveralls
Copy link

coveralls commented May 17, 2017

Coverage Status

Coverage remained the same at 86.526% when pulling db1160e on relayrides:firefox_ontouchstart into 01bd04f on airbnb:master.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing window.ontouchstart being null in my Firefox.

@@ -1,4 +1,4 @@
export default function isTouchDevice() {
return !!(typeof window !== 'undefined' && 'ontouchstart' in window) ||
return !!(typeof window !== 'undefined' && window.ontouchstart != null) ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What value will window.ontouchstart have in a browser that supports touch?

However, https://hacks.mozilla.org/2013/04/detecting-touch-its-the-why-not-the-how/ seems to imply that we should be checking a few more things.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By looking at what Modernizr does, it looks like they do some more checks too. Even though it is not still a bug I could still update this to have a better check.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would be super helpful!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, that'd be appreciated :-)

@tagoro9
Copy link
Contributor Author

tagoro9 commented May 18, 2017

I updated the isTouchDevice function to also check for mxMaxTouchPoints, which seems to be used on some old IE versions and to also use the DocumentTouch check, which is the one used by Modernizr.

They also use some media queries, but that involves adding a lot of vendor prefixes:

https://drafts.csswg.org/mediaqueries/#pointer
https://www.quirksmode.org/css/mediaqueries/touch.html

These two articles seemed relevant:

http://www.stucox.com/blog/you-cant-detect-a-touchscreen/
https://medium.com/@david.gilbertson/the-only-way-to-detect-touch-with-javascript-7791a3346685

Let me know what you think of this approach.

@coveralls
Copy link

coveralls commented May 18, 2017

Coverage Status

Coverage remained the same at 86.537% when pulling 4118ed8 on relayrides:firefox_ontouchstart into 380e6f9 on airbnb:master.

@ljharb ljharb changed the title fix(isTouchDevice): return false if ontouchstart exists but is null [Fix] isTouchDevice: improve detection logic May 19, 2017
Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

wrap()
.withOverride(() => window, 'DocumentTouch', () => DocumentTouch)
.withGlobal('document', () => new (function Document() {})())
.it('returns false when document is not an instance of DocumentTouch', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be nice to assert that document is not an instance of DocumentTouch, as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

wrap()
.withOverride(() => window, 'DocumentTouch', () => DocumentTouch)
.withGlobal('document', () => new DocumentTouch())
.it('returns true when document is an instance of DocumentTouch', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be nice to assert that document is an instance of DocumentTouch, as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

@coveralls
Copy link

coveralls commented May 19, 2017

Coverage Status

Coverage remained the same at 86.537% when pulling 0a91285 on relayrides:firefox_ontouchstart into 380e6f9 on airbnb:master.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I'll defer to @majapw for merging.

return !!(typeof window !== 'undefined' && 'ontouchstart' in window) ||
!!(typeof navigator !== 'undefined' && navigator.maxTouchPoints);
return (
!!(typeof window !== 'undefined' &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic could probably be split up into if/elses at this point, but nbd

@majapw majapw merged commit 045fd82 into react-dates:master May 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hover on a day on Firefox doesn't add classes to the day
4 participants