I have a drawer component with a button that opens this drawer. I want to close the drawer by clicking everywhere on the page except drawer area. I have tried this code. It works for opening but it isn't working for closing.
var Child = React.createClass({
render: function() {
return (
<div className="chatBar">
<div onClick={this.onClick} className="closeBTN">
<img src="../src/contents/images/svg/close.svg"/>
</div>
<Tab />
</div>
);
}
});
var ChatBar = React.createClass({
getInitialState: function () {
return { childVisible: false ,childInVisible: true ,};
},
render: function() {
return(
<div>
<div onClick={this.onClick} className="chatBTN">
<img src="../src/contents/images/svg/chat.svg"/>
</div>
{
this.state.childVisible
? <Child />
: null
}
</div>
)
},
onClick: function() {
this.setState({childVisible: !this.state.childVisible});
},
onClickClose: function(){
this.setState({childInVisible: !this.state.childInVisible});
},
});
export default ChatBar;
A panel that slides out from the edge of the page can replace Modal to present more content.
A Drawer is a panel that is typically overlaid on top of a page and slides in from the side. It contains a set of information or actions.
Material UI Drawer is the most widely used component of Material UI. Material UI Drawer is used as a navigation bar of the website which displays a list of items and then clicking on the item the user will be redirected to the specific part of the web page/website.
Please check the working demo JSFiddle.
var Child = React.createClass({
render: function() {
return (
<div className="chatBar">
<div onClick={this.props.onClick} className="closeBTN">
<img src="http://www.freeiconspng.com/uploads/silver-close-button-png-15.png"/>
</div>
</div>
);
}
});
var ChatBar = React.createClass({
getInitialState: function () {
return { childVisible: false };
},
render: function() {
return(
<div>
<div onClick={this.onToggle} className="chatBTN">
<img src="http://www.omeglechat.eu/wp-content/uploads/2016/10/omegle-mnogochat.png"/>
</div>
{
this.state.childVisible
? <Child onClick={this.onToggle.bind(this)} />
: null
}
</div>
)
},
onToggle: function() {
this.setState({childVisible: !this.state.childVisible});
}
});
React.render(<ChatBar />, document.body);
First, use one flag in the state
:
onToggle: function() {
this.setState({childVisible: !this.state.childVisible});
}
Secondly, in order to call a function (onClick
) in the child component, you need to pass in the handler through <Child onClick={this.onToggle.bind(this)} />
, and call it in the child component through onClick={this.props.onClick}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With