|
| 1 | +--- |
| 2 | +id: view-components-and-thread-adapter |
| 3 | +title: View Components and Thread Adapter |
| 4 | +--- |
| 5 | + |
| 6 | +ChatView set of components is a view switching mechanism that can be utilised by integrators to quickly implement switching between thread and channel views. |
| 7 | + |
| 8 | +Available components: |
| 9 | + |
| 10 | +- `ChatView` - wrapper with context holding the information about currently active view (`channels` & `threads`) |
| 11 | +- `ChatView.Threads` - view used for thread-focused application structure with `ThreadsViewContext` that _can be_ utilised by `ThreadList` to set active thread |
| 12 | +- `ChatView.Channels` - view used for channel-focused application structure |
| 13 | +- `ChatView.Selector` - list with buttons with bound actions for switching views |
| 14 | +- `ChatView.ThreadAdapter` - utilises `ThreadsViewContext` and wraps `Thread` component in necessary `ThreadProvider` |
| 15 | + |
| 16 | +This set of components is provided as-is and offers very limited customizability as the underlying logic is super simple. Integrators are encouraged to build their own switching system if they require it. |
| 17 | + |
| 18 | +### Usage |
| 19 | + |
| 20 | +```tsx |
| 21 | +import { |
| 22 | + Chat, |
| 23 | + ChatView, |
| 24 | + ChannelList, |
| 25 | + Channel, |
| 26 | + ThreadList, |
| 27 | + Thread, |
| 28 | + Window, |
| 29 | +} from 'stream-chat-react'; |
| 30 | + |
| 31 | +// application structure which allows users to switch between views |
| 32 | +<Chat client={client}> |
| 33 | + <ChatView> |
| 34 | + <ChatView.Selector /> |
| 35 | + {/* channel-focused structure */} |
| 36 | + <ChatView.Channels> |
| 37 | + <ChannelList filters={filters} options={options} sort={sort} /> |
| 38 | + <Channel> |
| 39 | + <Window> |
| 40 | + <ChannelHeader /> |
| 41 | + <MessageList /> |
| 42 | + <MessageInput focus /> |
| 43 | + </Window> |
| 44 | + <Thread /> |
| 45 | + </Channel> |
| 46 | + </ChatView.Channels> |
| 47 | + {/* thread-focused structure */} |
| 48 | + <ChatView.Threads> |
| 49 | + <ThreadList /> |
| 50 | + <ChatView.ThreadAdapter> |
| 51 | + <Thread /> |
| 52 | + </ChatView.ThreadAdapter> |
| 53 | + </ChatView.Threads> |
| 54 | + </ChatView> |
| 55 | +</Chat>; |
| 56 | +``` |
| 57 | + |
| 58 | +### Custom Thread-focused Structure |
| 59 | + |
| 60 | +To build your custom thread-focused structure you'll need these four baseline components; `Thread`, `ThreadList`, `ThreadProvier` and `WithComponents` for component overrides. |
| 61 | + |
| 62 | +:::note |
| 63 | +For presentation purposes our custom `ThreadListItemUi` component is loosely defined within `CustomThreadsView` and thus it isn't stable. To achieve best performance make sure your components |
| 64 | +are stable and defined outside other component's scope. |
| 65 | +::: |
| 66 | + |
| 67 | +```tsx |
| 68 | +import { WithComponents, ThreadListItemUi, ThreadList, ThreadProvider } from 'stream-chat-react'; |
| 69 | + |
| 70 | +export const CustomThreadsView = () => { |
| 71 | + const [activeThread, setActiveThread] = useState(undefined); |
| 72 | + |
| 73 | + return ( |
| 74 | + <div className='custom-threads-view'> |
| 75 | + <WithComponents |
| 76 | + overrides={{ |
| 77 | + ThreadListItemUi: () => { |
| 78 | + const thread = useThreadListItemContext()!; |
| 79 | + return ( |
| 80 | + <ThreadListItemUi |
| 81 | + onPointerDown={() => setActiveThread(thread)} |
| 82 | + aria-selected={thread === activeThread} |
| 83 | + /> |
| 84 | + ); |
| 85 | + }, |
| 86 | + }} |
| 87 | + > |
| 88 | + <ThreadList /> |
| 89 | + </WithComponents> |
| 90 | + |
| 91 | + <ThreadProvider thread={activeThread}> |
| 92 | + <Thread /> |
| 93 | + </ThreadProvider> |
| 94 | + </div> |
| 95 | + ); |
| 96 | +}; |
| 97 | +``` |
0 commit comments