mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-11-22 03:37:04 +00:00
Make dropdown close on clickoutside, not mouseout
Accomplished by adding an onClick event listener to the whole document and then closing if the click was not inside the dropdown.
This commit is contained in:
1 parent
a17a9ac0a0
commit
253db8304a
1 file changed
+16
-1
@@ -83,6 +83,20 @@ const Nav = {
|
|||||||
showDropdown : false
|
showDropdown : false
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
componentDidMount : function() {
|
||||||
|
if(this.props.trigger == 'click')
|
||||||
|
document.addEventListener('click', this.handleClickOutside);
|
||||||
|
},
|
||||||
|
componentWillUnmount : function() {
|
||||||
|
if(this.props.trigger == 'click')
|
||||||
|
document.removeEventListener('click', this.handleClickOutside);
|
||||||
|
},
|
||||||
|
handleClickOutside : function(e){
|
||||||
|
// Close dropdown when clicked outside
|
||||||
|
if(this.refs.dropdown && !this.refs.dropdown.contains(e.target)) {
|
||||||
|
this.handleDropdown(false);
|
||||||
|
}
|
||||||
|
},
|
||||||
handleDropdown : function(show){
|
handleDropdown : function(show){
|
||||||
this.setState({
|
this.setState({
|
||||||
showDropdown : show
|
showDropdown : show
|
||||||
@@ -105,9 +119,10 @@ const Nav = {
|
|||||||
});
|
});
|
||||||
return (
|
return (
|
||||||
<div className={`navDropdownContainer ${this.props.className}`}
|
<div className={`navDropdownContainer ${this.props.className}`}
|
||||||
|
ref='dropdown'
|
||||||
onMouseEnter={this.props.trigger == 'hover' ? ()=>{this.handleDropdown(true);} : undefined}
|
onMouseEnter={this.props.trigger == 'hover' ? ()=>{this.handleDropdown(true);} : undefined}
|
||||||
onClick= {this.props.trigger == 'click' ? ()=>{this.handleDropdown(true);} : undefined}
|
onClick= {this.props.trigger == 'click' ? ()=>{this.handleDropdown(true);} : undefined}
|
||||||
onMouseLeave={()=>this.handleDropdown(false)}>
|
onMouseLeave={this.props.trigger == 'hover' ? ()=>{this.handleDropdown(false);} : undefined}>
|
||||||
{this.props.children[0] || this.props.children /*children is not an array when only one child*/}
|
{this.props.children[0] || this.props.children /*children is not an array when only one child*/}
|
||||||
{this.renderDropdown(dropdownChildren)}
|
{this.renderDropdown(dropdownChildren)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in new issue
Block a user