Open History to see the last commit
1import { KeyboardController, KeyboardEvents } from "react-native-keyboard-controller";
2
3// The accessory leaves on willHide, before `useKeyboardState` sees didHide. Android
4// repairs a nonanimated IME change with didHide then didShow alone: that didHide must
5// not hide the accessory (it would clear the terminal's focus), but the didShow after
6// it starts a new show so a dismissed accessory comes back.
7const subscribers = new Set<() => void>();
8let showEpoch = 1;
9let keyboardShowing = KeyboardController.isVisible();
10let hiddenSinceShow = !keyboardShowing;
11
12const notify = () => {
13 for (const subscriber of subscribers) subscriber();
14};
15const show = () => {
16 if (hiddenSinceShow) showEpoch += 1;
17 hiddenSinceShow = false;
18 keyboardShowing = true;
19 notify();
20};
21KeyboardEvents.addListener("keyboardWillShow", show);
22KeyboardEvents.addListener("keyboardDidShow", show);
23KeyboardEvents.addListener("keyboardWillHide", () => {
24 keyboardShowing = false;
25 hiddenSinceShow = true;
26 notify();
27});
28KeyboardEvents.addListener("keyboardDidHide", () => {
29 hiddenSinceShow = true;
30});
31
32export function subscribeKeyboardShowing(onChange: () => void) {
33 subscribers.add(onChange);
34 return () => {
35 subscribers.delete(onChange);
36 };
37}
38
39// 0 while hidden; otherwise a number that changes with every new show.
40export const getKeyboardShowEpoch = () => (keyboardShowing ? showEpoch : 0);