Skip to content
This repository has been archived by the owner on Sep 1, 2020. It is now read-only.

Make menu closable with escape key #75

Merged
merged 2 commits into from
Feb 14, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/ContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export default class ContextMenu extends Component {
document.addEventListener('ontouchstart', this.handleOutsideClick);
document.addEventListener('scroll', this.handleHide);
document.addEventListener('contextmenu', this.handleHide);
document.addEventListener('keyup', this.handleEscape);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also remove the event listener in unregisterHandlers

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

window.addEventListener('resize', this.handleHide);
}

Expand Down Expand Up @@ -91,6 +92,12 @@ export default class ContextMenu extends Component {
callIfExists(this.props.onHide, e);
}

handleEscape = (e) => {
if (e.key === 'Escape') {
if (!this.menu.contains(e.target)) hideMenu();
}
}

handleOutsideClick = (e) => {
if (!this.menu.contains(e.target)) hideMenu();
}
Expand Down