Skip to content

Commit bfb4a45

Browse files
mbienhuelsMaximilian Bienhüls
andauthored
fix(issues): fix textarea always grows (#194)
check if the textarea overflows before resizing it Co-authored-by: Maximilian Bienhüls <[email protected]>
1 parent 3ad6985 commit bfb4a45

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/ui/c-input-text/c-input-text.vue

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,41 @@ watch(
116116
{ immediate: true },
117117
);
118118
119+
/**
120+
* Checks if the given HTMLElement overflows
121+
* copied from stackoverflow: https://stackoverflow.com/questions/143815/determine-if-an-html-elements-content-overflows
122+
* @param el The HTMLElement to check
123+
*/
124+
function checkOverflow(el: HTMLElement) {
125+
const curOverflow = el.style.overflow;
126+
if (!curOverflow || curOverflow === 'visible') {
127+
el.style.overflow = 'hidden';
128+
}
129+
130+
const isOverflowing = el.clientWidth < el.scrollWidth || el.clientHeight < el.scrollHeight;
131+
132+
el.style.overflow = curOverflow;
133+
134+
return isOverflowing;
135+
}
136+
119137
function resizeTextarea() {
120-
if (!textareaRef.value || !inputWrapperRef.value) {
138+
if (textareaRef.value === undefined) {
139+
return; // cannot chnge textarea if element is not available
140+
}
141+
142+
if (!multiline) {
143+
return; // textarea wont be displayed if multiline === false
144+
}
145+
146+
if (!checkOverflow(textareaRef.value)) {
121147
return;
122148
}
123149
124-
const scrollHeight = textareaRef.value.scrollHeight + 2;
150+
const textAreaElement = textareaRef.value!;
125151
126-
inputWrapperRef.value.style.height = `${scrollHeight}px`;
152+
const scrollHeight = textAreaElement.scrollHeight + 2;
153+
textAreaElement.style.height = `${scrollHeight}px`;
127154
}
128155
129156
const htmlInputType = computed(() => {

0 commit comments

Comments
 (0)