Skip to content

Instantly share code, notes, and snippets.

@abinavseelan
Last active December 8, 2020 12:02
Show Gist options
  • Save abinavseelan/58b5ea041ec6a7013248705cd76ff691 to your computer and use it in GitHub Desktop.
Save abinavseelan/58b5ea041ec6a7013248705cd76ff691 to your computer and use it in GitHub Desktop.
Component for a dropdown interaction (5)
import React, { Component } from 'react';
class Card extends Component {
constructor() {
super();
this.state = {
showMenu: false,
};
this.showMenu = this.showMenu.bind(this);
this.closeMenu = this.closeMenu.bind(this);
}
showMenu(event) {
event.preventDefault();
this.setState({ showMenu: true }, () => {
document.addEventListener('click', this.closeMenu);
});
}
closeMenu() {
this.setState({ showMenu: false }, () => {
document.removeEventListener('click', this.closeMenu);
});
}
render() {
return (
<div>
<button onClick={this.showMenu}>
Show menu
</button>
{
this.state.showMenu
? (
<div
className="menu"
ref={(element) => {
this.dropdownMenu = element;
}}
>
<button> Menu item 1 </button>
<button> Menu item 2 </button>
<button> Menu item 3 </button>
</div>
)
: (
null
)
}
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment