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
196 lines (183 loc) · 5.7 KB
/
index.tsx
File metadata and controls
196 lines (183 loc) · 5.7 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { LegendList, type LegendListRef } from "@legendapp/list";
import { FlashList, type FlashListRef } from "@shopify/flash-list";
import React, {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import {
FlatList,
Image,
StyleSheet,
TextInput,
TouchableOpacity,
View,
} from "react-native";
import {
KeyboardGestureArea,
KeyboardStickyView,
} from "react-native-keyboard-controller";
import { useSharedValue } from "react-native-reanimated";
import {
SafeAreaView,
useSafeAreaInsets,
} from "react-native-safe-area-context";
import BlurView from "../../../components/BlurView";
import Message from "./components/Message";
import ConfigSheet from "./config";
import { useChatConfigStore } from "./store";
import styles, {
MARGIN,
TEXT_INPUT_HEIGHT,
contentContainerStyle,
} from "./styles";
import VirtualizedListScrollView, {
type VirtualizedListScrollViewRef,
} from "./VirtualizedListScrollView";
import type { MessageProps } from "../../../components/Message/types";
import type { LayoutChangeEvent, ScrollViewProps } from "react-native";
function KeyboardChatScrollViewPlayground() {
const legendRef = useRef<LegendListRef>(null);
const flashRef = useRef<FlashListRef<MessageProps>>(null);
const flatRef = useRef<FlatList<MessageProps>>(null);
const scrollRef = useRef<VirtualizedListScrollViewRef>(null);
const [text, setText] = useState("");
const [inputHeight, setInputHeight] = useState(TEXT_INPUT_HEIGHT);
const extraContentPadding = useSharedValue(0);
const { inverted, messages, reversedMessages, addMessage, mode } =
useChatConfigStore();
const { bottom } = useSafeAreaInsets();
const stickyViewOffset = useMemo(
() => ({ opened: bottom - MARGIN }),
[bottom],
);
const onInputLayoutChanged = useCallback(
(e: LayoutChangeEvent) => {
const height = e.nativeEvent.layout.height;
// eslint-disable-next-line react-compiler/react-compiler
extraContentPadding.value = Math.max(height - TEXT_INPUT_HEIGHT, 0);
setInputHeight(height);
},
[extraContentPadding],
);
const onInput = useCallback((value: string) => {
setText(value);
}, []);
const onSend = useCallback(() => {
const message = text.trim();
if (message === "") {
return;
}
addMessage({ text: message, sender: true });
setText("");
}, [addMessage, text]);
useEffect(() => {
legendRef.current?.scrollToOffset({
animated: true,
offset: Number.MAX_SAFE_INTEGER,
});
flashRef.current?.scrollToEnd({ animated: true });
flatRef.current?.scrollToOffset({
animated: true,
offset: Number.MAX_SAFE_INTEGER,
});
scrollRef.current?.scrollToEnd({ animated: true });
}, [messages]);
const memoList = useCallback(
(props: ScrollViewProps) => (
<VirtualizedListScrollView
{...props}
extraContentPadding={extraContentPadding}
/>
),
[extraContentPadding],
);
return (
<SafeAreaView edges={["bottom"]} style={styles.container}>
<KeyboardGestureArea
interpolator="ios"
offset={inputHeight}
style={styles.container}
textInputNativeID="chat-input"
>
{mode === "legend" && (
<LegendList
ref={legendRef}
alignItemsAtEnd={inverted}
contentContainerStyle={contentContainerStyle}
data={messages}
initialScrollAtEnd={inverted}
keyExtractor={(item) => item.text}
renderItem={({ item }) => <Message {...item} />}
renderScrollComponent={memoList}
/>
)}
{mode === "flash" && (
<FlashList
ref={flashRef}
contentContainerStyle={contentContainerStyle}
data={messages}
keyExtractor={(item) => item.text}
maintainVisibleContentPosition={{
startRenderingFromBottom: inverted,
}}
renderItem={({ item }) => <Message {...item} />}
renderScrollComponent={memoList}
/>
)}
{mode === "flat" && (
<FlatList
ref={flatRef}
data={inverted ? reversedMessages : messages}
inverted={inverted}
keyExtractor={(item) => item.text}
renderItem={({ item }) => <Message {...item} />}
renderScrollComponent={memoList}
/>
)}
{mode === "scroll" && (
<VirtualizedListScrollView
ref={scrollRef}
extraContentPadding={extraContentPadding}
>
{messages.map((message, index) => (
<Message key={index} {...message} />
))}
</VirtualizedListScrollView>
)}
<KeyboardStickyView offset={stickyViewOffset} style={styles.composer}>
<View
style={[
StyleSheet.absoluteFillObject,
{ overflow: "hidden" },
styles.input,
]}
>
<BlurView
blurAmount={32}
blurType="light"
reducedTransparencyFallbackColor="white"
style={StyleSheet.absoluteFillObject}
/>
</View>
<TextInput
multiline
nativeID="chat-input"
style={styles.input}
testID="chat.input"
value={text}
onChangeText={onInput}
onLayout={onInputLayoutChanged}
/>
<TouchableOpacity style={styles.send} onPress={onSend}>
<Image source={require("./send.png")} style={styles.icon} />
</TouchableOpacity>
</KeyboardStickyView>
</KeyboardGestureArea>
<ConfigSheet />
</SafeAreaView>
);
}
export default KeyboardChatScrollViewPlayground;