Skip to content

Commit 8b23658

Browse files
authored
Merge pull request #22792 from hoangzinh/df/17603
Fix Emoji picker's position changes on decreasing the screen size
2 parents 4af18f6 + ba96ac2 commit 8b23658

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

src/components/EmojiPicker/EmojiPicker.js

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,6 @@ const EmojiPicker = forwardRef((props, ref) => {
3434
const onEmojiSelected = useRef(() => {});
3535
const emojiSearchInput = useRef();
3636

37-
useEffect(() => {
38-
if (isEmojiPickerVisible) {
39-
Keyboard.dismiss();
40-
}
41-
42-
const emojiPopoverDimensionListener = Dimensions.addEventListener('change', () => {
43-
calculateAnchorPosition(emojiPopoverAnchor.current).then((value) => {
44-
setEmojiPopoverAnchorPosition(value);
45-
});
46-
});
47-
return () => {
48-
emojiPopoverDimensionListener.remove();
49-
};
50-
}, [isEmojiPickerVisible]);
51-
5237
/**
5338
* Show the emoji picker menu.
5439
*
@@ -110,9 +95,7 @@ const EmojiPicker = forwardRef((props, ref) => {
11095
const selectEmoji = (emoji, emojiObject) => {
11196
// Prevent fast click / multiple emoji selection;
11297
// The first click will hide the emoji picker by calling the hideEmojiPicker() function
113-
// and in that function the emojiPopoverAnchor ref to will be set to null (synchronously)
114-
// thus we rely on that prop to prevent fast click / multiple emoji selection
115-
if (!emojiPopoverAnchor.current) {
98+
if (!isEmojiPickerVisible) {
11699
return;
117100
}
118101

@@ -130,7 +113,31 @@ const EmojiPicker = forwardRef((props, ref) => {
130113
*/
131114
const isActiveReportAction = (actionID) => Boolean(actionID) && reportAction.reportActionID === actionID;
132115

133-
useImperativeHandle(ref, () => ({showEmojiPicker, isActiveReportAction, hideEmojiPicker, isEmojiPickerVisible}));
116+
const resetEmojiPopoverAnchor = () => (emojiPopoverAnchor.current = null);
117+
118+
useImperativeHandle(ref, () => ({showEmojiPicker, isActiveReportAction, hideEmojiPicker, isEmojiPickerVisible, resetEmojiPopoverAnchor}));
119+
120+
useEffect(() => {
121+
if (isEmojiPickerVisible) {
122+
Keyboard.dismiss();
123+
}
124+
125+
const emojiPopoverDimensionListener = Dimensions.addEventListener('change', () => {
126+
if (!emojiPopoverAnchor.current) {
127+
// In small screen width, the window size change might be due to keyboard open/hide, we should avoid hide EmojiPicker in those cases
128+
if (isEmojiPickerVisible && !props.isSmallScreenWidth) {
129+
hideEmojiPicker();
130+
}
131+
return;
132+
}
133+
calculateAnchorPosition(emojiPopoverAnchor.current).then((value) => {
134+
setEmojiPopoverAnchorPosition(value);
135+
});
136+
});
137+
return () => {
138+
emojiPopoverDimensionListener.remove();
139+
};
140+
}, [isEmojiPickerVisible, props.isSmallScreenWidth]);
134141

135142
// There is no way to disable animations, and they are really laggy, because there are so many
136143
// emojis. The best alternative is to set it to 1ms so it just "pops" in and out

src/components/EmojiPicker/EmojiPickerButton.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, {useEffect} from 'react';
22
import PropTypes from 'prop-types';
33
import styles from '../../styles/styles';
44
import * as StyleUtils from '../../styles/StyleUtils';
@@ -35,6 +35,7 @@ const defaultProps = {
3535

3636
function EmojiPickerButton(props) {
3737
let emojiPopoverAnchor = null;
38+
useEffect(() => EmojiPickerAction.resetEmojiPopoverAnchor, []);
3839
return (
3940
<Tooltip text={props.translate('reportActionCompose.emoji')}>
4041
<PressableWithoutFeedback

src/components/Reactions/AddReactionBubble.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useRef} from 'react';
1+
import React, {useRef, useEffect} from 'react';
22
import {View} from 'react-native';
33
import PropTypes from 'prop-types';
44
import Tooltip from '../Tooltip';
@@ -55,6 +55,7 @@ const defaultProps = {
5555

5656
function AddReactionBubble(props) {
5757
const ref = useRef();
58+
useEffect(() => EmojiPickerAction.resetEmojiPopoverAnchor, []);
5859

5960
const onPress = () => {
6061
const openPicker = (refParam, anchorOrigin) => {

src/libs/actions/EmojiPickerAction.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,11 @@ function isEmojiPickerVisible() {
5252
return emojiPickerRef.current.isEmojiPickerVisible;
5353
}
5454

55-
export {emojiPickerRef, showEmojiPicker, hideEmojiPicker, isActiveReportAction, isEmojiPickerVisible};
55+
function resetEmojiPopoverAnchor() {
56+
if (!emojiPickerRef.current) {
57+
return;
58+
}
59+
return emojiPickerRef.current.resetEmojiPopoverAnchor();
60+
}
61+
62+
export {emojiPickerRef, showEmojiPicker, hideEmojiPicker, isActiveReportAction, isEmojiPickerVisible, resetEmojiPopoverAnchor};

0 commit comments

Comments
 (0)