Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is svg not identified inside a node using .contain method using javascript?

Tags:

javascript

i want to identify the click outside a div element and close the dropdown menu based on it. However, clicking the svg element inside the div element (containing input element and svg) would identify it as click outside div element. Not sure how to solve it. I have tried using css trick as below,

button > * {
    pointer-events: none;
}

But it didnt work as well.

What i am trying to do ?

I have a form input element. within that i have a input with dropdown with up and down svgs. so when i click the svg down it identifies it as document click.

class Parent extends React.PureComponent {
    state = {
        opened: false,
    };

    componentDidMount(){
        document.addEventListener('click', this.handle_document_click);
    }

    arrow_up_click = (e) => {
        e.stopPropagation();
        e.PreventDefault();
    }
    arrow_down_click = (e) => {
        e.stopPropagation();
        e.PreventDefault();
    }

    handle_document_click = () => {
        if (this.input_ref.current && 
            this.input_ref.current.contains(e.target)) {
                return;
        }
        this.setState({opened: false});
    };

    render = () => {
        return (
            <form onsubmit={handle_submit}>
                <div ref={this.input_ref}>
                    <input/>
                    <button>
                        {state.opened && <SvgUp height="22" width="22"/ 
                        onClick={arrow_up_click}>}
                        {!state.opened && <SvgDown height="22" 
                        width="22" onClick={arrow_down_click}/>}
                    </button>
               </div>
            </form>
        )
    }
}

Could someone help me fix this. I want the click on svg to not to be identified as document click. Thanks.

like image 506
stackoverflow_user Avatar asked Aug 26 '19 20:08

stackoverflow_user


People also ask

Can SVG integrate with JavaScript?

Since SVG images can be inlined in HTML, we can manipulate them with JavaScript. This means that we can animate parts of an image from code, make it interactive, or turn things around and generate graphics from data.

Does Z Index work with SVG?

Contrary to the rules in CSS 2.1, the ' z-index ' property applies to all SVG elements regardless of the value of the ' position ' property, with one exception: as for boxes in CSS 2.1, outer 'svg' elements must be positioned for ' z-index ' to apply to them.

Where can I find SVG element?

We can use xpath to grab SVG elements with Selenium webdriver. A SVG element is identified with tagname svg. The svg image has the attributes like width and height attributes. Let us investigate the html code of a svg element.


Video Answer


1 Answers

You change the SVG icon depending on the state on the component. When the event of the SVG you swapped out reaches the event handler it is no longer "contained" in the div.

Found the answer in this very similar question: Why a click on svg element is not captured by node contains method?

That means that you do not have to disable pointer-events for all SVG:s but just have to wrap the components that are swapped out with a div containing a css class with pointer-events: none;.

like image 53
LordCrocket Avatar answered Sep 20 '22 12:09

LordCrocket