Skip to content

Commit

Permalink
adjusted solution to fix additional issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ikevin127 committed Jan 7, 2025
1 parent f124c97 commit 8fce2ab
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/hooks/useResetComposerFocus.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import {useIsFocused} from '@react-navigation/native';
import type {MutableRefObject} from 'react';
import {useEffect, useRef} from 'react';
import {InteractionManager} from 'react-native';
import type {TextInput} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import ONYXKEYS from '@src/ONYXKEYS';
import usePrevious from './usePrevious';

export default function useResetComposerFocus(inputRef: MutableRefObject<TextInput | null>) {
const isFocused = useIsFocused();
const shouldResetFocusRef = useRef(false);
const [modal] = useOnyx(ONYXKEYS.MODAL);
const prevIsModalVisible = usePrevious(modal?.isVisible);

useEffect(() => {
if (!isFocused || !shouldResetFocusRef.current || prevIsModalVisible) {
if (!isFocused || !shouldResetFocusRef.current) {
return;
}
inputRef.current?.focus(); // focus input again
shouldResetFocusRef.current = false;
}, [isFocused, inputRef, prevIsModalVisible]);
const interactionTask = InteractionManager.runAfterInteractions(() => {
inputRef.current?.focus(); // focus input again
shouldResetFocusRef.current = false;
});
return () => {
interactionTask.cancel();
};
}, [isFocused, inputRef]);

return {isFocused, shouldResetFocusRef};
}

0 comments on commit 8fce2ab

Please sign in to comment.