@react-native-ohos/react-native-keyboard-controller 1.12.8-rc.7 → 1.12.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/LICENSE +21 -21
  2. package/README.OpenSource +10 -10
  3. package/README.md +603 -19
  4. package/harmony/keyboard_controller/BuildProfile.ets +16 -16
  5. package/harmony/keyboard_controller/Index.ets +8 -8
  6. package/harmony/keyboard_controller/ResourceTable.txt +8 -8
  7. package/harmony/keyboard_controller/build-profile.json5 +31 -31
  8. package/harmony/keyboard_controller/hvigorfile.ts +6 -6
  9. package/harmony/keyboard_controller/obfuscation-rules.txt +17 -17
  10. package/harmony/keyboard_controller/oh-package-lock.json5 +29 -0
  11. package/harmony/keyboard_controller/oh-package.json5 +11 -11
  12. package/harmony/keyboard_controller/src/main/cpp/CMakeLists.txt +7 -7
  13. package/harmony/keyboard_controller/src/main/cpp/EventEmitters.cpp +119 -96
  14. package/harmony/keyboard_controller/src/main/cpp/EventEmitters.h +90 -90
  15. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewComponentDescriptor.h +77 -77
  16. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewComponentInstance.cpp +816 -158
  17. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewComponentInstance.h +147 -74
  18. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewJSIBinder.h +43 -43
  19. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaComponentDescriptor.h +64 -64
  20. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaComponentInstance.cpp +88 -88
  21. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaComponentInstance.h +63 -63
  22. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaJSIBinder.h +39 -39
  23. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardController.cpp +25 -25
  24. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardController.h +27 -27
  25. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardControllerPackage.cpp +43 -43
  26. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardControllerPackage.h +56 -56
  27. package/harmony/keyboard_controller/src/main/cpp/RNStatusBarManagerCompat.cpp +24 -24
  28. package/harmony/keyboard_controller/src/main/cpp/RNStatusBarManagerCompat.h +28 -28
  29. package/harmony/keyboard_controller/src/main/cpp/ViewHierarchyNavigator.cpp +186 -0
  30. package/harmony/keyboard_controller/src/main/cpp/ViewHierarchyNavigator.h +112 -0
  31. package/harmony/keyboard_controller/src/main/cpp/keyboardControllerPackage.cpp +43 -43
  32. package/harmony/keyboard_controller/src/main/cpp/keyboardControllerPackage.h +56 -56
  33. package/harmony/keyboard_controller/src/main/ets/Logger.ts +45 -45
  34. package/harmony/keyboard_controller/src/main/ets/RNKeyboardControllerPackage.ets +49 -49
  35. package/harmony/keyboard_controller/src/main/ets/RNKeyboardControllerTurboModule.ts +405 -210
  36. package/harmony/keyboard_controller/src/main/ets/RNStatusBarManagerCompatTurboModule.ts +107 -98
  37. package/harmony/keyboard_controller/src/main/ets/Type.ts +41 -41
  38. package/harmony/keyboard_controller/src/main/module.json +29 -29
  39. package/harmony/keyboard_controller/src/main/module.json5 +11 -11
  40. package/harmony/keyboard_controller/src/main/resources/base/element/string.json +8 -8
  41. package/harmony/keyboard_controller/src/main/resources/en_US/element/string.json +8 -8
  42. package/harmony/keyboard_controller/src/main/resources/zh_CN/element/string.json +8 -8
  43. package/harmony/keyboard_controller/ts.ets +8 -8
  44. package/harmony/keyboard_controller.har +0 -0
  45. package/lib/commonjs/animated.js +4 -16
  46. package/lib/commonjs/animated.js.map +1 -1
  47. package/lib/commonjs/bindings.js +3 -3
  48. package/lib/commonjs/bindings.js.map +1 -1
  49. package/lib/commonjs/bindings.native.js +3 -3
  50. package/lib/commonjs/bindings.native.js.map +1 -1
  51. package/lib/commonjs/components/KeyboardAvoidingView/hooks.js.map +1 -1
  52. package/lib/commonjs/components/KeyboardAvoidingView/index.js +3 -3
  53. package/lib/commonjs/components/KeyboardAvoidingView/index.js.map +1 -1
  54. package/lib/commonjs/components/KeyboardAwareScrollView/index.js +43 -41
  55. package/lib/commonjs/components/KeyboardAwareScrollView/index.js.map +1 -1
  56. package/lib/commonjs/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js +3 -3
  57. package/lib/commonjs/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js.map +1 -1
  58. package/lib/commonjs/components/KeyboardAwareScrollView/utils.js.map +1 -1
  59. package/lib/commonjs/components/KeyboardStickyView/index.js.map +1 -1
  60. package/lib/commonjs/components/KeyboardToolbar/Arrow.js.map +1 -1
  61. package/lib/commonjs/components/KeyboardToolbar/Button.js.map +1 -1
  62. package/lib/commonjs/components/KeyboardToolbar/colors.js.map +1 -1
  63. package/lib/commonjs/components/KeyboardToolbar/colors.native.js.map +1 -1
  64. package/lib/commonjs/components/KeyboardToolbar/index.js +3 -3
  65. package/lib/commonjs/components/KeyboardToolbar/index.js.map +1 -1
  66. package/lib/commonjs/components/KeyboardToolbar/types.js.map +1 -1
  67. package/lib/commonjs/components/hooks/useColorScheme.js.map +1 -1
  68. package/lib/commonjs/components/index.js.map +1 -1
  69. package/lib/commonjs/constants.js +2 -0
  70. package/lib/commonjs/constants.js.map +1 -1
  71. package/lib/commonjs/context.js.map +1 -1
  72. package/lib/commonjs/hooks/index.js.map +1 -1
  73. package/lib/commonjs/hooks/useWindowDimensions/index.android.js.map +1 -1
  74. package/lib/commonjs/hooks/useWindowDimensions/index.js.map +1 -1
  75. package/lib/commonjs/index.js.map +1 -1
  76. package/lib/commonjs/internal.js +18 -18
  77. package/lib/commonjs/internal.js.map +1 -1
  78. package/lib/commonjs/monkey-patch.android.js +4 -4
  79. package/lib/commonjs/monkey-patch.android.js.map +1 -1
  80. package/lib/commonjs/monkey-patch.js.map +1 -1
  81. package/lib/commonjs/reanimated.js.map +1 -1
  82. package/lib/commonjs/reanimated.native.js.map +1 -1
  83. package/lib/commonjs/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
  84. package/lib/commonjs/specs/KeyboardGestureAreaNativeComponent.js.map +1 -1
  85. package/lib/commonjs/specs/NativeKeyboardController.js.map +1 -1
  86. package/lib/commonjs/specs/NativeStatusBarManagerCompat.js.map +1 -1
  87. package/lib/commonjs/types.js.map +1 -1
  88. package/lib/commonjs/utils.js.map +1 -1
  89. package/lib/module/animated.js +5 -17
  90. package/lib/module/animated.js.map +1 -1
  91. package/lib/module/bindings.js +3 -3
  92. package/lib/module/bindings.js.map +1 -1
  93. package/lib/module/bindings.native.js +3 -3
  94. package/lib/module/bindings.native.js.map +1 -1
  95. package/lib/module/components/KeyboardAvoidingView/hooks.js.map +1 -1
  96. package/lib/module/components/KeyboardAvoidingView/index.js +3 -3
  97. package/lib/module/components/KeyboardAvoidingView/index.js.map +1 -1
  98. package/lib/module/components/KeyboardAwareScrollView/index.js +44 -42
  99. package/lib/module/components/KeyboardAwareScrollView/index.js.map +1 -1
  100. package/lib/module/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js +3 -3
  101. package/lib/module/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js.map +1 -1
  102. package/lib/module/components/KeyboardAwareScrollView/utils.js.map +1 -1
  103. package/lib/module/components/KeyboardStickyView/index.js.map +1 -1
  104. package/lib/module/components/KeyboardToolbar/Arrow.js.map +1 -1
  105. package/lib/module/components/KeyboardToolbar/Button.js.map +1 -1
  106. package/lib/module/components/KeyboardToolbar/colors.js.map +1 -1
  107. package/lib/module/components/KeyboardToolbar/colors.native.js.map +1 -1
  108. package/lib/module/components/KeyboardToolbar/index.js +3 -3
  109. package/lib/module/components/KeyboardToolbar/index.js.map +1 -1
  110. package/lib/module/components/KeyboardToolbar/types.js.map +1 -1
  111. package/lib/module/components/hooks/useColorScheme.js.map +1 -1
  112. package/lib/module/components/index.js.map +1 -1
  113. package/lib/module/constants.js +2 -0
  114. package/lib/module/constants.js.map +1 -1
  115. package/lib/module/context.js.map +1 -1
  116. package/lib/module/hooks/index.js.map +1 -1
  117. package/lib/module/hooks/useWindowDimensions/index.android.js.map +1 -1
  118. package/lib/module/hooks/useWindowDimensions/index.js.map +1 -1
  119. package/lib/module/index.js.map +1 -1
  120. package/lib/module/internal.js +18 -18
  121. package/lib/module/internal.js.map +1 -1
  122. package/lib/module/monkey-patch.android.js +4 -4
  123. package/lib/module/monkey-patch.android.js.map +1 -1
  124. package/lib/module/monkey-patch.js.map +1 -1
  125. package/lib/module/reanimated.js.map +1 -1
  126. package/lib/module/reanimated.native.js.map +1 -1
  127. package/lib/module/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
  128. package/lib/module/specs/KeyboardGestureAreaNativeComponent.js.map +1 -1
  129. package/lib/module/specs/NativeKeyboardController.js.map +1 -1
  130. package/lib/module/specs/NativeStatusBarManagerCompat.js.map +1 -1
  131. package/lib/module/types.js.map +1 -1
  132. package/lib/module/utils.js.map +1 -1
  133. package/lib/typescript/types.d.ts +1 -0
  134. package/package.json +181 -177
  135. package/src/animated.tsx +246 -253
  136. package/src/bindings.native.ts +57 -57
  137. package/src/bindings.ts +38 -38
  138. package/src/components/KeyboardAvoidingView/hooks.ts +58 -58
  139. package/src/components/KeyboardAvoidingView/index.tsx +156 -156
  140. package/src/components/KeyboardAwareScrollView/index.tsx +381 -373
  141. package/src/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.ts +133 -133
  142. package/src/components/KeyboardAwareScrollView/utils.ts +41 -41
  143. package/src/components/KeyboardStickyView/index.tsx +57 -57
  144. package/src/components/KeyboardToolbar/Arrow.tsx +80 -80
  145. package/src/components/KeyboardToolbar/Button.tsx +98 -98
  146. package/src/components/KeyboardToolbar/colors.native.ts +37 -37
  147. package/src/components/KeyboardToolbar/colors.ts +16 -16
  148. package/src/components/KeyboardToolbar/index.tsx +211 -211
  149. package/src/components/KeyboardToolbar/types.ts +40 -40
  150. package/src/components/hooks/useColorScheme.ts +5 -5
  151. package/src/components/index.ts +11 -11
  152. package/src/constants.ts +19 -19
  153. package/src/context.ts +65 -65
  154. package/src/hooks/index.ts +90 -90
  155. package/src/hooks/useWindowDimensions/index.android.ts +33 -33
  156. package/src/hooks/useWindowDimensions/index.ts +1 -1
  157. package/src/index.ts +21 -21
  158. package/src/internal.ts +84 -84
  159. package/src/monkey-patch.android.ts +44 -44
  160. package/src/monkey-patch.ts +4 -4
  161. package/src/reanimated.native.ts +132 -132
  162. package/src/reanimated.ts +29 -29
  163. package/src/specs/KeyboardControllerViewNativeComponent.ts +70 -70
  164. package/src/specs/KeyboardGestureAreaNativeComponent.ts +15 -15
  165. package/src/specs/NativeKeyboardController.ts +19 -19
  166. package/src/specs/NativeStatusBarManagerCompat.ts +14 -14
  167. package/src/types.ts +212 -211
  168. package/src/utils.ts +1 -1
  169. package/react-native-keyboard-controller.podspec +0 -61
