Skip to content

Commit 2f4b819

Browse files
committed
unmount on click outside container
1 parent eaef7d9 commit 2f4b819

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

src/commands/Emoji/Emoji.scss

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
.emoji {
2-
height: 300px;
2+
height: 310px;
33
}
44
.tip {
55
font-size: 12px;
66
color: #aaa;
77
margin: 0;
8-
margin-bottom: 5px;
8+
margin-bottom: 10px;
9+
padding: 0;
910
}
1011
.emojiContainer {
1112
overflow: scroll;

src/commands/mount.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export function mountReactComponent(Component, field, onDone, onInsert) {
2727
left={caretOffset.left}
2828
onInsert={onInsert}
2929
onDone={_onDone}
30+
container={container}
3031
/>,
31-
getContainer()
32+
container
3233
)
3334
}

src/components/Container.jsx

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import $ from 'jquery'
12
import React from 'react'
3+
import ReactDOM from 'react-dom'
24
import classnames from 'classnames'
5+
import NativeListener from 'react-native-listener'
36

47
import * as Extension from 'lib/extension'
58
import styles from './Container.scss'
@@ -20,20 +23,41 @@ let Version = (props) => {
2023
)
2124
}
2225

23-
let Container = (props) => {
24-
let style = {
25-
top: props.top,
26-
left: props.left
26+
class Container extends React.Component {
27+
constructor(props) {
28+
super(props)
29+
this.unmountOnClickOutside = this.unmountOnClickOutside.bind(this)
2730
}
28-
let classes = classnames(styles.container, props.className)
2931

30-
return (
31-
<div className={classes} style={style}>
32-
{ props.children }
33-
<Icon />
34-
<Version />
35-
</div>
36-
)
32+
componentWillMount() {
33+
$('html').on('click', this.unmountOnClickOutside)
34+
}
35+
36+
componentWillUnmount() {
37+
$('html').off('click', this.unmountOnClickOutside)
38+
}
39+
40+
unmountOnClickOutside(evt) {
41+
if (!this.props.container.contains(evt.target)) {
42+
ReactDOM.unmountComponentAtNode(this.props.container)
43+
}
44+
}
45+
46+
render() {
47+
let style = {
48+
top: this.props.top,
49+
left: this.props.left
50+
}
51+
let classes = classnames(styles.container, this.props.className)
52+
53+
return (
54+
<div className={classes} style={style} >
55+
{ this.props.children }
56+
<Icon />
57+
<Version />
58+
</div>
59+
)
60+
}
3761
}
3862
Container.propTypes = {
3963
top: React.PropTypes.number.isRequired,

0 commit comments

Comments
 (0)