Skip to content

Commit

Permalink
Pressing esc closes dialog. Fixes #35.
Browse files Browse the repository at this point in the history
  • Loading branch information
hai-cea committed Nov 15, 2014
1 parent 326d5bc commit 0b75d29
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/js/dialog.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/** @jsx React.DOM */
/**
* @jsx React.DOM
*/

var Classable = require('./mixins/classable');
var Paper = require('./paper.jsx');
var React = require('react');
var React = require('react'),
Events = require('./utils/events.js'),
Classable = require('./mixins/classable'),
Paper = require('./paper.jsx');

var Dialog = React.createClass({

Expand All @@ -27,6 +30,14 @@ var Dialog = React.createClass({
};
},

componentDidMount: function() {
Events.on(document, 'keyup', this._checkEscapeKeyUp);
},

componentWillUnmount: function() {
Events.off(document, 'keyup', this._checkEscapeKeyUp);
},

componentDidUpdate: function (prevProps, prevState) {
//calculate height and use that to center the dialog vertically
var dom = this.getDOMNode(),
Expand All @@ -35,10 +46,6 @@ var Dialog = React.createClass({
dom.style.marginTop = -1 * height / 2 + 'px';
},

_handleClickAway: function() {
this.dismiss();
},

render: function() {
var mainClasses = this.getClasses('dialog', { 'show': this.state.open }),
actions = this.props.actions.map(function(a) {
Expand Down Expand Up @@ -73,8 +80,18 @@ var Dialog = React.createClass({
show: function() {
this.setState({ open: true });
if (this.props.onShow) this.props.onShow();
},

_handleClickAway: function() {
this.dismiss();
},

_checkEscapeKeyUp: function(e) {
if (e.keyCode == 27) {
this.dismiss();
}
}

});

module.exports = Dialog;
module.exports = Dialog;

0 comments on commit 0b75d29

Please sign in to comment.