Skip to content

Commit

Permalink
Merge pull request #36303 from Krishna2323/krishna2323/issue/36059
Browse files Browse the repository at this point in the history
fix: The back and forward keys are not working properly for navigating back to a characters
  • Loading branch information
amyevans authored Feb 12, 2024
2 parents cb656a0 + 7a65931 commit 84a6cda
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/components/EmojiPicker/EmojiPickerMenu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ function EmojiPickerMenu({forwardedRef, onEmojiSelected, activeEmoji}) {
initialFocusedIndex: -1,
disableCyclicTraversal: true,
onFocusedIndexChange,
disableHorizontalKeys: isFocused,
// We pass true without checking visibility of the component because if the popover is not visible this picker won't be mounted
isActive: true,
});

const filterEmojis = _.throttle((searchTerm) => {
Expand Down
15 changes: 13 additions & 2 deletions src/hooks/useArrowKeyFocusManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Config = {
isActive?: boolean;
itemsPerRow?: number;
disableCyclicTraversal?: boolean;
disableHorizontalKeys?: boolean;
};

type UseArrowKeyFocusManager = [number, (index: number) => void];
Expand All @@ -28,6 +29,7 @@ type UseArrowKeyFocusManager = [number, (index: number) => void];
* @param [config.isActive] – Whether the component is ready and should subscribe to KeyboardShortcut
* @param [config.itemsPerRow] – The number of items per row. If provided, the arrow keys will move focus horizontally as well as vertically
* @param [config.disableCyclicTraversal] – Whether to disable cyclic traversal of the list. If true, the arrow keys will have no effect when the first or last item is focused
* @param [config.disableHorizontalKeys] – Whether to disable the right/left keys
*/
export default function useArrowKeyFocusManager({
maxIndex,
Expand All @@ -41,6 +43,7 @@ export default function useArrowKeyFocusManager({
isActive,
itemsPerRow,
disableCyclicTraversal = false,
disableHorizontalKeys = false,
}: Config): UseArrowKeyFocusManager {
const allowHorizontalArrowKeys = !!itemsPerRow;
const [focusedIndex, setFocusedIndex] = useState(initialFocusedIndex);
Expand All @@ -52,6 +55,14 @@ export default function useArrowKeyFocusManager({
[isActive, shouldExcludeTextAreaNodes],
);

const horizontalArrowConfig = useMemo(
() => ({
excludedNodes: shouldExcludeTextAreaNodes ? ['TEXTAREA'] : [],
isActive: isActive && !disableHorizontalKeys,
}),
[isActive, shouldExcludeTextAreaNodes, disableHorizontalKeys],
);

// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => onFocusedIndexChange(focusedIndex), [focusedIndex]);

Expand Down Expand Up @@ -155,7 +166,7 @@ export default function useArrowKeyFocusManager({
});
}, [allowHorizontalArrowKeys, disableCyclicTraversal, disabledIndexes, maxIndex]);

useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ARROW_LEFT, arrowLeftCallback, arrowConfig);
useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ARROW_LEFT, arrowLeftCallback, horizontalArrowConfig);

const arrowRightCallback = useCallback(() => {
if (maxIndex < 0 || !allowHorizontalArrowKeys) {
Expand All @@ -182,7 +193,7 @@ export default function useArrowKeyFocusManager({
});
}, [allowHorizontalArrowKeys, disableCyclicTraversal, disabledIndexes, maxIndex]);

useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ARROW_RIGHT, arrowRightCallback, arrowConfig);
useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ARROW_RIGHT, arrowRightCallback, horizontalArrowConfig);

// Note: you don't need to manually manage focusedIndex in the parent. setFocusedIndex is only exposed in case you want to reset focusedIndex or focus a specific item
return [focusedIndex, setFocusedIndex];
Expand Down

0 comments on commit 84a6cda

Please sign in to comment.