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