package/src/animated.tsx CHANGED
@@ -1,253 +1,246 @@
1
- import React, { useEffect, useMemo, useState } from "react";
2
- import { Animated, Platform, StyleSheet } from "react-native";
3
- import Reanimated, { useSharedValue, Easing, withTiming } from "react-native-reanimated";
4
-
5
- import { KeyboardControllerView } from "./bindings";
6
- import { KeyboardContext } from "./context";
7
- import { useAnimatedValue, useSharedHandlers } from "./internal";
8
- import { applyMonkeyPatch, revertMonkeyPatch } from "./monkey-patch";
9
- import {
10
- useAnimatedKeyboardHandler,
11
- useFocusedInputLayoutHandler,
12
- useFocusedInputSelectionHandler,
13
- useFocusedInputTextHandler,
14
- } from "./reanimated";
15
-
16
- import type { KeyboardAnimationContext } from "./context";
17
- import type {
18
- FocusedInputHandler,
19
- FocusedInputLayoutChangedEvent,
20
- KeyboardControllerProps,
21
- KeyboardHandler,
22
- NativeEvent,
23
- } from "./types";
24
- import type { ViewStyle } from "react-native";
25
-
26
- const KeyboardControllerViewAnimated = Reanimated.createAnimatedComponent(
27
- Animated.createAnimatedComponent(
28
- KeyboardControllerView,
29
- ) as React.FC<KeyboardControllerProps>,
30
- );
31
-
32
- type Styles = {
33
- container: ViewStyle;
34
- hidden: ViewStyle;
35
- };
36
-
37
- const styles = StyleSheet.create<Styles>({
38
- container: {
39
- flex: 1,
40
- },
41
- hidden: {
42
- display: "none",
43
- position: "absolute",
44
- },
45
- });
46
-
47
- type KeyboardProviderProps = {
48
- children: React.ReactNode;
49
- /**
50
- * Set the value to `true`, if you use translucent status bar on Android.
51
- * If you already control status bar translucency via `react-native-screens`
52
- * or `StatusBar` component from `react-native`, you can ignore it.
53
- * Defaults to `false`.
54
- *
55
- * @see https://github.com/kirillzyusko/react-native-keyboard-controller/issues/14
56
- * @platform android
57
- */
58
- statusBarTranslucent?: boolean;
59
- /**
60
- * Set the value to `true`, if you use translucent navigation bar on Android.
61
- * Defaults to `false`.
62
- *
63
- * @see https://github.com/kirillzyusko/react-native-keyboard-controller/issues/119
64
- * @platform android
65
- */
66
- navigationBarTranslucent?: boolean;
67
- /**
68
- * A boolean prop indicating whether the module is enabled. It indicate only initial state,
69
- * i. e. if you try to change this prop after component mount it will not have any effect.
70
- * To change the property in runtime use `useKeyboardController` hook and `setEnabled` method.
71
- * Defaults to `true`.
72
- */
73
- enabled?: boolean;
74
- };
75
-
76
- // capture `Platform.OS` in separate variable to avoid deep workletization of entire RN package
77
- // see https://github.com/kirillzyusko/react-native-keyboard-controller/issues/393 and https://github.com/kirillzyusko/react-native-keyboard-controller/issues/294 for more details
78
- const OS = Platform.OS;
79
-
80
- export const KeyboardProvider = ({
81
- children,
82
- statusBarTranslucent,
83
- navigationBarTranslucent,
84
- enabled: initiallyEnabled = true,
85
- }: KeyboardProviderProps) => {
86
- // state
87
- const [enabled, setEnabled] = useState(initiallyEnabled);
88
- // animated values
89
- const progress = useAnimatedValue(0);
90
- const height = useAnimatedValue(0);
91
- // shared values
92
- const progressSV = useSharedValue(0);
93
- const heightSV = useSharedValue(0);
94
- const layout = useSharedValue<FocusedInputLayoutChangedEvent | null>(null);
95
- const [setKeyboardHandlers, broadcastKeyboardEvents] =
96
- useSharedHandlers<KeyboardHandler>();
97
- const [setInputHandlers, broadcastInputEvents] =
98
- useSharedHandlers<FocusedInputHandler>();
99
- // memo
100
- const context = useMemo<KeyboardAnimationContext>(
101
- () => ({
102
- enabled,
103
- animated: { progress: progress, height: Animated.multiply(height, -1) },
104
- reanimated: { progress: progressSV, height: heightSV },
105
- layout,
106
- setKeyboardHandlers,
107
- setInputHandlers,
108
- setEnabled,
109
- }),
110
- [enabled],
111
- );
112
- const style = useMemo(
113
- () => [
114
- styles.hidden,
115
- { transform: [{ translateX: height }, { translateY: progress }] },
116
- ],
117
- [],
118
- );
119
- const onKeyboardMove = useMemo(
120
- () =>
121
- Animated.event(
122
- [
123
- {
124
- nativeEvent: {
125
- progress,
126
- height,
127
- },
128
- },
129
- ],
130
- { useNativeDriver: Platform.OS as string == 'harmony' ? false : true },
131
- ),
132
- [],
133
- );
134
- // handlers
135
- const updateSharedValues = (event: NativeEvent, platforms: string[]) => {
136
- "worklet";
137
-
138
- if (platforms.includes(OS)) {
139
- // eslint-disable-next-line react-compiler/react-compiler
140
- if (Platform.OS as string == 'harmony') {
141
- const dur = 460
142
- progressSV.value = withTiming(event.progress, { duration: dur, easing: Easing.out(Easing.exp) });
143
- heightSV.value = withTiming(-event.height, { duration: dur, easing: Easing.out(Easing.exp) });
144
- } else {
145
- progressSV.value = event.progress;
146
- heightSV.value = -event.height;
147
-
148
- }
149
- }
150
- };
151
- const keyboardHandler = useAnimatedKeyboardHandler(
152
- {
153
- onKeyboardMoveStart: (event: NativeEvent) => {
154
- "worklet";
155
-
156
- broadcastKeyboardEvents("onStart", event);
157
- updateSharedValues(event, ["ios"]);
158
- },
159
- onKeyboardMove: (event: NativeEvent) => {
160
- "worklet";
161
-
162
- broadcastKeyboardEvents("onMove", event);
163
- updateSharedValues(event, ["android"]);
164
- },
165
- onKeyboardMoveEnd: (event: NativeEvent) => {
166
- "worklet";
167
- broadcastKeyboardEvents("onEnd", event);
168
- updateSharedValues(event, ['harmony']);
169
- },
170
- onKeyboardMoveInteractive: (event: NativeEvent) => {
171
- "worklet";
172
-
173
- updateSharedValues(event, ["android", "ios"]);
174
- broadcastKeyboardEvents("onInteractive", event);
175
- },
176
- },
177
- [],
178
- );
179
- const inputLayoutHandler = useFocusedInputLayoutHandler(
180
- {
181
- onFocusedInputLayoutChanged: (e) => {
182
- "worklet";
183
-
184
- if (e.target !== -1) {
185
- layout.value = e;
186
- } else {
187
- layout.value = null;
188
- }
189
- },
190
- },
191
- [],
192
- );
193
- const inputTextHandler = useFocusedInputTextHandler(
194
- {
195
- onFocusedInputTextChanged: (e) => {
196
- "worklet";
197
-
198
- broadcastInputEvents("onChangeText", e);
199
- },
200
- },
201
- [],
202
- );
203
- const inputSelectionHandler = useFocusedInputSelectionHandler(
204
- {
205
- onFocusedInputSelectionChanged: (e) => {
206
- "worklet";
207
- broadcastInputEvents("onSelectionChange", e);
208
- },
209
- },
210
- [],
211
- );
212
- // effects
213
- useEffect(() => {
214
- if (enabled) {
215
- applyMonkeyPatch();
216
- } else {
217
- revertMonkeyPatch();
218
- }
219
- }, [enabled]);
220
-
221
- return (
222
- <KeyboardContext.Provider value={context}>
223
- <KeyboardControllerViewAnimated
224
- enabled={enabled}
225
- onKeyboardMoveReanimated={keyboardHandler}
226
- onKeyboardMoveStart={OS === "ios" ? onKeyboardMove : undefined}
227
- onKeyboardMove={OS === "android" ? onKeyboardMove : undefined}
228
- onKeyboardMoveInteractive={onKeyboardMove}
229
- onKeyboardMoveEnd={OS as string === "harmony" ? onKeyboardMove : undefined}
230
- onFocusedInputLayoutChangedReanimated={inputLayoutHandler}
231
- onFocusedInputTextChangedReanimated={inputTextHandler}
232
- onFocusedInputSelectionChangedReanimated={inputSelectionHandler}
233
- navigationBarTranslucent={navigationBarTranslucent}
234
- statusBarTranslucent={statusBarTranslucent}
235
- style={styles.container}
236
- >
237
- <Reanimated.View style={styles.container}>{children}</Reanimated.View>
238
- {/* {children} */}
239
- </KeyboardControllerViewAnimated>
240
- <Animated.View
241
- // we are using this small hack, because if the component (where
242
- // animated value has been used) is unmounted, then animation will
243
- // stop receiving events (seems like it's react-native optimization).
244
- // So we need to keep a reference to the animated value, to keep it's
245
- // always mounted (keep a reference to an animated value).
246
- //
247
- // To test why it's needed, try to open screen which consumes Animated.Value
248
- // then close it and open it again (for example 'Animated transition').
249
- style={style}
250
- />
251
- </KeyboardContext.Provider>
252
- );
253
- };
1
+ import React, { useEffect, useMemo, useState } from "react";
2
+ import { Animated, Platform, StyleSheet } from "react-native";
3
+ import Reanimated, { useSharedValue } from "react-native-reanimated";
4
+
5
+ import { KeyboardControllerView } from "./bindings";
6
+ import { KeyboardContext } from "./context";
7
+ import { useAnimatedValue, useSharedHandlers } from "./internal";
8
+ import { applyMonkeyPatch, revertMonkeyPatch } from "./monkey-patch";
9
+ import {
10
+ useAnimatedKeyboardHandler,
11
+ useFocusedInputLayoutHandler,
12
+ useFocusedInputSelectionHandler,
13
+ useFocusedInputTextHandler,
14
+ } from "./reanimated";
15
+
16
+ import type { KeyboardAnimationContext } from "./context";
17
+ import type {
18
+ FocusedInputHandler,
19
+ FocusedInputLayoutChangedEvent,
20
+ KeyboardControllerProps,
21
+ KeyboardHandler,
22
+ NativeEvent,
23
+ } from "./types";
24
+ import type { ViewStyle } from "react-native";
25
+
26
+ const KeyboardControllerViewAnimated = Reanimated.createAnimatedComponent(
27
+ Animated.createAnimatedComponent(
28
+ KeyboardControllerView,
29
+ ) as React.FC<KeyboardControllerProps>,
30
+ );
31
+
32
+ type Styles = {
33
+ container: ViewStyle;
34
+ hidden: ViewStyle;
35
+ };
36
+
37
+ const styles = StyleSheet.create<Styles>({
38
+ container: {
39
+ flex: 1,
40
+ },
41
+ hidden: {
42
+ display: "none",
43
+ position: "absolute",
44
+ },
45
+ });
46
+
47
+ type KeyboardProviderProps = {
48
+ children: React.ReactNode;
49
+ /**
50
+ * Set the value to `true`, if you use translucent status bar on Android.
51
+ * If you already control status bar translucency via `react-native-screens`
52
+ * or `StatusBar` component from `react-native`, you can ignore it.
53
+ * Defaults to `false`.
54
+ *
55
+ * @see https://github.com/kirillzyusko/react-native-keyboard-controller/issues/14
56
+ * @platform android
57
+ */
58
+ statusBarTranslucent?: boolean;
59
+ /**
60
+ * Set the value to `true`, if you use translucent navigation bar on Android.
61
+ * Defaults to `false`.
62
+ *
63
+ * @see https://github.com/kirillzyusko/react-native-keyboard-controller/issues/119
64
+ * @platform android
65
+ */
66
+ navigationBarTranslucent?: boolean;
67
+ /**
68
+ * A boolean prop indicating whether the module is enabled. It indicate only initial state,
69
+ * i. e. if you try to change this prop after component mount it will not have any effect.
70
+ * To change the property in runtime use `useKeyboardController` hook and `setEnabled` method.
71
+ * Defaults to `true`.
72
+ */
73
+ enabled?: boolean;
74
+ };
75
+
76
+ // capture `Platform.OS` in separate variable to avoid deep workletization of entire RN package
77
+ // see https://github.com/kirillzyusko/react-native-keyboard-controller/issues/393 and https://github.com/kirillzyusko/react-native-keyboard-controller/issues/294 for more details
78
+ const OS = Platform.OS;
79
+
80
+ export const KeyboardProvider = ({
81
+ children,
82
+ statusBarTranslucent,
83
+ navigationBarTranslucent,
84
+ enabled: initiallyEnabled = true,
85
+ }: KeyboardProviderProps) => {
86
+ // state
87
+ const [enabled, setEnabled] = useState(initiallyEnabled);
88
+ // animated values
89
+ const progress = useAnimatedValue(0);
90
+ const height = useAnimatedValue(0);
91
+ // shared values
92
+ const progressSV = useSharedValue(0);
93
+ const heightSV = useSharedValue(0);
94
+ const layout = useSharedValue<FocusedInputLayoutChangedEvent | null>(null);
95
+ const [setKeyboardHandlers, broadcastKeyboardEvents] =
96
+ useSharedHandlers<KeyboardHandler>();
97
+ const [setInputHandlers, broadcastInputEvents] =
98
+ useSharedHandlers<FocusedInputHandler>();
99
+ // memo
100
+ const context = useMemo<KeyboardAnimationContext>(
101
+ () => ({
102
+ enabled,
103
+ animated: { progress: progress, height: Animated.multiply(height, -1) },
104
+ reanimated: { progress: progressSV, height: heightSV },
105
+ layout,
106
+ setKeyboardHandlers,
107
+ setInputHandlers,
108
+ setEnabled,
109
+ }),
110
+ [enabled],
111
+ );
112
+ const style = useMemo(
113
+ () => [
114
+ styles.hidden,
115
+ { transform: [{ translateX: height }, { translateY: progress }] },
116
+ ],
117
+ [],
118
+ );
119
+ const onKeyboardMove = useMemo(
120
+ () =>
121
+ Animated.event(
122
+ [
123
+ {
124
+ nativeEvent: {
125
+ progress,
126
+ height,
127
+ },
128
+ },
129
+ ],
130
+ { useNativeDriver: Platform.OS as string == 'harmony' ? false : true },
131
+ ),
132
+ [],
133
+ );
134
+ // handlers
135
+ const updateSharedValues = (event: NativeEvent, platforms: string[]) => {
136
+ "worklet";
137
+
138
+ if (platforms.includes(OS)) {
139
+ // eslint-disable-next-line react-compiler/react-compiler
140
+ progressSV.value = event.progress;
141
+ heightSV.value = -event.height;
142
+ }
143
+ };
144
+ const keyboardHandler = useAnimatedKeyboardHandler(
145
+ {
146
+ onKeyboardMoveStart: (event: NativeEvent) => {
147
+ "worklet";
148
+
149
+ broadcastKeyboardEvents("onStart", event);
150
+ updateSharedValues(event, ["ios"]);
151
+ },
152
+ onKeyboardMove: (event: NativeEvent) => {
153
+ "worklet";
154
+
155
+ broadcastKeyboardEvents("onMove", event);
156
+ updateSharedValues(event, ["android", "harmony"]);
157
+ },
158
+ onKeyboardMoveEnd: (event: NativeEvent) => {
159
+ "worklet";
160
+ broadcastKeyboardEvents("onEnd", event);
161
+ updateSharedValues(event, ["android", "harmony"]);
162
+ },
163
+ onKeyboardMoveInteractive: (event: NativeEvent) => {
164
+ "worklet";
165
+
166
+ updateSharedValues(event, ["android", "ios"]);
167
+ broadcastKeyboardEvents("onInteractive", event);
168
+ },
169
+ },
170
+ [],
171
+ );
172
+ const inputLayoutHandler = useFocusedInputLayoutHandler(
173
+ {
174
+ onFocusedInputLayoutChanged: (e) => {
175
+ "worklet";
176
+
177
+ if (e.target !== -1) {
178
+ layout.value = e;
179
+ } else {
180
+ layout.value = null;
181
+ }
182
+ },
183
+ },
184
+ [],
185
+ );
186
+ const inputTextHandler = useFocusedInputTextHandler(
187
+ {
188
+ onFocusedInputTextChanged: (e) => {
189
+ "worklet";
190
+
191
+ broadcastInputEvents("onChangeText", e);
192
+ },
193
+ },
194
+ [],
195
+ );
196
+ const inputSelectionHandler = useFocusedInputSelectionHandler(
197
+ {
198
+ onFocusedInputSelectionChanged: (e) => {
199
+ "worklet";
200
+ broadcastInputEvents("onSelectionChange", e);
201
+ },
202
+ },
203
+ [],
204
+ );
205
+ // effects
206
+ useEffect(() => {
207
+ if (enabled) {
208
+ applyMonkeyPatch();
209
+ } else {
210
+ revertMonkeyPatch();
211
+ }
212
+ }, [enabled]);
213
+
214
+ return (
215
+ <KeyboardContext.Provider value={context}>
216
+ <KeyboardControllerViewAnimated
217
+ enabled={enabled}
218
+ onKeyboardMoveReanimated={keyboardHandler}
219
+ onKeyboardMoveStart={OS === "ios" ? onKeyboardMove : undefined}
220
+ onKeyboardMove={OS === "android" ? onKeyboardMove : undefined}
221
+ onKeyboardMoveInteractive={onKeyboardMove}
222
+ onKeyboardMoveEnd={OS as string === "harmony" ? onKeyboardMove : undefined}
223
+ onFocusedInputLayoutChangedReanimated={inputLayoutHandler}
224
+ onFocusedInputTextChangedReanimated={inputTextHandler}
225
+ onFocusedInputSelectionChangedReanimated={inputSelectionHandler}
226
+ navigationBarTranslucent={navigationBarTranslucent}
227
+ statusBarTranslucent={statusBarTranslucent}
228
+ style={styles.container}
229
+ >
230
+ <Reanimated.View style={styles.container}>{children}</Reanimated.View>
231
+ {/* {children} */}
232
+ </KeyboardControllerViewAnimated>
233
+ <Animated.View
234
+ // we are using this small hack, because if the component (where
235
+ // animated value has been used) is unmounted, then animation will
236
+ // stop receiving events (seems like it's react-native optimization).
237
+ // So we need to keep a reference to the animated value, to keep it's
238
+ // always mounted (keep a reference to an animated value).
239
+ //
240
+ // To test why it's needed, try to open screen which consumes Animated.Value
241
+ // then close it and open it again (for example 'Animated transition').
242
+ style={style}
243
+ />
244
+ </KeyboardContext.Provider>
245
+ );
246
+ };
@@ -1,57 +1,57 @@
1
- import { NativeEventEmitter, Platform } from "react-native";
2
-
3
- import type {
4
- FocusedInputEventsModule,
5
- KeyboardControllerModule,
6
- KeyboardControllerProps,
7
- KeyboardEventsModule,
8
- KeyboardGestureAreaProps,
9
- WindowDimensionsEventsModule,
10
- } from "./types";
11
-
12
- const LINKING_ERROR =
13
- `The package 'react-native-keyboard-controller' doesn't seem to be linked. Make sure: \n\n` +
14
- Platform.select({ ios: "- You have run 'pod install'\n", default: "" }) +
15
- "- You rebuilt the app after installing the package\n" +
16
- "- You are not using Expo Go\n";
17
-
18
- const RCTKeyboardController =
19
- require("./specs/NativeKeyboardController").default;
20
- export const KeyboardController = (
21
- RCTKeyboardController
22
- ? RCTKeyboardController
23
- : new Proxy(
24
- {},
25
- {
26
- get() {
27
- throw new Error(LINKING_ERROR);
28
- },
29
- },
30
- )
31
- ) as KeyboardControllerModule;
32
-
33
- const KEYBOARD_CONTROLLER_NAMESPACE = "KeyboardController::";
34
- const eventEmitter = new NativeEventEmitter(KeyboardController);
35
-
36
- export const KeyboardEvents: KeyboardEventsModule = {
37
- addListener: (name, cb) =>
38
- eventEmitter.addListener(KEYBOARD_CONTROLLER_NAMESPACE + name, cb),
39
- };
40
- /**
41
- * This API is not documented, it's for internal usage only (for now), and is a subject to potential breaking changes in future.
42
- * Use it with cautious.
43
- */
44
- export const FocusedInputEvents: FocusedInputEventsModule = {
45
- addListener: (name, cb) =>
46
- eventEmitter.addListener(KEYBOARD_CONTROLLER_NAMESPACE + name, cb),
47
- };
48
- export const WindowDimensionsEvents: WindowDimensionsEventsModule = {
49
- addListener: (name, cb) =>
50
- eventEmitter.addListener(KEYBOARD_CONTROLLER_NAMESPACE + name, cb),
51
- };
52
- export const KeyboardControllerView: React.FC<KeyboardControllerProps> =
53
- require("./specs/KeyboardControllerViewNativeComponent").default;
54
- export const KeyboardGestureArea: React.FC<KeyboardGestureAreaProps> =
55
- Platform.OS === "android" && Platform.Version >= 30
56
- ? require("./specs/KeyboardGestureAreaNativeComponent").default
57
- : ({ children }: KeyboardGestureAreaProps) => children;
1
+ import { NativeEventEmitter, Platform } from "react-native";
2
+
3
+ import type {
4
+ FocusedInputEventsModule,
5
+ KeyboardControllerModule,
6
+ KeyboardControllerProps,
7
+ KeyboardEventsModule,
8
+ KeyboardGestureAreaProps,
9
+ WindowDimensionsEventsModule,
10
+ } from "./types";
11
+
12
+ const LINKING_ERROR =
13
+ `The package 'react-native-keyboard-controller' doesn't seem to be linked. Make sure: \n\n` +
14
+ Platform.select({ ios: "- You have run 'pod install'\n", default: "" }) +
15
+ "- You rebuilt the app after installing the package\n" +
16
+ "- You are not using Expo Go\n";
17
+
18
+ const RCTKeyboardController =
19
+ require("./specs/NativeKeyboardController").default;
20
+ export const KeyboardController = (
21
+ RCTKeyboardController
22
+ ? RCTKeyboardController
23
+ : new Proxy(
24
+ {},
25
+ {
26
+ get() {
27
+ throw new Error(LINKING_ERROR);
28
+ },
29
+ },
30
+ )
31
+ ) as KeyboardControllerModule;
32
+
33
+ const KEYBOARD_CONTROLLER_NAMESPACE = "KeyboardController::";
34
+ const eventEmitter = new NativeEventEmitter(KeyboardController);
35
+
36
+ export const KeyboardEvents: KeyboardEventsModule = {
37
+ addListener: (name, cb) =>
38
+ eventEmitter.addListener(KEYBOARD_CONTROLLER_NAMESPACE + name, cb),
39
+ };
40
+ /**
41
+ * This API is not documented, it's for internal usage only (for now), and is a subject to potential breaking changes in future.
42
+ * Use it with cautious.
43
+ */
44
+ export const FocusedInputEvents: FocusedInputEventsModule = {
45
+ addListener: (name, cb) =>
46
+ eventEmitter.addListener(KEYBOARD_CONTROLLER_NAMESPACE + name, cb),
47
+ };
48
+ export const WindowDimensionsEvents: WindowDimensionsEventsModule = {
49
+ addListener: (name, cb) =>
50
+ eventEmitter.addListener(KEYBOARD_CONTROLLER_NAMESPACE + name, cb),
51
+ };
52
+ export const KeyboardControllerView: React.FC<KeyboardControllerProps> =
53
+ require("./specs/KeyboardControllerViewNativeComponent").default;
54
+ export const KeyboardGestureArea: React.FC<KeyboardGestureAreaProps> =
55
+ Platform.OS === "android" && Platform.Version >= 30
56
+ ? require("./specs/KeyboardGestureAreaNativeComponent").default
57
+ : ({ children }: KeyboardGestureAreaProps) => children;