React: Click events are swallowed by SVG <use> elements in IE

Created on 24 Sep 2015  路  9Comments  路  Source: facebook/react

We've only managed to replicate this in Edge and <=IE11, not Chrome or Firefox. We've created a demo of the issue with this jsbin.

We're using a common SVG element and linking to it with xlink:href as is common with SVG icons. When linking to the icons with a <use> tag inside an anchor, all click events on the icon will not bubble to onClick handlers on the anchor.

An example: I have a common SVG element with an icon <svg><rect id="my-icon" width="100" height="100"></rect></svg>. I then make a link containing my icon <a href="https://github.com"><svg dangerouslySetInnerHTML={{__html: '<use xlink:href="#my-icon"></use>'}}></svg></a>. As xlink:href isn't supported by react yet, we have to use dangerouslySetInnerHTML. If we now click the SVG any onClick handlers on the anchor will not run.

A temporary workaround is setting pointer-events: none on the SVG element inside the anchor.

This might be a dupe of #1509

SVG

Most helpful comment

Per the Stack Overflow comments, what worked for us was just to add pointer-events: none to <svg> and <use> elements.

All 9 comments

Does this work correctly outside of React? This just sounds like an IE bug.

Looking at this jQuery bug this seems to have been an issue in Chrome earlier too, but it seems to have been fixed in Chrome now as I could only replicate it in Edge and the IEs. As this is only a bug that occurs when using event delegation and seeing as that's the only way to do onClick in React I feel like this needs to be fixed.

Sorry for the delay on this. We don't use SVG with React much so this hasn't been a big priority for us. Do you have a suggestion for how React could fix this?

We also seem to be experiencing the same thing on iOS Safari. We're not using anchors, but have an svg inside a button with a click event bound to it, and when clicking the svg the click event is not triggered. Here's a similar report on the issue jQuery had:
http://stackoverflow.com/questions/25259825/svg-use-xlink-buggy-click-event

No problem on the delay. :)

I think I have a working solution that has no real drawbacks as far as I can see. I'll make a PR within the next couple of days.

I'm attaching an onMouseDown event to <use> parent element: <g> and started having problems after upgraded to Edge 41 (EdgeHTML 16).

<g onMouseDown={ () => console.log('mouseDown') }>
  <use xlinkHref="#gc-block-symbol" />
</g>

No problem when attaching listener manually/ directly to either <use> or <g>, using addEventlistener.

Seems like this jQuery issue is related

If you have some problem with React and you're sure it's not a browser problem, please file a new issue. We don't read or act on closed issues.

Per the Stack Overflow comments, what worked for us was just to add pointer-events: none to <svg> and <use> elements.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

varghesep picture varghesep  路  3Comments

kocokolo picture kocokolo  路  3Comments

krave1986 picture krave1986  路  3Comments

jimfb picture jimfb  路  3Comments

zpao picture zpao  路  3Comments