-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
EmojiPicker.tsx
238 lines (220 loc) · 9.89 KB
/
EmojiPicker.tsx
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import React, {forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState} from 'react';
import type {ForwardedRef, RefObject} from 'react';
import {Dimensions, View} from 'react-native';
import type {Emoji} from '@assets/emojis/types';
import FocusTrapForModal from '@components/FocusTrap/FocusTrapForModal';
import PopoverWithMeasuredContent from '@components/PopoverWithMeasuredContent';
import type {BaseTextInputRef} from '@components/TextInput/BaseTextInput/types';
import withViewportOffsetTop from '@components/withViewportOffsetTop';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import type {AnchorOrigin, EmojiPickerRef, EmojiPopoverAnchor, OnEmojiSelected, OnModalHideValue, OnWillShowPicker} from '@libs/actions/EmojiPickerAction';
import * as Browser from '@libs/Browser';
import calculateAnchorPosition from '@libs/calculateAnchorPosition';
import * as Modal from '@userActions/Modal';
import CONST from '@src/CONST';
import EmojiPickerMenu from './EmojiPickerMenu';
const DEFAULT_ANCHOR_ORIGIN = {
horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.RIGHT,
vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.BOTTOM,
};
type EmojiPickerProps = {
viewportOffsetTop: number;
};
function EmojiPicker({viewportOffsetTop}: EmojiPickerProps, ref: ForwardedRef<EmojiPickerRef>) {
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const [isEmojiPickerVisible, setIsEmojiPickerVisible] = useState(false);
const [emojiPopoverAnchorPosition, setEmojiPopoverAnchorPosition] = useState({
horizontal: 0,
vertical: 0,
});
const [emojiPopoverAnchorOrigin, setEmojiPopoverAnchorOrigin] = useState<AnchorOrigin>(DEFAULT_ANCHOR_ORIGIN);
const [activeID, setActiveID] = useState<string | null>();
const emojiPopoverAnchorRef = useRef<EmojiPopoverAnchor | null>(null);
const emojiAnchorDimension = useRef({
width: 0,
height: 0,
});
const onModalHide = useRef<OnModalHideValue>(() => {});
const onEmojiSelected = useRef<OnEmojiSelected>(() => {});
const activeEmoji = useRef<string | undefined>();
const emojiSearchInput = useRef<BaseTextInputRef | null>();
const {windowHeight} = useWindowDimensions();
const {shouldUseNarrowLayout} = useResponsiveLayout();
/**
* Get the popover anchor ref
*
* emojiPopoverAnchorRef contains either null or the ref object of the anchor element.
* { current: { current: anchorElement } }
*
* Don't directly get the ref from emojiPopoverAnchorRef, instead use getEmojiPopoverAnchor()
*/
const getEmojiPopoverAnchor = useCallback(() => emojiPopoverAnchorRef.current ?? emojiPopoverAnchorRef?.current, []);
/**
* Show the emoji picker menu.
*
* @param [onModalHideValue=() => {}] - Run a callback when Modal hides.
* @param [onEmojiSelectedValue=() => {}] - Run a callback when Emoji selected.
* @param emojiPopoverAnchorValue - Element to which Popover is anchored
* @param [anchorOrigin=DEFAULT_ANCHOR_ORIGIN] - Anchor origin for Popover
* @param [onWillShow] - Run a callback when Popover will show
* @param id - Unique id for EmojiPicker
* @param activeEmojiValue - Selected emoji to be highlighted
*/
const showEmojiPicker = (
onModalHideValue: OnModalHideValue,
onEmojiSelectedValue: OnEmojiSelected,
emojiPopoverAnchorValue: EmojiPopoverAnchor,
anchorOrigin?: AnchorOrigin,
onWillShow?: OnWillShowPicker,
id?: string,
activeEmojiValue?: string,
) => {
onModalHide.current = onModalHideValue;
onEmojiSelected.current = onEmojiSelectedValue;
activeEmoji.current = activeEmojiValue;
emojiPopoverAnchorRef.current = emojiPopoverAnchorValue;
const emojiPopoverAnchor = getEmojiPopoverAnchor();
// Drop focus to avoid blue focus ring.
emojiPopoverAnchor?.current?.blur();
const anchorOriginValue = anchorOrigin ?? DEFAULT_ANCHOR_ORIGIN;
// It's possible that the anchor is inside an active modal (e.g., add emoji reaction in report context menu).
// So, we need to get the anchor position first before closing the active modal which will also destroy the anchor.
calculateAnchorPosition(emojiPopoverAnchor?.current, anchorOriginValue).then((value) => {
Modal.close(() => {
onWillShow?.();
setIsEmojiPickerVisible(true);
setEmojiPopoverAnchorPosition({
horizontal: value.horizontal,
vertical: value.vertical,
});
emojiAnchorDimension.current = {
width: value.width,
height: value.height,
};
setEmojiPopoverAnchorOrigin(anchorOriginValue);
setActiveID(id);
});
});
};
/**
* Hide the emoji picker menu.
*/
const hideEmojiPicker = (isNavigating?: boolean) => {
const currOnModalHide = onModalHide.current;
onModalHide.current = () => {
if (currOnModalHide) {
currOnModalHide(!!isNavigating);
}
// eslint-disable-next-line react-compiler/react-compiler
emojiPopoverAnchorRef.current = null;
};
setIsEmojiPickerVisible(false);
};
/**
* Focus the search input in the emoji picker.
*/
const focusEmojiSearchInput = () => {
if (!emojiSearchInput.current) {
return;
}
emojiSearchInput.current.focus();
};
/**
* Callback for the emoji picker to add whatever emoji is chosen into the main input
*/
const selectEmoji = (emoji: string, emojiObject: Emoji) => {
// Prevent fast click / multiple emoji selection;
// The first click will hide the emoji picker by calling the hideEmojiPicker() function
if (!isEmojiPickerVisible) {
return;
}
hideEmojiPicker(false);
if (typeof onEmojiSelected.current === 'function') {
onEmojiSelected.current(emoji, emojiObject);
}
};
/**
* Whether emoji picker is active for the given id.
*/
const isActive = (id: string) => !!id && id === activeID;
const clearActive = () => setActiveID(null);
const resetEmojiPopoverAnchor = () => (emojiPopoverAnchorRef.current = null);
useImperativeHandle(ref, () => ({showEmojiPicker, isActive, clearActive, hideEmojiPicker, isEmojiPickerVisible, resetEmojiPopoverAnchor}));
useEffect(() => {
const emojiPopoverDimensionListener = Dimensions.addEventListener('change', () => {
const emojiPopoverAnchor = getEmojiPopoverAnchor();
if (!emojiPopoverAnchor?.current) {
// In small screen width, the window size change might be due to keyboard open/hide, we should avoid hide EmojiPicker in those cases
if (isEmojiPickerVisible && !shouldUseNarrowLayout) {
hideEmojiPicker();
}
return;
}
calculateAnchorPosition(emojiPopoverAnchor?.current, emojiPopoverAnchorOrigin).then((value) => {
setEmojiPopoverAnchorPosition({
horizontal: value.horizontal,
vertical: value.vertical,
});
emojiAnchorDimension.current = {
width: value.width,
height: value.height,
};
});
});
return () => {
if (!emojiPopoverDimensionListener) {
return;
}
emojiPopoverDimensionListener.remove();
};
}, [isEmojiPickerVisible, shouldUseNarrowLayout, emojiPopoverAnchorOrigin, getEmojiPopoverAnchor]);
// 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
shouldHandleNavigationBack={Browser.isMobileChrome()}
isVisible={isEmojiPickerVisible}
onClose={hideEmojiPicker}
onModalShow={focusEmojiSearchInput}
onModalHide={onModalHide.current}
hideModalContentWhileAnimating
shouldSetModalVisibility={false}
animationInTiming={1}
animationOutTiming={1}
anchorPosition={{
vertical: emojiPopoverAnchorPosition.vertical,
horizontal: emojiPopoverAnchorPosition.horizontal,
}}
anchorRef={getEmojiPopoverAnchor() as RefObject<View | HTMLDivElement>}
withoutOverlay
popoverDimensions={{
width: CONST.EMOJI_PICKER_SIZE.WIDTH,
height: CONST.EMOJI_PICKER_SIZE.HEIGHT,
}}
anchorAlignment={emojiPopoverAnchorOrigin}
outerStyle={StyleUtils.getOuterModalStyle(windowHeight, viewportOffsetTop)}
innerContainerStyle={styles.popoverInnerContainer}
anchorDimensions={emojiAnchorDimension.current}
avoidKeyboard
shoudSwitchPositionIfOverflow
shouldEnableNewFocusManagement
restoreFocusType={CONST.MODAL.RESTORE_FOCUS_TYPE.DELETE}
>
<FocusTrapForModal active={isEmojiPickerVisible}>
<View>
<EmojiPickerMenu
onEmojiSelected={selectEmoji}
activeEmoji={activeEmoji.current}
ref={(el) => (emojiSearchInput.current = el)}
/>
</View>
</FocusTrapForModal>
</PopoverWithMeasuredContent>
);
}
EmojiPicker.displayName = 'EmojiPicker';
export default withViewportOffsetTop(forwardRef(EmojiPicker));