Skip to content

Commit 1aeec3a

Browse files
committed
Add renderBubble
1 parent 45989c2 commit 1aeec3a

File tree

8 files changed

+942
-850
lines changed

8 files changed

+942
-850
lines changed

docs/advanced-usage.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,58 @@ const App = () => {
330330
export default App
331331
```
332332

333+
## Custom chat bubbles
334+
335+
Just pass the `renderBubble` function to the `Chat` component. `child` parameter of the `renderBubble` function is a default message content (which you can further customize using `renderCustomMessage`, `renderFileMessage`, `renderImageMessage`, `renderTextMessage` etc.). `message` parameter gives you the actual message to work with, where you can see whether the current user is author, message type, or anything you'd like to customize the bubble. `nextMessageInGroup` parameter gives you a hint about message groups and if you want to add a nip only for the last message in the group, you can do that (messages are grouped when written in quick succession by the same author).
336+
337+
```ts
338+
import { Chat, defaultTheme, MessageType } from '@flyerhq/react-native-chat-ui'
339+
import { ReactNode } from 'react'
340+
import { View } from 'react-native'
341+
342+
const renderBubble = ({
343+
child,
344+
message,
345+
nextMessageInGroup,
346+
}: {
347+
child: ReactNode
348+
message: MessageType.Any
349+
nextMessageInGroup: boolean
350+
}) => {
351+
return (
352+
<View
353+
style={{
354+
backgroundColor: user.id !== message.author.id ? '#ffffff' : '#1d1c21',
355+
borderBottomLeftRadius:
356+
!nextMessageInGroup && user.id !== message.author.id ? 20 : 0,
357+
borderBottomRightRadius:
358+
!nextMessageInGroup && user.id === message.author.id ? 20 : 0,
359+
borderColor: '#1d1c21',
360+
borderWidth: 1,
361+
overflow: 'hidden',
362+
}}
363+
>
364+
{child}
365+
</View>
366+
)
367+
}
368+
369+
return (
370+
<Chat
371+
// ...
372+
renderBubble={renderBubble}
373+
theme={{
374+
...defaultTheme,
375+
colors: { ...defaultTheme.colors, primary: '#1d1c21' },
376+
}}
377+
/>
378+
)
379+
```
380+
381+
This is how it would look like
382+
383+
<img src="https://user-images.githubusercontent.com/14123304/133937546-6777b625-9f5b-46c2-812b-0416439618b6.png" width="288px" alt="Custom chat bubbles" />
384+
333385
## Custom messages
334386

335387
Use the `renderCustomMessage` function to render whatever message you want. To store the data use a `metadata` map of the `CustomMessage`. You can have multiple different custom messages, you will need to identify them based on some property inside the `metadata` and render accordingly.

example/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
"@types/react-native": "^0.65.0",
3232
"@types/react-test-renderer": "^17.0.1",
3333
"@types/uuid": "^8.3.1",
34-
"babel-jest": "^27.1.1",
34+
"babel-jest": "^27.2.0",
3535
"casual": "^1.6.2",
3636
"eslint": "^7.32.0",
3737
"eslint-plugin-simple-import-sort": "^7.0.0",
38-
"jest": "^27.1.1",
38+
"jest": "^27.2.0",
3939
"metro-react-native-babel-preset": "^0.66.2",
4040
"react-native-codegen": "^0.0.7",
4141
"react-test-renderer": "^17.0.2",

0 commit comments

Comments
 (0)