diff --git a/src/libs/setShouldShowComposeInputKeyboardAware/index.android.ts b/src/libs/setShouldShowComposeInputKeyboardAware/index.android.ts new file mode 100644 index 000000000000..68c750b05a5f --- /dev/null +++ b/src/libs/setShouldShowComposeInputKeyboardAware/index.android.ts @@ -0,0 +1,3 @@ +import setShouldShowComposeInputKeyboardAwareBuilder from './setShouldShowComposeInputKeyboardAwareBuilder'; + +export default setShouldShowComposeInputKeyboardAwareBuilder('keyboardDidHide'); diff --git a/src/libs/setShouldShowComposeInputKeyboardAware/index.ios.ts b/src/libs/setShouldShowComposeInputKeyboardAware/index.ios.ts new file mode 100644 index 000000000000..cd50938c70b9 --- /dev/null +++ b/src/libs/setShouldShowComposeInputKeyboardAware/index.ios.ts @@ -0,0 +1,5 @@ +import setShouldShowComposeInputKeyboardAwareBuilder from './setShouldShowComposeInputKeyboardAwareBuilder'; + +// On iOS, there is a visible delay in displaying input after the keyboard has been closed with the `keyboardDidHide` event +// Because of that - on iOS we can use `keyboardWillHide` that is not available on android +export default setShouldShowComposeInputKeyboardAwareBuilder('keyboardWillHide'); diff --git a/src/libs/setShouldShowComposeInputKeyboardAware/index.native.ts b/src/libs/setShouldShowComposeInputKeyboardAware/index.native.ts deleted file mode 100644 index dbfa0c6977b3..000000000000 --- a/src/libs/setShouldShowComposeInputKeyboardAware/index.native.ts +++ /dev/null @@ -1,29 +0,0 @@ -import {EmitterSubscription, Keyboard} from 'react-native'; -import * as Composer from '@userActions/Composer'; -import SetShouldShowComposeInputKeyboardAware from './types'; - -let keyboardDidHideListener: EmitterSubscription | null = null; -const setShouldShowComposeInputKeyboardAware: SetShouldShowComposeInputKeyboardAware = (shouldShow) => { - if (keyboardDidHideListener) { - keyboardDidHideListener.remove(); - keyboardDidHideListener = null; - } - - if (!shouldShow) { - Composer.setShouldShowComposeInput(false); - return; - } - - // If keyboard is already hidden, we should show composer immediately because keyboardDidHide event won't be called - if (!Keyboard.isVisible()) { - Composer.setShouldShowComposeInput(true); - return; - } - - keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', () => { - Composer.setShouldShowComposeInput(true); - keyboardDidHideListener?.remove(); - }); -}; - -export default setShouldShowComposeInputKeyboardAware; diff --git a/src/libs/setShouldShowComposeInputKeyboardAware/setShouldShowComposeInputKeyboardAwareBuilder.ts b/src/libs/setShouldShowComposeInputKeyboardAware/setShouldShowComposeInputKeyboardAwareBuilder.ts new file mode 100644 index 000000000000..528b71c45ab8 --- /dev/null +++ b/src/libs/setShouldShowComposeInputKeyboardAware/setShouldShowComposeInputKeyboardAwareBuilder.ts @@ -0,0 +1,34 @@ +import {EmitterSubscription, Keyboard} from 'react-native'; +import {KeyboardEventName} from 'react-native/Libraries/Components/Keyboard/Keyboard'; +import * as Composer from '@userActions/Composer'; +import SetShouldShowComposeInputKeyboardAware from './types'; + +let keyboardEventListener: EmitterSubscription | null = null; +// On iOS, there is a visible delay in displaying input after the keyboard has been closed with the `keyboardDidHide` event +// Because of that - on iOS we can use `keyboardWillHide` that is not available on android + +const setShouldShowComposeInputKeyboardAwareBuilder: (keyboardEvent: KeyboardEventName) => SetShouldShowComposeInputKeyboardAware = + (keyboardEvent: KeyboardEventName) => (shouldShow: boolean) => { + if (keyboardEventListener) { + keyboardEventListener.remove(); + keyboardEventListener = null; + } + + if (!shouldShow) { + Composer.setShouldShowComposeInput(false); + return; + } + + // If keyboard is already hidden, we should show composer immediately because keyboardDidHide event won't be called + if (!Keyboard.isVisible()) { + Composer.setShouldShowComposeInput(true); + return; + } + + keyboardEventListener = Keyboard.addListener(keyboardEvent, () => { + Composer.setShouldShowComposeInput(true); + keyboardEventListener?.remove(); + }); + }; + +export default setShouldShowComposeInputKeyboardAwareBuilder;