yysun/apprun

Problem with 'onclick' on SVG elements

jkleiser opened this issue · 7 comments

I'm building an SPA that shall make extensive use of SVG. This far I have not been able to get any form of 'onclick' working on my SVG elements. I have tried both $onclick="test" and onclick={e => this.run("test", e)}, in elements like these:

<circle cx={cx} cy={cy} r={rad} $onclick="test"/>
<rect x="8" y="8" width="90" height="90" stroke="blue" onclick={e => this.run("test", e)}/>

This, however, works fine: <h1 onclick={e => this.run("test", e)}>TEST</h1>
I'm using apprun 2.23.12. Is this a bug?

This works, at least in Safari and Brave Browser:

<rect x="10" y="80" width="90" height="40" stroke="blue" onclick="alert('You clicked the rect.')"/>
yysun commented

Are you using HTML or JSX? $onclick only works in JSX.

I'm using JSX.

view = (state) => <div>...

You can try with this, e.g. inside the SPA's ContactComponent:

<svg viewBox="0 0 520 520" xmlns="http://www.w3.org/2000/svg">
  <rect x="10" y="10" width="90" height="40" onclick={e => this.run("test", e)}/>
  <rect x="10" y="80" width="90" height="40" onclick="alert('You have clicked the rect.')"/>
</svg>

and then this:

update = {
  "test": (state, evt) => {
    console.log("test: %s", evt.target);
    return state;
  }
}  
yysun commented
yysun commented

It is now fixed in v2.23.13. I also added it as an example in the playground.
https://apprun.js.org/#play/23

Thanks! Works fine now.