Skip to content

Commit f8dff61

Browse files
committed
Try subbing textarea
1 parent cfed82e commit f8dff61

File tree

2 files changed

+109
-8
lines changed

2 files changed

+109
-8
lines changed

packages/module/src/MessageBar/MessageBar.scss

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
display: flex;
3535
justify-content: end;
3636
padding-block-start: var(--pf-chatbot__message-bar-child--PaddingBlockStart);
37-
padding-block-end: var(--pf-t--global--spacer--sm);
37+
padding-block-end: var(--pf-t--global--spacer--xs);
3838
gap: var(--pf-t--global--spacer--xs);
3939
}
4040

@@ -47,12 +47,9 @@
4747

4848
// - Form control textarea
4949
.pf-chatbot__message-textarea {
50-
padding-block-start: var(--pf-t--global--spacer--md);
51-
padding-block-end: var(--pf-t--global--spacer--md);
52-
padding-inline-start: var(--pf-t--global--spacer--md);
53-
padding-inline-end: var(--pf-t--global--spacer--md);
5450
--pf-v6-c-form-control--before--BorderStyle: none;
5551
--pf-v6-c-form-control--after--BorderStyle: none;
52+
resize: none;
5653
background-color: transparent;
5754
font-size: var(--pf-t--global--font--size--md);
5855
line-height: 1.5rem;
@@ -63,4 +60,13 @@
6360
.pf-v6-c-form-control__textarea:focus-visible {
6461
outline: none;
6562
}
63+
textarea {
64+
outline-offset: 0px;
65+
--pf-v6-c-form-control--PaddingBlockStart: 0;
66+
--pf-v6-c-form-control--PaddingBlockEnd: 0;
67+
--pf-v6-c-form-control--BorderRadius: 0;
68+
}
69+
textarea:focus-visible {
70+
outline: none;
71+
}
6672
}

packages/module/src/MessageBar/MessageBar.tsx

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { DropEvent, TextAreaProps } from '@patternfly/react-core';
2+
import { DropEvent, TextArea, TextAreaProps, TextInput } from '@patternfly/react-core';
33
import { AutoTextArea } from 'react-textarea-auto-witdth-height';
44

55
// Import Chatbot components
@@ -63,11 +63,79 @@ export const MessageBar: React.FunctionComponent<MessageBarProps> = ({
6363
// --------------------------------------------------------------------------
6464
const [message, setMessage] = React.useState<string>('');
6565
const [isListeningMessage, setIsListeningMessage] = React.useState<boolean>(false);
66+
const [height, setHeight] = React.useState<number>();
6667

67-
const textareaRef = React.useRef(null);
68+
const textareaRef = React.useRef<HTMLTextAreaElement>(null);
6869
const attachButtonRef = React.useRef<HTMLButtonElement>(null);
6970

71+
const setInitialLineHeight = (field: HTMLTextAreaElement) => {
72+
field.style.setProperty('line-height', '1rem');
73+
const parent = field.parentElement;
74+
if (parent) {
75+
parent.style.setProperty('margin-top', `0rem`);
76+
parent.style.setProperty('margin-bottom', `0rem`);
77+
}
78+
};
79+
80+
const setAutoHeight = (field: HTMLTextAreaElement) => {
81+
const parent = field.parentElement;
82+
if (parent) {
83+
parent.style.setProperty('height', 'inherit');
84+
const computed = window.getComputedStyle(field);
85+
// Calculate the height
86+
const height =
87+
parseInt(computed.getPropertyValue('border-top-width')) +
88+
parseInt(computed.getPropertyValue('padding-top')) +
89+
field.scrollHeight +
90+
parseInt(computed.getPropertyValue('padding-bottom')) +
91+
parseInt(computed.getPropertyValue('border-bottom-width'));
92+
parent.style.setProperty('height', `${height}px`);
93+
94+
if (height > 32 || window.innerWidth <= 359) {
95+
parent.style.setProperty('margin-bottom', `1rem`);
96+
parent.style.setProperty('margin-top', `1rem`);
97+
}
98+
99+
setHeight(height);
100+
}
101+
};
102+
103+
const handleNewLine = (field: HTMLTextAreaElement) => {
104+
const parent = field.parentElement;
105+
const oldHeight = height ?? 0;
106+
if (parent) {
107+
if (oldHeight === 0) {
108+
parent.style.setProperty('margin-bottom', `1rem`);
109+
parent.style.setProperty('margin-top', `1rem`);
110+
} else {
111+
parent.style.setProperty('height', `${oldHeight + 16}px`);
112+
}
113+
}
114+
};
115+
116+
const handleMobileHeight = (field: HTMLTextAreaElement) => {
117+
const parent = field.parentElement;
118+
if (parent) {
119+
parent.style.setProperty('margin-bottom', `1rem`);
120+
parent.style.setProperty('margin-top', `1rem`);
121+
}
122+
};
123+
124+
React.useEffect(() => {
125+
const field = textareaRef.current;
126+
if (field) {
127+
setInitialLineHeight(field);
128+
setAutoHeight(field);
129+
}
130+
}, []);
131+
70132
const handleChange = React.useCallback((event) => {
133+
if (textareaRef.current) {
134+
if (event.target.value === '') {
135+
setInitialLineHeight(textareaRef.current);
136+
}
137+
setAutoHeight(textareaRef.current);
138+
}
71139
setMessage(event.target.value);
72140
}, []);
73141

@@ -79,6 +147,13 @@ export const MessageBar: React.FunctionComponent<MessageBarProps> = ({
79147
});
80148
}, [onSendMessage]);
81149

