Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

Esc dismisses Modal component #386

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
10 changes: 10 additions & 0 deletions src/components/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class Modal extends PureComponent<Props, State> {
outdatedChildren: null,
};

componentDidMount() {
document.addEventListener('keydown', this.handleKeyboardEvent, false);
}

componentWillReceiveProps(nextProps: Props) {
// When the modal is dismissed, we want to render the "stale" children for
// a couple hundred milliseconds, until the modal has fully closed.
Expand All @@ -73,6 +77,12 @@ class Modal extends PureComponent<Props, State> {
}
}

handleKeyboardEvent = (event: any) => {
if (event.keyCode === 27) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit. Escape handling is working but I would use if (event.key === 'Escape') { }.
We could also leave it with the keyCode as it's only one key here but with the string, it's more readable.

this.props.onDismiss();
}
};

render() {
const { isVisible, width, height, onDismiss, children } = this.props;
const { outdatedChildren } = this.state;
Expand Down