Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit 3e16180

Browse files
Merge pull request #455 from lightninglabs/close-widgets-on-esc
Close channel/tx detail on escape press
2 parents 564ccd1 + 975f52d commit 3e16180

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

src/component/escapable.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Component } from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
class Escapable extends Component {
5+
constructor(props) {
6+
super(props);
7+
this.handleKeyDown = this.handleKeyDown.bind(this);
8+
}
9+
10+
componentDidMount() {
11+
if (typeof document !== 'undefined') {
12+
document.addEventListener('keydown', this.handleKeyDown);
13+
}
14+
}
15+
16+
componentWillUnmount() {
17+
if (typeof document !== 'undefined') {
18+
document.removeEventListener('keydown', this.handleKeyDown);
19+
}
20+
}
21+
22+
handleKeyDown(event) {
23+
if (event.keyCode === 27) {
24+
this.props.onClose();
25+
}
26+
}
27+
}
28+
29+
Escapable.propTypes = {
30+
onClose: PropTypes.func.isRequired,
31+
};
32+
33+
export default Escapable;

src/component/modal.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React from 'react';
22
import { View, ViewPropTypes, StyleSheet } from 'react-native';
33
import PropTypes from 'prop-types';
4-
import { Button } from '../../src/component/button';
4+
import { Button } from './button';
55
import { H4Text } from './text';
66
import Icon from './icon';
7+
import Escapable from './escapable';
78
import { color } from './style';
89

910
const styles = StyleSheet.create({
@@ -34,15 +35,20 @@ const styles = StyleSheet.create({
3435
},
3536
});
3637

37-
const Modal = ({ title = '', onClose, children, style }) => (
38-
<View style={[styles.modal, style]}>
39-
<H4Text style={styles.title}>{title.toUpperCase()}</H4Text>
40-
<Button style={styles.cancelBtn} onPress={onClose}>
41-
<Icon image="cancel-grey" style={styles.cancelIcon} />
42-
</Button>
43-
{children}
44-
</View>
45-
);
38+
class Modal extends Escapable {
39+
render() {
40+
const { title = '', style, onClose, children } = this.props;
41+
return (
42+
<View style={[styles.modal, style]}>
43+
<H4Text style={styles.title}>{title.toUpperCase()}</H4Text>
44+
<Button style={styles.cancelBtn} onPress={onClose}>
45+
<Icon image="cancel-grey" style={styles.cancelIcon} />
46+
</Button>
47+
{children}
48+
</View>
49+
);
50+
}
51+
}
4652

4753
Modal.propTypes = {
4854
title: PropTypes.string.isRequired,

0 commit comments

Comments
 (0)