Skip to content

Commit 38eda58

Browse files
authored
feat(MessageDivider): add MessageDivider, example, tests (#598)
1 parent 8e0663a commit 38eda58

File tree

9 files changed

+147
-1
lines changed

9 files changed

+147
-1
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { FunctionComponent } from 'react';
2+
import Message from '@patternfly/chatbot/dist/dynamic/Message';
3+
import patternflyAvatar from './patternfly_avatar.jpg';
4+
import MessageDivider from '@patternfly/chatbot/dist/dynamic/MessageDivider';
5+
6+
export const MessageWithDividersExample: FunctionComponent = () => (
7+
<>
8+
<Message
9+
name="Bot"
10+
role="bot"
11+
avatar={patternflyAvatar}
12+
content={`This is a text-based message from a bot named "Bot."`}
13+
/>
14+
<MessageDivider variant="inset" content="1 hour ago" />
15+
<Message
16+
name="Bot"
17+
role="bot"
18+
avatar={patternflyAvatar}
19+
content={`This is a text-based message from "Bot," with an updated timestamp.`}
20+
timestamp="1 hour ago"
21+
/>
22+
<MessageDivider variant="fullWidth" content="Agent joined the chat" />
23+
</>
24+
);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ sortValue: 3
3131
---
3232

3333
import Message from '@patternfly/chatbot/dist/dynamic/Message';
34+
import MessageDivider from '@patternfly/chatbot/dist/dynamic/MessageDivider';
3435
import { rehypeCodeBlockToggle } from '@patternfly/chatbot/dist/esm/Message/Plugins/rehypeCodeBlockToggle';
3536
import SourcesCard from '@patternfly/chatbot/dist/dynamic/SourcesCard';
3637
import { RobotIcon } from '@patternfly/react-icons/dist/esm/icons/robot-icon';
@@ -66,6 +67,16 @@ You can further customize the avatar by applying an additional class or passing
6667

6768
```
6869

70+
### Message dividers
71+
72+
To provide users with important contextual updates, you can add dividers between messages.
73+
74+
For example, you can use the default divider to display a "timestamp" for more significant gaps in the conversation, or you can pass `variant="fullWidth"` to a divider to announce that an agent has joined the chat.
75+
76+
```js file="./MessageWithDividers.tsx"
77+
78+
```
79+
6980
### Message actions
7081

7182
You can add actions to a message, to allow users to interact with the message content. These actions can include:

packages/module/src/Message/Message.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
display: flex;
66
align-items: flex-start;
77
gap: var(--pf-t--global--spacer--lg);
8-
padding-bottom: var(--pf-t--global--spacer--2xl);
8+
padding-bottom: var(--pf-t--global--spacer--xl);
99

1010
// Avatar
1111
// --------------------------------------------------------------------------
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// ============================================================================
2+
// Chatbot Main - Message Divider
3+
// ============================================================================
4+
.pf-chatbot__message-divider {
5+
display: grid;
6+
padding-block-end: var(--pf-t--global--spacer--xl);
7+
8+
.pf-v6-c-divider,
9+
.pf-v6-c-label {
10+
grid-row: 1 / 1;
11+
grid-column: 1 / 1;
12+
}
13+
14+
.pf-v6-c-label {
15+
--pf-v6-c-label--BackgroundColor: var(--pf-t--global--background--color--tertiary--default);
16+
--pf-v6-c-label--BorderColor: var(--pf-t--global--border--color--default);
17+
--pf-v6-c-label--PaddingInlineStart: var(--pf-t--global--spacer--action--horizontal--compact);
18+
--pf-v6-c-label--PaddingInlineEnd: var(--pf-t--global--spacer--action--horizontal--compact);
19+
20+
.pf-v6-c-label__text {
21+
font-weight: var(--pf-t--global--font--weight--body--bold);
22+
text-align: center;
23+
}
24+
}
25+
26+
&.pf-m-divider {
27+
.pf-v6-c-label {
28+
--pf-v6-c-label--BackgroundColor: var(--pf-t--global--background--color--secondary--default);
29+
--pf-v6-c-label--MaxWidth: 75%;
30+
31+
justify-self: center;
32+
}
33+
34+
.pf-v6-c-divider {
35+
align-self: center;
36+
}
37+
}
38+
39+
&.pf-m-wrap {
40+
.pf-v6-c-label,
41+
.pf-v6-c-label__text {
42+
white-space: normal;
43+
}
44+
}
45+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { render, screen } from '@testing-library/react';
2+
import '@testing-library/jest-dom';
3+
import MessageDivider from './MessageDivider';
4+
5+
describe('MessageDivider', () => {
6+
beforeEach(() => {
7+
jest.clearAllMocks();
8+
});
9+
it('should render default correctly with variant = date and content = new Date().toLocaleDateString()', () => {
10+
render(<MessageDivider data-testid="message-divider" />);
11+
expect(screen.getByText(new Date().toLocaleDateString())).toBeInTheDocument();
12+
expect(screen.getByTestId('message-divider')).toHaveClass('pf-m-divider');
13+
});
14+
it('should render inset variant correctly', () => {
15+
render(<MessageDivider variant="inset" content="test" data-testid="message-divider" />);
16+
expect(screen.getByText('test')).toBeInTheDocument();
17+
expect(screen.getByTestId('message-divider')).toHaveClass('pf-m-divider');
18+
});
19+
it('should render fullWidth variant correctly', () => {
20+
render(<MessageDivider variant="fullWidth" content="test" data-testid="message-divider" />);
21+
expect(screen.getByText('test')).toBeInTheDocument();
22+
expect(screen.getByTestId('message-divider')).not.toHaveClass('pf-m-divider');
23+
});
24+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// ============================================================================
2+
// Chatbot Main - Message Divider
3+
// ============================================================================
4+
import type { FunctionComponent } from 'react';
5+
import { Divider, Label } from '@patternfly/react-core';
6+
7+
export interface MessageDividerProps {
8+
/** Variant of the divider */
9+
variant?: 'inset' | 'fullWidth';
10+
/** Content of the message divider */
11+
content?: string;
12+
}
13+
14+
const MessageDivider: FunctionComponent<MessageDividerProps> = ({
15+
variant = 'inset',
16+
content = new Date().toLocaleDateString(),
17+
...props
18+
}: MessageDividerProps) => {
19+
if (variant === 'inset') {
20+
return (
21+
<div className="pf-chatbot__message-divider pf-m-divider pf-m-wrap" {...props}>
22+
<Divider />
23+
<Label variant="outline">{content}</Label>
24+
</div>
25+
);
26+
}
27+
28+
return (
29+
<div className="pf-chatbot__message-divider pf-m-wrap" {...props}>
30+
<Label>{content}</Label>
31+
</div>
32+
);
33+
};
34+
35+
export default MessageDivider;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { default } from './MessageDivider';
2+
3+
export * from './MessageDivider';

packages/module/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ export * from './MessageBar';
6363
export { default as MessageBox } from './MessageBox';
6464
export * from './MessageBox';
6565

66+
export { default as MessageDivider } from './MessageDivider';
67+
export * from './MessageDivider';
68+
6669
export { default as PreviewAttachment } from './PreviewAttachment';
6770
export * from './PreviewAttachment';
6871

packages/module/src/main.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
@import './Message/UserFeedback/UserFeedback';
2727
@import './MessageBar/MessageBar';
2828
@import './MessageBox/MessageBox';
29+
@import './MessageDivider/MessageDivider';
2930
@import './MessageBox/JumpButton';
3031
@import './ResponseActions/ResponseActions';
3132
@import './Settings/Settings';

0 commit comments

Comments
 (0)