The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { 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. |
| 7 | const subscribers = new Set<() => void>(); |
| 8 | let showEpoch = 1; |
| 9 | let keyboardShowing = KeyboardController.isVisible(); |
| 10 | let hiddenSinceShow = !keyboardShowing; |
| 11 | |
| 12 | const notify = () => { |
| 13 | for (const subscriber of subscribers) subscriber(); |
| 14 | }; |
| 15 | const show = () => { |
| 16 | if (hiddenSinceShow) showEpoch += 1; |
| 17 | hiddenSinceShow = false; |
| 18 | keyboardShowing = true; |
| 19 | notify(); |
| 20 | }; |
| 21 | KeyboardEvents.addListener("keyboardWillShow", show); |
| 22 | KeyboardEvents.addListener("keyboardDidShow", show); |
| 23 | KeyboardEvents.addListener("keyboardWillHide", () => { |
| 24 | keyboardShowing = false; |
| 25 | hiddenSinceShow = true; |
| 26 | notify(); |
| 27 | }); |
| 28 | KeyboardEvents.addListener("keyboardDidHide", () => { |
| 29 | hiddenSinceShow = true; |
| 30 | }); |
| 31 | |
| 32 | export 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. |
| 40 | export const getKeyboardShowEpoch = () => (keyboardShowing ? showEpoch : 0); |