react-native-keyboard-controller 1.21.0-beta.1 → 1.21.0-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/ios/animations/KeyboardAnimation.swift +2 -2
- package/ios/animations/SpringAnimation.swift +1 -1
- package/ios/animations/TimingAnimation.swift +3 -1
- package/ios/delegates/KCTextInputCompositeDelegate.swift +2 -2
- package/ios/extensions/CGFloat.swift +3 -1
- package/ios/interactive/KeyboardAreaExtender.swift +1 -2
- package/ios/observers/FocusedInputObserver.swift +2 -4
- package/ios/observers/movement/KeyboardTrackingView.swift +1 -4
- package/ios/observers/movement/observer/KeyboardMovementObserver+Interactive.swift +0 -2
- package/ios/observers/movement/observer/KeyboardMovementObserver+Listeners.swift +7 -30
- package/ios/observers/movement/observer/KeyboardMovementObserver.swift +48 -2
- package/ios/protocols/TextInput.swift +1 -1
- package/ios/swizzling/UIResponderSwizzle.swift +4 -2
- package/ios/traversal/FocusedInputHolder.swift +3 -3
- package/ios/traversal/KeyboardView.swift +1 -1
- package/ios/traversal/ViewHierarchyNavigator.swift +2 -2
- package/ios/views/KeyboardControllerViewManager.swift +5 -5
- package/jest/index.js +3 -0
- package/lib/commonjs/components/ChatKit/TODO.md +20 -0
- package/lib/commonjs/components/ChatKit/hooks.js +13 -0
- package/lib/commonjs/components/ChatKit/hooks.js.map +1 -0
- package/lib/commonjs/components/ChatKit/index.js +55 -0
- package/lib/commonjs/components/ChatKit/index.js.map +1 -0
- package/lib/commonjs/components/ChatKit/types.js +6 -0
- package/lib/commonjs/components/ChatKit/types.js.map +1 -0
- package/lib/commonjs/components/ChatKit/useChatKeyboard/helpers.js +101 -0
- package/lib/commonjs/components/ChatKit/useChatKeyboard/helpers.js.map +1 -0
- package/lib/commonjs/components/ChatKit/useChatKeyboard/index.js +147 -0
- package/lib/commonjs/components/ChatKit/useChatKeyboard/index.js.map +1 -0
- package/lib/commonjs/components/KeyboardAwareScrollView/index.js +7 -36
- package/lib/commonjs/components/KeyboardAwareScrollView/index.js.map +1 -1
- package/lib/commonjs/components/KeyboardStickyView/index.js +7 -5
- package/lib/commonjs/components/KeyboardStickyView/index.js.map +1 -1
- package/lib/commonjs/components/ScrollViewWithBottomPadding/index.js +38 -14
- package/lib/commonjs/components/ScrollViewWithBottomPadding/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/index.js +7 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/components/ChatKit/TODO.md +20 -0
- package/lib/module/components/ChatKit/hooks.js +2 -0
- package/lib/module/components/ChatKit/hooks.js.map +1 -0
- package/lib/module/components/ChatKit/index.js +48 -0
- package/lib/module/components/ChatKit/index.js.map +1 -0
- package/lib/module/components/ChatKit/types.js +2 -0
- package/lib/module/components/ChatKit/types.js.map +1 -0
- package/lib/module/components/ChatKit/useChatKeyboard/helpers.js +92 -0
- package/lib/module/components/ChatKit/useChatKeyboard/helpers.js.map +1 -0
- package/lib/module/components/ChatKit/useChatKeyboard/index.js +141 -0
- package/lib/module/components/ChatKit/useChatKeyboard/index.js.map +1 -0
- package/lib/module/components/KeyboardAwareScrollView/index.js +7 -36
- package/lib/module/components/KeyboardAwareScrollView/index.js.map +1 -1
- package/lib/module/components/KeyboardStickyView/index.js +7 -5
- package/lib/module/components/KeyboardStickyView/index.js.map +1 -1
- package/lib/module/components/ScrollViewWithBottomPadding/index.js +38 -14
- package/lib/module/components/ScrollViewWithBottomPadding/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/index.js +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/components/ChatKit/hooks.d.ts +2 -0
- package/lib/typescript/components/ChatKit/index.d.ts +14 -0
- package/lib/typescript/components/ChatKit/types.d.ts +49 -0
- package/lib/typescript/components/ChatKit/useChatKeyboard/helpers.d.ts +57 -0
- package/lib/typescript/components/ChatKit/useChatKeyboard/index.d.ts +36 -0
- package/lib/typescript/components/ScrollViewWithBottomPadding/index.d.ts +6 -1
- package/lib/typescript/components/index.d.ts +2 -0
- package/lib/typescript/index.d.ts +2 -2
- package/package.json +12 -4
- package/src/components/ChatKit/TODO.md +20 -0
- package/src/components/ChatKit/hooks.ts +2 -0
- package/src/components/ChatKit/index.tsx +63 -0
- package/src/components/ChatKit/types.ts +50 -0
- package/src/components/ChatKit/useChatKeyboard/helpers.ts +118 -0
- package/src/components/ChatKit/useChatKeyboard/index.ts +228 -0
- package/src/components/KeyboardAwareScrollView/index.tsx +7 -43
- package/src/components/KeyboardStickyView/index.tsx +4 -5
- package/src/components/ScrollViewWithBottomPadding/index.tsx +66 -17
- package/src/components/index.ts +2 -0
- package/src/index.ts +2 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Findings
|
|
2
|
+
|
|
3
|
+
1. We can not translate whole `ScrollView` because interactive dismissal will have double scroll if we move it by `translateY` or content will be truncated if we don't do it (same with external container).
|
|
4
|
+
2. So we need to move only content inside `ScrollView`.
|
|
5
|
+
3. If we use a separate `scrollTo` we still may have a random jump. The only one way is to use `contentOffset` + `padding` on `ScrollView`.
|
|
6
|
+
4. Input must be always in `KeyboardStickyView` because it's always pushing above the keyboard (unlike content, which may stay in place).
|
|
7
|
+
5. Changing `contentOffset` on `ScrollView` will not work. On iOS it works, but on Android sometimes it doesn't scroll to correct position (when you scroll list to the end) and on paper architecture it is flickering.
|
|
8
|
+
6. Next idea is to use `contentInset` on iOS and create custom `ClippingScrollView` on Android (with polyfill `contentInsetBottom` prop).
|
|
9
|
+
|
|
10
|
+
## To Do
|
|
11
|
+
|
|
12
|
+
- [x] check how `ClippingScrollView` works in `KeyboardAwareScrollView` on Android (seems to be working well)
|
|
13
|
+
- [x] introduce `useCombinedRef` hook in separate PR
|
|
14
|
+
- [x] make sure, that style property doesn't affect the behavior with/without `ClippingScrollView` (test on a ScrollView that doesn't tke full screen?) <-- tested by comparing iOS with Android + `ClippingScrollView` (`contentInsetBottom` never override `paddingBottom` from `style`/`contentContainerStyle` even if ScrollView has `maxHeight=150` and we have only 2 inputs limitation + also tested `ScrollView` without height restrictions, in this case `paddingBottom` on `style` is not getting applied (it doesn't have an effect on both iOS/Android), but `contentContainerStyle` + `paddingBottom` works as expected and if I add `contentInsetBottom` to `ClippingScrollView` it doesn't overwrite a padding and increases scrollable area)
|
|
15
|
+
- [x] `ClippingScrollView` + `KeyboardAwareScrollView` check by e2e tests (seems to be working, though on Android emulator + e2e tests sometimes when keyboard disappear we still have keyboard space)
|
|
16
|
+
- [x] introduce `ClippingScrollView` in separate PR
|
|
17
|
+
- [x] create a polyfill-version of ScrollView `that` can add scrollable padding in the bottom of the content? How to wrap custom `ScrollView` with it (cause we may pass one from react-native-gesture-handler)? Should just adjust `contentInset` (scroll management should belong to `KeyboardAwareScrollView`) (ScrollViewWithKeyboardPadding/ScrollViewWithKeyboardSpace)
|
|
18
|
+
- [x] use `ClippingScrollView` in `KeyboardAwareScrollView` in separate PR
|
|
19
|
+
- [-] introduce `ChatKit`
|
|
20
|
+
- [] create issue in reanimated repo about `scrollTo` performance regression
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import React, { forwardRef } from "react";
|
|
2
|
+
import { useAnimatedRef, useAnimatedStyle } from "react-native-reanimated";
|
|
3
|
+
import Reanimated from "react-native-reanimated";
|
|
4
|
+
|
|
5
|
+
import useCombinedRef from "../hooks/useCombinedRef";
|
|
6
|
+
import ScrollViewWithBottomPadding from "../ScrollViewWithBottomPadding";
|
|
7
|
+
|
|
8
|
+
import { useChatKeyboard } from "./useChatKeyboard";
|
|
9
|
+
|
|
10
|
+
import type { ChatKitScrollViewProps } from "./types";
|
|
11
|
+
|
|
12
|
+
const ChatScrollView = forwardRef<
|
|
13
|
+
Reanimated.ScrollView,
|
|
14
|
+
React.PropsWithChildren<ChatKitScrollViewProps>
|
|
15
|
+
>(
|
|
16
|
+
(
|
|
17
|
+
{
|
|
18
|
+
children,
|
|
19
|
+
ScrollViewComponent = Reanimated.ScrollView,
|
|
20
|
+
inverted = false,
|
|
21
|
+
keyboardLiftBehavior = "always",
|
|
22
|
+
freeze = false,
|
|
23
|
+
offset = 0,
|
|
24
|
+
...rest
|
|
25
|
+
},
|
|
26
|
+
ref,
|
|
27
|
+
) => {
|
|
28
|
+
const scrollViewRef = useAnimatedRef<Reanimated.ScrollView>();
|
|
29
|
+
const onRef = useCombinedRef(ref, scrollViewRef);
|
|
30
|
+
|
|
31
|
+
const { padding, contentOffsetY, containerTranslateY } = useChatKeyboard(
|
|
32
|
+
scrollViewRef,
|
|
33
|
+
{ inverted, keyboardLiftBehavior, freeze, offset },
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const containerStyle = useAnimatedStyle(
|
|
37
|
+
() => ({
|
|
38
|
+
transform: [{ translateY: containerTranslateY.value }],
|
|
39
|
+
}),
|
|
40
|
+
[],
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
return (
|
|
44
|
+
<ScrollViewWithBottomPadding
|
|
45
|
+
ref={onRef}
|
|
46
|
+
{...rest}
|
|
47
|
+
bottomPadding={padding}
|
|
48
|
+
containerStyle={containerStyle}
|
|
49
|
+
contentOffsetY={contentOffsetY}
|
|
50
|
+
inverted={inverted}
|
|
51
|
+
ScrollViewComponent={ScrollViewComponent}
|
|
52
|
+
>
|
|
53
|
+
{children}
|
|
54
|
+
</ScrollViewWithBottomPadding>
|
|
55
|
+
);
|
|
56
|
+
},
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const ChatKit = {
|
|
60
|
+
ScrollView: ChatScrollView,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export default ChatKit;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { AnimatedScrollViewComponent } from "../ScrollViewWithBottomPadding";
|
|
2
|
+
import type { ScrollViewProps } from "react-native";
|
|
3
|
+
|
|
4
|
+
type KeyboardLiftBehavior = "always" | "whenAtEnd" | "persistent" | "never";
|
|
5
|
+
|
|
6
|
+
export type ChatKitScrollViewProps = {
|
|
7
|
+
/** Custom component for `ScrollView`. Default is `ScrollView`. */
|
|
8
|
+
ScrollViewComponent: AnimatedScrollViewComponent;
|
|
9
|
+
/** Whether list are using `inverted` prop. Default is `false`. */
|
|
10
|
+
inverted?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* The distance (in pixels) between the bottom of the screen and the `ScrollView`.
|
|
13
|
+
* When the keyboard appears, the scroll view will only push content by the effective
|
|
14
|
+
* distance (`keyboardHeight - offset`) instead of the full keyboard height.
|
|
15
|
+
*
|
|
16
|
+
* Useful when the input is not at the very bottom of the screen (e.g., above safe area, above
|
|
17
|
+
* bottom tabs, etc. - in this case offset should be equal to the height of the elements between
|
|
18
|
+
* `ScrollView` and bottom of the screen).
|
|
19
|
+
*
|
|
20
|
+
* Default is `0`.
|
|
21
|
+
*/
|
|
22
|
+
offset?: number;
|
|
23
|
+
/**
|
|
24
|
+
* Determines how the chat content should behave when the keyboard appears, specifically whether
|
|
25
|
+
* the scroll view should automatically lift its content to keep it visible above the keyboard.
|
|
26
|
+
*
|
|
27
|
+
* Possible values:
|
|
28
|
+
* - `'always'`: The content always lifts along with the keyboard, ensuring the messages from the bottom part of screen
|
|
29
|
+
* remain visible regardless of the current scroll position. This is the default behavior for most chat applications (used in Telegram).
|
|
30
|
+
* - `'whenAtEnd'`: The content lifts only if the scroll view is at the end (i.e., the last message
|
|
31
|
+
* is visible or near the bottom). This prevents unnecessary adjustments when the user is scrolling
|
|
32
|
+
* through older messages (ChatGPT mobile app behavior).
|
|
33
|
+
* - `'persistent'`: The content always lifts when the keyboard appears (similar to `'always'`), but
|
|
34
|
+
* does not reset (lower) when the keyboard hides. This mimics behaviors where the view remains adjusted
|
|
35
|
+
* after keyboard dismissal to maintain focus on the latest content without shifting back (Claude mobile app behavior).
|
|
36
|
+
* - `'never'`: The content does not lift at all when the keyboard appears. Use this for scenarios
|
|
37
|
+
* when you don't want to disturb user attention with animations (Perplexity mobile app behavior).
|
|
38
|
+
*
|
|
39
|
+
* Default is `'always'`.
|
|
40
|
+
*/
|
|
41
|
+
keyboardLiftBehavior?: KeyboardLiftBehavior;
|
|
42
|
+
/**
|
|
43
|
+
* When `true`, freezes all keyboard-driven layout changes (padding, content offset, scroll position).
|
|
44
|
+
* Useful when dismissing the keyboard to open a bottom sheet — prevents visual disruption
|
|
45
|
+
* while the sheet is visible.
|
|
46
|
+
*
|
|
47
|
+
* Default is `false`.
|
|
48
|
+
*/
|
|
49
|
+
freeze?: boolean;
|
|
50
|
+
} & ScrollViewProps;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
type KeyboardLiftBehavior = "always" | "whenAtEnd" | "persistent" | "never";
|
|
2
|
+
|
|
3
|
+
const AT_END_THRESHOLD = 20;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Check whether the scroll view is at the end of its content.
|
|
7
|
+
*
|
|
8
|
+
* @param scrollOffset - Current vertical scroll offset.
|
|
9
|
+
* @param layoutHeight - Visible height of the scroll view.
|
|
10
|
+
* @param contentHeight - Total height of the scrollable content.
|
|
11
|
+
* @returns `true` if the scroll position is within the threshold of the content end.
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const atEnd = isScrollAtEnd(100, 800, 920); // true (100 + 800 >= 920 - 20)
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export function isScrollAtEnd(
|
|
18
|
+
scrollOffset: number,
|
|
19
|
+
layoutHeight: number,
|
|
20
|
+
contentHeight: number,
|
|
21
|
+
): boolean {
|
|
22
|
+
"worklet";
|
|
23
|
+
|
|
24
|
+
return scrollOffset + layoutHeight >= contentHeight - AT_END_THRESHOLD;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Decide whether content should be shifted based on the keyboard lift behavior.
|
|
29
|
+
*
|
|
30
|
+
* @param behavior - The configured keyboard lift behavior.
|
|
31
|
+
* @param isAtEnd - Whether the scroll view is currently at the end.
|
|
32
|
+
* @returns `true` if content should be shifted.
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* shouldShiftContent("always", false); // true
|
|
36
|
+
* shouldShiftContent("whenAtEnd", false); // false
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export function shouldShiftContent(
|
|
40
|
+
behavior: KeyboardLiftBehavior,
|
|
41
|
+
isAtEnd: boolean,
|
|
42
|
+
): boolean {
|
|
43
|
+
"worklet";
|
|
44
|
+
|
|
45
|
+
switch (behavior) {
|
|
46
|
+
case "always":
|
|
47
|
+
return true;
|
|
48
|
+
case "never":
|
|
49
|
+
return false;
|
|
50
|
+
case "whenAtEnd":
|
|
51
|
+
return isAtEnd;
|
|
52
|
+
case "persistent":
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Compute the clamped scroll target for non-inverted lists.
|
|
59
|
+
*
|
|
60
|
+
* @param offsetBeforeScroll - Scroll position before keyboard appeared.
|
|
61
|
+
* @param keyboardHeight - Current keyboard height.
|
|
62
|
+
* @param contentHeight - Total height of the scrollable content.
|
|
63
|
+
* @param layoutHeight - Visible height of the scroll view.
|
|
64
|
+
* @returns Clamped scroll target between 0 and maxScroll.
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* clampedScrollTarget(100, 300, 1000, 800); // 400
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export function clampedScrollTarget(
|
|
71
|
+
offsetBeforeScroll: number,
|
|
72
|
+
keyboardHeight: number,
|
|
73
|
+
contentHeight: number,
|
|
74
|
+
layoutHeight: number,
|
|
75
|
+
): number {
|
|
76
|
+
"worklet";
|
|
77
|
+
|
|
78
|
+
const maxScroll = Math.max(contentHeight - layoutHeight + keyboardHeight, 0);
|
|
79
|
+
|
|
80
|
+
return Math.min(Math.max(offsetBeforeScroll + keyboardHeight, 0), maxScroll);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Compute contentOffset.y for iOS lists.
|
|
85
|
+
*
|
|
86
|
+
* @param relativeScroll - Scroll position relative to current inset.
|
|
87
|
+
* @param keyboardHeight - Target keyboard height.
|
|
88
|
+
* @param contentHeight - Total height of the scrollable content.
|
|
89
|
+
* @param layoutHeight - Visible height of the scroll view.
|
|
90
|
+
* @param inverted - Whether the list is inverted.
|
|
91
|
+
* @returns The absolute contentOffset.y to set.
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* computeIOSContentOffset(100, 300, 1000, 800, false); // 400
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export function computeIOSContentOffset(
|
|
98
|
+
relativeScroll: number,
|
|
99
|
+
keyboardHeight: number,
|
|
100
|
+
contentHeight: number,
|
|
101
|
+
layoutHeight: number,
|
|
102
|
+
inverted: boolean,
|
|
103
|
+
): number {
|
|
104
|
+
"worklet";
|
|
105
|
+
|
|
106
|
+
if (inverted) {
|
|
107
|
+
const maxScroll = Math.max(contentHeight - layoutHeight, 0);
|
|
108
|
+
|
|
109
|
+
return Math.max(
|
|
110
|
+
Math.min(relativeScroll - keyboardHeight, maxScroll),
|
|
111
|
+
-keyboardHeight,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const maxScroll = Math.max(contentHeight - layoutHeight + keyboardHeight, 0);
|
|
116
|
+
|
|
117
|
+
return Math.min(Math.max(keyboardHeight + relativeScroll, 0), maxScroll);
|
|
118
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { Platform } from "react-native";
|
|
2
|
+
import { interpolate, scrollTo, useSharedValue } from "react-native-reanimated";
|
|
3
|
+
|
|
4
|
+
import { useKeyboardHandler } from "../../../hooks";
|
|
5
|
+
import useScrollState from "../../hooks/useScrollState";
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
clampedScrollTarget,
|
|
9
|
+
computeIOSContentOffset,
|
|
10
|
+
isScrollAtEnd,
|
|
11
|
+
shouldShiftContent,
|
|
12
|
+
} from "./helpers";
|
|
13
|
+
|
|
14
|
+
import type { AnimatedRef, SharedValue } from "react-native-reanimated";
|
|
15
|
+
import type Reanimated from "react-native-reanimated";
|
|
16
|
+
|
|
17
|
+
const OS = Platform.OS;
|
|
18
|
+
|
|
19
|
+
type KeyboardLiftBehavior = "always" | "whenAtEnd" | "persistent" | "never";
|
|
20
|
+
|
|
21
|
+
type UseChatKeyboardOptions = {
|
|
22
|
+
inverted: boolean;
|
|
23
|
+
keyboardLiftBehavior: KeyboardLiftBehavior;
|
|
24
|
+
freeze: boolean;
|
|
25
|
+
offset: number;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type UseChatKeyboardReturn = {
|
|
29
|
+
/** Extra scrollable space (= keyboard height). Used as contentInset on iOS, contentInsetBottom on Android. */
|
|
30
|
+
padding: SharedValue<number>;
|
|
31
|
+
/** Absolute Y content offset for iOS (set once in onStart). `undefined` on Android. */
|
|
32
|
+
contentOffsetY: SharedValue<number> | undefined;
|
|
33
|
+
/** TranslateY for the container wrapper on Android inverted lists. 0 otherwise. */
|
|
34
|
+
containerTranslateY: SharedValue<number>;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Hook that manages keyboard-driven scrolling for chat-style scroll views.
|
|
39
|
+
* Calculates padding (extra scrollable space) and content shift values,
|
|
40
|
+
* using the optimal strategy per platform.
|
|
41
|
+
*
|
|
42
|
+
* @param scrollViewRef - Animated ref to the scroll view.
|
|
43
|
+
* @param options - Configuration for inverted and keyboardLiftBehavior.
|
|
44
|
+
* @returns Shared values for padding, contentOffsetY (iOS), and containerTranslateY (Android inverted).
|
|
45
|
+
* @example
|
|
46
|
+
* ```tsx
|
|
47
|
+
* const { padding, contentOffsetY, containerTranslateY } = useChatKeyboard(ref, {
|
|
48
|
+
* inverted: false,
|
|
49
|
+
* keyboardLiftBehavior: "always",
|
|
50
|
+
* });
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
function useChatKeyboard(
|
|
54
|
+
scrollViewRef: AnimatedRef<Reanimated.ScrollView>,
|
|
55
|
+
options: UseChatKeyboardOptions,
|
|
56
|
+
): UseChatKeyboardReturn {
|
|
57
|
+
const { inverted, keyboardLiftBehavior, freeze, offset } = options;
|
|
58
|
+
|
|
59
|
+
const padding = useSharedValue(0);
|
|
60
|
+
const contentOffsetY = useSharedValue(0);
|
|
61
|
+
const containerTranslateY = useSharedValue(0);
|
|
62
|
+
const offsetBeforeScroll = useSharedValue(0);
|
|
63
|
+
const targetKeyboardHeight = useSharedValue(0);
|
|
64
|
+
|
|
65
|
+
const { layout, size, offset: scroll } = useScrollState(scrollViewRef);
|
|
66
|
+
|
|
67
|
+
const getEffectiveHeight = (height: number): number => {
|
|
68
|
+
"worklet";
|
|
69
|
+
|
|
70
|
+
if (offset === 0 || targetKeyboardHeight.value === 0) {
|
|
71
|
+
return height;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return interpolate(
|
|
75
|
+
height,
|
|
76
|
+
[0, targetKeyboardHeight.value],
|
|
77
|
+
[0, Math.max(targetKeyboardHeight.value - offset, 0)],
|
|
78
|
+
);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
useKeyboardHandler(
|
|
82
|
+
{
|
|
83
|
+
onStart: (e) => {
|
|
84
|
+
"worklet";
|
|
85
|
+
|
|
86
|
+
if (freeze) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (e.height > 0) {
|
|
91
|
+
// eslint-disable-next-line react-compiler/react-compiler
|
|
92
|
+
targetKeyboardHeight.value = e.height;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const effective = getEffectiveHeight(e.height);
|
|
96
|
+
|
|
97
|
+
const atEnd = isScrollAtEnd(
|
|
98
|
+
scroll.value,
|
|
99
|
+
layout.value.height,
|
|
100
|
+
size.value.height,
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
if (OS === "ios") {
|
|
104
|
+
// iOS: set padding + contentOffset once in onStart
|
|
105
|
+
const relativeScroll = inverted
|
|
106
|
+
? scroll.value + padding.value
|
|
107
|
+
: scroll.value - padding.value;
|
|
108
|
+
|
|
109
|
+
padding.value = effective;
|
|
110
|
+
|
|
111
|
+
if (!shouldShiftContent(keyboardLiftBehavior, atEnd)) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (
|
|
116
|
+
keyboardLiftBehavior === "persistent" &&
|
|
117
|
+
effective < padding.value
|
|
118
|
+
) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
contentOffsetY.value = computeIOSContentOffset(
|
|
123
|
+
relativeScroll,
|
|
124
|
+
effective,
|
|
125
|
+
size.value.height,
|
|
126
|
+
layout.value.height,
|
|
127
|
+
inverted,
|
|
128
|
+
);
|
|
129
|
+
} else if (e.height > 0) {
|
|
130
|
+
// Android: keyboard opening — set padding + capture scroll position
|
|
131
|
+
padding.value = effective;
|
|
132
|
+
offsetBeforeScroll.value = scroll.value;
|
|
133
|
+
|
|
134
|
+
if (keyboardLiftBehavior === "whenAtEnd" && !atEnd) {
|
|
135
|
+
// Sentinel: don't scroll in onMove
|
|
136
|
+
offsetBeforeScroll.value = -1;
|
|
137
|
+
}
|
|
138
|
+
} else {
|
|
139
|
+
// Android: keyboard closing — re-capture from current position
|
|
140
|
+
// so onMove smoothly scrolls back from where the user is now
|
|
141
|
+
offsetBeforeScroll.value = scroll.value - padding.value;
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
onMove: (e) => {
|
|
145
|
+
"worklet";
|
|
146
|
+
|
|
147
|
+
if (freeze) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// iOS doesn't need per-frame updates (contentOffset handles it)
|
|
152
|
+
if (OS === "ios") {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (!shouldShiftContent(keyboardLiftBehavior, true)) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// "whenAtEnd" sentinel check
|
|
161
|
+
if (offsetBeforeScroll.value === -1) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const effective = getEffectiveHeight(e.height);
|
|
166
|
+
|
|
167
|
+
if (inverted) {
|
|
168
|
+
// Android inverted: translateY on container
|
|
169
|
+
if (
|
|
170
|
+
keyboardLiftBehavior === "persistent" &&
|
|
171
|
+
effective < Math.abs(containerTranslateY.value)
|
|
172
|
+
) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
containerTranslateY.value = -effective;
|
|
177
|
+
} else {
|
|
178
|
+
// Android non-inverted: scrollTo per-frame
|
|
179
|
+
if (
|
|
180
|
+
keyboardLiftBehavior === "persistent" &&
|
|
181
|
+
effective < scroll.value - offsetBeforeScroll.value
|
|
182
|
+
) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const target = clampedScrollTarget(
|
|
187
|
+
offsetBeforeScroll.value,
|
|
188
|
+
effective,
|
|
189
|
+
size.value.height,
|
|
190
|
+
layout.value.height,
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
scrollTo(scrollViewRef, 0, target, false);
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
onEnd: (e) => {
|
|
197
|
+
"worklet";
|
|
198
|
+
|
|
199
|
+
if (freeze) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const effective = getEffectiveHeight(e.height);
|
|
204
|
+
|
|
205
|
+
padding.value = effective;
|
|
206
|
+
|
|
207
|
+
if (
|
|
208
|
+
OS !== "ios" &&
|
|
209
|
+
inverted &&
|
|
210
|
+
e.height === 0 &&
|
|
211
|
+
keyboardLiftBehavior !== "persistent"
|
|
212
|
+
) {
|
|
213
|
+
containerTranslateY.value = 0;
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
[inverted, keyboardLiftBehavior, freeze, offset],
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
padding,
|
|
222
|
+
contentOffsetY: OS === "ios" ? contentOffsetY : undefined,
|
|
223
|
+
containerTranslateY,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export { useChatKeyboard };
|
|
228
|
+
export type { KeyboardLiftBehavior };
|
|
@@ -149,7 +149,6 @@ const KeyboardAwareScrollView = forwardRef<
|
|
|
149
149
|
const lastSelection =
|
|
150
150
|
useSharedValue<FocusedInputSelectionChangedEvent | null>(null);
|
|
151
151
|
const ghostViewSpace = useSharedValue(-1);
|
|
152
|
-
const pendingSelectionForFocus = useSharedValue(false);
|
|
153
152
|
|
|
154
153
|
const { height } = useWindowDimensions();
|
|
155
154
|
|
|
@@ -325,18 +324,7 @@ const KeyboardAwareScrollView = forwardRef<
|
|
|
325
324
|
lastSelection.value = e;
|
|
326
325
|
|
|
327
326
|
if (e.target !== lastTarget) {
|
|
328
|
-
|
|
329
|
-
// selection arrived after onStart - complete the deferred setup
|
|
330
|
-
pendingSelectionForFocus.value = false;
|
|
331
|
-
updateLayoutFromSelection();
|
|
332
|
-
|
|
333
|
-
// if keyboard was already visible (focus change, no onMove expected),
|
|
334
|
-
// perform the deferred scroll now
|
|
335
|
-
if (!keyboardWillAppear.value && keyboardHeight.value > 0) {
|
|
336
|
-
position.value += maybeScroll(keyboardHeight.value, true);
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
|
|
327
|
+
// ignore this event, because "focus changed" event handled in `useSmoothKeyboardHandler`
|
|
340
328
|
return;
|
|
341
329
|
}
|
|
342
330
|
// caret in the end + end coordinates has been changed -> we moved to a new line
|
|
@@ -354,12 +342,7 @@ const KeyboardAwareScrollView = forwardRef<
|
|
|
354
342
|
|
|
355
343
|
onChangeTextHandler();
|
|
356
344
|
},
|
|
357
|
-
[
|
|
358
|
-
scrollFromCurrentPosition,
|
|
359
|
-
onChangeTextHandler,
|
|
360
|
-
updateLayoutFromSelection,
|
|
361
|
-
maybeScroll,
|
|
362
|
-
],
|
|
345
|
+
[scrollFromCurrentPosition, onChangeTextHandler],
|
|
363
346
|
);
|
|
364
347
|
|
|
365
348
|
useFocusedInputHandler(
|
|
@@ -392,7 +375,6 @@ const KeyboardAwareScrollView = forwardRef<
|
|
|
392
375
|
// on back transition need to interpolate as [0, keyboardHeight]
|
|
393
376
|
initialKeyboardSize.value = 0;
|
|
394
377
|
scrollPosition.value = scrollBeforeKeyboardMovement.value;
|
|
395
|
-
pendingSelectionForFocus.value = false;
|
|
396
378
|
}
|
|
397
379
|
|
|
398
380
|
if (
|
|
@@ -409,31 +391,17 @@ const KeyboardAwareScrollView = forwardRef<
|
|
|
409
391
|
// focus was changed
|
|
410
392
|
if (focusWasChanged) {
|
|
411
393
|
tag.value = e.target;
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
// selection arrived before onStart - use it to update layout
|
|
415
|
-
updateLayoutFromSelection();
|
|
416
|
-
pendingSelectionForFocus.value = false;
|
|
417
|
-
} else {
|
|
418
|
-
// selection hasn't arrived yet for the new target.
|
|
419
|
-
// use input layout as-is; will be refined when selection arrives.
|
|
420
|
-
if (input.value) {
|
|
421
|
-
layout.value = input.value;
|
|
422
|
-
}
|
|
423
|
-
pendingSelectionForFocus.value = true;
|
|
424
|
-
}
|
|
425
|
-
|
|
394
|
+
// save position of focused text input when keyboard starts to move
|
|
395
|
+
updateLayoutFromSelection();
|
|
426
396
|
// save current scroll position - when keyboard will hide we'll reuse
|
|
427
397
|
// this value to achieve smooth hide effect
|
|
428
398
|
scrollBeforeKeyboardMovement.value = position.value;
|
|
429
399
|
}
|
|
430
400
|
|
|
431
401
|
if (focusWasChanged && !keyboardWillAppear.value) {
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
position.value += maybeScroll(e.height, true);
|
|
436
|
-
}
|
|
402
|
+
// update position on scroll value, so `onEnd` handler
|
|
403
|
+
// will pick up correct values
|
|
404
|
+
position.value += maybeScroll(e.height, true);
|
|
437
405
|
}
|
|
438
406
|
|
|
439
407
|
ghostViewSpace.value =
|
|
@@ -467,10 +435,6 @@ const KeyboardAwareScrollView = forwardRef<
|
|
|
467
435
|
keyboardHeight.value = e.height;
|
|
468
436
|
scrollPosition.value = position.value;
|
|
469
437
|
|
|
470
|
-
if (e.height === 0) {
|
|
471
|
-
lastSelection.value = null;
|
|
472
|
-
}
|
|
473
|
-
|
|
474
438
|
syncKeyboardFrame(e);
|
|
475
439
|
},
|
|
476
440
|
},
|
|
@@ -58,17 +58,16 @@ const KeyboardStickyView = forwardRef<
|
|
|
58
58
|
outputRange: [closed, opened],
|
|
59
59
|
});
|
|
60
60
|
|
|
61
|
-
const styles = useMemo(
|
|
62
|
-
|
|
61
|
+
const styles = useMemo(() => {
|
|
62
|
+
return [
|
|
63
63
|
{
|
|
64
64
|
transform: [
|
|
65
65
|
{ translateY: enabled ? Animated.add(height, offset) : closed },
|
|
66
66
|
],
|
|
67
67
|
},
|
|
68
68
|
style,
|
|
69
|
-
]
|
|
70
|
-
|
|
71
|
-
);
|
|
69
|
+
];
|
|
70
|
+
}, [closed, enabled, height, offset, style]);
|
|
72
71
|
|
|
73
72
|
return (
|
|
74
73
|
<Animated.View ref={ref} style={styles} {...props}>
|