Skip to content

Commit 61a3001

Browse files
committed
Add horizontal resizing
1 parent b7fef89 commit 61a3001

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

packages/module/src/MessageBar/MessageBar.tsx

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ 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>();
6766
const [hasSentMessage, setHasSentMessage] = React.useState(false);
6867

6968
const textareaRef = React.useRef<HTMLTextAreaElement>(null);
@@ -75,6 +74,11 @@ export const MessageBar: React.FunctionComponent<MessageBarProps> = ({
7574
if (parent) {
7675
parent.style.setProperty('margin-top', `0rem`);
7776
parent.style.setProperty('margin-bottom', `0rem`);
77+
78+
const grandparent = parent.parentElement;
79+
if (grandparent) {
80+
grandparent.style.setProperty('flex-basis', 'auto');
81+
}
7882
}
7983
};
8084

@@ -92,25 +96,35 @@ export const MessageBar: React.FunctionComponent<MessageBarProps> = ({
9296
parseInt(computed.getPropertyValue('border-bottom-width'));
9397
parent.style.setProperty('height', `${height}px`);
9498

99+
console.log('height', height);
95100
if (height > 32 || window.innerWidth <= 359) {
96101
parent.style.setProperty('margin-bottom', `1rem`);
97102
parent.style.setProperty('margin-top', `1rem`);
98103
}
104+
}
105+
};
99106

100-
setHeight(height);
107+
const textIsLongerThan2Lines = (field: HTMLTextAreaElement) => {
108+
const lineHeight = parseFloat(window.getComputedStyle(field).lineHeight);
109+
const lines = field.scrollHeight / lineHeight;
110+
return lines > 2;
111+
};
112+
113+
const setAutoWidth = (field: HTMLTextAreaElement) => {
114+
const parent = field.parentElement;
115+
if (parent) {
116+
const grandparent = parent.parentElement;
117+
if (textIsLongerThan2Lines(field) && grandparent) {
118+
grandparent.style.setProperty('flex-basis', `100%`);
119+
}
101120
}
102121
};
103122

104123
const handleNewLine = (field: HTMLTextAreaElement) => {
105124
const parent = field.parentElement;
106-
const oldHeight = height ?? 0;
107125
if (parent) {
108-
if (oldHeight === 0) {
109-
parent.style.setProperty('margin-bottom', `1rem`);
110-
parent.style.setProperty('margin-top', `1rem`);
111-
} else {
112-
parent.style.setProperty('height', `${oldHeight + 16}px`);
113-
}
126+
parent.style.setProperty('margin-bottom', `1rem`);
127+
parent.style.setProperty('margin-top', `1rem`);
114128
}
115129
};
116130

@@ -128,6 +142,27 @@ export const MessageBar: React.FunctionComponent<MessageBarProps> = ({
128142
setInitialLineHeight(field);
129143
setAutoHeight(field);
130144
}
145+
const resetHeight = () => {
146+
if (field) {
147+
if (field.value === '') {
148+
if (window.innerWidth > 467) {
149+
setInitialLineHeight(field);
150+
} else {
151+
setInitialLineHeight(field);
152+
handleMobileHeight(field);
153+
}
154+
} else {
155+
setInitialLineHeight(field);
156+
setAutoHeight(field);
157+
setAutoWidth(field);
158+
}
159+
}
160+
};
161+
window.addEventListener('resize', resetHeight);
162+
163+
return () => {
164+
window.removeEventListener('resize', resetHeight);
165+
};
131166
}, []);
132167

133168
React.useEffect(() => {
@@ -139,6 +174,15 @@ export const MessageBar: React.FunctionComponent<MessageBarProps> = ({
139174
setHasSentMessage(false);
140175
}, [hasSentMessage]);
141176

177+
React.useEffect(() => {
178+
if (textareaRef.current) {
179+
if (window.innerWidth <= 467) {
180+
handleMobileHeight(textareaRef.current);
181+
}
182+
setAutoWidth(textareaRef.current);
183+
}
184+
}, [message]);
185+
142186
const handleChange = React.useCallback((event) => {
143187
if (textareaRef.current) {
144188
if (event.target.value === '') {
@@ -158,13 +202,6 @@ export const MessageBar: React.FunctionComponent<MessageBarProps> = ({
158202
});
159203
}, [onSendMessage]);
160204

161-
// want to exclude things like delete, shift, etc.
162-
const isTypeableKey = (key) => {
163-
if (key.length > 1) {
164-
return false;
165-
}
166-
return true;
167-
};
168205
const handleKeyDown = React.useCallback(
169206
(event) => {
170207
if (event.key === 'Enter' && !event.shiftKey) {
@@ -178,11 +215,6 @@ export const MessageBar: React.FunctionComponent<MessageBarProps> = ({
178215
handleNewLine(textareaRef.current);
179216
}
180217
}
181-
if (window.innerWidth <= 411 && isTypeableKey(event.key)) {
182-
if (textareaRef.current) {
183-
handleMobileHeight(textareaRef.current);
184-
}
185-
}
186218
},
187219
[handleSend]
188220
);

0 commit comments

Comments
 (0)