react-native-keyboard-controller 1.9.5 → 1.10.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.
- package/README.md +1 -1
- package/android/src/fabric/java/com/reactnativekeyboardcontroller/KeyboardControllerModule.kt +4 -0
- package/android/src/main/java/com/reactnativekeyboardcontroller/events/FocusedInputTextChangedEvent.kt +20 -0
- package/android/src/main/java/com/reactnativekeyboardcontroller/extensions/EditText.kt +36 -0
- package/android/src/main/java/com/reactnativekeyboardcontroller/extensions/WindowInsetsAnimationCompat.kt +7 -0
- package/android/src/main/java/com/reactnativekeyboardcontroller/listeners/FocusedInputObserver.kt +17 -0
- package/android/src/main/java/com/reactnativekeyboardcontroller/listeners/KeyboardAnimationCallback.kt +12 -0
- package/android/src/main/java/com/reactnativekeyboardcontroller/managers/KeyboardControllerViewManagerImpl.kt +2 -0
- package/android/src/main/java/com/reactnativekeyboardcontroller/modules/KeyboardControllerModuleImpl.kt +13 -0
- package/android/src/paper/java/com/reactnativekeyboardcontroller/KeyboardControllerModule.kt +5 -0
- package/ios/.clang-format +30 -30
- package/ios/.swiftlint.yml +1 -1
- package/ios/KeyboardController.xcodeproj/project.pbxproj +10 -0
- package/ios/KeyboardControllerModule.mm +14 -0
- package/ios/events/FocusedInputTextChangedEvent.h +16 -0
- package/ios/events/FocusedInputTextChangedEvent.m +72 -0
- package/ios/observers/FocusedInputObserver.swift +29 -14
- package/ios/observers/TextChangeObserver.swift +41 -0
- package/ios/views/KeyboardControllerView.mm +53 -31
- package/ios/views/KeyboardControllerViewManager.mm +1 -0
- package/ios/views/KeyboardControllerViewManager.swift +21 -10
- package/jest/index.js +6 -4
- package/lib/commonjs/animated.js +33 -27
- package/lib/commonjs/animated.js.map +1 -1
- package/lib/commonjs/bindings.js +1 -0
- package/lib/commonjs/bindings.js.map +1 -1
- package/lib/commonjs/bindings.native.js +6 -6
- package/lib/commonjs/bindings.native.js.map +1 -1
- package/lib/commonjs/components/KeyboardAvoidingView/hooks.js +5 -3
- package/lib/commonjs/components/KeyboardAvoidingView/hooks.js.map +1 -1
- package/lib/commonjs/components/KeyboardAvoidingView/index.js +17 -9
- package/lib/commonjs/components/KeyboardAvoidingView/index.js.map +1 -1
- package/lib/commonjs/components/KeyboardAwareScrollView/index.js +199 -0
- package/lib/commonjs/components/KeyboardAwareScrollView/index.js.map +1 -0
- package/lib/commonjs/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js +100 -0
- package/lib/commonjs/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js.map +1 -0
- package/lib/commonjs/components/KeyboardAwareScrollView/utils.js +28 -0
- package/lib/commonjs/components/KeyboardAwareScrollView/utils.js.map +1 -0
- package/lib/commonjs/components/KeyboardStickyView/index.js +7 -4
- package/lib/commonjs/components/KeyboardStickyView/index.js.map +1 -1
- package/lib/commonjs/components/hooks/useKeyboardInterpolation.js +81 -0
- package/lib/commonjs/components/hooks/useKeyboardInterpolation.js.map +1 -0
- package/lib/commonjs/components/index.js +7 -0
- package/lib/commonjs/components/index.js.map +1 -1
- package/lib/commonjs/constants.js.map +1 -1
- package/lib/commonjs/context.js +2 -1
- package/lib/commonjs/context.js.map +1 -1
- package/lib/commonjs/hooks.js +17 -2
- package/lib/commonjs/hooks.js.map +1 -1
- package/lib/commonjs/index.js +8 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/internal.js +3 -6
- package/lib/commonjs/internal.js.map +1 -1
- package/lib/commonjs/monkey-patch.android.js +1 -1
- package/lib/commonjs/monkey-patch.android.js.map +1 -1
- package/lib/commonjs/monkey-patch.js.map +1 -1
- package/lib/commonjs/reanimated.js +5 -3
- package/lib/commonjs/reanimated.js.map +1 -1
- package/lib/commonjs/reanimated.native.js +29 -12
- package/lib/commonjs/reanimated.native.js.map +1 -1
- package/lib/commonjs/replicas.js +7 -5
- package/lib/commonjs/replicas.js.map +1 -1
- package/lib/commonjs/specs/KeyboardControllerViewNativeComponent.js +1 -1
- package/lib/commonjs/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
- package/lib/commonjs/specs/KeyboardGestureAreaNativeComponent.js +2 -2
- package/lib/commonjs/specs/KeyboardGestureAreaNativeComponent.js.map +1 -1
- package/lib/commonjs/specs/NativeKeyboardController.js +1 -1
- package/lib/commonjs/specs/NativeKeyboardController.js.map +1 -1
- package/lib/commonjs/specs/NativeStatusBarManagerCompat.js +1 -1
- package/lib/commonjs/specs/NativeStatusBarManagerCompat.js.map +1 -1
- package/lib/commonjs/types.js.map +1 -1
- package/lib/commonjs/utils.js.map +1 -1
- package/lib/module/animated.js +41 -35
- package/lib/module/animated.js.map +1 -1
- package/lib/module/bindings.js +2 -1
- package/lib/module/bindings.js.map +1 -1
- package/lib/module/bindings.native.js +7 -7
- package/lib/module/bindings.native.js.map +1 -1
- package/lib/module/components/KeyboardAvoidingView/hooks.js +7 -5
- package/lib/module/components/KeyboardAvoidingView/hooks.js.map +1 -1
- package/lib/module/components/KeyboardAvoidingView/index.js +20 -13
- package/lib/module/components/KeyboardAvoidingView/index.js.map +1 -1
- package/lib/module/components/KeyboardAwareScrollView/index.js +190 -0
- package/lib/module/components/KeyboardAwareScrollView/index.js.map +1 -0
- package/lib/module/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js +93 -0
- package/lib/module/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js.map +1 -0
- package/lib/module/components/KeyboardAwareScrollView/utils.js +21 -0
- package/lib/module/components/KeyboardAwareScrollView/utils.js.map +1 -0
- package/lib/module/components/KeyboardStickyView/index.js +9 -7
- package/lib/module/components/KeyboardStickyView/index.js.map +1 -1
- package/lib/module/components/hooks/useKeyboardInterpolation.js +74 -0
- package/lib/module/components/hooks/useKeyboardInterpolation.js.map +1 -0
- package/lib/module/components/index.js +3 -2
- package/lib/module/components/index.js.map +1 -1
- package/lib/module/context.js +4 -3
- package/lib/module/context.js.map +1 -1
- package/lib/module/hooks.js +21 -7
- package/lib/module/hooks.js.map +1 -1
- package/lib/module/index.js +8 -8
- package/lib/module/index.js.map +1 -1
- package/lib/module/internal.js +6 -9
- package/lib/module/internal.js.map +1 -1
- package/lib/module/monkey-patch.android.js +2 -2
- package/lib/module/monkey-patch.android.js.map +1 -1
- package/lib/module/monkey-patch.js.map +1 -1
- package/lib/module/reanimated.js +2 -1
- package/lib/module/reanimated.js.map +1 -1
- package/lib/module/reanimated.native.js +27 -11
- package/lib/module/reanimated.native.js.map +1 -1
- package/lib/module/replicas.js +13 -11
- package/lib/module/replicas.js.map +1 -1
- package/lib/module/specs/KeyboardControllerViewNativeComponent.js +2 -2
- package/lib/module/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
- package/lib/module/specs/KeyboardGestureAreaNativeComponent.js +3 -3
- package/lib/module/specs/KeyboardGestureAreaNativeComponent.js.map +1 -1
- package/lib/module/specs/NativeKeyboardController.js +2 -2
- package/lib/module/specs/NativeKeyboardController.js.map +1 -1
- package/lib/module/specs/NativeStatusBarManagerCompat.js +2 -2
- package/lib/module/specs/NativeStatusBarManagerCompat.js.map +1 -1
- package/lib/module/types.js.map +1 -1
- package/lib/module/utils.js.map +1 -1
- package/lib/typescript/animated.d.ts +1 -1
- package/lib/typescript/bindings.d.ts +1 -1
- package/lib/typescript/bindings.native.d.ts +1 -1
- package/lib/typescript/components/KeyboardAvoidingView/index.d.ts +4 -4
- package/lib/typescript/components/KeyboardAwareScrollView/index.d.ts +45 -0
- package/lib/typescript/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.d.ts +6 -0
- package/lib/typescript/components/KeyboardAwareScrollView/utils.d.ts +1 -0
- package/lib/typescript/components/KeyboardStickyView/index.d.ts +2 -2
- package/lib/typescript/components/hooks/useKeyboardInterpolation.d.ts +20 -0
- package/lib/typescript/components/index.d.ts +3 -2
- package/lib/typescript/context.d.ts +6 -5
- package/lib/typescript/hooks.d.ts +4 -3
- package/lib/typescript/index.d.ts +8 -8
- package/lib/typescript/internal.d.ts +9 -6
- package/lib/typescript/reanimated.d.ts +3 -2
- package/lib/typescript/reanimated.native.d.ts +3 -2
- package/lib/typescript/replicas.d.ts +1 -1
- package/lib/typescript/specs/KeyboardControllerViewNativeComponent.d.ts +7 -3
- package/lib/typescript/specs/KeyboardGestureAreaNativeComponent.d.ts +4 -4
- package/lib/typescript/specs/NativeKeyboardController.d.ts +2 -1
- package/lib/typescript/specs/NativeStatusBarManagerCompat.d.ts +1 -1
- package/lib/typescript/types.d.ts +18 -5
- package/package.json +12 -11
- package/react-native-keyboard-controller.podspec +1 -1
- package/src/animated.tsx +59 -42
- package/src/bindings.native.ts +11 -11
- package/src/bindings.ts +4 -3
- package/src/components/KeyboardAvoidingView/hooks.ts +9 -6
- package/src/components/KeyboardAvoidingView/index.tsx +30 -27
- package/src/components/KeyboardAwareScrollView/index.tsx +262 -0
- package/src/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.ts +123 -0
- package/src/components/KeyboardAwareScrollView/utils.ts +26 -0
- package/src/components/KeyboardStickyView/index.tsx +12 -18
- package/src/components/hooks/useKeyboardInterpolation.ts +96 -0
- package/src/components/index.ts +3 -2
- package/src/context.ts +15 -9
- package/src/hooks.ts +30 -13
- package/src/index.ts +12 -8
- package/src/internal.ts +22 -13
- package/src/monkey-patch.android.ts +2 -2
- package/src/reanimated.native.ts +43 -18
- package/src/reanimated.ts +9 -3
- package/src/replicas.ts +18 -17
- package/src/specs/KeyboardControllerViewNativeComponent.ts +12 -5
- package/src/specs/KeyboardGestureAreaNativeComponent.ts +7 -7
- package/src/specs/NativeKeyboardController.ts +4 -3
- package/src/specs/NativeStatusBarManagerCompat.ts +3 -3
- package/src/types.ts +45 -19
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { useSharedValue } from
|
|
1
|
+
import { useSharedValue } from "react-native-reanimated";
|
|
2
2
|
|
|
3
|
-
import { useKeyboardHandler } from
|
|
3
|
+
import { useKeyboardHandler } from "../../hooks";
|
|
4
4
|
|
|
5
5
|
export const useKeyboardAnimation = () => {
|
|
6
6
|
const heightWhenOpened = useSharedValue(0);
|
|
@@ -11,7 +11,7 @@ export const useKeyboardAnimation = () => {
|
|
|
11
11
|
useKeyboardHandler(
|
|
12
12
|
{
|
|
13
13
|
onStart: (e) => {
|
|
14
|
-
|
|
14
|
+
"worklet";
|
|
15
15
|
|
|
16
16
|
if (e.height > 0) {
|
|
17
17
|
isClosed.value = false;
|
|
@@ -19,18 +19,21 @@ export const useKeyboardAnimation = () => {
|
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
onMove: (e) => {
|
|
22
|
-
|
|
22
|
+
"worklet";
|
|
23
23
|
|
|
24
24
|
progress.value = e.progress;
|
|
25
25
|
height.value = e.height;
|
|
26
26
|
},
|
|
27
27
|
onEnd: (e) => {
|
|
28
|
-
|
|
28
|
+
"worklet";
|
|
29
29
|
|
|
30
30
|
isClosed.value = e.height === 0;
|
|
31
|
+
|
|
32
|
+
progress.value = e.progress;
|
|
33
|
+
height.value = e.height;
|
|
31
34
|
},
|
|
32
35
|
},
|
|
33
|
-
[]
|
|
36
|
+
[],
|
|
34
37
|
);
|
|
35
38
|
|
|
36
39
|
return { height, progress, heightWhenOpened, isClosed };
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import React, { forwardRef, useCallback, useMemo } from
|
|
2
|
-
import { View, useWindowDimensions } from
|
|
1
|
+
import React, { forwardRef, useCallback, useMemo } from "react";
|
|
2
|
+
import { View, useWindowDimensions } from "react-native";
|
|
3
3
|
import Reanimated, {
|
|
4
|
-
interpolate,
|
|
5
4
|
runOnUI,
|
|
6
5
|
useAnimatedStyle,
|
|
7
6
|
useDerivedValue,
|
|
8
7
|
useSharedValue,
|
|
9
|
-
|
|
10
|
-
} from 'react-native-reanimated';
|
|
8
|
+
} from "react-native-reanimated";
|
|
11
9
|
|
|
12
|
-
import
|
|
10
|
+
import useKeyboardInterpolation from "../hooks/useKeyboardInterpolation";
|
|
13
11
|
|
|
14
|
-
import
|
|
12
|
+
import { useKeyboardAnimation } from "./hooks";
|
|
13
|
+
|
|
14
|
+
import type { LayoutRectangle, ViewProps } from "react-native";
|
|
15
15
|
|
|
16
16
|
type Props = {
|
|
17
17
|
/**
|
|
18
18
|
* Specify how to react to the presence of the keyboard.
|
|
19
19
|
*/
|
|
20
|
-
behavior?:
|
|
20
|
+
behavior?: "height" | "position" | "padding";
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* Style of the content container when `behavior` is 'position'.
|
|
24
24
|
*/
|
|
25
|
-
contentContainerStyle?: ViewProps[
|
|
25
|
+
contentContainerStyle?: ViewProps["style"];
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* Controls whether this `KeyboardAvoidingView` instance should take effect.
|
|
@@ -60,7 +60,7 @@ const KeyboardAvoidingView = forwardRef<View, React.PropsWithChildren<Props>>(
|
|
|
60
60
|
onLayout: onLayoutProps,
|
|
61
61
|
...props
|
|
62
62
|
},
|
|
63
|
-
ref
|
|
63
|
+
ref,
|
|
64
64
|
) => {
|
|
65
65
|
const initialFrame = useSharedValue<LayoutRectangle | null>(null);
|
|
66
66
|
const frame = useDerivedValue(() => initialFrame.value || defaultLayout);
|
|
@@ -68,36 +68,40 @@ const KeyboardAvoidingView = forwardRef<View, React.PropsWithChildren<Props>>(
|
|
|
68
68
|
const keyboard = useKeyboardAnimation();
|
|
69
69
|
const { height: screenHeight } = useWindowDimensions();
|
|
70
70
|
|
|
71
|
-
const relativeKeyboardHeight =
|
|
71
|
+
const relativeKeyboardHeight = useCallback(() => {
|
|
72
|
+
"worklet";
|
|
73
|
+
|
|
72
74
|
const keyboardY =
|
|
73
75
|
screenHeight - keyboard.heightWhenOpened.value - keyboardVerticalOffset;
|
|
74
76
|
|
|
75
77
|
return Math.max(frame.value.y + frame.value.height - keyboardY, 0);
|
|
76
78
|
}, [screenHeight, keyboardVerticalOffset]);
|
|
79
|
+
const { interpolate } = useKeyboardInterpolation();
|
|
80
|
+
|
|
81
|
+
const onLayoutWorklet = useCallback((layout: LayoutRectangle) => {
|
|
82
|
+
"worklet";
|
|
77
83
|
|
|
78
|
-
const onLayoutWorklet = useWorkletCallback((layout: LayoutRectangle) => {
|
|
79
84
|
if (keyboard.isClosed.value) {
|
|
80
85
|
initialFrame.value = layout;
|
|
81
86
|
}
|
|
82
|
-
});
|
|
83
|
-
const onLayout = useCallback<NonNullable<ViewProps[
|
|
87
|
+
}, []);
|
|
88
|
+
const onLayout = useCallback<NonNullable<ViewProps["onLayout"]>>(
|
|
84
89
|
(e) => {
|
|
85
90
|
runOnUI(onLayoutWorklet)(e.nativeEvent.layout);
|
|
86
91
|
onLayoutProps?.(e);
|
|
87
92
|
},
|
|
88
|
-
[onLayoutProps]
|
|
93
|
+
[onLayoutProps],
|
|
89
94
|
);
|
|
90
95
|
|
|
91
96
|
const animatedStyle = useAnimatedStyle(() => {
|
|
92
|
-
const bottom = interpolate(
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
);
|
|
97
|
+
const bottom = interpolate(keyboard.height.value, [
|
|
98
|
+
0,
|
|
99
|
+
relativeKeyboardHeight(),
|
|
100
|
+
]);
|
|
97
101
|
const bottomHeight = enabled ? bottom : 0;
|
|
98
102
|
|
|
99
103
|
switch (behavior) {
|
|
100
|
-
case
|
|
104
|
+
case "height":
|
|
101
105
|
if (!keyboard.isClosed.value) {
|
|
102
106
|
return {
|
|
103
107
|
height: frame.value.height - bottomHeight,
|
|
@@ -107,21 +111,21 @@ const KeyboardAvoidingView = forwardRef<View, React.PropsWithChildren<Props>>(
|
|
|
107
111
|
|
|
108
112
|
return {};
|
|
109
113
|
|
|
110
|
-
case
|
|
114
|
+
case "position":
|
|
111
115
|
return { bottom: bottomHeight };
|
|
112
116
|
|
|
113
|
-
case
|
|
117
|
+
case "padding":
|
|
114
118
|
return { paddingBottom: bottomHeight };
|
|
115
119
|
|
|
116
120
|
default:
|
|
117
121
|
return {};
|
|
118
122
|
}
|
|
119
123
|
}, [behavior, enabled, relativeKeyboardHeight]);
|
|
120
|
-
const isPositionBehavior = behavior ===
|
|
124
|
+
const isPositionBehavior = behavior === "position";
|
|
121
125
|
const containerStyle = isPositionBehavior ? contentContainerStyle : style;
|
|
122
126
|
const combinedStyles = useMemo(
|
|
123
127
|
() => [containerStyle, animatedStyle],
|
|
124
|
-
[containerStyle, animatedStyle]
|
|
128
|
+
[containerStyle, animatedStyle],
|
|
125
129
|
);
|
|
126
130
|
|
|
127
131
|
if (isPositionBehavior) {
|
|
@@ -134,7 +138,6 @@ const KeyboardAvoidingView = forwardRef<View, React.PropsWithChildren<Props>>(
|
|
|
134
138
|
|
|
135
139
|
return (
|
|
136
140
|
<Reanimated.View
|
|
137
|
-
// @ts-expect-error because `ref` from reanimated is not compatible with react-native
|
|
138
141
|
ref={ref}
|
|
139
142
|
onLayout={onLayout}
|
|
140
143
|
style={combinedStyles}
|
|
@@ -143,7 +146,7 @@ const KeyboardAvoidingView = forwardRef<View, React.PropsWithChildren<Props>>(
|
|
|
143
146
|
{children}
|
|
144
147
|
</Reanimated.View>
|
|
145
148
|
);
|
|
146
|
-
}
|
|
149
|
+
},
|
|
147
150
|
);
|
|
148
151
|
|
|
149
152
|
export default KeyboardAvoidingView;
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import React, { useCallback, useMemo } from "react";
|
|
2
|
+
import { useWindowDimensions } from "react-native";
|
|
3
|
+
import Reanimated, {
|
|
4
|
+
interpolate,
|
|
5
|
+
scrollTo,
|
|
6
|
+
useAnimatedReaction,
|
|
7
|
+
useAnimatedRef,
|
|
8
|
+
useAnimatedScrollHandler,
|
|
9
|
+
useAnimatedStyle,
|
|
10
|
+
useSharedValue,
|
|
11
|
+
} from "react-native-reanimated";
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
useFocusedInputHandler,
|
|
15
|
+
useReanimatedFocusedInput,
|
|
16
|
+
} from "react-native-keyboard-controller";
|
|
17
|
+
|
|
18
|
+
import { useSmoothKeyboardHandler } from "./useSmoothKeyboardHandler";
|
|
19
|
+
import { debounce } from "./utils";
|
|
20
|
+
|
|
21
|
+
import type { FC } from "react";
|
|
22
|
+
import type { ScrollViewProps } from "react-native";
|
|
23
|
+
import type { FocusedInputLayoutChangedEvent } from "react-native-keyboard-controller";
|
|
24
|
+
|
|
25
|
+
type KeyboardAwareScrollViewProps = {
|
|
26
|
+
bottomOffset?: number;
|
|
27
|
+
} & ScrollViewProps;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Everything begins from `onStart` handler. This handler is called every time,
|
|
31
|
+
* when keyboard changes its size or when focused `TextInput` was changed. In
|
|
32
|
+
* this handler we are calculating/memoizing values which later will be used
|
|
33
|
+
* during layout movement. For that we calculate:
|
|
34
|
+
* - layout of focused field (`layout`) - to understand whether there will be overlap
|
|
35
|
+
* - initial keyboard size (`initialKeyboardSize`) - used in scroll interpolation
|
|
36
|
+
* - future keyboard height (`keyboardHeight`) - used in scroll interpolation
|
|
37
|
+
* - current scroll position (`scrollPosition`) - used to scroll from this point
|
|
38
|
+
*
|
|
39
|
+
* Once we've calculated all necessary variables - we can actually start to use them.
|
|
40
|
+
* It happens in `onMove` handler - this function simply calls `maybeScroll` with
|
|
41
|
+
* current keyboard frame height. This functions makes the smooth transition.
|
|
42
|
+
*
|
|
43
|
+
* When the transition has finished we go to `onEnd` handler. In this handler
|
|
44
|
+
* we verify, that the current field is not overlapped within a keyboard frame.
|
|
45
|
+
* For full `onStart`/`onMove`/`onEnd` flow it may look like a redundant thing,
|
|
46
|
+
* however there could be some cases, when `onMove` is not called:
|
|
47
|
+
* - on iOS when TextInput was changed - keyboard transition is instant
|
|
48
|
+
* - on Android when TextInput was changed and keyboard size wasn't changed
|
|
49
|
+
* So `onEnd` handler handle the case, when `onMove` wasn't triggered.
|
|
50
|
+
*
|
|
51
|
+
* ====================================================================================================================+
|
|
52
|
+
* -----------------------------------------------------Flow chart-----------------------------------------------------+
|
|
53
|
+
* ====================================================================================================================+
|
|
54
|
+
*
|
|
55
|
+
* +============================+ +============================+ +==================================+
|
|
56
|
+
* + User Press on TextInput + => + Keyboard starts showing + => + As keyboard moves frame by frame + =>
|
|
57
|
+
* + + + (run `onStart`) + + `onMove` is getting called +
|
|
58
|
+
* +============================+ +============================+ +==================================+
|
|
59
|
+
*
|
|
60
|
+
*
|
|
61
|
+
* +============================+ +============================+ +=====================================+
|
|
62
|
+
* + Keyboard is shown and we + => + User moved focus to + => + Only `onStart`/`onEnd` maybe called +
|
|
63
|
+
* + call `onEnd` handler + + another `TextInput` + + (without involving `onMove`) +
|
|
64
|
+
* +============================+ +============================+ +=====================================+
|
|
65
|
+
*
|
|
66
|
+
*/
|
|
67
|
+
const KeyboardAwareScrollView: FC<KeyboardAwareScrollViewProps> = ({
|
|
68
|
+
children,
|
|
69
|
+
bottomOffset = 0,
|
|
70
|
+
...rest
|
|
71
|
+
}) => {
|
|
72
|
+
const scrollViewAnimatedRef = useAnimatedRef<Reanimated.ScrollView>();
|
|
73
|
+
const scrollPosition = useSharedValue(0);
|
|
74
|
+
const position = useSharedValue(0);
|
|
75
|
+
const keyboardHeight = useSharedValue(0);
|
|
76
|
+
const tag = useSharedValue(-1);
|
|
77
|
+
const initialKeyboardSize = useSharedValue(0);
|
|
78
|
+
const scrollBeforeKeyboardMovement = useSharedValue(0);
|
|
79
|
+
const { input } = useReanimatedFocusedInput();
|
|
80
|
+
const layout = useSharedValue<FocusedInputLayoutChangedEvent | null>(null);
|
|
81
|
+
|
|
82
|
+
const { height } = useWindowDimensions();
|
|
83
|
+
|
|
84
|
+
const onScroll = useAnimatedScrollHandler(
|
|
85
|
+
{
|
|
86
|
+
onScroll: (e) => {
|
|
87
|
+
position.value = e.contentOffset.y;
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
[],
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Function that will scroll a ScrollView as keyboard gets moving
|
|
95
|
+
*/
|
|
96
|
+
const maybeScroll = useCallback(
|
|
97
|
+
(e: number, animated: boolean = false) => {
|
|
98
|
+
"worklet";
|
|
99
|
+
|
|
100
|
+
const visibleRect = height - keyboardHeight.value;
|
|
101
|
+
const absoluteY = layout.value?.layout.absoluteY || 0;
|
|
102
|
+
const inputHeight = layout.value?.layout.height || 0;
|
|
103
|
+
const point = absoluteY + inputHeight;
|
|
104
|
+
|
|
105
|
+
if (visibleRect - point <= bottomOffset) {
|
|
106
|
+
const interpolatedScrollTo = interpolate(
|
|
107
|
+
e,
|
|
108
|
+
[initialKeyboardSize.value, keyboardHeight.value],
|
|
109
|
+
[0, keyboardHeight.value - (height - point) + bottomOffset],
|
|
110
|
+
);
|
|
111
|
+
const targetScrollY =
|
|
112
|
+
Math.max(interpolatedScrollTo, 0) + scrollPosition.value;
|
|
113
|
+
scrollTo(scrollViewAnimatedRef, 0, targetScrollY, animated);
|
|
114
|
+
|
|
115
|
+
return interpolatedScrollTo;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (absoluteY < 0) {
|
|
119
|
+
const positionOnScreen = visibleRect - inputHeight - bottomOffset;
|
|
120
|
+
const topOfScreen = scrollPosition.value + absoluteY;
|
|
121
|
+
|
|
122
|
+
scrollTo(
|
|
123
|
+
scrollViewAnimatedRef,
|
|
124
|
+
0,
|
|
125
|
+
topOfScreen - positionOnScreen,
|
|
126
|
+
animated,
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return 0;
|
|
131
|
+
},
|
|
132
|
+
[bottomOffset],
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
const onChangeText = useCallback(() => {
|
|
136
|
+
"worklet";
|
|
137
|
+
|
|
138
|
+
// if typing a text caused layout shift, then we need to ignore this handler
|
|
139
|
+
// because this event will be handled in `useAnimatedReaction` below
|
|
140
|
+
if (layout.value?.layout.height !== input.value?.layout.height) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const prevScrollPosition = scrollPosition.value;
|
|
145
|
+
const prevLayout = layout.value;
|
|
146
|
+
|
|
147
|
+
scrollPosition.value = position.value;
|
|
148
|
+
layout.value = input.value;
|
|
149
|
+
maybeScroll(keyboardHeight.value, true);
|
|
150
|
+
scrollPosition.value = prevScrollPosition;
|
|
151
|
+
layout.value = prevLayout;
|
|
152
|
+
}, [maybeScroll]);
|
|
153
|
+
const onChangeTextHandler = useMemo(
|
|
154
|
+
() => debounce(onChangeText, 200),
|
|
155
|
+
[onChangeText],
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
useFocusedInputHandler(
|
|
159
|
+
{
|
|
160
|
+
onChangeText: onChangeTextHandler,
|
|
161
|
+
},
|
|
162
|
+
[onChangeTextHandler],
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
useSmoothKeyboardHandler(
|
|
166
|
+
{
|
|
167
|
+
onStart: (e) => {
|
|
168
|
+
"worklet";
|
|
169
|
+
|
|
170
|
+
const keyboardWillChangeSize =
|
|
171
|
+
keyboardHeight.value !== e.height && e.height > 0;
|
|
172
|
+
const keyboardWillAppear = e.height > 0 && keyboardHeight.value === 0;
|
|
173
|
+
const keyboardWillHide = e.height === 0;
|
|
174
|
+
const focusWasChanged =
|
|
175
|
+
(tag.value !== e.target && e.target !== -1) || keyboardWillChangeSize;
|
|
176
|
+
|
|
177
|
+
if (keyboardWillChangeSize) {
|
|
178
|
+
initialKeyboardSize.value = keyboardHeight.value;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (keyboardWillHide) {
|
|
182
|
+
// on back transition need to interpolate as [0, keyboardHeight]
|
|
183
|
+
initialKeyboardSize.value = 0;
|
|
184
|
+
scrollPosition.value = scrollBeforeKeyboardMovement.value;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (keyboardWillAppear || keyboardWillChangeSize || focusWasChanged) {
|
|
188
|
+
// persist scroll value
|
|
189
|
+
scrollPosition.value = position.value;
|
|
190
|
+
// just persist height - later will be used in interpolation
|
|
191
|
+
keyboardHeight.value = e.height;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// focus was changed
|
|
195
|
+
if (focusWasChanged) {
|
|
196
|
+
tag.value = e.target;
|
|
197
|
+
|
|
198
|
+
// save position of focused text input when keyboard starts to move
|
|
199
|
+
layout.value = input.value;
|
|
200
|
+
// save current scroll position - when keyboard will hide we'll reuse
|
|
201
|
+
// this value to achieve smooth hide effect
|
|
202
|
+
scrollBeforeKeyboardMovement.value = position.value;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (focusWasChanged && !keyboardWillAppear) {
|
|
206
|
+
// update position on scroll value, so `onEnd` handler
|
|
207
|
+
// will pick up correct values
|
|
208
|
+
position.value += maybeScroll(e.height, true);
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
onMove: (e) => {
|
|
212
|
+
"worklet";
|
|
213
|
+
|
|
214
|
+
maybeScroll(e.height);
|
|
215
|
+
},
|
|
216
|
+
onEnd: (e) => {
|
|
217
|
+
"worklet";
|
|
218
|
+
|
|
219
|
+
keyboardHeight.value = e.height;
|
|
220
|
+
scrollPosition.value = position.value;
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
[height, maybeScroll],
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
useAnimatedReaction(
|
|
227
|
+
() => input.value,
|
|
228
|
+
(current, previous) => {
|
|
229
|
+
if (
|
|
230
|
+
current?.target === previous?.target &&
|
|
231
|
+
current?.layout.height !== previous?.layout.height
|
|
232
|
+
) {
|
|
233
|
+
const prevLayout = layout.value;
|
|
234
|
+
|
|
235
|
+
layout.value = input.value;
|
|
236
|
+
scrollPosition.value += maybeScroll(keyboardHeight.value, true);
|
|
237
|
+
layout.value = prevLayout;
|
|
238
|
+
}
|
|
239
|
+
},
|
|
240
|
+
[],
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
const view = useAnimatedStyle(
|
|
244
|
+
() => ({
|
|
245
|
+
paddingBottom: keyboardHeight.value,
|
|
246
|
+
}),
|
|
247
|
+
[],
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
return (
|
|
251
|
+
<Reanimated.ScrollView
|
|
252
|
+
ref={scrollViewAnimatedRef}
|
|
253
|
+
{...rest}
|
|
254
|
+
onScroll={onScroll}
|
|
255
|
+
scrollEventThrottle={16}
|
|
256
|
+
>
|
|
257
|
+
<Reanimated.View style={view}>{children}</Reanimated.View>
|
|
258
|
+
</Reanimated.ScrollView>
|
|
259
|
+
);
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
export default KeyboardAwareScrollView;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { Platform } from "react-native";
|
|
2
|
+
import {
|
|
3
|
+
Easing,
|
|
4
|
+
useAnimatedReaction,
|
|
5
|
+
useSharedValue,
|
|
6
|
+
withTiming,
|
|
7
|
+
} from "react-native-reanimated";
|
|
8
|
+
|
|
9
|
+
import { useKeyboardHandler } from "react-native-keyboard-controller";
|
|
10
|
+
|
|
11
|
+
const IS_ANDROID_ELEVEN_OR_HIGHER =
|
|
12
|
+
Platform.OS === "android" && Platform.Version >= 30;
|
|
13
|
+
// on these platforms keyboard transitions will be smooth
|
|
14
|
+
const IS_ANDROID_ELEVEN_OR_HIGHER_OR_IOS =
|
|
15
|
+
IS_ANDROID_ELEVEN_OR_HIGHER || Platform.OS === "ios";
|
|
16
|
+
// on Android Telegram is not using androidx.core values and uses custom interpolation
|
|
17
|
+
// duration is taken from here: https://github.com/DrKLO/Telegram/blob/e9a35cea54c06277c69d41b8e25d94b5d7ede065/TMessagesProj/src/main/java/org/telegram/ui/ActionBar/AdjustPanLayoutHelper.java#L39
|
|
18
|
+
// and bezier is taken from: https://github.com/DrKLO/Telegram/blob/e9a35cea54c06277c69d41b8e25d94b5d7ede065/TMessagesProj/src/main/java/androidx/recyclerview/widget/ChatListItemAnimator.java#L40
|
|
19
|
+
const TELEGRAM_ANDROID_TIMING_CONFIG = {
|
|
20
|
+
duration: 250,
|
|
21
|
+
easing: Easing.bezier(
|
|
22
|
+
0.19919472913616398,
|
|
23
|
+
0.010644531250000006,
|
|
24
|
+
0.27920937042459737,
|
|
25
|
+
0.91025390625,
|
|
26
|
+
),
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Hook that uses default transitions for iOS and Android > 11, and uses
|
|
31
|
+
* custom interpolation on Android < 11 to achieve more smooth animation
|
|
32
|
+
*/
|
|
33
|
+
export const useSmoothKeyboardHandler: typeof useKeyboardHandler = (
|
|
34
|
+
handler,
|
|
35
|
+
deps,
|
|
36
|
+
) => {
|
|
37
|
+
const target = useSharedValue(-1);
|
|
38
|
+
const persistedHeight = useSharedValue(0);
|
|
39
|
+
const animatedKeyboardHeight = useSharedValue(0);
|
|
40
|
+
|
|
41
|
+
useAnimatedReaction(
|
|
42
|
+
() => {
|
|
43
|
+
if (IS_ANDROID_ELEVEN_OR_HIGHER_OR_IOS) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const event = {
|
|
47
|
+
// it'll be always 250, since we're running animation via `withTiming` where
|
|
48
|
+
// duration in config (TELEGRAM_ANDROID_TIMING_CONFIG.duration) = 250ms
|
|
49
|
+
duration: 250,
|
|
50
|
+
target: target.value,
|
|
51
|
+
height: animatedKeyboardHeight.value,
|
|
52
|
+
progress: animatedKeyboardHeight.value / persistedHeight.value,
|
|
53
|
+
};
|
|
54
|
+
return event;
|
|
55
|
+
},
|
|
56
|
+
(evt) => {
|
|
57
|
+
if (!evt) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
handler.onMove?.(evt);
|
|
61
|
+
|
|
62
|
+
// dispatch `onEnd`
|
|
63
|
+
if (evt.height === persistedHeight.value) {
|
|
64
|
+
handler.onEnd?.(evt);
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
[handler],
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
useKeyboardHandler(
|
|
71
|
+
{
|
|
72
|
+
onStart: (e) => {
|
|
73
|
+
"worklet";
|
|
74
|
+
|
|
75
|
+
// immediately dispatch onStart/onEnd events if onStart dispatched with the same height
|
|
76
|
+
// and don't wait for animation 250ms
|
|
77
|
+
if (
|
|
78
|
+
!IS_ANDROID_ELEVEN_OR_HIGHER_OR_IOS &&
|
|
79
|
+
e.height === persistedHeight.value
|
|
80
|
+
) {
|
|
81
|
+
handler.onStart?.(e);
|
|
82
|
+
handler.onEnd?.(e);
|
|
83
|
+
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
target.value = e.target;
|
|
88
|
+
|
|
89
|
+
if (e.height > 0) {
|
|
90
|
+
persistedHeight.value = e.height;
|
|
91
|
+
}
|
|
92
|
+
// if we are running on Android < 9, then we are using custom interpolation
|
|
93
|
+
// to achieve smoother animation and use `animatedKeyboardHeight` as animation
|
|
94
|
+
// driver
|
|
95
|
+
if (!IS_ANDROID_ELEVEN_OR_HIGHER_OR_IOS) {
|
|
96
|
+
animatedKeyboardHeight.value = withTiming(
|
|
97
|
+
e.height,
|
|
98
|
+
TELEGRAM_ANDROID_TIMING_CONFIG,
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
handler.onStart?.(e);
|
|
103
|
+
},
|
|
104
|
+
onMove: (e) => {
|
|
105
|
+
"worklet";
|
|
106
|
+
|
|
107
|
+
if (IS_ANDROID_ELEVEN_OR_HIGHER_OR_IOS) {
|
|
108
|
+
handler.onMove?.(e);
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
onEnd: (e) => {
|
|
112
|
+
"worklet";
|
|
113
|
+
|
|
114
|
+
if (IS_ANDROID_ELEVEN_OR_HIGHER_OR_IOS) {
|
|
115
|
+
handler.onEnd?.(e);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
persistedHeight.value = e.height;
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
deps,
|
|
122
|
+
);
|
|
123
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export const debounce = <F extends (...args: Parameters<F>) => ReturnType<F>>(
|
|
2
|
+
worklet: F,
|
|
3
|
+
wait = 0,
|
|
4
|
+
) => {
|
|
5
|
+
"worklet";
|
|
6
|
+
|
|
7
|
+
const value = {
|
|
8
|
+
time: 0,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
return (...args: Parameters<F>): ReturnType<F> | void => {
|
|
12
|
+
"worklet";
|
|
13
|
+
|
|
14
|
+
const t = Date.now();
|
|
15
|
+
const now = t - value.time;
|
|
16
|
+
|
|
17
|
+
if (now < wait) {
|
|
18
|
+
value.time = t;
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
value.time = t;
|
|
23
|
+
|
|
24
|
+
return worklet(...args);
|
|
25
|
+
};
|
|
26
|
+
};
|
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
import React, { forwardRef, useMemo } from
|
|
2
|
-
import Reanimated, {
|
|
3
|
-
interpolate,
|
|
4
|
-
useAnimatedStyle,
|
|
5
|
-
} from 'react-native-reanimated';
|
|
1
|
+
import React, { forwardRef, useMemo } from "react";
|
|
2
|
+
import Reanimated, { useAnimatedStyle } from "react-native-reanimated";
|
|
6
3
|
|
|
7
|
-
import { useReanimatedKeyboardAnimation } from
|
|
4
|
+
import { useReanimatedKeyboardAnimation } from "../../hooks";
|
|
5
|
+
import useKeyboardInterpolation from "../hooks/useKeyboardInterpolation";
|
|
8
6
|
|
|
9
|
-
import type { View, ViewProps } from
|
|
7
|
+
import type { View, ViewProps } from "react-native";
|
|
10
8
|
|
|
11
9
|
type KeyboardStickyViewProps = {
|
|
12
10
|
/**
|
|
@@ -30,12 +28,13 @@ const KeyboardStickyView = forwardRef<
|
|
|
30
28
|
>(
|
|
31
29
|
(
|
|
32
30
|
{ children, offset: { closed = 0, opened = 0 } = {}, style, ...props },
|
|
33
|
-
ref
|
|
31
|
+
ref,
|
|
34
32
|
) => {
|
|
35
|
-
const { height
|
|
33
|
+
const { height } = useReanimatedKeyboardAnimation();
|
|
34
|
+
const { interpolate } = useKeyboardInterpolation();
|
|
36
35
|
|
|
37
36
|
const stickyViewStyle = useAnimatedStyle(() => {
|
|
38
|
-
const offset = interpolate(
|
|
37
|
+
const offset = interpolate(-height.value, [closed, opened]);
|
|
39
38
|
|
|
40
39
|
return {
|
|
41
40
|
transform: [{ translateY: height.value + offset }],
|
|
@@ -44,20 +43,15 @@ const KeyboardStickyView = forwardRef<
|
|
|
44
43
|
|
|
45
44
|
const styles = useMemo(
|
|
46
45
|
() => [style, stickyViewStyle],
|
|
47
|
-
[style, stickyViewStyle]
|
|
46
|
+
[style, stickyViewStyle],
|
|
48
47
|
);
|
|
49
48
|
|
|
50
49
|
return (
|
|
51
|
-
<Reanimated.View
|
|
52
|
-
// @ts-expect-error because `ref` from reanimated is not compatible with react-native
|
|
53
|
-
ref={ref}
|
|
54
|
-
style={styles}
|
|
55
|
-
{...props}
|
|
56
|
-
>
|
|
50
|
+
<Reanimated.View ref={ref} style={styles} {...props}>
|
|
57
51
|
{children}
|
|
58
52
|
</Reanimated.View>
|
|
59
53
|
);
|
|
60
|
-
}
|
|
54
|
+
},
|
|
61
55
|
);
|
|
62
56
|
|
|
63
57
|
export default KeyboardStickyView;
|