-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathEmojiPicker.js
189 lines (167 loc) · 6.75 KB
/
EmojiPicker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import React from 'react';
import {Dimensions, Keyboard} from 'react-native';
import _ from 'underscore';
import EmojiPickerMenu from './EmojiPickerMenu';
import CONST from '../../CONST';
import PopoverWithMeasuredContent from '../PopoverWithMeasuredContent';
const DEFAULT_ANCHOR_ORIGIN = {
horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.RIGHT,
vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.BOTTOM,
};
class EmojiPicker extends React.Component {
constructor(props) {
super(props);
this.hideEmojiPicker = this.hideEmojiPicker.bind(this);
this.showEmojiPicker = this.showEmojiPicker.bind(this);
this.selectEmoji = this.selectEmoji.bind(this);
this.measureEmojiPopoverAnchorPosition = this.measureEmojiPopoverAnchorPosition.bind(this);
this.measureEmojiPopoverAnchorPositionAndUpdateState = this.measureEmojiPopoverAnchorPositionAndUpdateState.bind(this);
this.focusEmojiSearchInput = this.focusEmojiSearchInput.bind(this);
this.measureContent = this.measureContent.bind(this);
this.onModalHide = () => {};
this.onEmojiSelected = () => {};
this.state = {
isEmojiPickerVisible: false,
// The horizontal and vertical position (relative to the window) where the emoji popover will display.
emojiPopoverAnchorPosition: {
horizontal: 0,
vertical: 0,
},
emojiPopoverAnchorOrigin: DEFAULT_ANCHOR_ORIGIN,
};
}
componentDidMount() {
this.emojiPopoverDimensionListener = Dimensions.addEventListener('change', this.measureEmojiPopoverAnchorPositionAndUpdateState);
}
componentDidUpdate(prevProps, prevState) {
if (prevState.isEmojiPickerVisible === this.state.isEmojiPickerVisible || !this.state.isEmojiPickerVisible) {
return;
}
// Dismiss the keyboard to provide a focus for the emoji picker to avoid selection issues.
Keyboard.dismiss();
}
componentWillUnmount() {
if (!this.emojiPopoverDimensionListener) {
return;
}
this.emojiPopoverDimensionListener.remove();
}
/**
* Callback for the emoji picker to add whatever emoji is chosen into the main input
*
* @param {String} emoji
* @param {Object} emojiObject
*/
selectEmoji(emoji, emojiObject) {
// Prevent fast click / multiple emoji selection;
// The first click will hide the emoji picker by calling the hideEmojiPicker() function
// and in that function the emojiPopoverAnchor prop to will be set to null (synchronously)
// thus we rely on that prop to prevent fast click / multiple emoji selection
if (!this.emojiPopoverAnchor) {
return;
}
this.hideEmojiPicker();
if (_.isFunction(this.onEmojiSelected)) {
this.onEmojiSelected(emoji, emojiObject);
}
}
/**
* Hide the emoji picker menu.
*
* @param {Boolean} isNavigating
*/
hideEmojiPicker(isNavigating) {
if (isNavigating) { this.onModalHide = () => {}; }
this.emojiPopoverAnchor = null;
this.setState({isEmojiPickerVisible: false});
}
/**
* Show the emoji picker menu.
*
* @param {Function} [onModalHide=() => {}] - Run a callback when Modal hides.
* @param {Function} [onEmojiSelected=() => {}] - Run a callback when Emoji selected.
* @param {Element} emojiPopoverAnchor - Element to which Popover is anchored
* @param {Object} [anchorOrigin=DEFAULT_ANCHOR_ORIGIN] - Anchor origin for Popover
* @param {Function} [onWillShow=() => {}] - Run a callback when Popover will show
*/
showEmojiPicker(onModalHide, onEmojiSelected, emojiPopoverAnchor, anchorOrigin, onWillShow = () => {}) {
this.onModalHide = onModalHide;
this.onEmojiSelected = onEmojiSelected;
this.emojiPopoverAnchor = emojiPopoverAnchor;
if (this.emojiPopoverAnchor) {
// Drop focus to avoid blue focus ring.
emojiPopoverAnchor.blur();
}
this.measureEmojiPopoverAnchorPosition().then((emojiPopoverAnchorPosition) => {
onWillShow();
this.setState({isEmojiPickerVisible: true, emojiPopoverAnchorPosition, emojiPopoverAnchorOrigin: anchorOrigin || DEFAULT_ANCHOR_ORIGIN});
});
}
measureEmojiPopoverAnchorPosition() {
return new Promise((resolve) => {
if (!this.emojiPopoverAnchor) {
return resolve({horizontal: 0, vertical: 0});
}
this.emojiPopoverAnchor.measureInWindow((x, y, width) => resolve({horizontal: x + width, vertical: y}));
});
}
measureEmojiPopoverAnchorPositionAndUpdateState() {
this.measureEmojiPopoverAnchorPosition().then((emojiPopoverAnchorPosition) => {
this.setState({emojiPopoverAnchorPosition});
});
}
/**
* Used to calculate the EmojiPicker Dimensions
*
* @returns {JSX}
*/
measureContent() {
return (
<EmojiPickerMenu
onEmojiSelected={this.selectEmoji}
ref={el => this.emojiSearchInput = el}
/>
);
}
/**
* Focus the search input in the emoji picker.
*/
focusEmojiSearchInput() {
if (!this.emojiSearchInput) {
return;
}
this.emojiSearchInput.focus();
}
render() {
// There is no way to disable animations and they are really laggy, because there are so many
// emojis. The best alternative is to set it to 1ms so it just "pops" in and out
return (
<PopoverWithMeasuredContent
isVisible={this.state.isEmojiPickerVisible}
onClose={this.hideEmojiPicker}
onModalShow={this.focusEmojiSearchInput}
onModalHide={this.onModalHide}
hideModalContentWhileAnimating
shouldSetModalVisibility={false}
animationInTiming={1}
animationOutTiming={1}
anchorPosition={{
vertical: this.state.emojiPopoverAnchorPosition.vertical,
horizontal: this.state.emojiPopoverAnchorPosition.horizontal,
}}
popoverDimensions={{
width: CONST.EMOJI_PICKER_SIZE.WIDTH,
height: CONST.EMOJI_PICKER_SIZE.HEIGHT,
}}
anchorOrigin={this.state.emojiPopoverAnchorOrigin}
measureContent={this.measureContent}
>
<EmojiPickerMenu
onEmojiSelected={this.selectEmoji}
ref={el => this.emojiSearchInput = el}
/>
</PopoverWithMeasuredContent>
);
}
}
export default EmojiPicker;