Skip to content

Commit b43fb63

Browse files
committed
feat(Message): Add support for code block custom actions
Allow for props passage down to code blocks so users can add custom message content. This was a request from OpenShift Lightspeed.
1 parent 2f25b87 commit b43fb63

File tree

4 files changed

+28
-24
lines changed

4 files changed

+28
-24
lines changed

packages/module/src/Message/CodeBlockMessage/CodeBlockMessage.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
font-family: var(--pf-t--global--font--family--body);
3131
}
3232

33-
.pf-v6-c-code-block__actions-item {
33+
// we are overriding some default PatternFly positioning here - it's only necessary for the first two items
34+
.pf-chatbot__message-code-block-default-action {
3435
display: flex;
3536
align-items: center;
3637
justify-content: space-between;

packages/module/src/Message/CodeBlockMessage/CodeBlockMessage.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export interface CodeBlockMessageProps {
3737
expandedText?: string;
3838
/** Link text applied to expandable toggle when collapsed */
3939
collapsedText?: string;
40+
/** Custom actions added to header of code block */
41+
customActions?: React.ReactNode;
4042
}
4143

4244
const DEFAULT_EXPANDED_TEXT = 'Show less';
@@ -51,6 +53,7 @@ const CodeBlockMessage = ({
5153
expandableSectionToggleProps,
5254
expandedText = DEFAULT_EXPANDED_TEXT,
5355
collapsedText = DEFAULT_COLLAPSED_TEXT,
56+
customActions,
5457
...props
5558
}: CodeBlockMessageProps) => {
5659
const [copied, setCopied] = useState(false);
@@ -114,7 +117,7 @@ const CodeBlockMessage = ({
114117
// Setup code block header
115118
const actions = (
116119
<>
117-
<CodeBlockAction>
120+
<CodeBlockAction className="pf-chatbot__message-code-block-default-action">
118121
{language && <div className="pf-chatbot__message-code-block-language">{language}</div>}
119122
<Button
120123
ref={buttonRef}
@@ -127,6 +130,7 @@ const CodeBlockMessage = ({
127130
</Button>
128131
<Tooltip id={tooltipID} content="Copy" position="top" triggerRef={buttonRef} />
129132
</CodeBlockAction>
133+
{customActions}
130134
</>
131135
);
132136

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import userEvent from '@testing-library/user-event';
66
import { monitorSampleAppQuickStart } from './QuickStarts/monitor-sampleapp-quickstart';
77
import { monitorSampleAppQuickStartWithImage } from './QuickStarts/monitor-sampleapp-quickstart-with-image';
88
import rehypeExternalLinks from '../__mocks__/rehype-external-links';
9-
import { AlertActionLink } from '@patternfly/react-core';
9+
import { AlertActionLink, Button, CodeBlockAction } from '@patternfly/react-core';
1010
import { DeepThinkingProps } from '../DeepThinking';
1111

1212
const ALL_ACTIONS = [
@@ -612,6 +612,24 @@ describe('Message', () => {
612612
);
613613
expect(screen.getByRole('button', { name: 'test' })).toBeTruthy();
614614
});
615+
it('should be able to add custom actions to CodeMessage', () => {
616+
render(
617+
<Message
618+
avatar="./img"
619+
role="user"
620+
name="User"
621+
content={CODE_MESSAGE}
622+
codeBlockProps={{
623+
customActions: (
624+
<CodeBlockAction>
625+
<Button>New custom action</Button>
626+
</CodeBlockAction>
627+
)
628+
}}
629+
/>
630+
);
631+
expect(screen.getByRole('button', { name: /New custom action/i })).toBeTruthy();
632+
});
615633
it('should handle hasRoundAvatar correctly when it is true', () => {
616634
render(<Message avatar="./img" role="user" name="User" content="Hi" hasRoundAvatar />);
617635
expect(screen.getByRole('img')).toBeTruthy();

packages/module/src/Message/Message.tsx

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@ import {
1111
AvatarProps,
1212
ButtonProps,
1313
ContentVariants,
14-
ExpandableSectionProps,
15-
ExpandableSectionToggleProps,
1614
FormProps,
1715
Label,
1816
LabelGroupProps,
1917
Timestamp,
2018
Truncate
2119
} from '@patternfly/react-core';
2220
import MessageLoading from './MessageLoading';
23-
import CodeBlockMessage from './CodeBlockMessage/CodeBlockMessage';
21+
import CodeBlockMessage, { CodeBlockMessageProps } from './CodeBlockMessage/CodeBlockMessage';
2422
import TextMessage from './TextMessage/TextMessage';
2523
import FileDetailsLabel from '../FileDetailsLabel/FileDetailsLabel';
2624
import ResponseActions, { ActionProps } from '../ResponseActions/ResponseActions';
@@ -114,24 +112,7 @@ export interface MessageProps extends Omit<HTMLProps<HTMLDivElement>, 'role'> {
114112
/** Label for the English "Loading message," displayed to screenreaders when loading a message */
115113
loadingWord?: string;
116114
/** Props for code blocks */
117-
codeBlockProps?: {
118-
/** Aria label applied to code blocks */
119-
'aria-label'?: string;
120-
/** Class name applied to code blocks */
121-
className?: string;
122-
/** Whether code blocks are expandable */
123-
isExpandable?: boolean;
124-
/** Length of text initially shown in expandable code blocks; defaults to 10 characters */
125-
maxLength?: number;
126-
/** Additional props passed to expandable section if isExpandable is applied */
127-
expandableSectionProps?: Omit<ExpandableSectionProps, 'ref'>;
128-
/** Additional props passed to expandable toggle if isExpandable is applied */
129-
expandableSectionToggleProps?: ExpandableSectionToggleProps;
130-
/** Link text applied to expandable toggle when expanded */
131-
expandedText?: string;
132-
/** Link text applied to expandable toggle when collapsed */
133-
collapsedText?: string;
134-
};
115+
codeBlockProps?: CodeBlockMessageProps;
135116
/** Props for quick responses */
136117
quickResponses?: QuickResponse[];
137118
/** Props for quick responses container */

0 commit comments

Comments
 (0)