@react-native-ohos/react-native-keyboard-controller 1.16.7-beta.2 → 1.16.8

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