Skip to content

Commit

Permalink
Merge pull request #53949 from margelo/fix/restore-focus-after-modal-…
Browse files Browse the repository at this point in the history
…shown

fix: restore focus after Modal gets hidden
  • Loading branch information
aldo-expensify authored Dec 12, 2024
2 parents b372ca2 + 2029249 commit a4b4933
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/hooks/useRestoreInputFocus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {useEffect, useRef} from 'react';
import {InteractionManager, Keyboard} from 'react-native';
import {KeyboardController} from 'react-native-keyboard-controller';

const useRestoreInputFocus = (isLostFocus: boolean) => {
const keyboardVisibleBeforeLoosingFocusRef = useRef(false);

useEffect(() => {
if (isLostFocus) {
keyboardVisibleBeforeLoosingFocusRef.current = Keyboard.isVisible();
}

if (!isLostFocus && keyboardVisibleBeforeLoosingFocusRef.current) {
InteractionManager.runAfterInteractions(() => {
KeyboardController.setFocusTo('current');
});
}
}, [isLostFocus]);
};

export default useRestoreInputFocus;
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import usePaginatedReportActions from '@hooks/usePaginatedReportActions';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useRestoreInputFocus from '@hooks/useRestoreInputFocus';
import useStyleUtils from '@hooks/useStyleUtils';
import * as PolicyUtils from '@libs/PolicyUtils';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
Expand Down Expand Up @@ -268,6 +269,7 @@ function BaseReportActionContextMenu({
},
{isActive: shouldEnableArrowNavigation && shouldEnableContextMenuEnterShortcut, shouldPreventDefault: false},
);
useRestoreInputFocus(isVisible);

const openOverflowMenu = (event: GestureResponderEvent | MouseEvent, anchorRef: MutableRefObject<View | null>) => {
showContextMenu(
Expand Down

0 comments on commit a4b4933

Please sign in to comment.