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