Skip to content

Commit b2a8ded

Browse files
authored
fix: bubble-aware colors for quotes, location, polls and the AI stop button (#3411)
1 parent 91b2c68 commit b2a8ded

13 files changed

Lines changed: 298 additions & 39 deletions

File tree

‎examples/cookbook/src/recipes/theming/replacing-the-icon-set/lucide-icons.css‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,6 @@
88
fill: none;
99
}
1010

11-
/*
12-
* The SDK's Stop button has no icon: its stylesheet reads a variable it never defines. The
13-
* recipe gives it one: Lucide's circle-stop becomes the button's mask, and the accent token
14-
* fills the shape the mask lets through. Only the mask's shape counts, not its stroke color.
15-
*/
16-
.str-chat__stop-ai-generation-button {
17-
background-color: var(--str-chat__accent-primary);
18-
mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='12' cy='12' r='10'/%3E%3Crect x='9' y='9' width='6' height='6' rx='1'/%3E%3C/svg%3E")
19-
center / contain no-repeat;
20-
}
21-
2211
.cookbook-file-icon {
2312
color: var(--str-chat__accent-primary);
2413
}

‎examples/cookbook/src/recipes/theming/replacing-the-icon-set/lucideIcons.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import {
6363
Search,
6464
Send,
6565
Shield,
66+
Square,
6667
SquareSlash,
6768
Trash,
6869
TriangleAlert,
@@ -84,7 +85,7 @@ import { createIcon, type IconComponent, type IconName } from 'stream-chat-react
8485
type LucideOptions = {
8586
/**
8687
* Draws the shape solid. Lucide draws outlines by default. Only plain shapes are filled (play,
87-
* pause, video, message bubble): a Lucide icon with marks inside, such as the eye's pupil, the
88+
* pause, stop, video, message bubble): a Lucide icon with marks inside, such as the eye's pupil, the
8889
* alerts' exclamation mark or the thread's arrow, would lose them, so those `…Fill` icons stay
8990
* outlines.
9091
*/
@@ -184,6 +185,7 @@ const lucideIconMap: Record<IconName, [LucideIcon, LucideOptions?]> = {
184185
IconSearch: [Search, { mirrorInRtl: true }],
185186
IconSend: [Send, { mirrorInRtl: true }],
186187
IconShield: [Shield],
188+
IconStopFill: [Square, { filled: true }],
187189
IconThread: [MessageSquareReply],
188190
IconThreadFill: [MessageSquareReply],
189191
IconTranslate: [Languages],

‎src/components/Attachment/styling/Geolocation.scss‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
display: flex;
5151
justify-content: center;
5252
padding: 0.5rem;
53-
background-color: var(--str-chat__background-core-surface-subtle);
5453
font: var(--str-chat__font-caption-default);
5554

5655
.str-chat__message-attachment-geolocation__status--active {
@@ -59,13 +58,28 @@
5958
align-items: center;
6059
gap: 0.375rem;
6160

61+
// The button sits on the bubble, so it takes the bubble's colors instead of the app-wide
62+
// secondary button colors.
63+
.str-chat__message-attachment-geolocation__stop-sharing-button {
64+
--str-chat__button-secondary-text: var(
65+
--chat-text,
66+
var(--str-chat__chat-text-incoming)
67+
);
68+
--str-chat__button-secondary-border: var(
69+
--chat-border-on-chat,
70+
var(--str-chat__chat-border-on-chat-incoming)
71+
);
72+
}
73+
74+
// The label sits on the bubble, so it takes the bubble's text color, which is legible on
75+
// either side and theme.
6276
.str-chat__message-attachment-geolocation__status--active-status {
63-
color: var(--str-chat__accent-success);
77+
color: var(--chat-text, var(--str-chat__chat-text-incoming));
6478
}
6579

80+
// Keeps the status area's font, like the static location's "Current location" line.
6681
.str-chat__message-attachment-geolocation__status--active-until {
6782
text-transform: lowercase;
68-
font: var(--str-chat__font-metadata-emphasis);
6983
}
7084
}
7185
}

‎src/components/Icons/icons.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,11 @@ export const IconPauseFill = createIcon(
741741
<path d='M16.875 3.75V16.25C16.875 16.5815 16.7433 16.8995 16.5089 17.1339C16.2745 17.3683 15.9565 17.5 15.625 17.5H12.5C12.1685 17.5 11.8505 17.3683 11.6161 17.1339C11.3817 16.8995 11.25 16.5815 11.25 16.25V3.75C11.25 3.41848 11.3817 3.10054 11.6161 2.86612C11.8505 2.6317 12.1685 2.5 12.5 2.5H15.625C15.9565 2.5 16.2745 2.6317 16.5089 2.86612C16.7433 3.10054 16.875 3.41848 16.875 3.75ZM7.5 2.5H4.375C4.04348 2.5 3.72554 2.6317 3.49112 2.86612C3.2567 3.10054 3.125 3.41848 3.125 3.75V16.25C3.125 16.5815 3.2567 16.8995 3.49112 17.1339C3.72554 17.3683 4.04348 17.5 4.375 17.5H7.5C7.83152 17.5 8.14946 17.3683 8.38388 17.1339C8.6183 16.8995 8.75 16.5815 8.75 16.25V3.75C8.75 3.41848 8.6183 3.10054 8.38388 2.86612C8.14946 2.6317 7.83152 2.5 7.5 2.5Z' />,
742742
);
743743

744+
export const IconStopFill = createIcon(
745+
'IconStopFill',
746+
<path d='M16.875 4.375V15.625C16.875 15.9565 16.7433 16.2745 16.5089 16.5089C16.2745 16.7433 15.9565 16.875 15.625 16.875H4.375C4.04348 16.875 3.72554 16.7433 3.49112 16.5089C3.2567 16.2745 3.125 15.9565 3.125 15.625V4.375C3.125 4.04348 3.2567 3.72554 3.49112 3.49112C3.72554 3.2567 4.04348 3.125 4.375 3.125H15.625C15.9565 3.125 16.2745 3.2567 16.5089 3.49112C16.7433 3.72554 16.875 4.04348 16.875 4.375Z' />,
747+
);
748+
744749
export const IconUser = createIcon(
745750
'IconUser',
746751
<path

‎src/components/Message/styling/QuotedMessage.scss‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
);
55

66
.str-chat__message {
7+
// Inside a message the quote sits on the bubble, so it takes the bubble's side tokens
8+
// whoever the quoted author is. The author-based --own modifier applies only in the composer.
79
.str-chat__quoted-message-preview {
810
cursor: pointer;
9-
background-color: var(--chat-bg-attachment-incoming);
11+
background-color: var(
12+
--chat-bg-attachment,
13+
var(--str-chat__chat-bg-attachment-incoming)
14+
);
15+
color: var(--chat-text, var(--str-chat__chat-text-incoming));
1016
width: calc(
1117
var(--str-chat__message-with-attachment-max-width) - calc(
1218
2 * var(--str-chat__spacing-xs)
@@ -15,8 +21,12 @@
1521
max-width: 100%;
1622
min-width: 0;
1723
}
18-
.str-chat__quoted-message-preview--own {
19-
background-color: var(--chat-bg-attachment-outgoing);
24+
25+
.str-chat__quoted-message-indicator {
26+
background-color: var(
27+
--chat-reply-indicator,
28+
var(--str-chat__chat-reply-indicator-incoming)
29+
);
2030
}
2131
}
2232
}

‎src/components/MessageComposer/MessageComposerActions.tsx‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,25 @@ export const MessageComposerActions = () => {
7575
// Accounts for an iOS requirement, see https://bugs.webkit.org/show_bug.cgi?id=252303
7676
const recordingEnabled = !!(recordingController.recorder && navigator.mediaDevices);
7777

78-
let content = SendButton ? (
79-
<SendButton sendMessage={submitMessageFn} />
80-
) : (
81-
<DefaultSendButton sendMessage={submitMessageFn}>
82-
{editedMessage || command ? <IconCheckmark /> : <IconSend />}
83-
</DefaultSendButton>
84-
);
85-
78+
// One control fills the slot, first match wins. While the AI is answering, stopping it is the
79+
// only useful action, so the stop button outranks everything else, `hideSendButton` included.
80+
let content: React.ReactNode;
8681
if (shouldDisplayStopAIGeneration) {
8782
content = <StopAIGenerationButton onClick={stopGenerating} />;
88-
} else if (hideSendButton) return null;
89-
90-
if (isCooldownActive) {
83+
} else if (hideSendButton) {
84+
return null;
85+
} else if (isCooldownActive) {
9186
content = <CooldownTimer />;
9287
} else if (contentIsEmpty && !editedMessage && !command && recordingEnabled) {
9388
content = <AudioRecordingButtonWithNotification />;
89+
} else if (SendButton) {
90+
content = <SendButton sendMessage={submitMessageFn} />;
91+
} else {
92+
content = (
93+
<DefaultSendButton sendMessage={submitMessageFn}>
94+
{editedMessage || command ? <IconCheckmark /> : <IconSend />}
95+
</DefaultSendButton>
96+
);
9497
}
9598

9699
return <div className='str-chat__message-composer__actions'>{content}</div>;
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
import React from 'react';
2-
import { useTranslationContext } from '../../context';
2+
import { useComponentContextIcons, useTranslationContext } from '../../context';
3+
import { Button } from '../Button';
34

45
export type StopAIGenerationButtonProps = React.ComponentProps<'button'>;
56

67
export const StopAIGenerationButton = ({
8+
children,
79
onClick,
810
...restProps
911
}: StopAIGenerationButtonProps) => {
12+
const { IconStopFill } = useComponentContextIcons();
1013
const { t } = useTranslationContext();
1114
return (
12-
<button
15+
<Button
16+
appearance='solid'
1317
aria-label={t(
1418
'messageComposer.stopAiGeneration.stopAiGeneration.ariaLabel',
1519
'Stop AI Generation',
1620
)}
21+
circular
1722
className='str-chat__stop-ai-generation-button'
1823
data-testid='stop-ai-generation-button'
1924
onClick={onClick}
25+
size='sm'
26+
variant='primary'
2027
{...restProps}
21-
/>
28+
>
29+
{children ?? <IconStopFill />}
30+
</Button>
2231
);
2332
};
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// MessageComposer's send/update hooks import `useChannel` from this root barrel, and a deep
2+
// component import first would re-enter it and leave `useChannel` undefined under Vitest.
3+
import '../../..';
4+
import React from 'react';
5+
import { act, render, screen } from '@testing-library/react';
6+
import { fromPartial } from '@total-typescript/shoehorn';
7+
import { AIStates } from 'stream-chat';
8+
import type { AIState, Channel as ChannelType, CooldownTimerState } from 'stream-chat';
9+
10+
import { MessageComposer } from '../MessageComposer';
11+
import { Channel } from '../../Channel';
12+
import { Chat } from '../../Chat';
13+
import { WithComponents } from '../../../context';
14+
import type { ComponentContextValue } from '../../../context';
15+
import { initClientWithChannels } from '../../../mock-builders';
16+
import { MediaRecorderMock } from '../../../mock-builders/browser';
17+
18+
const STOP_BUTTON_TEST_ID = 'stop-ai-generation-button';
19+
const RECORDING_BUTTON_TEST_ID = 'start-recording-audio-button';
20+
const COOLDOWN_TIMER_TEST_ID = 'cooldown-timer';
21+
const COOLDOWN_SECONDS = 30;
22+
23+
window.MediaRecorder = MediaRecorderMock as unknown as typeof MediaRecorder;
24+
25+
// Renders an empty composer with audio recording enabled, on a channel that allows uploads (which
26+
// a recording needs) and has a cooldown configured. `overrides` go through `WithComponents`.
27+
const renderComposer = async ({
28+
hideSendButton,
29+
overrides = {},
30+
}: {
31+
hideSendButton?: boolean;
32+
overrides?: Partial<ComponentContextValue>;
33+
} = {}) => {
34+
const {
35+
channels: [channel],
36+
client,
37+
} = await initClientWithChannels({
38+
channelsData: [
39+
{ channel: { cooldown: COOLDOWN_SECONDS, own_capabilities: ['upload-file'] } },
40+
],
41+
});
42+
await act(() => {
43+
render(
44+
<WithComponents overrides={overrides}>
45+
<Chat client={client}>
46+
<Channel channel={channel}>
47+
<MessageComposer audioRecordingEnabled hideSendButton={hideSendButton} />
48+
</Channel>
49+
</Chat>
50+
</WithComponents>,
51+
);
52+
});
53+
return channel;
54+
};
55+
56+
const setAIState = (channel: ChannelType, aiState: AIState) =>
57+
act(() => {
58+
channel.state.partialNext({ aiState });
59+
});
60+
61+
const startCooldown = (channel: ChannelType) =>
62+
act(() => {
63+
channel.cooldownTimer.state.next(
64+
fromPartial<CooldownTimerState>({ cooldownRemaining: COOLDOWN_SECONDS }),
65+
);
66+
});
67+
68+
describe('MessageComposerActions', () => {
69+
beforeEach(() => {
70+
(navigator as any).mediaDevices = { getUserMedia: vi.fn().mockResolvedValue({}) };
71+
});
72+
73+
afterEach(() => {
74+
(navigator as any).mediaDevices = undefined;
75+
});
76+
77+
it('shows the recording button for an empty input while the AI is idle', async () => {
78+
await renderComposer();
79+
80+
expect(screen.getByTestId(RECORDING_BUTTON_TEST_ID)).toBeInTheDocument();
81+
expect(screen.queryByTestId(STOP_BUTTON_TEST_ID)).not.toBeInTheDocument();
82+
});
83+
84+
it.each([AIStates.Thinking, AIStates.Generating])(
85+
'shows the stop button instead of the recording button for an empty input while the AI state is %s',
86+
async (aiState) => {
87+
const channel = await renderComposer();
88+
89+
await setAIState(channel, aiState);
90+
91+
expect(screen.getByTestId(STOP_BUTTON_TEST_ID)).toBeInTheDocument();
92+
expect(screen.queryByTestId(RECORDING_BUTTON_TEST_ID)).not.toBeInTheDocument();
93+
},
94+
);
95+
96+
it('shows the cooldown timer during a cooldown while the AI is idle', async () => {
97+
const channel = await renderComposer();
98+
99+
await startCooldown(channel);
100+
101+
expect(screen.getByTestId(COOLDOWN_TIMER_TEST_ID)).toBeInTheDocument();
102+
expect(screen.queryByTestId(STOP_BUTTON_TEST_ID)).not.toBeInTheDocument();
103+
});
104+
105+
it('shows the stop button instead of the cooldown timer while the AI is generating', async () => {
106+
const channel = await renderComposer();
107+
108+
await startCooldown(channel);
109+
await setAIState(channel, AIStates.Generating);
110+
111+
expect(screen.getByTestId(STOP_BUTTON_TEST_ID)).toBeInTheDocument();
112+
expect(screen.queryByTestId(COOLDOWN_TIMER_TEST_ID)).not.toBeInTheDocument();
113+
});
114+
115+
it('shows the stop button while the AI is generating even when the send button is hidden', async () => {
116+
const channel = await renderComposer({ hideSendButton: true });
117+
118+
expect(screen.queryByTestId(RECORDING_BUTTON_TEST_ID)).not.toBeInTheDocument();
119+
120+
await setAIState(channel, AIStates.Generating);
121+
122+
expect(screen.getByTestId(STOP_BUTTON_TEST_ID)).toBeInTheDocument();
123+
});
124+
125+
it('keeps the recording button while the AI is generating when the stop button is removed', async () => {
126+
const channel = await renderComposer({ overrides: { StopAIGenerationButton: null } });
127+
128+
await setAIState(channel, AIStates.Generating);
129+
130+
expect(screen.getByTestId(RECORDING_BUTTON_TEST_ID)).toBeInTheDocument();
131+
expect(screen.queryByTestId(STOP_BUTTON_TEST_ID)).not.toBeInTheDocument();
132+
});
133+
134+
it('shows the send button once the input has text', async () => {
135+
const channel = await renderComposer();
136+
137+
await act(() => {
138+
channel.messageComposer.textComposer.setText('Hello');
139+
});
140+
141+
expect(screen.getByTestId('send-button')).toBeInTheDocument();
142+
expect(screen.queryByTestId(RECORDING_BUTTON_TEST_ID)).not.toBeInTheDocument();
143+
});
144+
145+
it('shows a custom send button once the input has text', async () => {
146+
const CustomSendButton = () => (
147+
<button data-testid='custom-send-button' type='button' />
148+
);
149+
const channel = await renderComposer({ overrides: { SendButton: CustomSendButton } });
150+
151+
await act(() => {
152+
channel.messageComposer.textComposer.setText('Hello');
153+
});
154+
155+
expect(screen.getByTestId('custom-send-button')).toBeInTheDocument();
156+
expect(screen.queryByTestId(RECORDING_BUTTON_TEST_ID)).not.toBeInTheDocument();
157+
});
158+
159+
it('goes back to the recording button once the AI stops generating', async () => {
160+
const channel = await renderComposer();
161+
162+
await setAIState(channel, AIStates.Generating);
163+
await setAIState(channel, AIStates.Idle);
164+
165+
expect(screen.getByTestId(RECORDING_BUTTON_TEST_ID)).toBeInTheDocument();
166+
expect(screen.queryByTestId(STOP_BUTTON_TEST_ID)).not.toBeInTheDocument();
167+
});
168+
});

0 commit comments

Comments
 (0)