Skip to content

Commit

Permalink
chore: add reanimated chat flatlist example (#236)
Browse files Browse the repository at this point in the history
## 馃摐 Description
<!-- Describe your changes in detail -->
Add an example for a chat with reanimated and flatlist 

## 馃挕 Motivation and Context
<!-- Why is this change required? What problem does it solve? -->
<!-- If it fixes an open issue, please link to the issue here. -->
There is only an example with a ScrollView, in a real chat app it's more
common to encounter a FlatList
It may solve this issue
#156

## 馃 How Has This Been Tested?
Tested iPhone 14 Pro 

## 馃摳 Screenshots (if appropriate):

https://github.com/kirillzyusko/react-native-keyboard-controller/assets/11976270/9d73f4f0-e1a4-4ea6-a0f8-a870284c0322
## 馃摑 Checklist

- [ ] CI successfully passed

---------

Co-authored-by: kirillzyusko <zyusko.kirik@gmail.com>
  • Loading branch information
dayze and kirillzyusko committed Sep 14, 2023
1 parent 5e8d45d commit 8b2041c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions example/src/constants/screenNames.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export enum ScreenNames {
ANIMATED_EXAMPLE = 'ANIMATED_EXAMPLE',
REANIMATED_CHAT = 'REANIMATED_CHAT',
REANIMATED_CHAT_FLATLIST = 'REANIMATED_CHAT_FLATLIST',
EVENTS = 'EVENTS',
AWARE_SCROLL_VIEW = 'AWARE_SCROLL_VIEW',
STATUS_BAR = 'STATUS_BAR',
Expand Down
9 changes: 9 additions & 0 deletions example/src/navigation/ExamplesStack/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import InteractiveKeyboard from '../../screens/Examples/InteractiveKeyboard';
import InteractiveKeyboardIOS from '../../screens/Examples/InteractiveKeyboardIOS';
import NativeStack from '../NestedStack';
import KeyboardAvoidingViewExample from '../../screens/Examples/KeyboardAvoidingView';
import ReanimatedChatFlatlist from '../../screens/Examples/ReanimatedChatFlatlist';

export type ExamplesStackParamList = {
[ScreenNames.ANIMATED_EXAMPLE]: undefined;
Expand All @@ -38,6 +39,9 @@ const options = {
[ScreenNames.REANIMATED_CHAT]: {
title: 'Chat',
},
[ScreenNames.REANIMATED_CHAT_FLATLIST]: {
title: 'Chat Flatlist',
},
[ScreenNames.EVENTS]: {
title: 'Events',
},
Expand Down Expand Up @@ -80,6 +84,11 @@ const ExamplesStack = () => (
component={ReanimatedChat}
options={options[ScreenNames.REANIMATED_CHAT]}
/>
<Stack.Screen
name={ScreenNames.REANIMATED_CHAT_FLATLIST}
component={ReanimatedChatFlatlist}
options={options[ScreenNames.REANIMATED_CHAT]}
/>
<Stack.Screen
name={ScreenNames.EVENTS}
component={Events}
Expand Down
5 changes: 5 additions & 0 deletions example/src/screens/Examples/Main/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export const examples: Example[] = [
icons: '馃槏鈱笍',
},
{ title: 'Chat', info: ScreenNames.REANIMATED_CHAT, icons: '馃挰' },
{
title: 'Chat flatlist',
info: ScreenNames.REANIMATED_CHAT_FLATLIST,
icons: '馃挰',
},
{ title: 'Events', info: ScreenNames.EVENTS, icons: '馃巸 馃搮' },
{
title: 'Aware scroll view',
Expand Down
43 changes: 43 additions & 0 deletions example/src/screens/Examples/ReanimatedChatFlatlist/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from 'react';
import { FlatList, ListRenderItem, TextInput, View } from 'react-native';
import { useReanimatedKeyboardAnimation } from 'react-native-keyboard-controller';
import Animated, { useAnimatedStyle } from 'react-native-reanimated';

import Message from '../../../components/Message';
import { history } from '../../../components/Message/data';
import { MessageProps } from '../../../components/Message/types';

import styles from './styles';

const reversedMessages = [...history].reverse();

const RenderItem: ListRenderItem<MessageProps> = ({ item, index }) => {
return <Message key={index} {...item} />;
};

function ReanimatedChatFlatlist() {
const { height } = useReanimatedKeyboardAnimation();

const fakeView = useAnimatedStyle(
() => ({
height: Math.abs(height.value),
}),
[]
);

return (
<View style={styles.container}>
<FlatList
inverted
initialNumToRender={15}
contentContainerStyle={styles.contentContainer}
data={reversedMessages}
renderItem={RenderItem}
/>
<TextInput style={styles.textInput} />
<Animated.View style={fakeView} />
</View>
);
}

export default ReanimatedChatFlatlist;
13 changes: 13 additions & 0 deletions example/src/screens/Examples/ReanimatedChatFlatlist/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { StyleSheet } from 'react-native';

export default StyleSheet.create({
container: {
flex: 1,
},
header: {
color: 'black',
marginRight: 12,
},
contentContainer: { justifyContent: 'flex-end', flexGrow: 1 },
textInput: { height: 50, width: '100%', backgroundColor: '#BCBCBC' },
});

0 comments on commit 8b2041c

Please sign in to comment.