react-native-keyboard-controller 1.9.5 → 1.10.0
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/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 +2 -0
- package/lib/commonjs/animated.js +20 -14
- 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/components/KeyboardAvoidingView/index.js +7 -4
- 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 +0 -1
- package/lib/commonjs/components/KeyboardStickyView/index.js.map +1 -1
- package/lib/commonjs/components/index.js +7 -0
- package/lib/commonjs/components/index.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 +2 -5
- package/lib/commonjs/internal.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 +20 -3
- package/lib/commonjs/reanimated.native.js.map +1 -1
- package/lib/commonjs/replicas.js +3 -1
- package/lib/commonjs/replicas.js.map +1 -1
- package/lib/commonjs/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
- package/lib/commonjs/specs/NativeKeyboardController.js.map +1 -1
- package/lib/commonjs/types.js.map +1 -1
- package/lib/module/animated.js +21 -15
- package/lib/module/animated.js.map +1 -1
- package/lib/module/bindings.js +1 -0
- package/lib/module/bindings.js.map +1 -1
- package/lib/module/components/KeyboardAvoidingView/index.js +8 -5
- 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 +0 -1
- package/lib/module/components/KeyboardStickyView/index.js.map +1 -1
- package/lib/module/components/index.js +1 -0
- package/lib/module/components/index.js.map +1 -1
- package/lib/module/context.js +2 -1
- package/lib/module/context.js.map +1 -1
- package/lib/module/hooks.js +16 -2
- package/lib/module/hooks.js.map +1 -1
- package/lib/module/index.js +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/internal.js +2 -5
- package/lib/module/internal.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 +17 -1
- package/lib/module/reanimated.native.js.map +1 -1
- package/lib/module/replicas.js +5 -3
- package/lib/module/replicas.js.map +1 -1
- package/lib/module/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
- package/lib/module/specs/NativeKeyboardController.js.map +1 -1
- package/lib/module/types.js.map +1 -1
- 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/index.d.ts +1 -0
- package/lib/typescript/context.d.ts +3 -2
- package/lib/typescript/hooks.d.ts +2 -1
- package/lib/typescript/index.d.ts +1 -1
- package/lib/typescript/internal.d.ts +8 -5
- package/lib/typescript/reanimated.d.ts +3 -2
- package/lib/typescript/reanimated.native.d.ts +3 -2
- package/lib/typescript/specs/KeyboardControllerViewNativeComponent.d.ts +4 -0
- package/lib/typescript/specs/NativeKeyboardController.d.ts +1 -0
- package/lib/typescript/types.d.ts +15 -2
- package/package.json +5 -2
- package/react-native-keyboard-controller.podspec +1 -1
- package/src/animated.tsx +27 -10
- package/src/bindings.ts +1 -0
- package/src/components/KeyboardAvoidingView/index.tsx +7 -5
- 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 +1 -6
- package/src/components/index.ts +1 -0
- package/src/context.ts +9 -3
- package/src/hooks.ts +20 -3
- package/src/index.ts +5 -1
- package/src/internal.ts +17 -8
- package/src/reanimated.native.ts +27 -2
- package/src/reanimated.ts +8 -2
- package/src/replicas.ts +4 -3
- package/src/specs/KeyboardControllerViewNativeComponent.ts +7 -0
- package/src/specs/NativeKeyboardController.ts +1 -0
- package/src/types.ts +28 -2
|
@@ -16,20 +16,25 @@ export type FocusedInputLayoutChangedEvent = {
|
|
|
16
16
|
absoluteY: number;
|
|
17
17
|
};
|
|
18
18
|
};
|
|
19
|
+
export type FocusedInputTextChangedEvent = {
|
|
20
|
+
text: string;
|
|
21
|
+
};
|
|
19
22
|
export type EventWithName<T> = {
|
|
20
23
|
eventName: string;
|
|
21
24
|
} & T;
|
|
22
25
|
export type KeyboardControllerProps = {
|
|
23
|
-
enabled?: boolean;
|
|
24
26
|
onKeyboardMoveStart?: (e: NativeSyntheticEvent<EventWithName<NativeEvent>>) => void;
|
|
25
27
|
onKeyboardMove?: (e: NativeSyntheticEvent<EventWithName<NativeEvent>>) => void;
|
|
26
28
|
onKeyboardMoveEnd?: (e: NativeSyntheticEvent<EventWithName<NativeEvent>>) => void;
|
|
27
29
|
onKeyboardMoveInteractive?: (e: NativeSyntheticEvent<EventWithName<NativeEvent>>) => void;
|
|
28
30
|
onFocusedInputLayoutChanged?: (e: NativeSyntheticEvent<EventWithName<FocusedInputLayoutChangedEvent>>) => void;
|
|
31
|
+
onFocusedInputTextChanged?: (e: NativeSyntheticEvent<EventWithName<FocusedInputTextChangedEvent>>) => void;
|
|
29
32
|
onKeyboardMoveReanimated?: (e: NativeSyntheticEvent<EventWithName<NativeEvent>>) => void;
|
|
30
33
|
onFocusedInputLayoutChangedReanimated?: (e: NativeSyntheticEvent<EventWithName<FocusedInputLayoutChangedEvent>>) => void;
|
|
34
|
+
onFocusedInputTextChangedReanimated?: (e: NativeSyntheticEvent<EventWithName<FocusedInputTextChangedEvent>>) => void;
|
|
31
35
|
statusBarTranslucent?: boolean;
|
|
32
36
|
navigationBarTranslucent?: boolean;
|
|
37
|
+
enabled?: boolean;
|
|
33
38
|
} & ViewProps;
|
|
34
39
|
export type KeyboardGestureAreaProps = {
|
|
35
40
|
interpolator: 'ios' | 'linear';
|
|
@@ -48,6 +53,7 @@ export type KeyboardGestureAreaProps = {
|
|
|
48
53
|
export type KeyboardControllerModule = {
|
|
49
54
|
setDefaultMode: () => void;
|
|
50
55
|
setInputMode: (mode: number) => void;
|
|
56
|
+
dismiss: () => void;
|
|
51
57
|
addListener: (eventName: string) => void;
|
|
52
58
|
removeListeners: (count: number) => void;
|
|
53
59
|
};
|
|
@@ -67,9 +73,12 @@ export type KeyboardHandlerHook<TContext, Event> = (handlers: {
|
|
|
67
73
|
onKeyboardMoveEnd?: (e: NativeEvent, context: TContext) => void;
|
|
68
74
|
onKeyboardMoveInteractive?: (e: NativeEvent, context: TContext) => void;
|
|
69
75
|
}, dependencies?: unknown[]) => (e: NativeSyntheticEvent<Event>) => void;
|
|
70
|
-
export type
|
|
76
|
+
export type FocusedInputLayoutHandlerHook<TContext, Event> = (handlers: {
|
|
71
77
|
onFocusedInputLayoutChanged?: (e: FocusedInputLayoutChangedEvent, context: TContext) => void;
|
|
72
78
|
}, dependencies?: unknown[]) => (e: NativeSyntheticEvent<Event>) => void;
|
|
79
|
+
export type FocusedInputTextHandlerHook<TContext, Event> = (handlers: {
|
|
80
|
+
onFocusedInputTextChanged?: (e: FocusedInputTextChangedEvent, context: TContext) => void;
|
|
81
|
+
}, dependencies?: unknown[]) => (e: NativeSyntheticEvent<Event>) => void;
|
|
73
82
|
export type Handlers<T> = Record<string, T | undefined>;
|
|
74
83
|
export type KeyboardHandler = Partial<{
|
|
75
84
|
onStart: (e: NativeEvent) => void;
|
|
@@ -78,3 +87,7 @@ export type KeyboardHandler = Partial<{
|
|
|
78
87
|
onInteractive: (e: NativeEvent) => void;
|
|
79
88
|
}>;
|
|
80
89
|
export type KeyboardHandlers = Handlers<KeyboardHandler>;
|
|
90
|
+
export type FocusedInputHandler = Partial<{
|
|
91
|
+
onChangeText: (e: FocusedInputTextChangedEvent) => void;
|
|
92
|
+
}>;
|
|
93
|
+
export type FocusedInputHandlers = Handlers<FocusedInputHandler>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-keyboard-controller",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.0",
|
|
4
4
|
"description": "Keyboard manager which works in identical way on both iOS and Android",
|
|
5
5
|
"main": "lib/commonjs/index",
|
|
6
6
|
"module": "lib/module/index",
|
|
@@ -41,9 +41,12 @@
|
|
|
41
41
|
"react-native",
|
|
42
42
|
"keyboard",
|
|
43
43
|
"animation",
|
|
44
|
+
"dismiss",
|
|
44
45
|
"focused input",
|
|
46
|
+
"text changed",
|
|
45
47
|
"avoiding view",
|
|
46
48
|
"sticky view",
|
|
49
|
+
"keyboard aware scroll view",
|
|
47
50
|
"ios",
|
|
48
51
|
"android"
|
|
49
52
|
],
|
|
@@ -80,7 +83,7 @@
|
|
|
80
83
|
"react": "18.2.0",
|
|
81
84
|
"react-native": "0.72.4",
|
|
82
85
|
"react-native-builder-bob": "^0.18.0",
|
|
83
|
-
"react-native-reanimated": "3.
|
|
86
|
+
"react-native-reanimated": "3.6.1",
|
|
84
87
|
"release-it": "^14.2.2",
|
|
85
88
|
"typescript": "^4.8.4"
|
|
86
89
|
},
|
|
@@ -24,7 +24,7 @@ Pod::Spec.new do |s|
|
|
|
24
24
|
if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
|
|
25
25
|
s.compiler_flags = folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1"
|
|
26
26
|
s.pod_target_xcconfig = {
|
|
27
|
-
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"",
|
|
27
|
+
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\" \"${PODS_ROOT}/Headers/Private/Yoga\"",
|
|
28
28
|
"OTHER_CPLUSPLUSFLAGS" => "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1",
|
|
29
29
|
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17",
|
|
30
30
|
# This is handy when we want to detect if new arch is enabled in Swift code
|
package/src/animated.tsx
CHANGED
|
@@ -8,11 +8,13 @@ import { useAnimatedValue, useSharedHandlers } from './internal';
|
|
|
8
8
|
import { applyMonkeyPatch, revertMonkeyPatch } from './monkey-patch';
|
|
9
9
|
import {
|
|
10
10
|
useAnimatedKeyboardHandler,
|
|
11
|
-
|
|
11
|
+
useFocusedInputLayoutHandler,
|
|
12
|
+
useFocusedInputTextHandler,
|
|
12
13
|
} from './reanimated';
|
|
13
14
|
|
|
14
15
|
import type { KeyboardAnimationContext } from './context';
|
|
15
16
|
import type {
|
|
17
|
+
FocusedInputHandler,
|
|
16
18
|
FocusedInputLayoutChangedEvent,
|
|
17
19
|
KeyboardControllerProps,
|
|
18
20
|
KeyboardHandler,
|
|
@@ -76,6 +78,7 @@ export const KeyboardProvider = ({
|
|
|
76
78
|
navigationBarTranslucent,
|
|
77
79
|
enabled: initiallyEnabled = true,
|
|
78
80
|
}: KeyboardProviderProps) => {
|
|
81
|
+
// state
|
|
79
82
|
const [enabled, setEnabled] = useState(initiallyEnabled);
|
|
80
83
|
// animated values
|
|
81
84
|
const progress = useAnimatedValue(0);
|
|
@@ -84,7 +87,10 @@ export const KeyboardProvider = ({
|
|
|
84
87
|
const progressSV = useSharedValue(0);
|
|
85
88
|
const heightSV = useSharedValue(0);
|
|
86
89
|
const layout = useSharedValue<FocusedInputLayoutChangedEvent | null>(null);
|
|
87
|
-
const
|
|
90
|
+
const [setKeyboardHandlers, broadcastKeyboardEvents] =
|
|
91
|
+
useSharedHandlers<KeyboardHandler>();
|
|
92
|
+
const [setInputHandlers, broadcastInputEvents] =
|
|
93
|
+
useSharedHandlers<FocusedInputHandler>();
|
|
88
94
|
// memo
|
|
89
95
|
const context = useMemo<KeyboardAnimationContext>(
|
|
90
96
|
() => ({
|
|
@@ -92,7 +98,8 @@ export const KeyboardProvider = ({
|
|
|
92
98
|
animated: { progress: progress, height: Animated.multiply(height, -1) },
|
|
93
99
|
reanimated: { progress: progressSV, height: heightSV },
|
|
94
100
|
layout,
|
|
95
|
-
|
|
101
|
+
setKeyboardHandlers,
|
|
102
|
+
setInputHandlers,
|
|
96
103
|
setEnabled,
|
|
97
104
|
}),
|
|
98
105
|
[enabled]
|
|
@@ -133,30 +140,30 @@ export const KeyboardProvider = ({
|
|
|
133
140
|
onKeyboardMoveStart: (event: NativeEvent) => {
|
|
134
141
|
'worklet';
|
|
135
142
|
|
|
136
|
-
|
|
143
|
+
broadcastKeyboardEvents('onStart', event);
|
|
137
144
|
updateSharedValues(event, ['ios']);
|
|
138
145
|
},
|
|
139
146
|
onKeyboardMove: (event: NativeEvent) => {
|
|
140
147
|
'worklet';
|
|
141
148
|
|
|
142
|
-
|
|
149
|
+
broadcastKeyboardEvents('onMove', event);
|
|
143
150
|
updateSharedValues(event, ['android']);
|
|
144
151
|
},
|
|
145
152
|
onKeyboardMoveEnd: (event: NativeEvent) => {
|
|
146
153
|
'worklet';
|
|
147
154
|
|
|
148
|
-
|
|
155
|
+
broadcastKeyboardEvents('onEnd', event);
|
|
149
156
|
},
|
|
150
157
|
onKeyboardMoveInteractive: (event: NativeEvent) => {
|
|
151
158
|
'worklet';
|
|
152
159
|
|
|
153
160
|
updateSharedValues(event, ['android', 'ios']);
|
|
154
|
-
|
|
161
|
+
broadcastKeyboardEvents('onInteractive', event);
|
|
155
162
|
},
|
|
156
163
|
},
|
|
157
164
|
[]
|
|
158
165
|
);
|
|
159
|
-
const
|
|
166
|
+
const inputLayoutHandler = useFocusedInputLayoutHandler(
|
|
160
167
|
{
|
|
161
168
|
onFocusedInputLayoutChanged: (e) => {
|
|
162
169
|
'worklet';
|
|
@@ -170,6 +177,16 @@ export const KeyboardProvider = ({
|
|
|
170
177
|
},
|
|
171
178
|
[]
|
|
172
179
|
);
|
|
180
|
+
const inputTextHandler = useFocusedInputTextHandler(
|
|
181
|
+
{
|
|
182
|
+
onFocusedInputTextChanged: (e) => {
|
|
183
|
+
'worklet';
|
|
184
|
+
|
|
185
|
+
broadcastInputEvents('onChangeText', e);
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
[]
|
|
189
|
+
);
|
|
173
190
|
// effects
|
|
174
191
|
useEffect(() => {
|
|
175
192
|
if (enabled) {
|
|
@@ -187,10 +204,10 @@ export const KeyboardProvider = ({
|
|
|
187
204
|
onKeyboardMoveStart={Platform.OS === 'ios' ? onKeyboardMove : undefined}
|
|
188
205
|
onKeyboardMove={Platform.OS === 'android' ? onKeyboardMove : undefined}
|
|
189
206
|
onKeyboardMoveInteractive={onKeyboardMove}
|
|
190
|
-
onFocusedInputLayoutChangedReanimated={
|
|
207
|
+
onFocusedInputLayoutChangedReanimated={inputLayoutHandler}
|
|
208
|
+
onFocusedInputTextChangedReanimated={inputTextHandler}
|
|
191
209
|
navigationBarTranslucent={navigationBarTranslucent}
|
|
192
210
|
statusBarTranslucent={statusBarTranslucent}
|
|
193
|
-
// @ts-expect-error https://github.com/software-mansion/react-native-reanimated/pull/4923
|
|
194
211
|
style={styles.container}
|
|
195
212
|
>
|
|
196
213
|
{children}
|
package/src/bindings.ts
CHANGED
|
@@ -6,7 +6,6 @@ import Reanimated, {
|
|
|
6
6
|
useAnimatedStyle,
|
|
7
7
|
useDerivedValue,
|
|
8
8
|
useSharedValue,
|
|
9
|
-
useWorkletCallback,
|
|
10
9
|
} from 'react-native-reanimated';
|
|
11
10
|
|
|
12
11
|
import { useKeyboardAnimation } from './hooks';
|
|
@@ -68,18 +67,22 @@ const KeyboardAvoidingView = forwardRef<View, React.PropsWithChildren<Props>>(
|
|
|
68
67
|
const keyboard = useKeyboardAnimation();
|
|
69
68
|
const { height: screenHeight } = useWindowDimensions();
|
|
70
69
|
|
|
71
|
-
const relativeKeyboardHeight =
|
|
70
|
+
const relativeKeyboardHeight = useCallback(() => {
|
|
71
|
+
'worklet';
|
|
72
|
+
|
|
72
73
|
const keyboardY =
|
|
73
74
|
screenHeight - keyboard.heightWhenOpened.value - keyboardVerticalOffset;
|
|
74
75
|
|
|
75
76
|
return Math.max(frame.value.y + frame.value.height - keyboardY, 0);
|
|
76
77
|
}, [screenHeight, keyboardVerticalOffset]);
|
|
77
78
|
|
|
78
|
-
const onLayoutWorklet =
|
|
79
|
+
const onLayoutWorklet = useCallback((layout: LayoutRectangle) => {
|
|
80
|
+
'worklet';
|
|
81
|
+
|
|
79
82
|
if (keyboard.isClosed.value) {
|
|
80
83
|
initialFrame.value = layout;
|
|
81
84
|
}
|
|
82
|
-
});
|
|
85
|
+
}, []);
|
|
83
86
|
const onLayout = useCallback<NonNullable<ViewProps['onLayout']>>(
|
|
84
87
|
(e) => {
|
|
85
88
|
runOnUI(onLayoutWorklet)(e.nativeEvent.layout);
|
|
@@ -134,7 +137,6 @@ const KeyboardAvoidingView = forwardRef<View, React.PropsWithChildren<Props>>(
|
|
|
134
137
|
|
|
135
138
|
return (
|
|
136
139
|
<Reanimated.View
|
|
137
|
-
// @ts-expect-error because `ref` from reanimated is not compatible with react-native
|
|
138
140
|
ref={ref}
|
|
139
141
|
onLayout={onLayout}
|
|
140
142
|
style={combinedStyles}
|
|
@@ -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
|
+
};
|
|
@@ -48,12 +48,7 @@ const KeyboardStickyView = forwardRef<
|
|
|
48
48
|
);
|
|
49
49
|
|
|
50
50
|
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
|
-
>
|
|
51
|
+
<Reanimated.View ref={ref} style={styles} {...props}>
|
|
57
52
|
{children}
|
|
58
53
|
</Reanimated.View>
|
|
59
54
|
);
|
package/src/components/index.ts
CHANGED