Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions ai-docs/ai-migration-v14-v15.md
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,47 @@ export const App = ({ client }: { client: StreamChat }) => (
- **`<Slot>` children replace the renderer.** A `<Slot>` with children renders them for the slot's own binding; one without children renders the `slotRenderers` entry for the bound kind. Layers stacked over a slot always render through `slotRenderers`, so register a renderer for every kind that can land there.
- **`ChannelSlot` / `ThreadSlot`** resolve the entity bound to their slot and render `<Channel>` / `<Thread>` around their children. Without one bound they render `fallback`. UI that used to live inside `<Channel>` but must show while no channel is open (an empty state, a "create channel" panel) goes in `ChannelSlot`'s `fallback`, wrapped in `ChannelPlaceholder` so it fills the same column.
- **`ThreadSlot` hides its slot while empty.** `hideIfEmpty` defaults to `true`: with no thread bound it calls `layoutController.hide(slot)`, which marks that `<Slot>` section `str-chat__chat-view__slot--hidden`, and unhides it when a thread opens. Pass `hideIfEmpty={false}` to keep the slot visible (for example to show a `fallback`).
- [`examples/vite/src/ChatLayout/Panels.tsx`](https://github.com/GetStream/stream-chat-react/blob/master/examples/vite/src/ChatLayout/Panels.tsx) (`ChannelsPanels`, `ThreadsPanels`) is a complete app layout built this way, with resizable side panels.
- **Layers come from `<Slot>`.** `ChannelSlot` and `ThreadSlot` show only the slot's base binding. Wrapped in a `<Slot>` for the same slot, as above, they get the layers stacked over it (a member's profile, a second channel), and they stay mounted beneath, keeping their scroll position, until the last layer closes.
- [`examples/vite/src/ChatLayout/`](https://github.com/GetStream/stream-chat-react/tree/master/examples/vite/src/ChatLayout) (`Views/` with `ChannelsView` and `ThreadsView`, `Panels/` with `chatViewSlotRenderers`) is a complete app layout built on `WorkspaceLayout` ([below](#composing-a-layout-with-workspacelayout)), with a collapsible, resizable sidebar and a resizable secondary panel.

#### Composing a layout with `WorkspaceLayout`

`WorkspaceLayout` (`stream-chat-react/slot-layout`) is the layout the built-in `layout='nav-rail-entity-list-workspace'` renders. A view can render it too, to keep the SDK's slot behaviour while laying out its own columns:

```tsx
import { useRef } from 'react';
import { ChannelNavigation } from 'stream-chat-react';
import { ChatView, WorkspaceLayout } from 'stream-chat-react/slot-layout';

// Your own handle; `layoutRef` lets one measure the layout while resizing.
const ResizeHandle = () => <div aria-hidden className='app-resize-handle' />;

const ChannelsView = () => {
const layoutRef = useRef<HTMLDivElement>(null);
return (
<WorkspaceLayout
className='app-channels-layout'
ref={layoutRef}
renderBetweenSlots={() => <ResizeHandle />}
sidebar={
<div className='app-sidebar'>
<ChatView.Selector />
<ChannelNavigation />
</div>
}
slots={[
{ className: 'app-main-panel', slot: 'channel' },
{ className: 'app-secondary-panel', slot: 'channel-thread' },
]}
/>
);
};
```

- **Each slot is a `<Slot>`.** Without `content` it renders its binding through `slotRenderers`, with its layers over it. `fallback` shows while it renders nothing. Each renderer runs in a component of its own (one per slot entry: the base and each layer), so it may call hooks.
- **The placeholder** (`ChatViewEmptyPlaceholder`) replaces the slots while none of them shows anything: no `content`, no binding of a kind with a renderer, no `fallback` and no layer. A binding counts whatever its renderer returns. Hidden slots count too, so the placeholder never unmounts a hidden list. `navRail` and `sidebar` stay.
- **Your columns.** `sidebar` renders as given between `navRail` and the slots. `renderBetweenSlots(previousSlot, nextSlot, { isPreviousSlotOccupied, isNextSlotOccupied })` renders between each two neighbouring slots, for every pair, including a slot that shows nothing. The third argument says whether each neighbour shows anything and is not hidden, so a resize handle can hide itself next to a closed slot. Each slot's `className`, `style` and `elementRef` go on its `section`, and `className` and `ref` on the layout's root.
- **Breakpoints stay in your CSS.** The SDK hides a slot that renders nothing (`.str-chat__chat-view__slot:empty`); use the same selector to tell which columns are open. What a slot shows is the slot's direct child. While a layer covers it, it stays mounted and gets an inline `display: none` (plus `aria-hidden` and `inert`), and the layer renders in a `div.str-chat__chat-view__slot-layer`.

#### Where an opened entity lands

Expand Down Expand Up @@ -813,7 +853,7 @@ The channel-detail "send direct message" member action and the pinned-message en

Nothing to wire. `ChatView` provides the navigation and binds what is opened into a layout slot. To customize it (for example, open beside the current channel on ⌘/ctrl-click), pass `deriveWorkspaceNavigation`, which receives the SDK-derived navigation and returns the members to override. It must be referentially stable.

The same holds for your own components. Anything that reads `useWorkspaceNavigation()` (an "in a call" panel, a header badge for the open channel) has to render inside `ChatView`. Pass it as a `ChatView` child in a `views` layout: children render inside the provider, before the active view, and stay mounted when the view switches. The built-in `layout="nav-rail-entity-list-workspace"` does not render `children`. `openChannels` lists the active view's channels, so it is empty while the threads view is shown.
The same holds for your own components. Anything that reads `useWorkspaceNavigation()` (an "in a call" panel, a header badge for the open channel) has to render inside `ChatView`. Pass it as a `ChatView` child, with `views` or with the built-in `layout="nav-rail-entity-list-workspace"`: children render inside the provider, before the active view or the layout, and stay mounted when the view switches. Children that render markup sit next to the view or layout in `ChatView`'s root, so position them with your own CSS. `openChannels` lists the active view's channels, so it is empty while the threads view is shown.

```tsx
// `CallControls` stands for a component of your own.
Expand Down Expand Up @@ -3753,7 +3793,7 @@ v14 laid the thread beside the channel inside `Channel` itself: `.str-chat__chan
}
```

A panel your layout renders without `<Slot>` (for example a `ThreadSlot` placed directly in a view, as [`examples/vite`](https://github.com/GetStream/stream-chat-react/tree/master/examples/vite) does) has no slot section; size the element you wrap it in.
A panel your layout renders without `<Slot>` (for example a `ThreadSlot` placed directly in a view) has no slot section; size the element you wrap it in. `WorkspaceLayout` renders each of its slots as a `<Slot>`, and takes a `className` and a `style` for each ([composing a layout](#composing-a-layout-with-workspacelayout)).

**Give the lists a width.** `ChannelNavigation` / `ChannelList` and `ThreadList` size themselves from `--str-chat__channel-list-width` and `--str-chat__thread-list-width`. Both default to `100%` with `flex-shrink: 0`. In a view that puts a list beside a `<Slot>` (the [channels and threads views](#channels-and-threads-views) above), the list takes the whole row, the slot collapses to nothing and the page scrolls sideways. Set both on the `ChatView` root (`div.str-chat.str-chat__chat-view`):

Expand Down
2 changes: 1 addition & 1 deletion ai-docs/v15-breaking-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ const App = () => (
);
```

A threads view is declared the same way (a `{ id: 'threads', slots: [...] }` layout and a `threads` entry in `views` rendering `<ThreadList />` plus a `ThreadSlot`); `examples/vite/src/ChatLayout/Panels.tsx` (`ThreadsPanels`) is the reference.
A threads view is declared the same way (a `{ id: 'threads', slots: [...] }` layout and a `threads` entry in `views` rendering `<ThreadList />` plus a `ThreadSlot`); `examples/vite/src/ChatLayout/Views/ThreadsView.tsx` (`ThreadsView`) is the reference.

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
CHANNELS_SELECTOR_BUTTON_TARGET_QUERY,
THREAD_LIST_TARGET_ID,
THREAD_MESSAGE_COMPOSER_TEXTAREA_TARGET_ID,
} from '../ChatLayout/Panels.tsx';
} from './skipNavigationTargets';

