@react-native-ohos/react-native-keyboard-controller 1.16.6-rc.1

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 (269) hide show
  1. package/LICENSE +21 -0
  2. package/README.OpenSource +11 -0
  3. package/README.md +19 -0
  4. package/harmony/keyboard_controller/BuildProfile.ets +17 -0
  5. package/harmony/keyboard_controller/Index.ets +7 -0
  6. package/harmony/keyboard_controller/ResourceTable.txt +1 -0
  7. package/harmony/keyboard_controller/build-profile.json5 +31 -0
  8. package/harmony/keyboard_controller/consumer-rules.txt +0 -0
  9. package/harmony/keyboard_controller/hvigorfile.ts +6 -0
  10. package/harmony/keyboard_controller/obfuscation-rules.txt +18 -0
  11. package/harmony/keyboard_controller/oh-package.json5 +11 -0
  12. package/harmony/keyboard_controller/src/main/cpp/CMakeLists.txt +7 -0
  13. package/harmony/keyboard_controller/src/main/cpp/EventEmitters.cpp +96 -0
  14. package/harmony/keyboard_controller/src/main/cpp/EventEmitters.h +90 -0
  15. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewComponentDescriptor.h +73 -0
  16. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewComponentInstance.cpp +172 -0
  17. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewComponentInstance.h +76 -0
  18. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewJSIBinder.h +44 -0
  19. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaComponentDescriptor.h +58 -0
  20. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaComponentInstance.cpp +87 -0
  21. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaComponentInstance.h +62 -0
  22. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaJSIBinder.h +39 -0
  23. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardController.cpp +25 -0
  24. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardController.h +27 -0
  25. package/harmony/keyboard_controller/src/main/cpp/RNStatusBarManagerCompat.cpp +24 -0
  26. package/harmony/keyboard_controller/src/main/cpp/RNStatusBarManagerCompat.h +28 -0
  27. package/harmony/keyboard_controller/src/main/cpp/keyboardControllerPackage.cpp +43 -0
  28. package/harmony/keyboard_controller/src/main/cpp/keyboardControllerPackage.h +56 -0
  29. package/harmony/keyboard_controller/src/main/ets/Logger.ts +46 -0
  30. package/harmony/keyboard_controller/src/main/ets/RNKeyboardControllerPackage.ts +45 -0
  31. package/harmony/keyboard_controller/src/main/ets/RNKeyboardControllerTurboModule.ts +238 -0
  32. package/harmony/keyboard_controller/src/main/ets/RNStatusBarManagerCompatTurboModule.ts +98 -0
  33. package/harmony/keyboard_controller/src/main/ets/Type.ts +41 -0
  34. package/harmony/keyboard_controller/src/main/module.json5 +11 -0
  35. package/harmony/keyboard_controller/src/main/resources/base/element/string.json +8 -0
  36. package/harmony/keyboard_controller/src/main/resources/en_US/element/string.json +8 -0
  37. package/harmony/keyboard_controller/src/main/resources/zh_CN/element/string.json +8 -0
  38. package/harmony/keyboard_controller/ts.ts +9 -0
  39. package/harmony/keyboard_controller.har +0 -0
  40. package/lib/commonjs/animated.js +176 -0
  41. package/lib/commonjs/animated.js.map +1 -0
  42. package/lib/commonjs/bindings.js +39 -0
  43. package/lib/commonjs/bindings.js.map +1 -0
  44. package/lib/commonjs/bindings.native.js +39 -0
  45. package/lib/commonjs/bindings.native.js.map +1 -0
  46. package/lib/commonjs/components/KeyboardAvoidingView/hooks.js +126 -0
  47. package/lib/commonjs/components/KeyboardAvoidingView/hooks.js.map +1 -0
  48. package/lib/commonjs/components/KeyboardAvoidingView/index.js +120 -0
  49. package/lib/commonjs/components/KeyboardAvoidingView/index.js.map +1 -0
  50. package/lib/commonjs/components/KeyboardAwareScrollView/index.js +262 -0
  51. package/lib/commonjs/components/KeyboardAwareScrollView/index.js.map +1 -0
  52. package/lib/commonjs/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js +110 -0
  53. package/lib/commonjs/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js.map +1 -0
  54. package/lib/commonjs/components/KeyboardAwareScrollView/utils.js +37 -0
  55. package/lib/commonjs/components/KeyboardAwareScrollView/utils.js.map +1 -0
  56. package/lib/commonjs/components/KeyboardStickyView/index.js +41 -0
  57. package/lib/commonjs/components/KeyboardStickyView/index.js.map +1 -0
  58. package/lib/commonjs/components/KeyboardToolbar/Arrow.js +76 -0
  59. package/lib/commonjs/components/KeyboardToolbar/Arrow.js.map +1 -0
  60. package/lib/commonjs/components/KeyboardToolbar/Button.js +70 -0
  61. package/lib/commonjs/components/KeyboardToolbar/Button.js.map +1 -0
  62. package/lib/commonjs/components/KeyboardToolbar/colors.js +21 -0
  63. package/lib/commonjs/components/KeyboardToolbar/colors.js.map +1 -0
  64. package/lib/commonjs/components/KeyboardToolbar/colors.native.js +40 -0
  65. package/lib/commonjs/components/KeyboardToolbar/colors.native.js.map +1 -0
  66. package/lib/commonjs/components/KeyboardToolbar/index.js +161 -0
  67. package/lib/commonjs/components/KeyboardToolbar/index.js.map +1 -0
  68. package/lib/commonjs/components/KeyboardToolbar/types.js +6 -0
  69. package/lib/commonjs/components/KeyboardToolbar/types.js.map +1 -0
  70. package/lib/commonjs/components/hooks/useColorScheme.js +10 -0
  71. package/lib/commonjs/components/hooks/useColorScheme.js.map +1 -0
  72. package/lib/commonjs/components/index.js +42 -0
  73. package/lib/commonjs/components/index.js.map +1 -0
  74. package/lib/commonjs/constants.js +27 -0
  75. package/lib/commonjs/constants.js.map +1 -0
  76. package/lib/commonjs/context.js +45 -0
  77. package/lib/commonjs/context.js.map +1 -0
  78. package/lib/commonjs/hooks/index.js +104 -0
  79. package/lib/commonjs/hooks/index.js.map +1 -0
  80. package/lib/commonjs/hooks/useWindowDimensions/index.android.js +34 -0
  81. package/lib/commonjs/hooks/useWindowDimensions/index.android.js.map +1 -0
  82. package/lib/commonjs/hooks/useWindowDimensions/index.js +13 -0
  83. package/lib/commonjs/hooks/useWindowDimensions/index.js.map +1 -0
  84. package/lib/commonjs/index.js +136 -0
  85. package/lib/commonjs/index.js.map +1 -0
  86. package/lib/commonjs/internal.js +73 -0
  87. package/lib/commonjs/internal.js.map +1 -0
  88. package/lib/commonjs/module.js +47 -0
  89. package/lib/commonjs/module.js.map +1 -0
  90. package/lib/commonjs/monkey-patch.android.js +57 -0
  91. package/lib/commonjs/monkey-patch.android.js.map +1 -0
  92. package/lib/commonjs/monkey-patch.js +11 -0
  93. package/lib/commonjs/monkey-patch.js.map +1 -0
  94. package/lib/commonjs/reanimated.js +12 -0
  95. package/lib/commonjs/reanimated.js.map +1 -0
  96. package/lib/commonjs/reanimated.native.js +88 -0
  97. package/lib/commonjs/reanimated.native.js.map +1 -0
  98. package/lib/commonjs/specs/KeyboardControllerViewNativeComponent.js +10 -0
  99. package/lib/commonjs/specs/KeyboardControllerViewNativeComponent.js.map +1 -0
  100. package/lib/commonjs/specs/KeyboardGestureAreaNativeComponent.js +12 -0
  101. package/lib/commonjs/specs/KeyboardGestureAreaNativeComponent.js.map +1 -0
  102. package/lib/commonjs/specs/NativeKeyboardController.js +9 -0
  103. package/lib/commonjs/specs/NativeKeyboardController.js.map +1 -0
  104. package/lib/commonjs/specs/NativeStatusBarManagerCompat.js +9 -0
  105. package/lib/commonjs/specs/NativeStatusBarManagerCompat.js.map +1 -0
  106. package/lib/commonjs/specs/OverKeyboardViewNativeComponent.js +10 -0
  107. package/lib/commonjs/specs/OverKeyboardViewNativeComponent.js.map +1 -0
  108. package/lib/commonjs/types.js +6 -0
  109. package/lib/commonjs/types.js.map +1 -0
  110. package/lib/commonjs/utils.js +9 -0
  111. package/lib/commonjs/utils.js.map +1 -0
  112. package/lib/commonjs/views/OverKeyboardView/index.js +42 -0
  113. package/lib/commonjs/views/OverKeyboardView/index.js.map +1 -0
  114. package/lib/commonjs/views/index.js +14 -0
  115. package/lib/commonjs/views/index.js.map +1 -0
  116. package/lib/module/animated.js +168 -0
  117. package/lib/module/animated.js.map +1 -0
  118. package/lib/module/bindings.js +33 -0
  119. package/lib/module/bindings.js.map +1 -0
  120. package/lib/module/bindings.native.js +33 -0
  121. package/lib/module/bindings.native.js.map +1 -0
  122. package/lib/module/components/KeyboardAvoidingView/hooks.js +118 -0
  123. package/lib/module/components/KeyboardAvoidingView/hooks.js.map +1 -0
  124. package/lib/module/components/KeyboardAvoidingView/index.js +113 -0
  125. package/lib/module/components/KeyboardAvoidingView/index.js.map +1 -0
  126. package/lib/module/components/KeyboardAwareScrollView/index.js +255 -0
  127. package/lib/module/components/KeyboardAwareScrollView/index.js.map +1 -0
  128. package/lib/module/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js +103 -0
  129. package/lib/module/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js.map +1 -0
  130. package/lib/module/components/KeyboardAwareScrollView/utils.js +29 -0
  131. package/lib/module/components/KeyboardAwareScrollView/utils.js.map +1 -0
  132. package/lib/module/components/KeyboardStickyView/index.js +34 -0
  133. package/lib/module/components/KeyboardStickyView/index.js.map +1 -0
  134. package/lib/module/components/KeyboardToolbar/Arrow.js +68 -0
  135. package/lib/module/components/KeyboardToolbar/Arrow.js.map +1 -0
  136. package/lib/module/components/KeyboardToolbar/Button.js +62 -0
  137. package/lib/module/components/KeyboardToolbar/Button.js.map +1 -0
  138. package/lib/module/components/KeyboardToolbar/colors.js +15 -0
  139. package/lib/module/components/KeyboardToolbar/colors.js.map +1 -0
  140. package/lib/module/components/KeyboardToolbar/colors.native.js +34 -0
  141. package/lib/module/components/KeyboardToolbar/colors.native.js.map +1 -0
  142. package/lib/module/components/KeyboardToolbar/index.js +149 -0
  143. package/lib/module/components/KeyboardToolbar/index.js.map +1 -0
  144. package/lib/module/components/KeyboardToolbar/types.js +2 -0
  145. package/lib/module/components/KeyboardToolbar/types.js.map +1 -0
  146. package/lib/module/components/hooks/useColorScheme.js +4 -0
  147. package/lib/module/components/hooks/useColorScheme.js.map +1 -0
  148. package/lib/module/components/index.js +5 -0
  149. package/lib/module/components/index.js.map +1 -0
  150. package/lib/module/constants.js +21 -0
  151. package/lib/module/constants.js.map +1 -0
  152. package/lib/module/context.js +38 -0
  153. package/lib/module/context.js.map +1 -0
  154. package/lib/module/hooks/index.js +68 -0
  155. package/lib/module/hooks/index.js.map +1 -0
  156. package/lib/module/hooks/useWindowDimensions/index.android.js +27 -0
  157. package/lib/module/hooks/useWindowDimensions/index.android.js.map +1 -0
  158. package/lib/module/hooks/useWindowDimensions/index.js +2 -0
  159. package/lib/module/hooks/useWindowDimensions/index.js.map +1 -0
  160. package/lib/module/index.js +12 -0
  161. package/lib/module/index.js.map +1 -0
  162. package/lib/module/internal.js +66 -0
  163. package/lib/module/internal.js.map +1 -0
  164. package/lib/module/module.js +41 -0
  165. package/lib/module/module.js.map +1 -0
  166. package/lib/module/monkey-patch.android.js +47 -0
  167. package/lib/module/monkey-patch.android.js.map +1 -0
  168. package/lib/module/monkey-patch.js +5 -0
  169. package/lib/module/monkey-patch.js.map +1 -0
  170. package/lib/module/reanimated.js +6 -0
  171. package/lib/module/reanimated.js.map +1 -0
  172. package/lib/module/reanimated.native.js +78 -0
  173. package/lib/module/reanimated.native.js.map +1 -0
  174. package/lib/module/specs/KeyboardControllerViewNativeComponent.js +3 -0
  175. package/lib/module/specs/KeyboardControllerViewNativeComponent.js.map +1 -0
  176. package/lib/module/specs/KeyboardGestureAreaNativeComponent.js +5 -0
  177. package/lib/module/specs/KeyboardGestureAreaNativeComponent.js.map +1 -0
  178. package/lib/module/specs/NativeKeyboardController.js +3 -0
  179. package/lib/module/specs/NativeKeyboardController.js.map +1 -0
  180. package/lib/module/specs/NativeStatusBarManagerCompat.js +3 -0
  181. package/lib/module/specs/NativeStatusBarManagerCompat.js.map +1 -0
  182. package/lib/module/specs/OverKeyboardViewNativeComponent.js +3 -0
  183. package/lib/module/specs/OverKeyboardViewNativeComponent.js.map +1 -0
  184. package/lib/module/types.js +2 -0
  185. package/lib/module/types.js.map +1 -0
  186. package/lib/module/utils.js +2 -0
  187. package/lib/module/utils.js.map +1 -0
  188. package/lib/module/views/OverKeyboardView/index.js +35 -0
  189. package/lib/module/views/OverKeyboardView/index.js.map +1 -0
  190. package/lib/module/views/index.js +2 -0
  191. package/lib/module/views/index.js.map +1 -0
  192. package/lib/typescript/animated.d.ts +39 -0
  193. package/lib/typescript/bindings.d.ts +12 -0
  194. package/lib/typescript/bindings.native.d.ts +12 -0
  195. package/lib/typescript/components/KeyboardAvoidingView/hooks.d.ts +10 -0
  196. package/lib/typescript/components/KeyboardAvoidingView/index.d.ts +40 -0
  197. package/lib/typescript/components/KeyboardAwareScrollView/index.d.ts +29 -0
  198. package/lib/typescript/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.d.ts +6 -0
  199. package/lib/typescript/components/KeyboardAwareScrollView/utils.d.ts +2 -0
  200. package/lib/typescript/components/KeyboardStickyView/index.d.ts +39 -0
  201. package/lib/typescript/components/KeyboardToolbar/Arrow.d.ts +9 -0
  202. package/lib/typescript/components/KeyboardToolbar/Button.d.ts +16 -0
  203. package/lib/typescript/components/KeyboardToolbar/colors.d.ts +2 -0
  204. package/lib/typescript/components/KeyboardToolbar/colors.native.d.ts +2 -0
  205. package/lib/typescript/components/KeyboardToolbar/index.d.ts +52 -0
  206. package/lib/typescript/components/KeyboardToolbar/types.d.ts +18 -0
  207. package/lib/typescript/components/hooks/useColorScheme.d.ts +2 -0
  208. package/lib/typescript/components/index.d.ts +8 -0
  209. package/lib/typescript/constants.d.ts +16 -0
  210. package/lib/typescript/context.d.ts +23 -0
  211. package/lib/typescript/hooks/index.d.ts +17 -0
  212. package/lib/typescript/hooks/useWindowDimensions/index.android.d.ts +2 -0
  213. package/lib/typescript/hooks/useWindowDimensions/index.d.ts +1 -0
  214. package/lib/typescript/index.d.ts +10 -0
  215. package/lib/typescript/internal.d.ts +30 -0
  216. package/lib/typescript/module.d.ts +2 -0
  217. package/lib/typescript/monkey-patch.android.d.ts +2 -0
  218. package/lib/typescript/monkey-patch.d.ts +2 -0
  219. package/lib/typescript/reanimated.d.ts +5 -0
  220. package/lib/typescript/reanimated.native.d.ts +7 -0
  221. package/lib/typescript/specs/KeyboardControllerViewNativeComponent.d.ts +54 -0
  222. package/lib/typescript/specs/KeyboardGestureAreaNativeComponent.d.ts +12 -0
  223. package/lib/typescript/specs/NativeKeyboardController.d.ts +12 -0
  224. package/lib/typescript/specs/NativeStatusBarManagerCompat.d.ts +10 -0
  225. package/lib/typescript/specs/OverKeyboardViewNativeComponent.d.ts +7 -0
  226. package/lib/typescript/types.d.ts +161 -0
  227. package/lib/typescript/utils.d.ts +1 -0
  228. package/lib/typescript/views/OverKeyboardView/index.d.ts +5 -0
  229. package/lib/typescript/views/index.d.ts +1 -0
  230. package/package.json +173 -0
  231. package/react-native-keyboard-controller.podspec +61 -0
  232. package/src/animated.tsx +256 -0
  233. package/src/bindings.native.ts +62 -0
  234. package/src/bindings.ts +42 -0
  235. package/src/components/KeyboardAvoidingView/hooks.ts +123 -0
  236. package/src/components/KeyboardAvoidingView/index.tsx +188 -0
  237. package/src/components/KeyboardAwareScrollView/index.tsx +389 -0
  238. package/src/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.ts +133 -0
  239. package/src/components/KeyboardAwareScrollView/utils.ts +41 -0
  240. package/src/components/KeyboardStickyView/index.tsx +66 -0
  241. package/src/components/KeyboardToolbar/Arrow.tsx +80 -0
  242. package/src/components/KeyboardToolbar/Button.tsx +98 -0
  243. package/src/components/KeyboardToolbar/colors.native.ts +37 -0
  244. package/src/components/KeyboardToolbar/colors.ts +16 -0
  245. package/src/components/KeyboardToolbar/index.tsx +223 -0
  246. package/src/components/KeyboardToolbar/types.ts +40 -0
  247. package/src/components/hooks/useColorScheme.ts +5 -0
  248. package/src/components/index.ts +11 -0
  249. package/src/constants.ts +19 -0
  250. package/src/context.ts +68 -0
  251. package/src/hooks/index.ts +90 -0
  252. package/src/hooks/useWindowDimensions/index.android.ts +38 -0
  253. package/src/hooks/useWindowDimensions/index.ts +1 -0
  254. package/src/index.ts +23 -0
  255. package/src/internal.ts +84 -0
  256. package/src/module.ts +52 -0
  257. package/src/monkey-patch.android.ts +44 -0
  258. package/src/monkey-patch.ts +4 -0
  259. package/src/reanimated.native.ts +132 -0
  260. package/src/reanimated.ts +29 -0
  261. package/src/specs/KeyboardControllerViewNativeComponent.ts +71 -0
  262. package/src/specs/KeyboardGestureAreaNativeComponent.ts +20 -0
  263. package/src/specs/NativeKeyboardController.ts +19 -0
  264. package/src/specs/NativeStatusBarManagerCompat.ts +14 -0
  265. package/src/specs/OverKeyboardViewNativeComponent.ts +12 -0
  266. package/src/types.ts +243 -0
  267. package/src/utils.ts +1 -0
  268. package/src/views/OverKeyboardView/index.tsx +43 -0
  269. package/src/views/index.ts +1 -0
