Skip to content

Commit d0601e6

Browse files
Merge branch 'master' into rc
2 parents dfecf45 + 8dcb1ac commit d0601e6

File tree

6 files changed

+95
-4
lines changed

6 files changed

+95
-4
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
id: message_list_context
3+
sidebar_position: 8
4+
title: MessageListContext
5+
---
6+
7+
The context value is provided by `MessageListContextProvider` which wraps the contents rendered by [`MessageList`](../core-components/message-list.mdx). It exposes API that the default and custom components rendered by `MessageList` can take advantage of. The components that can consume the context are:
8+
9+
- `EmptyStateIndicator` - rendered when there are no messages in the channel. The [`component can be customized`](./component-context.mdx#emptystateindicator).
10+
- `LoadingIndicator` - rendered while loading more messages to the channel. The [`component can be customized`](./component-context.mdx#loadingindicator).
11+
- `MessageListNotifications` - component rendering application notifications. The [`component can be customized`](./component-context.mdx#messagelistnotifications).
12+
- `MessageNotification` - component used to display a single notification message in `MessageListNotifications`. The [`component can be customized`](./component-context.mdx#messagenotification).
13+
- `TypingIndicator` - component indicating that another user is typing a message in a given channel. The [`component can be customized`](./component-context.mdx#typingindicator).
14+
- `Message` and its children - component to render a message. The [`component can be customized`](./component-context.mdx#message).
15+
- `DateSeparator` - component rendered to separate messages posted on different dates. The [`component can be customized`](./component-context.mdx#dateseparator).
16+
- `MessageSystem` - component to display system messages in the message list. The [`component can be customized`](./component-context.mdx#messagesystem).
17+
- `HeaderComponent` - component to be displayed before the oldest message in the message list. The [`component can be customized`](./component-context.mdx#headercomponent).
18+
19+
## Basic Usage
20+
21+
Pull the value from context with our custom hook:
22+
23+
```jsx
24+
import { useMessageListContext } from 'stream-chat-react';
25+
26+
export const CustomComponent = () => {
27+
const { listElement, scrollToBottom } = useMessageListContext();
28+
// component logic ...
29+
return(
30+
{/* rendered elements */}
31+
);
32+
}
33+
```
34+
35+
## Value
36+
37+
### listElement
38+
39+
The scroll container within which the messages and typing indicator are rendered. You may want to perform scroll-to-bottom operations based on the `listElement`'s scroll state.
40+
41+
| Type |
42+
|--------------------------|
43+
| `HTMLDivElement \| null` |
44+
45+
### scrollToBottom
46+
47+
Function that scrolls the `listElement` to the bottom.
48+
49+
| Type |
50+
|--------------|
51+
| `() => void` |

docusaurus/docs/React/components/contexts/translation-context.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
id: translation_context
3-
sidebar_position: 8
3+
sidebar_position: 9
44
title: TranslationContext
55
---
66

docusaurus/docs/React/components/contexts/typing-context.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
id: typing_context
3-
sidebar_position: 9
3+
sidebar_position: 10
44
title: TypingContext
55
---
66

src/components/MessageList/MessageList.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from '../../context/ChannelStateContext';
2020
import { useChatContext } from '../../context/ChatContext';
2121
import { useComponentContext } from '../../context/ComponentContext';
22+
import { MessageListContextProvider } from '../../context/MessageListContext';
2223
import { EmptyStateIndicator as DefaultEmptyStateIndicator } from '../EmptyStateIndicator';
2324
import { InfiniteScroll, InfiniteScrollProps } from '../InfiniteScrollPaginator/InfiniteScroll';
2425
import { LoadingIndicator as DefaultLoadingIndicator } from '../Loading';
@@ -180,7 +181,7 @@ const MessageListWithContext = <
180181
const showEmptyStateIndicator = elements.length === 0 && !threadList;
181182

182183
return (
183-
<>
184+
<MessageListContextProvider value={{ listElement, scrollToBottom }}>
184185
<MessageListMainPanel>
185186
<div
186187
className={`${messageListClass} ${threadListClass}`}
@@ -230,7 +231,7 @@ const MessageListWithContext = <
230231
scrollToBottom={scrollToBottomFromNotification}
231232
threadList={threadList}
232233
/>
233-
</>
234+
</MessageListContextProvider>
234235
);
235236
};
236237

src/context/MessageListContext.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React, { createContext, PropsWithChildren, useContext } from 'react';
2+
3+
export type MessageListContextValue = {
4+
/** The scroll container within which the messages and typing indicator are rendered */
5+
listElement: HTMLDivElement | null;
6+
/** Function that scrolls the `listElement` to the bottom. */
7+
scrollToBottom: () => void;
8+
};
9+
10+
export const MessageListContext = createContext<MessageListContextValue | undefined>(undefined);
11+
12+
/**
13+
* Context provider for components rendered within the `MessageList`
14+
*/
15+
export const MessageListContextProvider = ({
16+
children,
17+
value,
18+
}: PropsWithChildren<{
19+
value: MessageListContextValue;
20+
}>) => (
21+
<MessageListContext.Provider value={value as MessageListContextValue}>
22+
{children}
23+
</MessageListContext.Provider>
24+
);
25+
26+
export const useMessageListContext = (componentName?: string) => {
27+
const contextValue = useContext(MessageListContext);
28+
29+
if (!contextValue) {
30+
console.warn(
31+
`The useMessageListContext hook was called outside of the MessageListContext provider. Make sure this hook is called within the MessageList component. The errored call is located in the ${componentName} component.`,
32+
);
33+
34+
return {} as MessageListContextValue;
35+
}
36+
37+
return contextValue as MessageListContextValue;
38+
};

src/context/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export * from './ChatContext';
44
export * from './ComponentContext';
55
export * from './MessageContext';
66
export * from './MessageInputContext';
7+
export * from './MessageListContext';
78
export * from './TranslationContext';
89
export * from './TypingContext';

0 commit comments

Comments
 (0)