@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
@@ -1,373 +1,381 @@
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;
1
+ import React, { forwardRef, useCallback, useMemo } from "react";
2
+ import { findNodeHandle, Platform } 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
+ const IS_HARMONY = (Platform.OS as string) === "harmony";
33
+
34
+ export type KeyboardAwareScrollViewProps = {
35
+ /** The distance between keyboard and focused `TextInput` when keyboard is shown. Default is `0`. */
36
+ bottomOffset?: number;
37
+ /** Prevents automatic scrolling of the `ScrollView` when the keyboard gets hidden, maintaining the current screen position. Default is `false`. */
38
+ disableScrollOnKeyboardHide?: boolean;
39
+ /** Controls whether this `KeyboardAwareScrollView` instance should take effect. Default is `true` */
40
+ enabled?: boolean;
41
+ /** Adjusting the bottom spacing of KeyboardAwareScrollView. Default is `0` */
42
+ extraKeyboardSpace?: number;
43
+ } & ScrollViewProps;
44
+
45
+ /*
46
+ * Everything begins from `onStart` handler. This handler is called every time,
47
+ * when keyboard changes its size or when focused `TextInput` was changed. In
48
+ * this handler we are calculating/memoizing values which later will be used
49
+ * during layout movement. For that we calculate:
50
+ * - layout of focused field (`layout`) - to understand whether there will be overlap
51
+ * - initial keyboard size (`initialKeyboardSize`) - used in scroll interpolation
52
+ * - future keyboard height (`keyboardHeight`) - used in scroll interpolation
53
+ * - current scroll position (`scrollPosition`) - used to scroll from this point
54
+ *
55
+ * Once we've calculated all necessary variables - we can actually start to use them.
56
+ * It happens in `onMove` handler - this function simply calls `maybeScroll` with
57
+ * current keyboard frame height. This functions makes the smooth transition.
58
+ *
59
+ * When the transition has finished we go to `onEnd` handler. In this handler
60
+ * we verify, that the current field is not overlapped within a keyboard frame.
61
+ * For full `onStart`/`onMove`/`onEnd` flow it may look like a redundant thing,
62
+ * however there could be some cases, when `onMove` is not called:
63
+ * - on iOS when TextInput was changed - keyboard transition is instant
64
+ * - on Android when TextInput was changed and keyboard size wasn't changed
65
+ * So `onEnd` handler handle the case, when `onMove` wasn't triggered.
66
+ *
67
+ * ====================================================================================================================+
68
+ * -----------------------------------------------------Flow chart-----------------------------------------------------+
69
+ * ====================================================================================================================+
70
+ *
71
+ * +============================+ +============================+ +==================================+
72
+ * + User Press on TextInput + => + Keyboard starts showing + => + As keyboard moves frame by frame + =>
73
+ * + + + (run `onStart`) + + `onMove` is getting called +
74
+ * +============================+ +============================+ +==================================+
75
+ *
76
+ *
77
+ * +============================+ +============================+ +=====================================+
78
+ * + Keyboard is shown and we + => + User moved focus to + => + Only `onStart`/`onEnd` maybe called +
79
+ * + call `onEnd` handler + + another `TextInput` + + (without involving `onMove`) +
80
+ * +============================+ +============================+ +=====================================+
81
+ *
82
+ */
83
+ const KeyboardAwareScrollView = forwardRef<
84
+ ScrollView,
85
+ React.PropsWithChildren<KeyboardAwareScrollViewProps>
86
+ >(
87
+ (
88
+ {
89
+ children,
90
+ onLayout,
91
+ bottomOffset = 0,
92
+ disableScrollOnKeyboardHide = false,
93
+ enabled = true,
94
+ extraKeyboardSpace = 0,
95
+ ...rest
96
+ },
97
+ ref,
98
+ ) => {
99
+ const scrollViewAnimatedRef = useAnimatedRef<Reanimated.ScrollView>();
100
+ const scrollViewTarget = useSharedValue<number | null>(null);
101
+ const scrollPosition = useSharedValue(0);
102
+ const position = useScrollViewOffset(scrollViewAnimatedRef);
103
+ const currentKeyboardFrameHeight = useSharedValue(0);
104
+ const keyboardHeight = useSharedValue(0);
105
+ const keyboardWillAppear = useSharedValue(false);
106
+ const tag = useSharedValue(-1);
107
+ const initialKeyboardSize = useSharedValue(0);
108
+ const scrollBeforeKeyboardMovement = useSharedValue(0);
109
+ const { input } = useReanimatedFocusedInput();
110
+ const layout = useSharedValue<FocusedInputLayoutChangedEvent | null>(null);
111
+
112
+ const { height } = useWindowDimensions();
113
+
114
+ const onRef = useCallback((assignedRef: Reanimated.ScrollView) => {
115
+ if (typeof ref === "function") {
116
+ ref(assignedRef);
117
+ } else if (ref) {
118
+ ref.current = assignedRef;
119
+ }
120
+
121
+ scrollViewAnimatedRef(assignedRef);
122
+ }, []);
123
+ const onScrollViewLayout = useCallback(
124
+ (e: LayoutChangeEvent) => {
125
+ scrollViewTarget.value = findNodeHandle(scrollViewAnimatedRef.current);
126
+
127
+ onLayout?.(e);
128
+ },
129
+ [onLayout],
130
+ );
131
+
132
+ /**
133
+ * Function that will scroll a ScrollView as keyboard gets moving
134
+ */
135
+ const maybeScroll = useCallback(
136
+ (e: number, animated: boolean = false) => {
137
+ "worklet";
138
+
139
+ if (!enabled) {
140
+ return 0;
141
+ }
142
+
143
+ // input belongs to ScrollView
144
+ if (layout.value?.parentScrollViewTarget !== scrollViewTarget.value) {
145
+ return 0;
146
+ }
147
+
148
+ const visibleRect = height - keyboardHeight.value;
149
+ const absoluteY = layout.value?.layout.absoluteY || 0;
150
+ const inputHeight = layout.value?.layout.height || 0;
151
+ const point = absoluteY + inputHeight;
152
+
153
+ if (visibleRect - point <= bottomOffset) {
154
+ const relativeScrollTo =
155
+ keyboardHeight.value - (height - point) + bottomOffset;
156
+ const interpolatedScrollTo = interpolate(
157
+ e,
158
+ [initialKeyboardSize.value, keyboardHeight.value],
159
+ [
160
+ 0,
161
+ scrollDistanceWithRespectToSnapPoints(
162
+ relativeScrollTo + scrollPosition.value,
163
+ rest.snapToOffsets,
164
+ ) - scrollPosition.value,
165
+ ],
166
+ );
167
+ const targetScrollY =
168
+ Math.max(interpolatedScrollTo, 0) + scrollPosition.value;
169
+ scrollTo(scrollViewAnimatedRef, 0, targetScrollY, animated);
170
+
171
+ return interpolatedScrollTo;
172
+ }
173
+
174
+ if (absoluteY < 0) {
175
+ const positionOnScreen = visibleRect - inputHeight - bottomOffset;
176
+ const topOfScreen = scrollPosition.value + absoluteY;
177
+
178
+ scrollTo(
179
+ scrollViewAnimatedRef,
180
+ 0,
181
+ topOfScreen - positionOnScreen,
182
+ animated,
183
+ );
184
+ }
185
+
186
+ return 0;
187
+ },
188
+ [bottomOffset, enabled, height, rest.snapToOffsets],
189
+ );
190
+
191
+ const scrollFromCurrentPosition = useCallback(
192
+ (customHeight?: number) => {
193
+ "worklet";
194
+
195
+ const prevScrollPosition = scrollPosition.value;
196
+ const prevLayout = layout.value;
197
+
198
+ if (!input.value?.layout) {
199
+ return;
200
+ }
201
+
202
+ // eslint-disable-next-line react-compiler/react-compiler
203
+ layout.value = {
204
+ ...input.value,
205
+ layout: {
206
+ ...input.value.layout,
207
+ height: customHeight ?? input.value.layout.height,
208
+ },
209
+ };
210
+ scrollPosition.value = position.value;
211
+ maybeScroll(keyboardHeight.value, true);
212
+ scrollPosition.value = prevScrollPosition;
213
+ layout.value = prevLayout;
214
+ },
215
+ [maybeScroll],
216
+ );
217
+ const onChangeText = useCallback(() => {
218
+ "worklet";
219
+
220
+ // if typing a text caused layout shift, then we need to ignore this handler
221
+ // because this event will be handled in `useAnimatedReaction` below
222
+ if (layout.value?.layout.height !== input.value?.layout.height) {
223
+ return;
224
+ }
225
+
226
+ scrollFromCurrentPosition();
227
+ }, [scrollFromCurrentPosition]);
228
+ const onSelectionChange = useCallback(
229
+ (e: FocusedInputSelectionChangedEvent) => {
230
+ "worklet";
231
+
232
+ if (e.selection.start.position !== e.selection.end.position) {
233
+ scrollFromCurrentPosition(e.selection.end.y);
234
+ }
235
+ },
236
+ [scrollFromCurrentPosition],
237
+ );
238
+
239
+ const onChangeTextHandler = useMemo(
240
+ () => debounce(onChangeText, 200),
241
+ [onChangeText],
242
+ );
243
+
244
+ useFocusedInputHandler(
245
+ {
246
+ onChangeText: onChangeTextHandler,
247
+ onSelectionChange: onSelectionChange,
248
+ },
249
+ [onChangeTextHandler, onSelectionChange],
250
+ );
251
+
252
+ useSmoothKeyboardHandler(
253
+ {
254
+ onStart: (e) => {
255
+ "worklet";
256
+
257
+ const keyboardWillChangeSize =
258
+ keyboardHeight.value !== e.height && e.height > 0;
259
+ keyboardWillAppear.value = e.height > 0 && keyboardHeight.value === 0;
260
+ const keyboardWillHide = e.height === 0;
261
+ const focusWasChanged =
262
+ (tag.value !== e.target && e.target !== -1) ||
263
+ keyboardWillChangeSize;
264
+
265
+ if (keyboardWillChangeSize) {
266
+ initialKeyboardSize.value = keyboardHeight.value;
267
+ }
268
+
269
+ if (keyboardWillHide) {
270
+ // on back transition need to interpolate as [0, keyboardHeight]
271
+ initialKeyboardSize.value = 0;
272
+ scrollPosition.value = scrollBeforeKeyboardMovement.value;
273
+ }
274
+
275
+ if (
276
+ keyboardWillAppear.value ||
277
+ keyboardWillChangeSize ||
278
+ focusWasChanged
279
+ ) {
280
+ // persist scroll value
281
+ scrollPosition.value = position.value;
282
+ // just persist height - later will be used in interpolation
283
+ keyboardHeight.value = e.height;
284
+ }
285
+
286
+ // focus was changed
287
+ if (focusWasChanged) {
288
+ tag.value = e.target;
289
+
290
+ // save position of focused text input when keyboard starts to move
291
+ layout.value = input.value;
292
+ // save current scroll position - when keyboard will hide we'll reuse
293
+ // this value to achieve smooth hide effect
294
+ scrollBeforeKeyboardMovement.value = position.value;
295
+ }
296
+
297
+ if (focusWasChanged && (IS_HARMONY || !keyboardWillAppear.value)) {
298
+ // update position on scroll value, so `onEnd` handler
299
+ // will pick up correct values
300
+ position.value += maybeScroll(e.height, true);
301
+ }
302
+ },
303
+ onMove: (e) => {
304
+ "worklet";
305
+
306
+ const keyboardFrame = interpolate(
307
+ e.height,
308
+ [0, keyboardHeight.value],
309
+ [0, keyboardHeight.value + extraKeyboardSpace],
310
+ );
311
+ currentKeyboardFrameHeight.value = keyboardFrame;
312
+
313
+ // if the user has set disableScrollOnKeyboardHide, only auto-scroll when the keyboard opens
314
+ if (
315
+ (!IS_HARMONY || !keyboardWillAppear.value) &&
316
+ (!disableScrollOnKeyboardHide || keyboardWillAppear.value)
317
+ ) {
318
+ maybeScroll(e.height);
319
+ }
320
+ },
321
+ onEnd: (e) => {
322
+ "worklet";
323
+
324
+ keyboardHeight.value = e.height;
325
+ scrollPosition.value = position.value;
326
+ },
327
+ },
328
+ [maybeScroll, disableScrollOnKeyboardHide, extraKeyboardSpace],
329
+ );
330
+
331
+ useAnimatedReaction(
332
+ () => input.value,
333
+ (current, previous) => {
334
+ if (
335
+ current?.target === previous?.target &&
336
+ current?.layout.height !== previous?.layout.height
337
+ ) {
338
+ const prevLayout = layout.value;
339
+
340
+ layout.value = input.value;
341
+ scrollPosition.value += maybeScroll(keyboardHeight.value, true);
342
+ layout.value = prevLayout;
343
+ }
344
+ },
345
+ [],
346
+ );
347
+
348
+ const view = useAnimatedStyle(
349
+ () =>
350
+ enabled
351
+ ? {
352
+ // animations become choppy when scrolling to the end of the `ScrollView` (when the last input is focused)
353
+ // this happens because the layout recalculates on every frame. To avoid this we slightly increase padding
354
+ // by `+1`. In this way we assure, that `scrollTo` will never scroll to the end, because it uses interpolation
355
+ // from 0 to `keyboardHeight`, and here our padding is `keyboardHeight + 1`. It allows us not to re-run layout
356
+ // re-calculation on every animation frame and it helps to achieve smooth animation.
357
+ // see: https://github.com/kirillzyusko/react-native-keyboard-controller/pull/342
358
+ paddingBottom: currentKeyboardFrameHeight.value + 1,
359
+ }
360
+ : {},
361
+ [enabled],
362
+ );
363
+
364
+ return (
365
+ <Reanimated.ScrollView
366
+ ref={onRef}
367
+ {...rest}
368
+ onLayout={onScrollViewLayout}
369
+ scrollToOverflowEnabled={
370
+ IS_HARMONY ? true : rest.scrollToOverflowEnabled
371
+ }
372
+ scrollEventThrottle={16}
373
+ >
374
+ {children}
375
+ <Reanimated.View style={view} />
376
+ </Reanimated.ScrollView>
377
+ );
378
+ },
379
+ );
380
+
381
+ export default KeyboardAwareScrollView;