Skip to content

Commit

Permalink
move hideModal logic to Modal component
Browse files Browse the repository at this point in the history
  • Loading branch information
echenley committed Sep 19, 2015
1 parent 4d8116d commit 147b893
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
18 changes: 1 addition & 17 deletions src/js/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,13 @@ const App = React.createClass({
},

onModalUpdate(newModalState) {
const oldModalState = this.state.modal;

function onKeyUp(e) {
// esc key closes modal
if (e.keyCode === 27) {
Actions.hideModal();
}
}

// pressing esc closes modal
if (!oldModalState.type && newModalState.type) {
document.addEventListener('keyup', onKeyUp);
} else if (oldModalState.type && !newModalState.type) {
document.removeEventListener('keyup', onKeyUp);
}

this.setState({
modal: newModalState
});
},

hideModal(e) {
e.preventDefault();
if (e) { e.preventDefault(); }
Actions.hideModal();
},

Expand Down
16 changes: 16 additions & 0 deletions src/js/components/Modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ const Modal = React.createClass({
]).isRequired
},

componentDidMount() {
// allow esc to close modal
document.addEventListener('keyup', this.onKeyUp);
},

componentWillUnmount() {
document.removeEventListener('keyup', this.onKeyUp);
},

onKeyUp(e) {
// esc key closes modal
if (e.keyCode === 27) {
this.props.hideModal();
}
},

render() {
const { hideModal, children } = this.props;

Expand Down

0 comments on commit 147b893

Please sign in to comment.