Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: highlighting the first emoji while filtering #30243

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/components/EmojiPicker/EmojiPickerMenu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ function EmojiPickerMenu(props) {
const [selection, setSelection] = useState({start: 0, end: 0});
const [isFocused, setIsFocused] = useState(false);
const [isUsingKeyboardMovement, setIsUsingKeyboardMovement] = useState(false);
const [highlightFirstEmoji, setHighlightFirstEmoji] = useState(false);

useEffect(() => {
const emojisAndHeaderRowIndices = getEmojisAndHeaderRowIndices();
Expand Down Expand Up @@ -174,6 +175,7 @@ function EmojiPickerMenu(props) {
setHeaderIndices(headerRowIndices.current);
setHighlightedIndex(-1);
updateFirstNonHeaderIndex(emojis.current);
setHighlightFirstEmoji(false);
return;
}
const newFilteredEmojiList = EmojiUtils.suggestEmojis(`:${normalizedSearchTerm}`, preferredLocale, emojis.current.length);
Expand All @@ -183,6 +185,7 @@ function EmojiPickerMenu(props) {
setHeaderIndices([]);
setHighlightedIndex(0);
updateFirstNonHeaderIndex(newFilteredEmojiList);
setHighlightFirstEmoji(true);
}, throttleTime);

/**
Expand All @@ -209,6 +212,7 @@ function EmojiPickerMenu(props) {
searchInputRef.current.blur();
setArePointerEventsDisabled(true);
setIsUsingKeyboardMovement(true);
setHighlightFirstEmoji(false);

// We only want to hightlight the Emoji if none was highlighted already
// If we already have a highlighted Emoji, lets just skip the first navigation
Expand Down Expand Up @@ -434,11 +438,13 @@ function EmojiPickerMenu(props) {
const emojiCode = types && types[preferredSkinTone] ? types[preferredSkinTone] : code;

const isEmojiFocused = index === highlightedIndex && isUsingKeyboardMovement;
const shouldEmojiBeHighlighted = index === highlightedIndex && highlightFirstEmoji;

return (
<EmojiPickerMenuItem
onPress={(emoji) => onEmojiSelected(emoji, item)}
onHoverIn={() => {
setHighlightFirstEmoji(false);
if (!isUsingKeyboardMovement) {
return;
}
Expand All @@ -452,10 +458,11 @@ function EmojiPickerMenu(props) {
setHighlightedIndex((prevState) => (prevState === index ? -1 : prevState))
}
isFocused={isEmojiFocused}
isHighlighted={shouldEmojiBeHighlighted}
/>
);
},
[isUsingKeyboardMovement, highlightedIndex, onEmojiSelected, preferredSkinTone, translate],
[isUsingKeyboardMovement, highlightedIndex, onEmojiSelected, preferredSkinTone, translate, highlightFirstEmoji],
);

const isFiltered = emojis.current.length !== filteredEmojis.length;
Expand Down
12 changes: 10 additions & 2 deletions src/components/EmojiPicker/EmojiPickerMenuItem/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const propTypes = {

/** Whether this menu item is currently focused or not */
isFocused: PropTypes.bool,

/** Whether the menu item should be highlighted or not */
isHighlighted: PropTypes.bool,
};

class EmojiPickerMenuItem extends PureComponent {
Expand Down Expand Up @@ -56,6 +59,7 @@ class EmojiPickerMenuItem extends PureComponent {
if (!this.props.isFocused) {
return;
}

this.focusAndScroll();
}

Expand Down Expand Up @@ -91,7 +95,7 @@ class EmojiPickerMenuItem extends PureComponent {
ref={(ref) => (this.ref = ref)}
style={({pressed}) => [
this.props.isFocused ? styles.emojiItemKeyboardHighlighted : {},
this.state.isHovered ? styles.emojiItemHighlighted : {},
this.state.isHovered || this.props.isHighlighted ? styles.emojiItemHighlighted : {},
Browser.isMobile() && StyleUtils.getButtonBackgroundColorStyle(getButtonState(false, pressed)),
styles.emojiItem,
]}
Expand All @@ -107,6 +111,7 @@ class EmojiPickerMenuItem extends PureComponent {
EmojiPickerMenuItem.propTypes = propTypes;
EmojiPickerMenuItem.defaultProps = {
isFocused: false,
isHighlighted: false,
onHoverIn: () => {},
onHoverOut: () => {},
onFocus: () => {},
Expand All @@ -115,4 +120,7 @@ EmojiPickerMenuItem.defaultProps = {

// Significantly speeds up re-renders of the EmojiPickerMenu's FlatList
// by only re-rendering at most two EmojiPickerMenuItems that are highlighted/un-highlighted per user action.
export default React.memo(EmojiPickerMenuItem, (prevProps, nextProps) => prevProps.isFocused === nextProps.isFocused && prevProps.emoji === nextProps.emoji);
export default React.memo(
EmojiPickerMenuItem,
(prevProps, nextProps) => prevProps.isFocused === nextProps.isFocused && prevProps.isHighlighted === nextProps.isHighlighted && prevProps.emoji === nextProps.emoji,
);
Loading