@react-native-ohos/react-native-keyboard-controller 1.12.8 → 1.12.10-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 (166) 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 -29
  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 +28 -18
  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 -111
  15. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewComponentDescriptor.h +77 -98
  16. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewComponentInstance.cpp +837 -274
  17. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewComponentInstance.h +152 -101
  18. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewJSIBinder.h +43 -64
  19. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaComponentDescriptor.h +64 -85
  20. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaComponentInstance.cpp +88 -109
  21. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaComponentInstance.h +63 -84
  22. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaJSIBinder.h +39 -60
  23. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardController.cpp +25 -46
  24. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardController.h +27 -48
  25. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardControllerPackage.cpp +43 -64
  26. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardControllerPackage.h +56 -77
  27. package/harmony/keyboard_controller/src/main/cpp/RNStatusBarManagerCompat.cpp +24 -45
  28. package/harmony/keyboard_controller/src/main/cpp/RNStatusBarManagerCompat.h +28 -49
  29. package/harmony/keyboard_controller/src/main/cpp/ViewHierarchyNavigator.cpp +186 -186
  30. package/harmony/keyboard_controller/src/main/cpp/ViewHierarchyNavigator.h +112 -112
  31. package/harmony/keyboard_controller/src/main/cpp/keyboardControllerPackage.cpp +43 -64
  32. package/harmony/keyboard_controller/src/main/cpp/keyboardControllerPackage.h +56 -77
  33. package/harmony/keyboard_controller/src/main/ets/Logger.ts +45 -66
  34. package/harmony/keyboard_controller/src/main/ets/RNKeyboardControllerPackage.ets +49 -70
  35. package/harmony/keyboard_controller/src/main/ets/RNKeyboardControllerTurboModule.ts +405 -245
  36. package/harmony/keyboard_controller/src/main/ets/RNStatusBarManagerCompatTurboModule.ts +107 -119
  37. package/harmony/keyboard_controller/src/main/ets/Type.ts +41 -62
  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 -29
  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 +39 -39
  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.map +1 -1
  70. package/lib/commonjs/context.js.map +1 -1
  71. package/lib/commonjs/hooks/index.js.map +1 -1
  72. package/lib/commonjs/hooks/useWindowDimensions/index.android.js.map +1 -1
  73. package/lib/commonjs/hooks/useWindowDimensions/index.js.map +1 -1
  74. package/lib/commonjs/index.js.map +1 -1
  75. package/lib/commonjs/internal.js +18 -18
  76. package/lib/commonjs/internal.js.map +1 -1
  77. package/lib/commonjs/monkey-patch.android.js +4 -4
  78. package/lib/commonjs/monkey-patch.android.js.map +1 -1
  79. package/lib/commonjs/monkey-patch.js.map +1 -1
  80. package/lib/commonjs/reanimated.js.map +1 -1
  81. package/lib/commonjs/reanimated.native.js.map +1 -1
  82. package/lib/commonjs/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
  83. package/lib/commonjs/specs/KeyboardGestureAreaNativeComponent.js.map +1 -1
  84. package/lib/commonjs/specs/NativeKeyboardController.js.map +1 -1
  85. package/lib/commonjs/specs/NativeStatusBarManagerCompat.js.map +1 -1
  86. package/lib/commonjs/types.js.map +1 -1
  87. package/lib/commonjs/utils.js.map +1 -1
  88. package/lib/module/animated.js +5 -17
  89. package/lib/module/animated.js.map +1 -1
  90. package/lib/module/bindings.js +3 -3
  91. package/lib/module/bindings.js.map +1 -1
  92. package/lib/module/bindings.native.js +3 -3
  93. package/lib/module/bindings.native.js.map +1 -1
  94. package/lib/module/components/KeyboardAvoidingView/hooks.js.map +1 -1
  95. package/lib/module/components/KeyboardAvoidingView/index.js +3 -3
  96. package/lib/module/components/KeyboardAvoidingView/index.js.map +1 -1
  97. package/lib/module/components/KeyboardAwareScrollView/index.js +39 -39
  98. package/lib/module/components/KeyboardAwareScrollView/index.js.map +1 -1
  99. package/lib/module/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js +3 -3
  100. package/lib/module/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js.map +1 -1
  101. package/lib/module/components/KeyboardAwareScrollView/utils.js.map +1 -1
  102. package/lib/module/components/KeyboardStickyView/index.js.map +1 -1
  103. package/lib/module/components/KeyboardToolbar/Arrow.js.map +1 -1
  104. package/lib/module/components/KeyboardToolbar/Button.js.map +1 -1
  105. package/lib/module/components/KeyboardToolbar/colors.js.map +1 -1
  106. package/lib/module/components/KeyboardToolbar/colors.native.js.map +1 -1
  107. package/lib/module/components/KeyboardToolbar/index.js +3 -3
  108. package/lib/module/components/KeyboardToolbar/index.js.map +1 -1
  109. package/lib/module/components/KeyboardToolbar/types.js.map +1 -1
  110. package/lib/module/components/hooks/useColorScheme.js.map +1 -1
  111. package/lib/module/components/index.js.map +1 -1
  112. package/lib/module/constants.js.map +1 -1
  113. package/lib/module/context.js.map +1 -1
  114. package/lib/module/hooks/index.js.map +1 -1
  115. package/lib/module/hooks/useWindowDimensions/index.android.js.map +1 -1
  116. package/lib/module/hooks/useWindowDimensions/index.js.map +1 -1
  117. package/lib/module/index.js.map +1 -1
  118. package/lib/module/internal.js +18 -18
  119. package/lib/module/internal.js.map +1 -1
  120. package/lib/module/monkey-patch.android.js +4 -4
  121. package/lib/module/monkey-patch.android.js.map +1 -1
  122. package/lib/module/monkey-patch.js.map +1 -1
  123. package/lib/module/reanimated.js.map +1 -1
  124. package/lib/module/reanimated.native.js.map +1 -1
  125. package/lib/module/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
  126. package/lib/module/specs/KeyboardGestureAreaNativeComponent.js.map +1 -1
  127. package/lib/module/specs/NativeKeyboardController.js.map +1 -1
  128. package/lib/module/specs/NativeStatusBarManagerCompat.js.map +1 -1
  129. package/lib/module/types.js.map +1 -1
  130. package/lib/module/utils.js.map +1 -1
  131. package/lib/typescript/types.d.ts +1 -0
  132. package/package.json +181 -178
  133. package/src/animated.tsx +246 -253
  134. package/src/bindings.native.ts +57 -57
  135. package/src/bindings.ts +38 -38
  136. package/src/components/KeyboardAvoidingView/hooks.ts +58 -58
  137. package/src/components/KeyboardAvoidingView/index.tsx +156 -156
  138. package/src/components/KeyboardAwareScrollView/index.tsx +373 -373
  139. package/src/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.ts +133 -133
  140. package/src/components/KeyboardAwareScrollView/utils.ts +41 -41
  141. package/src/components/KeyboardStickyView/index.tsx +57 -57
  142. package/src/components/KeyboardToolbar/Arrow.tsx +80 -80
  143. package/src/components/KeyboardToolbar/Button.tsx +98 -98
  144. package/src/components/KeyboardToolbar/colors.native.ts +37 -37
  145. package/src/components/KeyboardToolbar/colors.ts +16 -16
  146. package/src/components/KeyboardToolbar/index.tsx +211 -211
  147. package/src/components/KeyboardToolbar/types.ts +40 -40
  148. package/src/components/hooks/useColorScheme.ts +5 -5
  149. package/src/components/index.ts +11 -11
  150. package/src/constants.ts +19 -19
  151. package/src/context.ts +65 -65
  152. package/src/hooks/index.ts +90 -90
  153. package/src/hooks/useWindowDimensions/index.android.ts +33 -33
  154. package/src/hooks/useWindowDimensions/index.ts +1 -1
  155. package/src/index.ts +21 -21
  156. package/src/internal.ts +84 -84
  157. package/src/monkey-patch.android.ts +44 -44
  158. package/src/monkey-patch.ts +4 -4
  159. package/src/reanimated.native.ts +132 -132
  160. package/src/reanimated.ts +29 -29
  161. package/src/specs/KeyboardControllerViewNativeComponent.ts +70 -70
  162. package/src/specs/KeyboardGestureAreaNativeComponent.ts +15 -15
  163. package/src/specs/NativeKeyboardController.ts +19 -19
  164. package/src/specs/NativeStatusBarManagerCompat.ts +14 -14
  165. package/src/types.ts +212 -211
  166. package/src/utils.ts +1 -1
@@ -1,373 +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;
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;