-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
focusWithDelay.ts
35 lines (32 loc) · 1.06 KB
/
focusWithDelay.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import {InteractionManager, TextInput} from 'react-native';
import ComposerFocusManager from './ComposerFocusManager';
type FocusWithDelay = (shouldDelay?: boolean) => void;
/**
* Create a function that focuses a text input.
*/
function focusWithDelay(textInput: TextInput | null): FocusWithDelay {
/**
* Focus the text input
* @param [shouldDelay] Impose delay before focusing the text input
*/
return (shouldDelay = false) => {
// There could be other animations running while we trigger manual focus.
// This prevents focus from making those animations janky.
InteractionManager.runAfterInteractions(() => {
if (!textInput) {
return;
}
if (!shouldDelay) {
textInput.focus();
return;
}
ComposerFocusManager.isReadyToFocus().then(() => {
if (!textInput) {
return;
}
textInput.focus();
});
});
};
}
export default focusWithDelay;