@@ -0,0 +1,123 @@
1
+ import { useSharedValue } from "react-native-reanimated";
2
+
3
+ import {useKeyboardHandler} from "../../hooks";
4
+ import {useKeyboardContext} from "../../context";
5
+ import { useState } from "react";
6
+ import { Platform } from "react-native";
7
+
8
+ const OS = Platform.OS;
9
+
10
+ export const useKeyboardAnimation = () => {
11
+ const { reanimated } = useKeyboardContext();
12
+ // calculate it only once on mount, to avoid `SharedValue` reads during a render
13
+ const [initialHeight] = useState(() => -reanimated.height.value);
14
+ const [initialProgress] = useState(() => reanimated.progress.value);
15
+
16
+ const heightWhenOpened = useSharedValue(initialHeight);
17
+ const height = useSharedValue(initialHeight);
18
+ const progress = useSharedValue(initialProgress);
19
+ const isClosed = useSharedValue(initialProgress === 0);
20
+
21
+ useKeyboardHandler(
22
+ {
23
+ onStart: (e) => {
24
+ "worklet";
25
+
26
+ if (e.height > 0) {
27
+ // eslint-disable-next-line react-compiler/react-compiler
28
+ isClosed.value = false;
29
+ heightWhenOpened.value = e.height;
30
+ }
31
+ },
32
+ onMove: (e) => {
33
+ "worklet";
34
+
35
+ progress.value = e.progress;
36
+ height.value = e.height;
37
+ },
38
+ onInteractive: (e) => {
39
+ "worklet";
40
+
41
+ progress.value = e.progress;
42
+ height.value = e.height;
43
+ },
44
+ onEnd: (e) => {
45
+ "worklet";
46
+ if(Platform.OS as string === 'harmony'){
47
+ heightWhenOpened.value = e.height;
48
+ }
49
+
50
+ isClosed.value = e.height === 0;
51
+
52
+ // `height` update happens in `onMove` handler
53
+ // in `onEnd` we need to update only if `onMove`
54
+ // wasn't called (i. e. duration === 0)
55
+ //
56
+ // we can not call this code without condition below
57
+ // because in some corner cases (iOS with `secureTextEntry`)
58
+ // `onEnd` can be emitted before `onMove` and in this case
59
+ // it may lead to choppy/glitchy/jumpy UI
60
+ // see https://github.com/kirillzyusko/react-native-keyboard-controller/issues/327
61
+ if (e.duration === 0) {
62
+ progress.value = e.progress;
63
+ height.value = e.height;
64
+ }
65
+ },
66
+ },
67
+ [],
68
+ );
69
+
70
+ return { height, progress, heightWhenOpened, isClosed };
71
+ };
72
+
73
+ export const useTranslateAnimation = () => {
74
+ const { reanimated } = useKeyboardContext();
75
+
76
+ // calculate it only once on mount, to avoid `SharedValue` reads during a render
77
+ const [initialProgress] = useState(() => reanimated.progress.value);
78
+
79
+ const padding = useSharedValue(initialProgress);
80
+ const translate = useSharedValue(0);
81
+
82
+ useKeyboardHandler(
83
+ {
84
+ onStart: (e) => {
85
+ "worklet";
86
+
87
+ if (e.height === 0) {
88
+ // eslint-disable-next-line react-compiler/react-compiler
89
+ padding.value = 0;
90
+ }
91
+ if (OS === "ios") {
92
+ translate.value = e.progress;
93
+ }
94
+ },
95
+ onMove: (e) => {
96
+ "worklet";
97
+
98
+ if (OS === "android") {
99
+ translate.value = e.progress;
100
+ }
101
+ },
102
+ onInteractive: (e) => {
103
+ "worklet";
104
+
105
+ padding.value = 0;
106
+
107
+ translate.value = e.progress;
108
+ },
109
+ onEnd: (e) => {
110
+ "worklet";
111
+
112
+ padding.value = e.progress;
113
+
114
+ if (OS === "android") {
115
+ translate.value = e.progress;
116
+ }
117
+ },
118
+ },
119
+ [],
120
+ );
121
+
122
+ return { translate, padding };
123
+ };
@@ -0,0 +1,188 @@
1
+ import React, { forwardRef, useCallback, useMemo } from "react";
2
+ import { View } from "react-native";
3
+ import Reanimated, {
4
+ interpolate,
5
+ runOnUI,
6
+ useAnimatedStyle,
7
+ useDerivedValue,
8
+ useSharedValue,
9
+ } from "react-native-reanimated";
10
+
11
+ import { useWindowDimensions } from "../../hooks";
12
+
13
+ import { useKeyboardAnimation, useTranslateAnimation } from "./hooks";
14
+
15
+ import type { LayoutRectangle, ViewProps } from "react-native";
16
+
17
+ export type KeyboardAvoidingViewBaseProps = {
18
+ /**
19
+ * Controls whether this `KeyboardAvoidingView` instance should take effect.
20
+ * This is useful when more than one is on the screen. Defaults to true.
21
+ */
22
+ enabled?: boolean;
23
+
24
+ /**
25
+ * Distance between the top of the user screen and the React Native view. This
26
+ * may be non-zero in some cases. Defaults to 0.
27
+ */
28
+ keyboardVerticalOffset?: number;
29
+ } & ViewProps;
30
+
31
+ export type KeyboardAvoidingViewProps = KeyboardAvoidingViewBaseProps &
32
+ (
33
+ | {
34
+ /**
35
+ * Specify how to react to the presence of the keyboard.
36
+ */
37
+ behavior?: "position";
38
+
39
+ /**
40
+ * Style of the content container when `behavior` is 'position'.
41
+ */
42
+ contentContainerStyle?: ViewProps["style"];
43
+ }
44
+ | {
45
+ /**
46
+ * Specify how to react to the presence of the keyboard.
47
+ */
48
+ behavior?: "height" | "padding" | "translate-with-padding";
49
+
50
+ /**
51
+ * `contentContainerStyle` is not allowed for these behaviors.
52
+ */
53
+ contentContainerStyle?: never;
54
+ }
55
+ );
56
+
57
+ const defaultLayout: LayoutRectangle = {
58
+ x: 0,
59
+ y: 0,
60
+ width: 0,
61
+ height: 0,
62
+ };
63
+
64
+ /**
65
+ * View that moves out of the way when the keyboard appears by automatically
66
+ * adjusting its height, position, or bottom padding.
67
+ */
68
+ const KeyboardAvoidingView = forwardRef<
69
+ View,
70
+ React.PropsWithChildren<KeyboardAvoidingViewProps>
71
+ >(
72
+ (
73
+ {
74
+ behavior,
75
+ children,
76
+ contentContainerStyle,
77
+ enabled = true,
78
+ keyboardVerticalOffset = 0,
79
+ style,
80
+ onLayout: onLayoutProps,
81
+ ...props
82
+ },
83
+ ref,
84
+ ) => {
85
+ const initialFrame = useSharedValue<LayoutRectangle | null>(null);
86
+ const frame = useDerivedValue(() => initialFrame.value || defaultLayout);
87
+
88
+ const { translate, padding } = useTranslateAnimation();
89
+ const keyboard = useKeyboardAnimation();
90
+ const { height: screenHeight } = useWindowDimensions();
91
+
92
+ const relativeKeyboardHeight = useCallback(() => {
93
+ "worklet";
94
+
95
+ const keyboardY =
96
+ screenHeight - keyboard.heightWhenOpened.value - keyboardVerticalOffset;
97
+
98
+ return Math.max(frame.value.y + frame.value.height - keyboardY, 0);
99
+ }, [screenHeight, keyboardVerticalOffset]);
100
+ const interpolateToRelativeKeyboardHeight = useCallback(
101
+ (value: number) => {
102
+ "worklet";
103
+
104
+ return interpolate(value, [0, 1], [0, relativeKeyboardHeight()]);
105
+ },
106
+ [relativeKeyboardHeight],
107
+ );
108
+
109
+ const onLayoutWorklet = useCallback((layout: LayoutRectangle) => {
110
+ "worklet";
111
+
112
+ if (keyboard.isClosed.value || initialFrame.value === null) {
113
+ // eslint-disable-next-line react-compiler/react-compiler
114
+ initialFrame.value = layout;
115
+ }
116
+ }, []);
117
+ const onLayout = useCallback<NonNullable<ViewProps["onLayout"]>>(
118
+ (e) => {
119
+ runOnUI(onLayoutWorklet)(e.nativeEvent.layout);
120
+ onLayoutProps?.(e);
121
+ },
122
+ [onLayoutProps],
123
+ );
124
+
125
+ const animatedStyle = useAnimatedStyle(() => {
126
+ const bottom = interpolateToRelativeKeyboardHeight(
127
+ keyboard.progress.value,
128
+ );
129
+ const translateY = interpolateToRelativeKeyboardHeight(translate.value);
130
+ const paddingBottom = interpolateToRelativeKeyboardHeight(padding.value);
131
+ const bottomHeight = enabled ? bottom : 0;
132
+
133
+ switch (behavior) {
134
+ case "height":
135
+ if (!keyboard.isClosed.value) {
136
+ return {
137
+ height: frame.value.height - bottomHeight,
138
+ flex: 0,
139
+ };
140
+ }
141
+
142
+ return {};
143
+
144
+ case "position":
145
+ return { bottom: bottomHeight };
146
+
147
+ case "padding":
148
+ return { paddingBottom: bottomHeight };
149
+
150
+ case "translate-with-padding":
151
+ return {
152
+ paddingBottom: paddingBottom,
153
+ transform: [{ translateY: -translateY}],
154
+ };
155
+
156
+ default:
157
+ return {};
158
+ }
159
+ }, [behavior, enabled, interpolateToRelativeKeyboardHeight]);
160
+ const isPositionBehavior = behavior === "position";
161
+ const containerStyle = isPositionBehavior ? contentContainerStyle : style;
162
+ const combinedStyles = useMemo(
163
+ () => [containerStyle, animatedStyle],
164
+ [containerStyle, animatedStyle],
165
+ );
166
+
167
+ if (isPositionBehavior) {
168
+ return (
169
+ <View ref={ref} style={style} onLayout={onLayout} {...props}>
170
+ <Reanimated.View style={combinedStyles}>{children}</Reanimated.View>
171
+ </View>
172
+ );
173
+ }
174
+
175
+ return (
176
+ <Reanimated.View
177
+ ref={ref}
178
+ onLayout={onLayout}
179
+ style={combinedStyles}
180
+ {...props}
181
+ >
182
+ {children}
183
+ </Reanimated.View>
184
+ );
185
+ },
186
+ );
187
+
188
+ export default KeyboardAvoidingView;
@@ -0,0 +1,389 @@
1
+ import React, { forwardRef, useCallback, useMemo } from "react";
2
+ import { findNodeHandle } from "react-native";
3
+ import Reanimated, {
4
+ interpolate,
5
+ scrollTo,
6
+ useAnimatedReaction,
7
+ useAnimatedRef,
8
+ useAnimatedStyle,
9
+ useScrollViewOffset,
10
+ useSharedValue,
11
+ } from "react-native-reanimated";
12
+
13
+ import {
14
+ useFocusedInputHandler,
15
+ useReanimatedFocusedInput,
16
+ useWindowDimensions,
17
+ } from "../../hooks";
18
+
19
+ import { useSmoothKeyboardHandler } from "./useSmoothKeyboardHandler";
20
+ import { debounce, scrollDistanceWithRespectToSnapPoints } from "./utils";
21
+
22
+ import type {
23
+ LayoutChangeEvent,
24
+ ScrollView,
25
+ ScrollViewProps,
26
+ } from "react-native";
27
+ import type {
28
+ FocusedInputLayoutChangedEvent,
29
+ FocusedInputSelectionChangedEvent,
30
+ NativeEvent,
31
+ } from "..//../types";
32
+
33
+ export type KeyboardAwareScrollViewProps = {
34
+ /** The distance between keyboard and focused `TextInput` when keyboard is shown. Default is `0`. */
35
+ bottomOffset?: number;
36
+ /** Prevents automatic scrolling of the `ScrollView` when the keyboard gets hidden, maintaining the current screen position. Default is `false`. */
37
+ disableScrollOnKeyboardHide?: boolean;
38
+ /** Controls whether this `KeyboardAwareScrollView` instance should take effect. Default is `true` */
39
+ enabled?: boolean;
40
+ /** Adjusting the bottom spacing of KeyboardAwareScrollView. Default is `0` */
41
+ extraKeyboardSpace?: number;
42
+ /** Custom component for `ScrollView`. Default is `ScrollView` */
43
+ ScrollViewComponent?: React.ComponentType<ScrollViewProps>;
44
+ } & ScrollViewProps;
45
+
46
+ /*
47
+ * Everything begins from `onStart` handler. This handler is called every time,
48
+ * when keyboard changes its size or when focused `TextInput` was changed. In
49
+ * this handler we are calculating/memoizing values which later will be used
50
+ * during layout movement. For that we calculate:
51
+ * - layout of focused field (`layout`) - to understand whether there will be overlap
52
+ * - initial keyboard size (`initialKeyboardSize`) - used in scroll interpolation
53
+ * - future keyboard height (`keyboardHeight`) - used in scroll interpolation
54
+ * - current scroll position (`scrollPosition`) - used to scroll from this point
55
+ *
56
+ * Once we've calculated all necessary variables - we can actually start to use them.
57
+ * It happens in `onMove` handler - this function simply calls `maybeScroll` with
58
+ * current keyboard frame height. This functions makes the smooth transition.
59
+ *
60
+ * When the transition has finished we go to `onEnd` handler. In this handler
61
+ * we verify, that the current field is not overlapped within a keyboard frame.
62
+ * For full `onStart`/`onMove`/`onEnd` flow it may look like a redundant thing,
63
+ * however there could be some cases, when `onMove` is not called:
64
+ * - on iOS when TextInput was changed - keyboard transition is instant
65
+ * - on Android when TextInput was changed and keyboard size wasn't changed
66
+ * So `onEnd` handler handle the case, when `onMove` wasn't triggered.
67
+ *
68
+ * ====================================================================================================================+
69
+ * -----------------------------------------------------Flow chart-----------------------------------------------------+
70
+ * ====================================================================================================================+
71
+ *
72
+ * +============================+ +============================+ +==================================+
73
+ * + User Press on TextInput + => + Keyboard starts showing + => + As keyboard moves frame by frame + =>
74
+ * + + + (run `onStart`) + + `onMove` is getting called +
75
+ * +============================+ +============================+ +==================================+
76
+ *
77
+ *
78
+ * +============================+ +============================+ +=====================================+
79
+ * + Keyboard is shown and we + => + User moved focus to + => + Only `onStart`/`onEnd` maybe called +
80
+ * + call `onEnd` handler + + another `TextInput` + + (without involving `onMove`) +
81
+ * +============================+ +============================+ +=====================================+
82
+ *
83
+ */
84
+ const KeyboardAwareScrollView = forwardRef<
85
+ ScrollView,
86
+ React.PropsWithChildren<KeyboardAwareScrollViewProps>
87
+ >(
88
+ (
89
+ {
90
+ children,
91
+ onLayout,
92
+ bottomOffset = 0,
93
+ disableScrollOnKeyboardHide = false,
94
+ enabled = true,
95
+ extraKeyboardSpace = 0,
96
+ ScrollViewComponent = Reanimated.ScrollView,
97
+ ...rest
98
+ },
99
+ ref,
100
+ ) => {
101
+ const scrollViewAnimatedRef = useAnimatedRef<Reanimated.ScrollView>();
102
+ const scrollViewTarget = useSharedValue<number | null>(null);
103
+ const scrollPosition = useSharedValue(0);
104
+ const position = useScrollViewOffset(scrollViewAnimatedRef);
105
+ const currentKeyboardFrameHeight = useSharedValue(0);
106
+ const keyboardHeight = useSharedValue(0);
107
+ const keyboardWillAppear = useSharedValue(false);
108
+ const tag = useSharedValue(-1);
109
+ const initialKeyboardSize = useSharedValue(0);
110
+ const scrollBeforeKeyboardMovement = useSharedValue(0);
111
+ const { input } = useReanimatedFocusedInput();
112
+ const layout = useSharedValue<FocusedInputLayoutChangedEvent | null>(null);
113
+
114
+ const { height } = useWindowDimensions();
115
+
116
+ const onRef = useCallback((assignedRef: Reanimated.ScrollView) => {
117
+ if (typeof ref === "function") {
118
+ ref(assignedRef);
119
+ } else if (ref) {
120
+ ref.current = assignedRef;
121
+ }
122
+
123
+ scrollViewAnimatedRef(assignedRef);
124
+ }, []);
125
+ const onScrollViewLayout = useCallback(
126
+ (e: LayoutChangeEvent) => {
127
+ scrollViewTarget.value = findNodeHandle(scrollViewAnimatedRef.current);
128
+
129
+ onLayout?.(e);
130
+ },
131
+ [onLayout],
132
+ );
133
+
134
+ /**
135
+ * Function that will scroll a ScrollView as keyboard gets moving
136
+ */
137
+ const maybeScroll = useCallback(
138
+ (e: number, animated: boolean = false) => {
139
+ "worklet";
140
+
141
+ if (!enabled) {
142
+ return 0;
143
+ }
144
+
145
+ // input belongs to ScrollView
146
+ if (layout.value?.parentScrollViewTarget !== scrollViewTarget.value) {
147
+ return 0;
148
+ }
149
+
150
+ const visibleRect = height - keyboardHeight.value;
151
+ const absoluteY = layout.value?.layout.absoluteY || 0;
152
+ const inputHeight = layout.value?.layout.height || 0;
153
+ const point = absoluteY + inputHeight;
154
+
155
+ if (visibleRect - point <= bottomOffset) {
156
+ const relativeScrollTo =
157
+ keyboardHeight.value - (height - point) + bottomOffset;
158
+ const interpolatedScrollTo = interpolate(
159
+ e,
160
+ [initialKeyboardSize.value, keyboardHeight.value],
161
+ [
162
+ 0,
163
+ scrollDistanceWithRespectToSnapPoints(
164
+ relativeScrollTo + scrollPosition.value,
165
+ rest.snapToOffsets,
166
+ ) - scrollPosition.value,
167
+ ],
168
+ );
169
+ const targetScrollY =
170
+ Math.max(interpolatedScrollTo, 0) + scrollPosition.value;
171
+ scrollTo(scrollViewAnimatedRef, 0, targetScrollY, animated);
172
+
173
+ return interpolatedScrollTo;
174
+ }
175
+
176
+ if (absoluteY < 0) {
177
+ const positionOnScreen = visibleRect - inputHeight - bottomOffset;
178
+ const topOfScreen = scrollPosition.value + absoluteY;
179
+
180
+ scrollTo(
181
+ scrollViewAnimatedRef,
182
+ 0,
183
+ topOfScreen - positionOnScreen,
184
+ animated,
185
+ );
186
+ }
187
+
188
+ return 0;
189
+ },
190
+ [bottomOffset, enabled, height, rest.snapToOffsets],
191
+ );
192
+
193
+ const syncKeyboardFrame = useCallback(
194
+ (e: NativeEvent) => {
195
+ "worklet";
196
+
197
+ const keyboardFrame = interpolate(
198
+ e.height,
199
+ [0, keyboardHeight.value],
200
+ [0, keyboardHeight.value + extraKeyboardSpace],
201
+ );
202
+
203
+ currentKeyboardFrameHeight.value = keyboardFrame;
204
+ },
205
+ [extraKeyboardSpace],
206
+ );
207
+
208
+ const scrollFromCurrentPosition = useCallback(
209
+ (customHeight?: number) => {
210
+ "worklet";
211
+
212
+ const prevScrollPosition = scrollPosition.value;
213
+ const prevLayout = layout.value;
214
+
215
+ if (!input.value?.layout) {
216
+ return;
217
+ }
218
+
219
+ // eslint-disable-next-line react-compiler/react-compiler
220
+ layout.value = {
221
+ ...input.value,
222
+ layout: {
223
+ ...input.value.layout,
224
+ height: customHeight ?? input.value.layout.height,
225
+ },
226
+ };
227
+ scrollPosition.value = position.value;
228
+ maybeScroll(keyboardHeight.value, true);
229
+ scrollPosition.value = prevScrollPosition;
230
+ layout.value = prevLayout;
231
+ },
232
+ [maybeScroll],
233
+ );
234
+ const onChangeText = useCallback(() => {
235
+ "worklet";
236
+
237
+ // if typing a text caused layout shift, then we need to ignore this handler
238
+ // because this event will be handled in `useAnimatedReaction` below
239
+ if (layout.value?.layout.height !== input.value?.layout.height) {
240
+ return;
241
+ }
242
+
243
+ scrollFromCurrentPosition();
244
+ }, [scrollFromCurrentPosition]);
245
+ const onSelectionChange = useCallback(
246
+ (e: FocusedInputSelectionChangedEvent) => {
247
+ "worklet";
248
+
249
+ if (e.selection.start.position !== e.selection.end.position) {
250
+ scrollFromCurrentPosition(e.selection.end.y);
251
+ }
252
+ },
253
+ [scrollFromCurrentPosition],
254
+ );
255
+
256
+ const onChangeTextHandler = useMemo(
257
+ () => debounce(onChangeText, 200),
258
+ [onChangeText],
259
+ );
260
+
261
+ useFocusedInputHandler(
262
+ {
263
+ onChangeText: onChangeTextHandler,
264
+ onSelectionChange: onSelectionChange,
265
+ },
266
+ [onChangeTextHandler, onSelectionChange],
267
+ );
268
+
269
+ useSmoothKeyboardHandler(
270
+ {
271
+ onStart: (e) => {
272
+ "worklet";
273
+
274
+ const keyboardWillChangeSize =
275
+ keyboardHeight.value !== e.height && e.height > 0;
276
+ keyboardWillAppear.value = e.height > 0 && keyboardHeight.value === 0;
277
+ const keyboardWillHide = e.height === 0;
278
+ const focusWasChanged =
279
+ (tag.value !== e.target && e.target !== -1) ||
280
+ keyboardWillChangeSize;
281
+
282
+ if (keyboardWillChangeSize) {
283
+ initialKeyboardSize.value = keyboardHeight.value;
284
+ }
285
+
286
+ if (keyboardWillHide) {
287
+ // on back transition need to interpolate as [0, keyboardHeight]
288
+ initialKeyboardSize.value = 0;
289
+ scrollPosition.value = scrollBeforeKeyboardMovement.value;
290
+ }
291
+
292
+ if (
293
+ keyboardWillAppear.value ||
294
+ keyboardWillChangeSize ||
295
+ focusWasChanged
296
+ ) {
297
+ // persist scroll value
298
+ scrollPosition.value = position.value;
299
+ // just persist height - later will be used in interpolation
300
+ keyboardHeight.value = e.height;
301
+ }
302
+
303
+ // focus was changed
304
+ if (focusWasChanged) {
305
+ tag.value = e.target;
306
+
307
+ // save position of focused text input when keyboard starts to move
308
+ layout.value = input.value;
309
+ // save current scroll position - when keyboard will hide we'll reuse
310
+ // this value to achieve smooth hide effect
311
+ scrollBeforeKeyboardMovement.value = position.value;
312
+ }
313
+
314
+ if (focusWasChanged && !keyboardWillAppear.value) {
315
+ // update position on scroll value, so `onEnd` handler
316
+ // will pick up correct values
317
+ position.value += maybeScroll(e.height, true);
318
+ }
319
+ },
320
+ onMove: (e) => {
321
+ "worklet";
322
+
323
+ syncKeyboardFrame(e);
324
+
325
+ // if the user has set disableScrollOnKeyboardHide, only auto-scroll when the keyboard opens
326
+ if (!disableScrollOnKeyboardHide || keyboardWillAppear.value) {
327
+ maybeScroll(e.height);
328
+ }
329
+ },
330
+ onEnd: (e) => {
331
+ "worklet";
332
+
333
+ keyboardHeight.value = e.height;
334
+ scrollPosition.value = position.value;
335
+
336
+ syncKeyboardFrame(e);
337
+ },
338
+ },
339
+ [maybeScroll, disableScrollOnKeyboardHide, syncKeyboardFrame],
340
+ );
341
+
342
+ useAnimatedReaction(
343
+ () => input.value,
344
+ (current, previous) => {
345
+ if (
346
+ current?.target === previous?.target &&
347
+ current?.layout.height !== previous?.layout.height
348
+ ) {
349
+ const prevLayout = layout.value;
350
+
351
+ layout.value = input.value;
352
+ scrollPosition.value += maybeScroll(keyboardHeight.value, true);
353
+ layout.value = prevLayout;
354
+ }
355
+ },
356
+ [],
357
+ );
358
+
359
+ const view = useAnimatedStyle(
360
+ () =>
361
+ enabled
362
+ ? {
363
+ // animations become choppy when scrolling to the end of the `ScrollView` (when the last input is focused)
364
+ // this happens because the layout recalculates on every frame. To avoid this we slightly increase padding
365
+ // by `+1`. In this way we assure, that `scrollTo` will never scroll to the end, because it uses interpolation
366
+ // from 0 to `keyboardHeight`, and here our padding is `keyboardHeight + 1`. It allows us not to re-run layout
367
+ // re-calculation on every animation frame and it helps to achieve smooth animation.
368
+ // see: https://github.com/kirillzyusko/react-native-keyboard-controller/pull/342
369
+ paddingBottom: currentKeyboardFrameHeight.value + 1,
370
+ }
371
+ : {},
372
+ [enabled],
373
+ );
374
+
375
+ return (
376
+ <ScrollViewComponent
377
+ ref={onRef}
378
+ {...rest}
379
+ onLayout={onScrollViewLayout}
380
+ scrollEventThrottle={16}
381
+ >
382
+ {children}
383
+ <Reanimated.View style={view} />
384
+ </ScrollViewComponent>
385
+ );
386
+ },
387
+ );
388
+
389
+ export default KeyboardAwareScrollView;