Skip to content

Commit 303497f

Browse files
committed
feat: scroll to start of list on scrollToBottomComponent press
1 parent e5414b8 commit 303497f

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

src/CuteChat.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@ import React, {
77
useEffect,
88
useLayoutEffect,
99
useMemo,
10+
useRef,
1011
useState,
1112
} from 'react';
12-
import { Alert, NativeScrollEvent, View } from 'react-native';
13+
import {
14+
Alert,
15+
FlatList,
16+
NativeScrollEvent,
17+
TouchableOpacity,
18+
View,
19+
} from 'react-native';
1320
import type { IMessage } from 'react-native-gifted-chat';
1421
import { GiftedChat, GiftedChatProps } from 'react-native-gifted-chat';
1522
import { appendSnapshot } from './utils/appendSnapshot';
@@ -285,6 +292,12 @@ export function CuteChat(props: CuteChatProps) {
285292

286293
console.log('Close to top:', closeToTop);
287294

295+
const chatRef = useRef<FlatList<IMessage>>(null);
296+
297+
const scrollToBottom = () => {
298+
chatRef.current?.scrollToOffset({ offset: 0, animated: true });
299+
};
300+
288301
return (
289302
<GiftedChat
290303
{...props}
@@ -294,7 +307,8 @@ export function CuteChat(props: CuteChatProps) {
294307
renderChatFooter={() => (
295308
<>
296309
{!closeToTop && props.scrollToBottomComponent && (
297-
<View
310+
<TouchableOpacity
311+
onPress={scrollToBottom}
298312
style={
299313
props.scrollToBottomStyle ?? {
300314
position: 'absolute',
@@ -307,7 +321,7 @@ export function CuteChat(props: CuteChatProps) {
307321
}
308322
>
309323
{props.scrollToBottomComponent()}
310-
</View>
324+
</TouchableOpacity>
311325
)}
312326
{props.renderChatFooter?.()}
313327
</>
@@ -317,6 +331,7 @@ export function CuteChat(props: CuteChatProps) {
317331
user={memoizedUser}
318332
inverted={true}
319333
listViewProps={{
334+
ref: chatRef,
320335
onScroll: ({ nativeEvent }: { nativeEvent: NativeScrollEvent }) => {
321336
if (isCloseToBottom(nativeEvent)) fetchMoreMessages();
322337

0 commit comments

Comments
 (0)