150+
// want to exclude things like delete, shift, etc.
151+
const isTypeableKey = (key) => {
152+
if (key.length > 1) {
153+
return false;
154+
}
155+
return true;
156+
};
82157
const handleKeyDown = React.useCallback(
83158
(event) => {
84159
if (event.key === 'Enter' && !event.shiftKey) {
@@ -87,6 +162,16 @@ export const MessageBar: React.FunctionComponent<MessageBarProps> = ({
87162
handleSend();
88163
}
89164
}
165+
if (event.key === 'Enter' && event.shiftKey) {
166+
if (textareaRef.current) {
167+
handleNewLine(textareaRef.current);
168+
}
169+
}
170+
if (window.innerWidth <= 411 && isTypeableKey(event.key)) {
171+
if (textareaRef.current) {
172+
handleMobileHeight(textareaRef.current);
173+
}
174+
}
90175
},
91176
[handleSend]
92177
);
@@ -99,7 +184,7 @@ export const MessageBar: React.FunctionComponent<MessageBarProps> = ({
99184
const messageBarContents = (
100185
<>
101186
<div className="pf-chatbot__message-bar-input">
102-
<AutoTextArea
187+
{/* <AutoTextArea
103188
ref={textareaRef}
104189
className="pf-chatbot__message-textarea"
105190
value={message as any} // Added any to make the third part TextArea component types happy. Remove when replced with PF TextArea
@@ -108,6 +193,16 @@ export const MessageBar: React.FunctionComponent<MessageBarProps> = ({
108193
placeholder={isListeningMessage ? 'Listening' : 'Send a message...'}
109194
aria-label={isListeningMessage ? 'Listening' : 'Send a message...'}
110195
{...props}
196+
/>*/}
197+
<TextArea
198+
className="pf-chatbot__message-textarea"
199+
value={message}
200+
onChange={handleChange}
201+
aria-label={isListeningMessage ? 'Listening' : 'Send a message...'}
202+
placeholder={isListeningMessage ? 'Listening' : 'Send a message...'}
203+
ref={textareaRef}
204+
onKeyDown={handleKeyDown}
205+
{...props}
111206
/>
112207
</div>
113208
<div className="pf-chatbot__message-bar-actions">

0 commit comments

Comments
 (0)