Skip to content

Commit bbb6290

Browse files
authored
Merge pull request #805 from thatblindgeye/iss791_rightAlignMsg
feat(Messages): added right-aligned and no metadata variants
2 parents 15a3d1e + cbf3d96 commit bbb6290

File tree

5 files changed

+74
-14
lines changed

5 files changed

+74
-14
lines changed

packages/module/patternfly-docs/content/extensions/chatbot/examples/Messages/BotMessage.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,13 @@ _Italic text, formatted with single underscores_
319319
hasRoundAvatar={false}
320320
/>
321321
<Message name="Bot" role="bot" content="This is a message from a bot with no avatar." />
322+
<Message
323+
name="Bot"
324+
role="bot"
325+
avatar={patternflyAvatar}
326+
isMetadataVisible={false}
327+
content="This is a message from a bot with metadata not visible."
328+
/>
322329
<Select
323330
id="single-select"
324331
isOpen={isOpen}

packages/module/patternfly-docs/content/extensions/chatbot/examples/Messages/UserMessage.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,13 @@ _Italic text, formatted with single underscores_
334334
avatarProps={{ isBordered: true }}
335335
/>
336336
<Message name="User" role="user" content="This is a user message with no avatar" />
337+
<Message
338+
name="User"
339+
role="user"
340+
content="This is a user message with metadata not visible."
341+
avatar={userAvatar}
342+
isMetadataVisible={false}
343+
/>
337344
<Message
338345
name="User"
339346
role="user"
@@ -345,6 +352,13 @@ _Italic text, formatted with single underscores_
345352
avatar={userAvatar}
346353
inputRef={messageInputRef}
347354
/>
355+
<Message
356+
name="User"
357+
role="user"
358+
avatar={userAvatar}
359+
alignment="end"
360+
content="This is a user message that is aligned at the end of the message container."
361+
/>
348362
<Select
349363
id="single-select"
350364
isOpen={isOpen}

packages/module/src/Message/Message.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@
134134
.footnotes {
135135
background-color: var(--pf-t--global--background--color--tertiary--default);
136136
}
137+
138+
// Right/End-aligned messages
139+
&.pf-m-end {
140+
flex-direction: row-reverse;
141+
142+
.pf-chatbot__message-contents {
143+
align-items: flex-end;
144+
}
145+
}
137146
}
138147

139148
// Attachments

packages/module/src/Message/Message.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,17 @@ describe('Message', () => {
255255
})
256256
).not.toBeInTheDocument();
257257
});
258+
259+
it('Does not render metadata when isMetadataVisible is false', () => {
260+
render(
261+
<Message isMetadataVisible={false} avatar="./img" role="bot" name="Bot" content="Hi" timestamp="2 hours ago" />
262+
);
263+
264+
expect(screen.queryByText('Bot')).not.toBeInTheDocument();
265+
expect(screen.queryByText('AI')).not.toBeInTheDocument();
266+
expect(screen.queryByText('2 hours ago')).not.toBeInTheDocument();
267+
});
268+
258269
it('should render attachments', () => {
259270
render(<Message avatar="./img" role="user" content="Hi" attachments={[{ name: 'testAttachment' }]} />);
260271
expect(screen.getByText('Hi')).toBeTruthy();
@@ -1330,4 +1341,14 @@ describe('Message', () => {
13301341
);
13311342
expect(container.querySelector('.pf-m-outline')).toBeFalsy();
13321343
});
1344+
1345+
it('Renders without pf-m-end class by default', () => {
1346+
render(<Message avatar="./img" role="user" name="User" content="" />);
1347+
expect(screen.getByRole('region')).not.toHaveClass('pf-m-end');
1348+
});
1349+
1350+
it('Renders with pf-m-end class when alignment="end"', () => {
1351+
render(<Message alignment="end" avatar="./img" role="user" name="User" content="" />);
1352+
expect(screen.getByRole('region')).toHaveClass('pf-m-end');
1353+
});
13331354
});

packages/module/src/Message/Message.tsx

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import ToolResponse, { ToolResponseProps } from '../ToolResponse';
3535
import DeepThinking, { DeepThinkingProps } from '../DeepThinking';
3636
import ToolCall, { ToolCallProps } from '../ToolCall';
3737
import MarkdownContent from '../MarkdownContent';
38+
import { css } from '@patternfly/react-styles';
3839

3940
export interface MessageAttachment {
4041
/** Name of file attached to the message */
@@ -73,6 +74,10 @@ export interface MessageProps extends Omit<HTMLProps<HTMLDivElement>, 'role'> {
7374
id?: string;
7475
/** Role of the user sending the message */
7576
role: 'user' | 'bot';
77+
/** Whether the message is aligned at the horizontal start or end of the message container. */
78+
alignment?: 'start' | 'end';
79+
/** Flag indicating whether message metadata (user name and timestamp) are visible. */
80+
isMetadataVisible?: boolean;
7681
/** Message content */
7782
content?: string;
7883
/** Extra Message content */
@@ -197,6 +202,8 @@ export interface MessageProps extends Omit<HTMLProps<HTMLDivElement>, 'role'> {
197202
export const MessageBase: FunctionComponent<MessageProps> = ({
198203
children,
199204
role,
205+
alignment = 'start',
206+
isMetadataVisible = true,
200207
content,
201208
extraContent,
202209
name,
@@ -314,7 +321,7 @@ export const MessageBase: FunctionComponent<MessageProps> = ({
314321
return (
315322
<section
316323
aria-label={`Message from ${role} - ${dateString}`}
317-
className={`pf-chatbot__message pf-chatbot__message--${role}`}
324+
className={css(`pf-chatbot__message pf-chatbot__message--${role}`, alignment === 'end' && 'pf-m-end')}
318325
aria-live={isLiveRegion ? 'polite' : undefined}
319326
aria-atomic={isLiveRegion ? false : undefined}
320327
ref={innerRef}
@@ -330,19 +337,21 @@ export const MessageBase: FunctionComponent<MessageProps> = ({
330337
/>
331338
)}
332339
<div className="pf-chatbot__message-contents">
333-
<div className="pf-chatbot__message-meta">
334-
{name && (
335-
<span className="pf-chatbot__message-name">
336-
<Truncate content={name} />
337-
</span>
338-
)}
339-
{role === 'bot' && (
340-
<Label variant="outline" isCompact>
341-
{botWord}
342-
</Label>
343-
)}
344-
<Timestamp date={date}>{timestamp}</Timestamp>
345-
</div>
340+
{isMetadataVisible && (
341+
<div className="pf-chatbot__message-meta">
342+
{name && (
343+
<span className="pf-chatbot__message-name">
344+
<Truncate content={name} />
345+
</span>
346+
)}
347+
{role === 'bot' && (
348+
<Label variant="outline" isCompact>
349+
{botWord}
350+
</Label>
351+
)}
352+
<Timestamp date={date}>{timestamp}</Timestamp>
353+
</div>
354+
)}
346355
<div className="pf-chatbot__message-response">
347356
{children ? (
348357
<>{children}</>

0 commit comments

Comments
 (0)