React & SVG: How do I make support onClick?

This is what React SVG currently supports:

I'm trying to figure out how to make a shape I drew using the SVG path clickable. If there is another way to draw a shape that can be made clickable, that works too.

Thanks!

0

6 Answers

This worked for me.

svg { pointer-events: none; } path{ pointer-events: auto; } 

Then we can add on click event on path. worked!! thanks

I wrap my SVG with a div and apply any attributes that I desire (click handlers, fill colors, classes, width, etc..), like so (fiddle link):

import React, { PropTypes } from 'react' function XMark({ width, height, fill, onClick }) { return ( <div className="xmark-container" onClick={onClick}> <svg className='xmark' viewBox="67 8 8 8" width={width} height={height} version="1.1" xmlns="" xlink=""> <polygon stroke="none" fill={fill} fillRule="evenodd" points="74.0856176 9.4287633 71.5143809 12 74.0856176 14.5712367 73.5712367 15.0856176 71 12.5143809 68.4287633 15.0856176 67.9143824 14.5712367 70.4856191 12 67.9143824 9.4287633 68.4287633 8.91438245 71 11.4856191 73.5712367 8.91438245 74.0856176 9.4287633 74.0856176 9.4287633 74.0856176 9.4287633" /> </svg> </div> ) } XMark.propTypes = { width: PropTypes.number, height: PropTypes.number, fill: PropTypes.string, onClick: PropTypes.func, } XMark.defaultProps = { width: 8, height: 8, fill: '#979797', onClick: null, } export default XMark 

You can of course ditch the wrapper and apply the onClick to the svg element as well, but I've found this approach works well for me! (I also try and use pure functions when possible )

0

I did it this way:

Just using polyline for example, it could be any SVG element.

export default class Polyline extends React.Component { componentDidMount() { this.polyline.addEventListener('click', this.props.onClick); } componentWillUnmount(){ this.polyline.removeEventListener('click', this.props.onClick); } render() { const {points, style, markerEnd} = this.props; return <polyline points={points} ref={ref => this.polyline = ref} style={style} markerEnd={markerEnd}/>; } } 
  • get ref in ref callback
  • on componentDidMount add click event listener to the ref
  • remove event listener in componentWillUnmount
3

You can use onClick as you do with other DOM elements.

1

Two major ways to do this:

  1. You put an HTML event listener on the 'path' tag in the svg code. You will have to escape your code properly if you choose this method. The following example features a star shape cut in two paths each of which logs "Hello" in the console ( console.log("Hello") ) Example:
<?xml version="1.0" encoding="utf-8"?> <svg version="1.1" xmlns="" xmlns:xlink="" x="0px" y="0px" viewBox="0 0 225 213.6" xml:space="preserve"> <style type="text/css"> .st0{fill:#4D4D4D;} .st1{fill:#494949;} </style> <g> <path onmouseover="console.log(&quot;Hello&quot;)" d="M43.5,131c4.4,4.2,6.3,8.7,5,15.1 c-3.4,17.2-6,34.6-9,51.9c-1.5,8.5,1.6,14.4,9.1,15.5c3.1,0.5,6.8-0.9,9.8-2.4c14.9-7.6,29.8-15.4,44.5-23.5c3-1.6,5.8-2.5,8.6-2.7 V0.1c-1.1,0.2-2.1,0.5-3.2,1.1c-2.7,1.4-5.2,4.2-6.7,7c-7.8,15.2-15.6,30.5-22.8,46C75.6,61,71,64.7,63.5,65.7 c-16.6,2.2-33.1,5.1-49.7,6.9C6.4,73.5,2.4,77.2,0,83.5c0,0.7,0,1.3,0,2c3.5,4.5,6.7,9.4,10.7,13.4C21.4,109.8,32.5,120.4,43.5,131 z"/> <path onmouseover="console.log(&quot;Hello&quot;)" d="M206.4,71.9c-15.9-2.1-31.8-4.7-47.7-6.9c-5.3-0.7-8.8-3.5-11-8.2c-8-16.2-16-32.5-24.1-48.7 c-2.9-5.8-7.3-8.8-12-8v184.8c3.5-0.2,6.9,0.7,10.6,2.7c14.7,8.1,29.6,15.8,44.5,23.5c2.9,1.5,6.7,2.9,9.8,2.4 c7.3-1,10.5-6.8,9.2-15c-3-17.8-5.9-35.6-9.2-53.3c-1.1-5.6,0.7-9.8,4.5-13.4c11.2-10.9,22.6-21.7,33.6-32.8c4-4,7.1-8.9,10.7-13.4 c0-0.7,0-1.3,0-2C222.3,74.1,214.5,72.9,206.4,71.9z"/> </g> </svg> 

Example:

  1. In Adobe Illustrator there is (currently called) SVG Iteractivity tool. You can find it under the Window top menu. Then select the path you need with direct selection tool, choose the HTML event from the SVG Interactivity widow and write your Javascript Code below. Then click 'Export Selection' from the file menu or Save As... and save all as .svg

The result will be .svg file with automatically properly escaped code and HTML event on the 'path' tag.

I had similar problem with react, I was trying to handle onclick event for svg.

Simple css solved problem for me:

svg { pointer-events: none; } 
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like