Skip to content

Consider removing Mobile Safari empty onclick hack #12989

Open

Description

@gaearon

See #238 and #1536 for historical context.

Is that still relevant? The code is here:

https://github.com/facebook/react/blob/52fbe7612e0527b8c86decac519c344626f6bd72/packages/react-dom/src/client/ReactDOMFiberComponent.js#L244-L245

Even if it's relevant, can we just feature test it, and not do this hack on other browsers? Seems like a waste of memory for event handlers (even though the function is the same every time).

Activity

  1. nhunzaker commented on Jun 11, 2018

    @nhunzaker
    Contributor

    I'd like to limit this to Safari. Looks like we still need this for non-interactive elements.

    I did a few quick checks using:
    https://codepen.io/nhunzaker/pen/yEMpJO?editors=1010

    • This definitely is still an issue on iOS 11
    • It looks like this isn't a problem in desktop Safari (7.1, 8, 11.1)

    I wonder what is the best way to detect Safari (or generally when to apply this fix)

  2. gaearon commented on Jun 11, 2018

    @gaearon
    CollaboratorAuthor

    Can we literally feature-test it? Like dispatching an event and seeing if it bubbles, or something?

  3. gaearon commented on Jun 11, 2018

    @gaearon
    CollaboratorAuthor

    The problem with relying on useragent etc is that those can get messed up in webviews.

  4. nhunzaker commented on Jun 11, 2018

    @nhunzaker
    Contributor

    Ah totally. I agree that we should feature detect this. I can check later today, but I wonder if it could be something like:

    let noBubble = true
    
    // where should document come from?
    
    let parent = document.createElement('div')
    let child = document.createElement('div')
    let event = document.createEvent('Event')
    
    parent.appendChild(child)
    
    event.initEvent('click', true, false)
    
    parent.addEventListener('click', () => {
      noBubble = false
    })
    
    child.dispatchEvent(event);
    
  5. philipp-spiess commented on Jul 15, 2018

    @philipp-spiess
    Contributor

    The feature test using manual event dispatching does not seem to work (here's what I used to test it). I was thinking that this bail out maybe happens in Safari's hit-detection logic and we could feature detect using elementFromPoint() but that also behaved correctly. 🤔

  6. hectorcoronado commented on Jul 19, 2018

    @hectorcoronado

    Hello! Would I be correct in surmising this may plausibly be a good first contribution? Haven't really dug into it sufficiently, but it looks doable. If so, and if one of you isn't already working on it, any chance you could assign to me?

  7. gaearon commented on Aug 2, 2018

    @gaearon
    CollaboratorAuthor

    @hectorcoronado Feel free to dig in. This will need some research — it's great if you can do it!

  8. hectorcoronado commented on Aug 2, 2018

    @hectorcoronado

    I'll look into it in the coming days and update accordingly -- thanks!

  9. gaearon commented on Aug 17, 2018

    @gaearon
    CollaboratorAuthor

    I did a few quick checks using:
    https://codepen.io/nhunzaker/pen/yEMpJO?editors=1010
    This definitely is still an issue on iOS 11

    This doesn't seem to check whether our particular strategy of attaching it to every node with a click handler is necessary. It only checks that the click doesn't bubble to the root. But maybe we're doing more than we have to.

  10. gaearon commented on Aug 17, 2018

    @gaearon
    CollaboratorAuthor

    Oh...

    The target element, or any of its ancestors up to but not including the , has an explicit event handler set for any of the mouse events. This event handler may be an empty function.

    This explains why #11927 works. But it also means we might get away with only setting onclick on the root if the click delay / grey overlay issue is solved in version we support.

  11. philipp-spiess commented on Aug 20, 2018

    @philipp-spiess
    Contributor

    But it also means we might get away with only setting onclick on the root if the click delay / grey overlay issue is solved in version we support.

    To give some additional information: The "grey overlay" is a tap highlight that is shown when an element is pressed on iOS Safari. Safari will detect all interactive elements (e.g buttons) as tapable by default and adding an onclick handler will also make non-interactive elements (e.g div) tapable.

    It's important UX wise to respect the proper granularity for adding these listeners, otherwise the tap highlight will be shown above the whole container:

    (Source code)


    Back to the original topic I just realized that this is tightly intertwined with the tap highlight color so maybe a feasible feature test would check if the browser supports that? Maybe we can check if the -webkit-tap-highlight-color CSS property is supported.

  12. philipp-spiess commented on Aug 20, 2018

    @philipp-spiess
    Contributor

    @hectorcoronado Do you still want to work on this issue? You can try to explore the CSS property based feature-detection if you want 😊 (See my comment above)

  13. hectorcoronado commented on Aug 20, 2018

    @hectorcoronado

    @philipp-spiess @gaearon yes! I will spend time on it after work tomorrow and will report back if it seems beyond my capabilities at present -- thank you!

  14. added
    React Core Team
    on Jan 8, 2020
  15. binhpv commented on Mar 30, 2021

    @binhpv

    @gaearon
    I tested several iOS simulator and found out that from iOS 13 and higher, the click event bubbling issue is not an issue anymore. This test was done following test cases in https://www.quirksmode.org/blog/archives/2010/09/click_event_del.html

    Maybe we could be more pragmatic and add the check only for iOS 12 and lower. This will of course cause some issues, but only for very small amount of user that are on iOS 12 or lower, and the user agent is modified and therefore we can't detect iOS anymore.

    Currently our application is public sector application, and accessibility certificate is a must. But as in #13798, we are facing this "clickable" issue.

    Another solution could be to provide a way to configure this behavior in the application code?

  16. gaearon commented on Mar 30, 2021

    @gaearon
    CollaboratorAuthor

    Do we have a good way to detect iOS <= 13? I was just thinking about getting rid of this too a few days ago.

  17. binhpv commented on Apr 3, 2021

    @binhpv
  18. gaearon commented on Apr 3, 2021

    @gaearon
    CollaboratorAuthor

    I don’t think we can ignore webviews. The cost of the mistake is pretty high — things literally won’t be clickable.

  19. eps1lon commented on Nov 30, 2021

    @eps1lon
    Collaborator

    Back to the original topic I just realized that this is tightly intertwined with the tap highlight color so maybe a feasible feature test would check if the browser supports that? Maybe we can check if the -webkit-tap-highlight-color CSS property is supported.

    FYI: Even desktop browsers implement -webkit-tap-highlight-color property and actually have values set for this property (window.getComputedStyle(document.body).webkitTapHighlightColor). So a naive webkitTapHighlightColor in document.body.style and even window.getComputedStyle(document.body).webkitTapHighlightColor !== '' would not tell you whether this color is applied when tapping.

    Just setting -webkit-tap-highlight-color: transparent on React containers would also not work since that CSS property is unfortunately inherited i.e. #react-root button would also have a transparent tap highlight color. Note that MDN says it's not inherited but that's not how Chrome 96 implements it: https://codesandbox.io/s/wizardly-vaughan-2cbh5?file=/src/App.js. Though we can prevent inheritance with #react-root > * { -webkit-tap-highlight-color: initial; }.

    So for clickable elements (elements with a click handler) we have to set an explicit node.onclick unless we find a robust way to implement browser heuristics.

    And even then it's questionable if we truly want to see the tap highlight color in case the click handler isn't even used to make that specific element interactive (e.g. tracking clicks, click-away detection)

    With regards to calling click handlers:
    The event delegation quirk only applies when only document.body has the click handler. If you have a click handler on any other element then iOS will properly bubble click events up even when non-interactive elements are targeted. Tested on Safari iOS 9 (iPhone 6s): https://csb-sel0s-31qhjklet-eps1lon.vercel.app/ Note how only a click on the last fake button doesn't register. Every other click is registered because its (fake) react root has a click handler attached.

    It doesn't look like we can safely remove this hack without affecting visual feedback on iOS which is unfortunate considering the empty click handler negatively affects NVDA + Firefox: nvaccess/nvda#13108

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: DOMReact Core TeamType: Needs Investigation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions