Skip to content

Commit 04c7b04

Browse files
committed
allows to close dialog (and all overlays) when pressing esc
1 parent 7dfa13f commit 04c7b04

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

components/dialog/Dialog.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const Dialog = (props) => {
2121
onMouseDown={props.onOverlayMouseDown}
2222
onMouseUp={props.onOverlayMouseUp}
2323
onMouseMove={props.onOverlayMouseMove}
24+
onEscKeyDown={props.onOverlayEscKeyDown}
2425
>
2526
<div data-react-toolbox='dialog' className={className}>
2627
<section role='body' className={style.body}>
@@ -41,6 +42,7 @@ Dialog.propTypes = {
4142
children: React.PropTypes.node,
4243
className: React.PropTypes.string,
4344
onOverlayClick: React.PropTypes.func,
45+
onOverlayEscKeyDown: React.PropTypes.func,
4446
onOverlayMouseDown: React.PropTypes.func,
4547
onOverlayMouseMove: React.PropTypes.func,
4648
onOverlayMouseUp: React.PropTypes.func,

components/dialog/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@ class DialogTest extends React.Component {
4444
| `onOverlayMouseDown` | `Function` | | Callback called when the mouse button is pressed on the overlay. |
4545
| `onOverlayMouseUp` | `Function` | | Callback called when the mouse button is released over the overlay. |
4646
| `onOverlayMouseMove` | `Function` | | Callback called when the mouse is moving over the overlay. |
47+
| `onOverlayEscKeyDown` | `Function` | | Callback called when the ESC is pressed with the overlay active. |
4748
| `title` | `String` | | The text string to use as standar title of the dialog.|
4849
| `type` | `String` | `normal` | Used to determine the size of the dialog. It can be `small`, `normal` or `large`. |

components/overlay/Overlay.jsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ class Overlay extends React.Component {
99
children: React.PropTypes.node,
1010
className: React.PropTypes.string,
1111
invisible: React.PropTypes.bool,
12-
onClick: React.PropTypes.func
12+
onClick: React.PropTypes.func,
13+
onEscKeyDown: React.PropTypes.func
1314
};
1415

1516
static defaultProps = {
@@ -22,15 +23,31 @@ class Overlay extends React.Component {
2223
this.node.setAttribute('data-react-toolbox', 'overlay');
2324
this.app.appendChild(this.node);
2425
this.handleRender();
26+
if (this.props.active) {
27+
this.escKeyListener = document.body.addEventListener('keydown', this.handleEscKey.bind(this));
28+
}
2529
}
2630

2731
componentDidUpdate () {
2832
this.handleRender();
33+
if (this.props.active && !this.escKeyListener) {
34+
this.escKeyListener = document.body.addEventListener('keydown', this.handleEscKey.bind(this));
35+
}
2936
}
3037

3138
componentWillUnmount () {
3239
ReactDOM.unmountComponentAtNode(this.node);
3340
this.app.removeChild(this.node);
41+
if (this.escKeyListener) {
42+
document.body.removeEventListener('keydown', this.handleEscKey)
43+
this.escKeyListener = null;
44+
}
45+
}
46+
47+
handleEscKey (e) {
48+
if (this.props.active && this.props.onEscKeyDown && e.which == 27) {
49+
this.props.onEscKeyDown(e);
50+
}
3451
}
3552

3653
handleRender () {

spec/components/dialog.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class DialogTest extends React.Component {
3030
active={this.state.active}
3131
title="Use Google's location service?"
3232
onOverlayClick={this.handleToggle}
33+
onOverlayEscKeyDown={this.handleToggle}
3334
>
3435
<p>Let Google help apps <strong>determine location</strong>. This means sending anonymous location data to Google, even when no apps are running.</p>
3536
<DialogChild />

0 commit comments

Comments
 (0)