export const CHAT_SKIP_NAVIGATION_TARGET_ID = 'app-chat-skip-navigation';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Elements the quick-navigation links jump to. The panels and lists put these ids on their elements.
export const CHANNEL_MESSAGE_COMPOSER_TEXTAREA_TARGET_ID =
'app-channel-message-composer-textarea';
export const THREAD_MESSAGE_COMPOSER_TEXTAREA_TARGET_ID =
'app-thread-message-composer-textarea';
export const CHANNEL_LIST_TARGET_ID = 'app-channel-list';
export const THREAD_LIST_TARGET_ID = 'app-thread-list';
export const CHANNELS_SELECTOR_BUTTON_TARGET_QUERY =
'[id^="str-chat__chat-view-"][id$="-tab-channels"]';
16 changes: 9 additions & 7 deletions examples/vite/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,21 @@ import { ChatSkipNavigation } from './AccessibilityNavigation/ChatSkipNavigation
import { SlotGeometryProvider } from 'stream-chat-react/slot-geometry';
import { AlsoSentInChannelIndicator } from './ChatLayout/AlsoSentInChannelIndicator.tsx';
import { useAdditiveWorkspaceNavigation } from './ChatLayout/useAdditiveWorkspaceNavigation.ts';
import { ChannelsPanels, ThreadsPanels } from './ChatLayout/Panels.tsx';
import { SidebarProvider } from './ChatLayout/SidebarContext.tsx';
import { chatViewSlotRenderers } from './ChatLayout/Panels';
import { ChannelsView, ThreadsView } from './ChatLayout/Views';
import { SidebarProvider } from './Sidebar/SidebarContext.tsx';
import {
ChatViewSelectorWidthSync,
PanelLayoutStyleSync,
SidebarLayoutSync,
} from './ChatLayout/Resize.tsx';
} from './ChatLayout/Resize';
import {
getFocusTargetsFromUrl,
getInitialChannelIdFromUrl,
getInitialChatViewFromUrl,
getInitialThreadIdFromUrl,
WorkspaceUrlSync,
} from './ChatLayout/WorkspaceUrlSync.tsx';
import { getFocusTargetsFromUrl } from './ChatLayout/focusUrlParam.ts';
} from './ChatLayout/UrlSync';
import { LoadingScreen } from './LoadingScreen/LoadingScreen.tsx';
import {
resolveSingleChannel,
Expand Down Expand Up @@ -634,14 +635,14 @@ const App = () => {
const chatViews = useMemo(
() => ({
channels: (
<ChannelsPanels
<ChannelsView
iconOnly={chatView.iconOnly}
initialChannelId={initialChannelId ?? undefined}
itemSet={chatViewSelectorItemSet}
/>
),
threads: (
<ThreadsPanels iconOnly={chatView.iconOnly} itemSet={chatViewSelectorItemSet} />
<ThreadsView iconOnly={chatView.iconOnly} itemSet={chatViewSelectorItemSet} />
),
}),
[chatView.iconOnly, initialChannelId],
Expand Down Expand Up @@ -740,6 +741,7 @@ const App = () => {
deriveWorkspaceNavigation={deriveWorkspaceNavigation}
dialogManagerId={globalDialogManager}
layouts={chatViewLayouts}
slotRenderers={chatViewSlotRenderers}
views={chatViews}
>
<WorkspaceUrlSync />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import { useAppSettingsSelector } from '../AppSettings/state';
import { getChannelMembersHeaderActionSet } from '../AppSettings/tabs/ChannelDetail';
import { ChannelMembersRemoveView } from './ChannelMembersRemoveView';
import { CHANNEL_THREAD_SLOT } from './constants.ts';
import { CHANNEL_THREAD_SLOT } from '../ChatLayout/constants.ts';

const ChannelMembersRemoveTitle = () => {
const { t } = useTranslationContext();
Expand Down
2 changes: 2 additions & 0 deletions examples/vite/src/ChannelDetail/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// The channel details: the SDK's ChannelDetail configured for this app, with bulk member removal.
export * from './ConfiguredChannelDetail';
3 changes: 3 additions & 0 deletions examples/vite/src/ChannelList/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// The channels view's channel list: list switching, presence avatars, scroll-to-active.
export * from './ChannelListItemPresence';
export * from './SwitchableChannelNavigation';
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { MAIN_CHANNEL_SLOT } from './constants';
* geometry provider) to reveal the channel column when jumping to it from inside a reply thread.
*
* The actual "close the covering thread" decision is deferred to a channels-view effect
* (in `ResponsiveChannelPanels`), because it must run *after* navigation: in the cross-view
* (in `ChannelsView`), because it must run *after* navigation: in the cross-view
* case (threads β†’ channels) the channel isn't mounted or measurable at click time. The effect closes
* the thread only when the geometry plugin reports the channel is actually obscured, so wide
* side-by-side layouts keep the thread. This scopes the behavior to this navigation only β€” it does
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type { Channel as StreamChannel } from 'stream-chat';
import {
asDynamicKey,
GlobalModal,
Prompt,
useComponentContext,
useTranslationContext,
} from 'stream-chat-react';
import {
ChannelDetailProvider,
ChannelMemberDetail,
} from 'stream-chat-react/channel-detail';

import { useChannelMember } from './hooks/useChannelMember';

// A member's profile in a modal, opened with a plain click on a message avatar or a mention.
export const ChannelMemberDetailModal = ({
channel,
onClose,
userId,
}: {
channel: StreamChannel;
onClose: () => void;
userId: string;
}) => {
const { t } = useTranslationContext();
const { Modal = GlobalModal } = useComponentContext();
const member = useChannelMember(channel, userId);

return (
<Modal
aria-label={t(asDynamicKey('viteExample.memberDetail.ariaLabel'), 'Member details')}
dialogRootProps={{ className: 'str-chat__channel-detail-modal' }}
onClose={onClose}
open
>
{/* The prompt is the modal's surface, styled as the channel details but sized as the user's
own profile modal. */}
<Prompt.Root className='str-chat__channel-detail app-member-detail-modal'>
{member ? (
<ChannelDetailProvider channel={channel} onClose={onClose}>
<ChannelMemberDetail layout='tabs' member={member} />
</ChannelDetailProvider>
) : null}
</Prompt.Root>
</Modal>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
type BaseSyntheticEvent,
createContext,
type PropsWithChildren,
useCallback,
useState,
} from 'react';
import type { Channel as StreamChannel } from 'stream-chat';
import { useChatViewNavigation, useSlotChannel } from 'stream-chat-react/slot-layout';

import { CHANNEL_THREAD_SLOT, MAIN_CHANNEL_SLOT } from '../constants';
import { ChannelMemberDetailModal } from './ChannelMemberDetailModal';
import { opensMemberDetailBeside } from './utils';

export type OpenMemberDetail = (
userId: string | undefined,
event: BaseSyntheticEvent,
channel: StreamChannel,
) => void;

// Opens a member's profile from a click on a message avatar or a mention. Panels are rendered by
// the slots, not by the view, so the view provides this to them; outside the channels view it is
// missing and those clicks open nothing.
export const MemberDetailContext = createContext<OpenMemberDetail | undefined>(undefined);

// A plain click opens the profile in a modal, which this provider renders; a ⌘/ctrl-click opens it
// in the secondary slot, as the `userProfile` kind (`ChannelMemberDetailPanel` renders it).
export const MemberDetailProvider = ({ children }: PropsWithChildren) => {
const { open } = useChatViewNavigation();
const mainChannel = useSlotChannel({ slot: MAIN_CHANNEL_SLOT });
const [modalMember, setModalMember] = useState<{
channel: StreamChannel;
userId: string;
}>();

const openMemberDetail = useCallback<OpenMemberDetail>(
(userId, event, channel) => {
if (!userId) return;
if (!opensMemberDetailBeside(event, channel, mainChannel)) {
setModalMember({ channel, userId });
return;
}
open(
{ key: userId, kind: 'userProfile', source: { userId } },
{ slot: CHANNEL_THREAD_SLOT },
);
},
[mainChannel, open],
);

return (
<MemberDetailContext.Provider value={openMemberDetail}>
{children}
{modalMember && (
<ChannelMemberDetailModal
channel={modalMember.channel}
onClose={() => setModalMember(undefined)}
userId={modalMember.userId}
/>
)}
</MemberDetailContext.Provider>
);
};
2 changes: 2 additions & 0 deletions examples/vite/src/ChatLayout/MemberDetail/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './useChannelMember';
export * from './useMemberDetailClickHandlers';
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useEffect, useState } from 'react';
import type { ChannelMemberResponse, Channel as StreamChannel } from 'stream-chat';

// The channel member with `userId`: the cached one, or, for a member not loaded yet (a large
// channel, say), the one fetched for it. `undefined` while fetching, `null` when the channel has no
// such member or the fetch failed.
export const useChannelMember = (channel: StreamChannel, userId: string) => {
const [member, setMember] = useState<ChannelMemberResponse | null | undefined>(
() => channel.state.members[userId],
);

useEffect(() => {
const existing = channel.state.members[userId];
if (existing) {
setMember(existing);
return;
}
let cancelled = false;
setMember(undefined);
channel
.queryMembers({ payload: { filter_conditions: { id: { $in: [userId] } } } })
.then((response) => {
if (!cancelled) setMember(response.members[0] ?? null);
})
.catch(() => {
if (!cancelled) setMember(null);
});
return () => {
cancelled = true;
};
}, [channel, userId]);

return member;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useContext, useMemo } from 'react';
import type { Channel as StreamChannel } from 'stream-chat';
import type { MessageListProps } from 'stream-chat-react';

import { MemberDetailContext } from '../MemberDetailContext';
import { getMentionedUserId } from '../utils';

// The message list props that open a member's profile from `channel`'s messages. The virtualized
// list doesn't forward them, so its avatars and mentions open nothing.
export const useMemberDetailClickHandlers = (
channel: StreamChannel,
): Pick<MessageListProps, 'onMentionsClick' | 'onUserClick'> => {
const openMemberDetail = useContext(MemberDetailContext);
return useMemo(
() =>
openMemberDetail
? {
onMentionsClick: (event, message) =>
openMemberDetail(getMentionedUserId(event, message), event, channel),
onUserClick: (event, user) => openMemberDetail(user.id, event, channel),
}
: {},
[channel, openMemberDetail],
);
};
3 changes: 3 additions & 0 deletions examples/vite/src/ChatLayout/MemberDetail/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './ChannelMemberDetailModal';
export * from './hooks';
export * from './MemberDetailContext';
Loading