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

Add proper event listener to Portal #4359

Merged
merged 1 commit into from
Jan 31, 2017
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
"react-dom": "15.4.1",
"react-dropzone": "3.7.3",
"react-element-to-jsx-string": "6.0.0",
"react-event-listener": "0.4.1",
"react-intl": "2.1.5",
"react-portal": "3.0.0",
"react-redux": "4.4.6",
Expand Down
31 changes: 25 additions & 6 deletions js/src/ui/Portal/portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

import EventListener from 'react-event-listener';
import React, { Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
import ReactPortal from 'react-portal';
Expand Down Expand Up @@ -85,6 +86,7 @@ export default class Portal extends Component {
onKeyDown={ this.handleKeyDown }
>
<ParityBackground className={ styles.parityBackground } />
{ this.renderBindings() }
{ this.renderCloseIcon() }
{ children }
</div>
Expand All @@ -93,6 +95,21 @@ export default class Portal extends Component {
);
}

renderBindings () {
const { expanded } = this.state;

if (!expanded) {
return null;
}

return (
<EventListener
target='window'
onKeyUp={ this.handleKeyUp }
/>
);
}

renderCloseIcon () {
const { expanded } = this.state;

Expand Down Expand Up @@ -121,18 +138,20 @@ export default class Portal extends Component {

handleKeyDown = (event) => {
const { onKeyDown } = this.props;

event.persist();
return onKeyDown
? onKeyDown(event)
: false;
}

handleKeyUp = (event) => {
const codeName = keycode(event);

switch (codeName) {
case 'esc':
event.preventDefault();
return this.handleClose();

default:
event.persist();
return onKeyDown
? onKeyDown(event)
: false;
}
}

Expand Down