@@ -11,57 +11,62 @@ npx react-native init MyApp --template stream-chat-react-native-template
1111```
1212
1313## Basic Custom Background
14+
1415You can change the background statically by simply wrapping ` MessageList ` and ` MessageInput ` by using the ` ImageBackground ` component.
1516
16- <img src = ' https://user-images.githubusercontent.com/25864161/167857632-c0bc9d67-0a84-4cf5-9d75-305e3bcd1f3d.png'
17- width = " 320px" />
17+ <img
18+ src = ' https://user-images.githubusercontent.com/25864161/167857632-c0bc9d67-0a84-4cf5-9d75-305e3bcd1f3d.png'
19+ width = ' 320px'
20+ />
1821
1922Also make sure you adjust the ` theme ` correctly.
2023
2124``` tsx
22- import {Channel , MessageInput , MessageList , ThemeProvider , } from ' stream-chat-react-native' ;
23- import {ImageBackground } from ' react-native' ;
25+ import { Channel , MessageInput , MessageList , ThemeProvider } from ' stream-chat-react-native' ;
26+ import { ImageBackground } from ' react-native' ;
2427
25- const IMAGE_URI = ' https://images.unsplash.com/photo-1549125764-91425ca48850?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8NjF8fHxlbnwwfHx8fA%3D%3D&auto=format&fit=crop&w=800&q=60'
28+ const IMAGE_URI =
29+ ' https://images.unsplash.com/photo-1549125764-91425ca48850?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8NjF8fHxlbnwwfHx8fA%3D%3D&auto=format&fit=crop&w=800&q=60' ;
2630
27- const ChannelScreen = ({channel }) =>
31+ const ChannelScreen = ({ channel }) => (
2832 <ThemeProvider style = { theme } >
2933 <Channel channel = { channel } >
3034 <ImageBackground
31- style = { {flex: 1 }}
35+ style = { { flex: 1 }}
3236 source = { {
3337 uri: IMAGE_URI ,
34- }} >
38+ }}
39+ >
3540 <MessageList />
3641 <MessageInput />
3742 </ImageBackground >
3843 </Channel >
3944 </ThemeProvider >
45+ );
4046
4147export const theme = {
4248 messageList: {
4349 container: {
4450 backgroundColor: ' transparent' ,
4551 },
4652 },
47- }
53+ };
4854```
4955
5056---
5157
5258## Custom Background With Selection Screen
59+
5360In this step, we will add a button that will navigate to a separate screen, where the user will be able to select his
5461favorite background image for a specific channel.
5562
5663<table >
5764 <tr >
5865 <td align = ' center' width = ' 50%' >
59- <img
60- src = ' https://user-images.githubusercontent.com/25864161/168006715-acae4b85-00cb-4b45-a127-aa8f94c13895.png' />
66+ <img src = ' https://user-images.githubusercontent.com/25864161/168006715-acae4b85-00cb-4b45-a127-aa8f94c13895.png' />
6167 </td >
6268 <td align = ' center' width = ' 50%' >
63- <img
64- src = ' https://user-images.githubusercontent.com/25864161/168006193-8fd4ad85-7553-4956-a7c6-5d6979e15ee4.png' />
69+ <img src = ' https://user-images.githubusercontent.com/25864161/168006193-8fd4ad85-7553-4956-a7c6-5d6979e15ee4.png' />
6570 </td >
6671 </tr >
6772 <tr ></tr >
@@ -76,26 +81,26 @@ favorite background image for a specific channel.
7681</table >
7782
7883### Store and manage channel preferences
84+
7985To persist the channel preferences (at the moment, only the URI of the selected background), we will need to store some data.
8086We will do it by using ` react-native-mmkv ` , a key-value storage [ framework] ( https://github.com/mrousavy/react-native-mmkv#mmkv ) .
8187
8288Follow the [ installation steps] ( https://github.com/mrousavy/react-native-mmkv#installation ) , and let's get started, shall we?
8389
84-
8590We will start by creating our ` ChannelBackgroundView ` component.
8691This component will be in charge of rendering the custom background by retrieving it from our key-value store.
8792We save an object to a key-value store to be scalable and future-proof.
8893You'd might want to add other preferences later, such as dimming value, background color, etc.
8994
9095``` tsx
91- import type {ViewProps } from ' react-native' ;
92- import {useMMKVObject } from ' react-native-mmkv'
96+ import type { ViewProps } from ' react-native' ;
97+ import { useMMKVObject } from ' react-native-mmkv' ;
9398
9499type ChannelPreferences = {
95- imageUri: string
96- }
100+ imageUri: string ;
101+ };
97102
98- const DEFAULT_BACKGROUND_URI = ' https://i.redd.it/3jfjc53fsyb61.jpg'
103+ const DEFAULT_BACKGROUND_URI = ' https://i.redd.it/3jfjc53fsyb61.jpg' ;
99104
100105const ChannelBackgroundView = ({
101106 channelId ,
@@ -104,98 +109,96 @@ const ChannelBackgroundView = ({
104109 channelId: string ;
105110} & ViewProps ) => {
106111 const [channelPreferences] = useMMKVObject <ChannelPreferences >(channelId );
107- let uri: string | undefined =
108- channelPreferences ?.imageUri || DEFAULT_BACKGROUND_URI ;
112+ let uri: string | undefined = channelPreferences ?.imageUri || DEFAULT_BACKGROUND_URI ;
109113
110- return <ImageBackground { ... props } source = { {uri }} />;
114+ return <ImageBackground { ... props } source = { { uri }} />;
111115};
112- ````
116+ ```
113117
114118We will then use it in our previously built ` ChannelScreen ` .
115119Replace the static ` ImageBackground ` with ` ChannelBackgroundView ` and pass the ` channelId `
116120
117121``` tsx
118- const ChannelScreen = ({channel}) => {
122+ const ChannelScreen = ({ channel }) => {
119123 return (
120124 <ThemeProvider style = { theme } >
121125 <Channel channel = { channel } >
122- <ChannelBackgroundView channelId={channel?.id} style={{flex: 1}}>
126+ <ChannelBackgroundView channelId = { channel ?.id } style = { { flex: 1 }} >
123127 <MessageList />
124128 <MessageInput />
125129 </ChannelBackgroundView >
126130 </Channel >
127131 </ThemeProvider >
128- )
129- }
132+ );
133+ };
130134```
131135
132136---
133137
134138### Wallpaper overview screen
139+
135140Let's now add a screen where the user can choose a wallpaper from a particular predefined list of images.
136141
137142``` tsx
138- import {StackNavigationProp} from '@react-navigation/stack';
139- import {RouteProp} from '@react-navigation/native';
140- import {useMMKVObject} from 'react-native-mmkv'
143+ import { StackNavigationProp } from ' @react-navigation/stack' ;
144+ import { RouteProp } from ' @react-navigation/native' ;
145+ import { useMMKVObject } from ' react-native-mmkv' ;
141146import { View , SafeAreaView , Pressable , Image , StyleSheet } from ' react-native' ;
142147
143148const WallpaperOverviewScreen = ({
144- navigation: {navigate},
149+ navigation: { navigate },
145150 route: {
146- params: {channelId},
151+ params: { channelId },
147152 },
148153}: WallpaperOverviewScreenProps ) => {
149- const [_, setChannelPreferences] =
150- useMMKVObject<ChannelPreferences>(channelId)
154+ const [_, setChannelPreferences] = useMMKVObject <ChannelPreferences >(channelId );
151155 return (
152156 <SafeAreaView
153157 style = { {
154158 flex: 1 ,
155159 justifyContent: ' center' ,
156- }}>
160+ }}
161+ >
157162 <View style = { styles .container } >
158- {BRIGHT_IMAGES?.map(({imageUri = ''}, i) => {
163+ { BRIGHT_IMAGES ?.map (({ imageUri = ' ' }, i ) => {
159164 const handleOnPress = () => {
160- setChannelPreferences({imageUri})
161- navigate('Channel')
162- }
165+ setChannelPreferences ({ imageUri });
166+ navigate (' Channel' );
167+ };
163168 return (
164169 <Pressable
165170 key = { i }
166171 onPress = { handleOnPress }
167172 style = { {
168173 margin: 1 ,
169174 width: GRID_ITEM_WIDTH ,
170- }}>
171- <Image style={styles.image} source={{uri: imageUri}} />
175+ }}
176+ >
177+ <Image style = { styles .image } source = { { uri: imageUri }} />
172178 </Pressable >
173- )
179+ );
174180 })}
175181 </View >
176182 </SafeAreaView >
177- )
178- }
183+ );
184+ };
179185
180186type StackNavigatorParamList = {
181187 WallpaperOverviewScreen: {
182- channelId: string
183- }
184- }
188+ channelId: string ;
189+ };
190+ };
185191
186192type WallpaperOverviewScreenProps = {
187- navigation: StackNavigationProp<
188- StackNavigatorParamList,
189- 'WallpaperOverviewScreen'
190- >
191- route: RouteProp<StackNavigatorParamList, 'WallpaperOverviewScreen'>
192- }
193+ navigation: StackNavigationProp <StackNavigatorParamList , ' WallpaperOverviewScreen' >;
194+ route: RouteProp <StackNavigatorParamList , ' WallpaperOverviewScreen' >;
195+ };
193196
194197type ChannelPreferences = {
195- imageUri: string
196- }
198+ imageUri: string ;
199+ };
197200
198- const GRID_ITEM_WIDTH = '32.7%'
201+ const GRID_ITEM_WIDTH = ' 32.7%' ;
199202
200203// Some random images that will get you started
201204const BRIGHT_IMAGES = [
@@ -213,7 +216,7 @@ const BRIGHT_IMAGES = [
213216 ' https://images.unsplash.com/photo-1553526777-5ffa3b3248d8?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MTk4fHx8ZW58MHx8fHw%3D&auto=format&fit=crop&w=800&q=60' ,
214217].map (imageUri => ({
215218 imageUri ,
216- }))
219+ }));
217220
218221const styles = StyleSheet .create ({
219222 container: {
@@ -226,8 +229,8 @@ const styles = StyleSheet.create({
226229 image: {
227230 flex: 1 ,
228231 width: ' 100%' ,
229- }
230- })
232+ },
233+ });
231234```
232235
233236:::note
@@ -236,32 +239,32 @@ There are alternative approaches to achieve the same goal, for example by saving
236239:::
237240
238241### Add a configuration button
242+
239243We will add now a button that will take the user from the Channel screen to our new WallpaperOverview screen
240244
241245``` tsx
242- import {useNavigation} from '@react-navigation/native';
243- import {Channel, MessageInput, MessageList, ThemeProvider} from 'stream-chat-react-native';
244- import {Pressable, Text, StyleSheet} from 'react-native';
246+ import { useNavigation } from ' @react-navigation/native' ;
247+ import { Channel , MessageInput , MessageList , ThemeProvider } from ' stream-chat-react-native' ;
248+ import { Pressable , Text , StyleSheet } from ' react-native' ;
245249
246- const ChannelScreen = ({channel})=> {
247- const {navigate} = useNavigation()
248- const handleMenuOnPress = () =>
249- navigate('WallpaperOverviewScreen', {channelId: channel?.id})
250+ const ChannelScreen = ({ channel }) => {
251+ const { navigate } = useNavigation ();
252+ const handleMenuOnPress = () => navigate (' WallpaperOverviewScreen' , { channelId: channel ?.id });
250253
251254 return (
252255 <ThemeProvider style = { theme } >
253256 <Channel channel = { channel } >
254- <ChannelBackgroundView channelId={channel?.id} style={{flex: 1}}>
255- <Pressable style={styles.menuButton} onPress={handleMenuOnPress}>
256- <Text>🎨</Text>
257- </Pressable>
257+ <ChannelBackgroundView channelId = { channel ?.id } style = { { flex: 1 }} >
258+ <Pressable style = { styles .menuButton } onPress = { handleMenuOnPress } >
259+ <Text >🎨</Text >
260+ </Pressable >
258261 <MessageList />
259262 <MessageInput />
260263 </ChannelBackgroundView >
261264 </Channel >
262265 </ThemeProvider >
263- )
264- }
266+ );
267+ };
265268
266269const styles = StyleSheet .create ({
267270 menuButton: {
@@ -275,49 +278,41 @@ const styles = StyleSheet.create({
275278 alignItems: ' center' ,
276279 zIndex: 10 ,
277280 },
278- })
281+ });
279282
280283export const theme = {
281284 messageList: {
282285 container: {
283286 backgroundColor: ' transparent' ,
284287 },
285288 },
286- }
289+ };
287290```
288291
289292---
290293
291294### Optional: Connect all screens by navigation
295+
292296If applicable to your use case, add our screens to a Navigation Stack by doing the following.
293297
294298``` tsx
295- import {createNativeStackNavigator} from 'react-native-screens/native-stack';
296- import {NavigationContainer, } from '@react-navigation/native';
299+ import { createNativeStackNavigator } from ' react-native-screens/native-stack' ;
300+ import { NavigationContainer } from ' @react-navigation/native' ;
297301
298- const Stack = createNativeStackNavigator()
302+ const Stack = createNativeStackNavigator ();
299303
300304export default () => {
301305 return (
302306 <SafeAreaProvider >
303307 <ThemeProvider style = { theme } >
304308 <NavigationContainer >
305- <Stack.Navigator initialRouteName="Channel">
306- <Stack.Screen
307- component={ChannelScreen}
308- name="Channel"
309- options={noHeaderOptions}
310- />
311- <Stack.Screen
312- component={WallpaperOverviewScreen}
313- name="WallpaperOverviewScreen"
314- />
309+ <Stack.Navigator initialRouteName = ' Channel' >
310+ <Stack.Screen component = { ChannelScreen } name = ' Channel' options = { noHeaderOptions } />
311+ <Stack.Screen component = { WallpaperOverviewScreen } name = ' WallpaperOverviewScreen' />
315312 </Stack.Navigator >
316313 </NavigationContainer >
317314 </ThemeProvider >
318315 </SafeAreaProvider >
319- )
320- }
316+ );
317+ };
321318```
322-
323-
0 commit comments