Skip to content

Instantly share code, notes, and snippets.

@giancarlosisasi
Last active November 5, 2017 02:31
Show Gist options
  • Select an option

  • Save giancarlosisasi/1589ca9a88187e5a5d181052651627a0 to your computer and use it in GitHub Desktop.

Select an option

Save giancarlosisasi/1589ca9a88187e5a5d181052651627a0 to your computer and use it in GitHub Desktop.
class Dropdown extends Component {
state = {
show: false
}
componentDidMount() {
document.addEventListener('click', this.handleClickOutside, true);
}
componentWillUnmount() {
document.removeEventListener('click', this.handleClickOutside, true);
}
showContent = () => this.setState({ show: true })
hideContent = () => this.setState({ show: false })
// Handle function when user click outside container
handleClickOutside = (e) => {
const { onClickOutside } = this.props;
if (!this.container.contains(e.target) && this.state.show) {
onClickOutside(e);
}
}
render() {
if (!show) return null;
return (
<div ref={(el) => { this.container = el; }}>
the content dude
</div>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment