Skip to content

Click events not being triggered with ios touch devices #7635

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

Closed
jooj123 opened this issue Sep 2, 2016 · 9 comments
Closed

Click events not being triggered with ios touch devices #7635

jooj123 opened this issue Sep 2, 2016 · 9 comments

Comments

@jooj123
Copy link

jooj123 commented Sep 2, 2016

Possible bug, I have created this JS Fiddle:
http://jsbin.com/piqolixixi/edit?html,css,js,output

On desktop if i click anywhere (on the components or outside of the components) I get the "Clicked!" alert - which is expected, as on mount I have registered the click event onClick in the Hello component.

If I try the same thing on a ios touch device (im using an ipad mini model A1432, ios 9.3.2)
The click events are not triggered when i touch the react components (Hello and HelloOther).
But if I touch outside of the components the alert comes up.

Whats interesting is that if i add cursor: pointer; css to the components they will then get triggered.
Looks like its some sort of regression around this issue:
#2055

I have tried react versions 0.14 to 15.x - same issue

@aweary
Copy link
Contributor

aweary commented Sep 2, 2016

@jooj123 your click event is not being registered with React's event system. By using document.addEventListener you're registering your own event handler completely separate from React.

React only normalizes and delegates event handlers registered on React components (like <div onClick={this.onClick} />) so you're not getting the normalization that was implemented by React to fix this. This is expected behavior for iOS, see #2055 (comment)

If you want to capture click events anywhere on the page, try rendering a <div/> that fills the page and attack an onClick handler there.

@aweary aweary closed this as completed Sep 2, 2016
@jooj123
Copy link
Author

jooj123 commented Sep 2, 2016

Its what i figured - its just not ideal to do a full page <div/> in my use case as only parts of the page are react

@aweary
Copy link
Contributor

aweary commented Sep 2, 2016

Yeah, but if you want React to normalize your click event, it needs to be registered via React. React uses it's own top-level listener to delegate events for components, so when you use addEventListener React doesn't know that event handler exists at all.

@jooj123
Copy link
Author

jooj123 commented Sep 2, 2016

Going forward is it possible for react to provide an api to cater for this scenario (maybe registers react events on top document level somehow) ? Or is that going against react architectural principles ?

@aweary
Copy link
Contributor

aweary commented Sep 2, 2016

You can see #284 for some discussion on that topic

@Yogi2001
Copy link

Yogi2001 commented Feb 12, 2018

This occurs under very specific conditions:

  • have a :hover rule for an element that is clickable. The styles in that rule don't matter, the rule can even be empty.
  • have a CSS3 adjacent sibling rule with a most specific term that matches at least one element in the DOM. It doesn't need to match the entire rule. Again, the rule can even be empty.
  • The clickable element is followed by an element (which could also be wrapped as the first element inside a element).

You have a :hover rule for an element that is clickable. The styles in that rule don't matter, the rule can even be empty.
and you have a CSS3 adjacent sibling rule with a most specific term that matches at least one element in the DOM. It doesn't need to match the entire rule. Again, the rule can even be empty.
and the clickable element is followed by an element (which could also be wrapped as the first element inside a element).
In that case, tapping the clickable element (whether that click is a JavaScript onclick handler, or just a simple < a href="..." > does not register. Instead, it applies the :hover style and only on the second tap does it navigate or run the JavaScript. This happens only on Mobile Safari and changing pretty much anything will make the bug disappear

@demanzonderjas
Copy link

@Yogi2001 what is the solution? I cannot find a valid workaround for this specific usecase and your link seems to be dead unfortunately? I have exactly the same specific issue/conditions..

Hope you can help out.

@Yogi2001
Copy link

Yogi2001 commented May 2, 2018

@demanzonderjas
you need to break any of the 3 conditions (break one is enough)
e.g.
remove the hover rule (break condition 1)
remove the adjacent sibling rule (break condition 2)
restructure DOM around that element (break condition 3)

@olarsson
Copy link

This occurs under very specific conditions:

  • have a :hover rule for an element that is clickable. The styles in that rule don't matter, the rule can even be empty.
  • have a CSS3 adjacent sibling rule with a most specific term that matches at least one element in the DOM. It doesn't need to match the entire rule. Again, the rule can even be empty.
  • The clickable element is followed by an element (which could also be wrapped as the first element inside a element).

You have a :hover rule for an element that is clickable. The styles in that rule don't matter, the rule can even be empty.
and you have a CSS3 adjacent sibling rule with a most specific term that matches at least one element in the DOM. It doesn't need to match the entire rule. Again, the rule can even be empty.
and the clickable element is followed by an element (which could also be wrapped as the first element inside a element).
In that case, tapping the clickable element (whether that click is a JavaScript onclick handler, or just a simple < a href="..." > does not register. Instead, it applies the :hover style and only on the second tap does it navigate or run the JavaScript. This happens only on Mobile Safari and changing pretty much anything will make the bug disappear

After trying what seemed like everything, including all of the above, I found one more case where this would occur, other event(s) was interfering with the click event. I had a mouseenter and mouseleave event that for some reason took some focus away, they certainly shouldn't have, but they did. Maybe that can help someone looking at solving this annoying problem.

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

No branches or pull requests

5 participants