forked from kirillzyusko/react-native-keyboard-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
151 lines (137 loc) · 4.19 KB
/
index.tsx
File metadata and controls
151 lines (137 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import React, { forwardRef, useCallback, useMemo } from "react";
import { StyleSheet } from "react-native";
import {
makeMutable,
useAnimatedRef,
useAnimatedStyle,
useDerivedValue,
} from "react-native-reanimated";
import Reanimated from "react-native-reanimated";
import useCombinedRef from "../hooks/useCombinedRef";
import ScrollViewWithBottomPadding from "../ScrollViewWithBottomPadding";
import { useChatKeyboard } from "./useChatKeyboard";
import { useExtraContentPadding } from "./useExtraContentPadding";
import type { KeyboardChatScrollViewProps } from "./types";
import type { LayoutChangeEvent } from "react-native";
const ZERO_CONTENT_PADDING = makeMutable(0);
const ZERO_BLANK_SPACE = makeMutable(0);
const KeyboardChatScrollView = forwardRef<
Reanimated.ScrollView,
React.PropsWithChildren<KeyboardChatScrollViewProps>
>(
(
{
children,
ScrollViewComponent = Reanimated.ScrollView,
inverted = false,
keyboardLiftBehavior = "always",
freeze = false,
offset = 0,
extraContentPadding = ZERO_CONTENT_PADDING,
blankSpace = ZERO_BLANK_SPACE,
applyWorkaroundForContentInsetHitTestBug = false,
onLayout: onLayoutProp,
onContentSizeChange: onContentSizeChangeProp,
...rest
},
ref,
) => {
const scrollViewRef = useAnimatedRef<Reanimated.ScrollView>();
const onRef = useCombinedRef(ref, scrollViewRef);
const {
padding,
currentHeight,
contentOffsetY,
scroll,
layout,
size,
onLayout: onLayoutInternal,
onContentSizeChange: onContentSizeChangeInternal,
} = useChatKeyboard(scrollViewRef, {
inverted,
keyboardLiftBehavior,
freeze,
offset,
blankSpace,
extraContentPadding,
});
useExtraContentPadding({
scrollViewRef,
extraContentPadding,
keyboardPadding: padding,
blankSpace,
scroll,
layout,
size,
contentOffsetY,
inverted,
keyboardLiftBehavior,
freeze,
});
const totalPadding = useDerivedValue(() =>
Math.max(blankSpace.value, padding.value + extraContentPadding.value),
);
// Scroll indicator inset = keyboard + extraContentPadding (excludes blankSpace).
// Apps that render into the unsafe area can supply a negative
// scrollIndicatorInsets adjustment at the application layer.
const indicatorPadding = useDerivedValue(
() => padding.value + extraContentPadding.value,
);
const onLayout = useCallback(
(e: LayoutChangeEvent) => {
onLayoutInternal(e);
onLayoutProp?.(e);
},
[onLayoutInternal, onLayoutProp],
);
const onContentSizeChange = useCallback(
(w: number, h: number) => {
onContentSizeChangeInternal(w, h);
onContentSizeChangeProp?.(w, h);
},
[onContentSizeChangeInternal, onContentSizeChangeProp],
);
// Invisible view whose animated style changes every frame during keyboard
// animation. On Fabric, this forces Reanimated to schedule a commit,
// which flushes the scrollTo call in the same frame (fixing de-synchronization).
// see https://github.com/software-mansion/react-native-reanimated/issues/9000
const commitStyle = useAnimatedStyle(
() => ({
transform: [{ translateY: -currentHeight.value }],
}),
[],
);
const commit = useMemo(
() => [styles.commitView, commitStyle],
[commitStyle],
);
return (
<>
<ScrollViewWithBottomPadding
ref={onRef}
{...rest}
applyWorkaroundForContentInsetHitTestBug={
applyWorkaroundForContentInsetHitTestBug
}
bottomPadding={totalPadding}
contentOffsetY={contentOffsetY}
inverted={inverted}
scrollIndicatorPadding={indicatorPadding}
ScrollViewComponent={ScrollViewComponent}
onContentSizeChange={onContentSizeChange}
onLayout={onLayout}
>
{children}
</ScrollViewWithBottomPadding>
<Reanimated.View style={commit} />
</>
);
},
);
const styles = StyleSheet.create({
commitView: {
display: "none",
position: "absolute",
},
});
export default KeyboardChatScrollView;