You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docusaurus/docs/React/components/core-components/message-list.mdx
+8-164
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ By default, the `MessageList` loads the most recent 25 messages held in the `cha
15
15
from the Stream Chat API and loaded into the DOM on scrolling up the list. The currently loaded messages are held in
16
16
the `ChannelStateContext` and can be referenced with our custom hook.
17
17
18
-
```jsx
18
+
```tsx
19
19
const { messages } =useChannelStateContext();
20
20
```
21
21
@@ -31,7 +31,7 @@ value drawn from the `ChannelStateContext`.
31
31
32
32
**Example 1** - without `messages` prop
33
33
34
-
```jsx
34
+
```tsx
35
35
<Chatclient={client}>
36
36
<ChannelList />
37
37
<Channel>
@@ -43,7 +43,7 @@ value drawn from the `ChannelStateContext`.
43
43
44
44
**Example 2** - with `messages` prop
45
45
46
-
```jsx
46
+
```tsx
47
47
const customMessages = [
48
48
// array of messages
49
49
];
@@ -61,163 +61,7 @@ const customMessages = [
61
61
62
62
The `MessageList` internally creates a mapping of message id to a style group. There are 4 style groups - ` 'middle' | 'top' | 'bottom' | 'single'`. Later these group style tags are incorporated into the class of the `<li/>` element that wraps the `Message` component. This allows us to style messages by their position in the message group. An example of such class is `str-chat__li str-chat__li--bottom`.
63
63
64
-
## Rendering message text with `renderText` function
65
-
66
-
### Default behaviour
67
-
68
-
The default [`renderText`](#render-text) function parses a markdown string and outputs a `ReactElement`. Under the hood, the output is generated by the `ReactMarkdown` component from [react-markdown library](https://github.com/remarkjs/react-markdown). The component transforms the markdown to `ReactElement` by using [`remark` parser](https://github.com/remarkjs/remark/tree/main) and [`remark`](https://github.com/remarkjs/remark/blob/main/doc/plugins.md) and [`rehype`](https://github.com/rehypejs/rehype/blob/main/doc/plugins.md) plugins.
69
-
70
-
The default `remark` plugins used by SDK are:
71
-
72
-
1.[`remark-gfm`](https://github.com/remarkjs/remark-gfm) - a third party plugin to add GitHub-like markdown support
73
-
74
-
The default `rehype` plugins (both specific to this SDK) are:
75
-
1. plugin to render user mentions
76
-
2. plugin to render emojis
77
-
78
-
### Overriding defaults
79
-
80
-
#### Custom `renderText` function
81
-
82
-
If you don't want your chat implementation to support markdown syntax by default you can override the default behaviour by creating a custom `renderText` function which returns a React node and passing it down to the `MessageList` or `MessageSimple` component via `renderText` property.
83
-
84
-
For this particular example we'll create a very primitive one which takes the message text passed down to it as a first argument and returns it wrapped in `span` element:
85
-
86
-
```tsx
87
-
const customRenderText = (text) => {
88
-
return <span>{text}</span>;
89
-
};
90
-
91
-
const App = () => (
92
-
<Chatclient={client}>
93
-
<Channel>
94
-
<Window>
95
-
<MessageListrenderText={customRenderText} />
96
-
</Window>
97
-
</Channel>
98
-
</Chat>
99
-
);
100
-
```
101
-
102
-
Here's also an example with `VirtualizedMessageList` which currently does not accept `renderText` directly:
If you feel like the default output is sufficient, but you'd like to adjust how certain [ReactMarkdown components](https://github.com/remarkjs/react-markdown#appendix-b-components) look like (like `strong` element generated by typing \*\*strong\*\*) you can do so by passing down options to a third argument of the default `renderText` function:
127
-
128
-
:::note
129
-
Types `mention` and `emoji` are special case component types generated by our SDK's custom rehype plugins.
130
-
:::
131
-
132
-
```tsx
133
-
import { renderText } from'stream-chat-react';
134
-
135
-
const StrongComponent = ({ children }) => <bclassName='custom-strong-class-name'>{children}</b>;
If you would like to extend the array of plugins used to parse the markdown, you can provide your own lists of remark resp. rehype plugins. The logic that determines what plugins are used and in which order can be specified in custom `getRehypePlugins` and `getRemarkPlugins` functions. These receive the default array of rehype and remark plugins for further customization. Both custom functions ought to be passed to the third `renderText()` parameter. An example follows:
163
-
164
-
:::note
165
-
It is important to understand what constitutes a rehype or remark plugin. A good start is to learn about the library called [`react-remark`](https://github.com/remarkjs/react-remark) which is used under the hood in our `renderText()` function.
It is also possible to define your custom set of allowed tag names for the elements rendered from the parsed markdown. To perform the tree transformations, you will need to use libraries like [`unist-builder`](https://github.com/syntax-tree/unist-builder) to build the trees and [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit-parents) or [`hast-util-find-and-replace`](https://github.com/syntax-tree/hast-util-find-and-replace) to traverse the tree:
You can completely change the way the message list is rendered by providing a custom `renderMessages` function. This function takes all the messages fetched so far (along with some additional data) and returns an array of React elements to render. By overriding the default behavior, you can add custom elements to the message list, change the way messages are grouped, add custom separators between messages, etc.
223
67
@@ -228,13 +72,13 @@ In this example, we use the default implementation for rendering a message list,
Make sure that the elements you return have `key`, as they will be rendered as an array. It's also a good idea to wrap each element with `<li>` to keep your markup semantically correct.
@@ -620,7 +464,7 @@ The floating notification informing about unread messages will be shown when the
The `renderText` function is a core piece of functionality, which handles how the text of our message is going to be formatted/look like. The default [`renderText`](#render-text) function parses a markdown string and outputs a `ReactElement`. Under the hood, the output is generated by the `ReactMarkdown` component from [react-markdown library](https://github.com/remarkjs/react-markdown). The component transforms the markdown to `ReactElement` by using [`remark` parser](https://github.com/remarkjs/remark/tree/main) and [`remark`](https://github.com/remarkjs/remark/blob/main/doc/plugins.md) and [`rehype`](https://github.com/rehypejs/rehype/blob/main/doc/plugins.md) plugins.
7
+
8
+
The default `remark` plugins used by SDK are:
9
+
10
+
1.[`remark-gfm`](https://github.com/remarkjs/remark-gfm) - a third party plugin to add GitHub-like markdown support
11
+
12
+
The default `rehype` plugins (both specific to this SDK) are:
13
+
14
+
1. plugin to render user mentions
15
+
2. plugin to render emojis
16
+
17
+
### Overriding defaults
18
+
19
+
#### Custom `renderText` function
20
+
21
+
If you don't want your chat implementation to support markdown syntax by default you can override the default behaviour by creating a custom `renderText` function which returns a React node and passing it down to the `MessageList` or `MessageSimple` component via `renderText` property.
22
+
23
+
For this particular example we'll create a very primitive one which takes the message text passed down to it as a first argument and returns it wrapped in `span` element:
24
+
25
+
```tsx
26
+
const customRenderText = (text) => {
27
+
return <span>{text}</span>;
28
+
};
29
+
30
+
const App = () => (
31
+
<Chatclient={client}>
32
+
<Channel>
33
+
<Window>
34
+
<MessageListrenderText={customRenderText} />
35
+
</Window>
36
+
</Channel>
37
+
</Chat>
38
+
);
39
+
```
40
+
41
+
Here's also an example with `VirtualizedMessageList` which currently does not accept `renderText` directly:
If you feel like the default output is sufficient, but you'd like to adjust how certain [ReactMarkdown components](https://github.com/remarkjs/react-markdown#appendix-b-components) look like (like `strong` element generated by typing \*\*strong\*\*) you can do so by passing down options to a third argument of the default `renderText` function:
66
+
67
+
:::note
68
+
Types `mention` and `emoji` are special case component types generated by our SDK's custom rehype plugins.
69
+
:::
70
+
71
+
```tsx
72
+
import { renderText } from'stream-chat-react';
73
+
74
+
const StrongComponent = ({ children }) => <bclassName='custom-strong-class-name'>{children}</b>;
If you would like to extend the array of plugins used to parse the markdown, you can provide your own lists of remark resp. rehype plugins. The logic that determines what plugins are used and in which order can be specified in custom `getRehypePlugins` and `getRemarkPlugins` functions. These receive the default array of rehype and remark plugins for further customization. Both custom functions ought to be passed to the third `renderText()` parameter. An example follows:
102
+
103
+
:::note
104
+
It is important to understand what constitutes a rehype or remark plugin. A good start is to learn about the library called [`react-remark`](https://github.com/remarkjs/react-remark) which is used under the hood in our `renderText()` function.
It is also possible to define your custom set of allowed tag names for the elements rendered from the parsed markdown. To perform the tree transformations, you will need to use libraries like [`unist-builder`](https://github.com/syntax-tree/unist-builder) to build the trees and [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit-parents) or [`hast-util-find-and-replace`](https://github.com/syntax-tree/hast-util-find-and-replace) to traverse the tree:
Optional remark plugins `htmlToTextPlugin`, `keepLineBreaksPlugin` introduced with [stream-chat-react@v10.19.0](https://github.com/GetStream/stream-chat-react/releases/tag/v10.19.0) are now included among the default remark plugins. That means that in the messages that originally contained a sequence of multiple newline characters `\n`, these will be replaced with line break elements `<br/>`. The number of line breaks equals count of newline characters minus 1.
490
-
The dependencies used to parse the markdown with [`renderText()` function](../components/core-components/message-list.mdx#rendering-message-text-with-rendertext-function) have been upgraded as a result of that, the following changes had to be performed in stream-chat-react library:
490
+
The dependencies used to parse the markdown with [`renderText` function](../components/message-components/render-text.mdx) have been upgraded as a result of that, the following changes had to be performed in stream-chat-react library:
491
491
492
492
### `ReactMarkdownProps` dropped from `customMarkDownRenderers`
0 commit comments